From 1b648a25980da1de01428759a1be57450c8b2cbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Duli=C3=A7i?= Date: Wed, 1 Sep 2021 23:16:41 +0300 Subject: [PATCH] added customer scopes to user model --- app/Http/Controllers/Auth/Login.php | 2 +- app/Http/Controllers/Auth/Reset.php | 2 +- app/Http/Controllers/Auth/Users.php | 2 +- app/Http/ViewComposers/Header.php | 2 +- .../Document/CreateDocumentTransaction.php | 2 +- app/Models/Auth/User.php | 44 ++++++++++++++++++- app/Traits/Users.php | 2 +- 7 files changed, 49 insertions(+), 7 deletions(-) diff --git a/app/Http/Controllers/Auth/Login.php b/app/Http/Controllers/Auth/Login.php index 5d8c3a035..85ce3202a 100644 --- a/app/Http/Controllers/Auth/Login.php +++ b/app/Http/Controllers/Auth/Login.php @@ -83,7 +83,7 @@ class Login extends Controller } // Redirect to portal if is customer - if ($user->can('read-client-portal')) { + if ($user->isCustomer()) { $path = session('url.intended', ''); // Path must start with company id and 'portal' prefix diff --git a/app/Http/Controllers/Auth/Reset.php b/app/Http/Controllers/Auth/Reset.php index 454fe2858..21e349582 100644 --- a/app/Http/Controllers/Auth/Reset.php +++ b/app/Http/Controllers/Auth/Reset.php @@ -104,7 +104,7 @@ class Reset extends Controller } // Redirect to portal if is customer - if ($user->can('read-client-portal')) { + if ($user->isCustomer()) { $this->redirectTo = route('portal.dashboard', ['company_id' => $company->id]); } diff --git a/app/Http/Controllers/Auth/Users.php b/app/Http/Controllers/Auth/Users.php index 66bb78cbe..d006b8d34 100644 --- a/app/Http/Controllers/Auth/Users.php +++ b/app/Http/Controllers/Auth/Users.php @@ -121,7 +121,7 @@ class Users extends Controller $landing_pages = $u->landing_pages; - if ($user->can('read-client-portal')) { + if ($user->isCustomer()) { // Show only roles with customer permission $roles = Role::all()->reject(function ($r) { return !$r->hasPermission('read-client-portal'); diff --git a/app/Http/ViewComposers/Header.php b/app/Http/ViewComposers/Header.php index 31a9e38ba..dd756f827 100644 --- a/app/Http/ViewComposers/Header.php +++ b/app/Http/ViewComposers/Header.php @@ -26,7 +26,7 @@ class Header if (!empty($user)) { // Get customer company - if ($user->can('read-client-portal')) { + if ($user->isCustomer()) { $company = (object) [ 'company_name' => setting('company.name'), 'company_email' => setting('company.email'), diff --git a/app/Listeners/Document/CreateDocumentTransaction.php b/app/Listeners/Document/CreateDocumentTransaction.php index a299bcf1a..6d5487fb9 100644 --- a/app/Listeners/Document/CreateDocumentTransaction.php +++ b/app/Listeners/Document/CreateDocumentTransaction.php @@ -39,7 +39,7 @@ class CreateDocumentTransaction return $this->getResponse('signed.' . $type . '.show', $document, $message); } - if ($user->can('read-client-portal')) { + if ($user->isCustomer()) { flash($message)->error()->important(); return $this->getResponse('portal.' . $type . '.show', $document, $message); diff --git a/app/Models/Auth/User.php b/app/Models/Auth/User.php index 709ea5b47..d922eeb20 100644 --- a/app/Models/Auth/User.php +++ b/app/Models/Auth/User.php @@ -182,7 +182,7 @@ class User extends Authenticatable implements HasLocalePreference } /** - * Scope to only include active currencies. + * Scope to only include active users. * * @param \Illuminate\Database\Eloquent\Builder $query * @return \Illuminate\Database\Eloquent\Builder @@ -192,6 +192,28 @@ class User extends Authenticatable implements HasLocalePreference return $query->where('enabled', 1); } + /** + * Scope to only customers. + * + * @param \Illuminate\Database\Eloquent\Builder $query + * @return \Illuminate\Database\Eloquent\Builder + */ + public function scopeIsCustomer($query) + { + return $query->wherePermissionIs('read-client-portal'); + } + + /** + * Scope to only users. + * + * @param \Illuminate\Database\Eloquent\Builder $query + * @return \Illuminate\Database\Eloquent\Builder + */ + public function scopeIsNotCustomer($query) + { + return $query->wherePermissionIs('read-admin-panel'); + } + /** * Attach company_ids attribute to model. * @@ -216,6 +238,26 @@ class User extends Authenticatable implements HasLocalePreference $this->offsetUnset('company_ids'); } + /** + * Determine if user is a customer. + * + * @return bool + */ + public function isCustomer() + { + return (bool) $this->can('read-client-portal'); + } + + /** + * Determine if user is not a customer. + * + * @return bool + */ + public function isNotCustomer() + { + return (bool) $this->can('read-admin-panel'); + } + /** * Get the user's preferred locale. * diff --git a/app/Traits/Users.php b/app/Traits/Users.php index 90ea61b5c..00f603204 100644 --- a/app/Traits/Users.php +++ b/app/Traits/Users.php @@ -100,7 +100,7 @@ trait Users return route('login'); } - $route_name = $user->can('read-client-portal') ? 'portal.dashboard' : $user->landing_page; + $route_name = $user->isCustomer() ? 'portal.dashboard' : $user->landing_page; $company_id = company_id() ?: optional($this->getFirstCompanyOfUser())->id;