From c47c10cbc380a3b7df0fcbf1b0b070b2129dd5cf Mon Sep 17 00:00:00 2001 From: Noah Caldwell Date: Fri, 6 Oct 2023 14:03:34 -0500 Subject: [PATCH 1/2] correctly lists users in the share site dropdown --- .../DeleteAccountRequestController.php | 7 +++--- app/Http/Controllers/SiteController.php | 9 ++++++- app/Http/Controllers/UserSiteController.php | 25 +++++++++++-------- resources/js/forms/OwnerForm.vue | 1 + 4 files changed, 27 insertions(+), 15 deletions(-) diff --git a/app/Http/Controllers/DeleteAccountRequestController.php b/app/Http/Controllers/DeleteAccountRequestController.php index 43fe762b..0a5cd905 100644 --- a/app/Http/Controllers/DeleteAccountRequestController.php +++ b/app/Http/Controllers/DeleteAccountRequestController.php @@ -32,9 +32,10 @@ public function myIndex(): JsonResponse { $this->authorize('accessList', DeleteAccountRequest::class); - return $this->success(DeleteAccountRequest::where('user_id', auth()->id()) - ->orderByDesc('id') - ->first()); + return $this->success(DeleteAccountRequest::findOrFail(auth()->id())); +// return $this->success(DeleteAccountRequest::where('user_id', auth()->id()) +// ->orderByDesc('id') +// ->first()); } /** diff --git a/app/Http/Controllers/SiteController.php b/app/Http/Controllers/SiteController.php index 2a9068f0..ab3f55e9 100644 --- a/app/Http/Controllers/SiteController.php +++ b/app/Http/Controllers/SiteController.php @@ -8,11 +8,13 @@ use App\Plant; use App\User; use App\Species; +use Illuminate\Auth\Access\AuthorizationException; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use App\Exports\SiteExport; use App\Imports\SiteImport; use Illuminate\Http\Response; +use Illuminate\Validation\ValidationException; use Storage; use Maatwebsite\Excel\Facades\Excel; @@ -287,8 +289,10 @@ public function toggleReminders(Site $site, Request $request) * @param \App\Site $site * @param \App\User $user * @param \Illuminate\Http\Request $request + * @throws AuthorizationException + * @throws ValidationException */ - public function changeOwner(Site $site, Request $request) + public function changeOwner(Site $site, Request $request): JsonResponse { $this->authorize('changeOwner', Site::class); @@ -296,6 +300,9 @@ public function changeOwner(Site $site, Request $request) 'user_id' => 'required|exists:users,id', ]); + // if site is already shared, remove the entry from user_sites table + (new UserSiteController)->delete($site, User::find($request->user_id)); + $site->fill(['user_id' => $request->user_id])->save(); return $this->success($site); diff --git a/app/Http/Controllers/UserSiteController.php b/app/Http/Controllers/UserSiteController.php index 3f454248..165d953a 100644 --- a/app/Http/Controllers/UserSiteController.php +++ b/app/Http/Controllers/UserSiteController.php @@ -26,7 +26,7 @@ public function toggleEdit(Site $site, User $user) if ($user_site) { $user_site->fill([ - 'editable' => ! $user_site->editable, + 'editable' => !$user_site->editable, ])->save(); } @@ -94,6 +94,9 @@ public function users(Request $request) if ($request->site_id) { $users->whereDoesntHave('userSites', function ($query) use ($request) { $query->where('user_sites.site_id', $request->site_id); + if ($request->transferring_ownership) { + $query->where('user_sites.is_shared', false); + } })->whereDoesntHave('siteInvitations', function ($query) use ($request) { $query->where('site_invitations.site_id', $request->site_id); $query->where('site_invitations.status', 'pending'); @@ -136,7 +139,7 @@ public function sharedUsers(Request $request) 'userSites' => function ($query) use ($request) { $query->where('site_id', $request->site_id); } - ])->orderBy('name', 'asc') + ])->orderBy('name', 'asc') ->whereHas('userSites', function ($query) use ($request) { $query->where('site_id', $request->site_id)->where('is_shared', true); }) @@ -167,15 +170,15 @@ public function toggleReminders(Site $site, Request $request) $user_site = UserSite::where('site_id', $site->id)->where('user_id', $user->id)->first(); if ($user_site === null) { - $user_site = UserSite::create([ - 'user_id' => $user->id, - 'site_id' => $site->id, - 'editable' => false, - 'sends_reminders' => true, - 'is_shared' => false, - ]); - - return $this->success($user_site); + $user_site = UserSite::create([ + 'user_id' => $user->id, + 'site_id' => $site->id, + 'editable' => false, + 'sends_reminders' => true, + 'is_shared' => false, + ]); + + return $this->success($user_site); } $user_site->fill(['sends_reminders' => !$user_site->sends_reminders])->save(); diff --git a/resources/js/forms/OwnerForm.vue b/resources/js/forms/OwnerForm.vue index 185c5bcb..5dac9096 100644 --- a/resources/js/forms/OwnerForm.vue +++ b/resources/js/forms/OwnerForm.vue @@ -131,6 +131,7 @@ export default { params : { search : this.userSearch.length >= 2 ? this.userSearch : null, site_id: this.site.id, + transferring_ownership: true, }, cancelToken: new axios.CancelToken(c => this._request = c), }) From 3691b0c35670e397c7428c277b0902ba163f00b9 Mon Sep 17 00:00:00 2001 From: Noah Caldwell Date: Fri, 6 Oct 2023 14:10:20 -0500 Subject: [PATCH 2/2] remove comments --- app/Http/Controllers/DeleteAccountRequestController.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/app/Http/Controllers/DeleteAccountRequestController.php b/app/Http/Controllers/DeleteAccountRequestController.php index 0a5cd905..3ded59a1 100644 --- a/app/Http/Controllers/DeleteAccountRequestController.php +++ b/app/Http/Controllers/DeleteAccountRequestController.php @@ -33,9 +33,6 @@ public function myIndex(): JsonResponse $this->authorize('accessList', DeleteAccountRequest::class); return $this->success(DeleteAccountRequest::findOrFail(auth()->id())); -// return $this->success(DeleteAccountRequest::where('user_id', auth()->id()) -// ->orderByDesc('id') -// ->first()); } /**