added customer scopes to user model

This commit is contained in:
Denis Duliçi 2021-09-01 23:16:41 +03:00
parent 980fd9a198
commit 1b648a2598
7 changed files with 49 additions and 7 deletions

View File

@ -83,7 +83,7 @@ class Login extends Controller
} }
// Redirect to portal if is customer // Redirect to portal if is customer
if ($user->can('read-client-portal')) { if ($user->isCustomer()) {
$path = session('url.intended', ''); $path = session('url.intended', '');
// Path must start with company id and 'portal' prefix // Path must start with company id and 'portal' prefix

View File

@ -104,7 +104,7 @@ class Reset extends Controller
} }
// Redirect to portal if is customer // Redirect to portal if is customer
if ($user->can('read-client-portal')) { if ($user->isCustomer()) {
$this->redirectTo = route('portal.dashboard', ['company_id' => $company->id]); $this->redirectTo = route('portal.dashboard', ['company_id' => $company->id]);
} }

View File

@ -121,7 +121,7 @@ class Users extends Controller
$landing_pages = $u->landing_pages; $landing_pages = $u->landing_pages;
if ($user->can('read-client-portal')) { if ($user->isCustomer()) {
// Show only roles with customer permission // Show only roles with customer permission
$roles = Role::all()->reject(function ($r) { $roles = Role::all()->reject(function ($r) {
return !$r->hasPermission('read-client-portal'); return !$r->hasPermission('read-client-portal');

View File

@ -26,7 +26,7 @@ class Header
if (!empty($user)) { if (!empty($user)) {
// Get customer company // Get customer company
if ($user->can('read-client-portal')) { if ($user->isCustomer()) {
$company = (object) [ $company = (object) [
'company_name' => setting('company.name'), 'company_name' => setting('company.name'),
'company_email' => setting('company.email'), 'company_email' => setting('company.email'),

View File

@ -39,7 +39,7 @@ class CreateDocumentTransaction
return $this->getResponse('signed.' . $type . '.show', $document, $message); return $this->getResponse('signed.' . $type . '.show', $document, $message);
} }
if ($user->can('read-client-portal')) { if ($user->isCustomer()) {
flash($message)->error()->important(); flash($message)->error()->important();
return $this->getResponse('portal.' . $type . '.show', $document, $message); return $this->getResponse('portal.' . $type . '.show', $document, $message);

View File

@ -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 * @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder * @return \Illuminate\Database\Eloquent\Builder
@ -192,6 +192,28 @@ class User extends Authenticatable implements HasLocalePreference
return $query->where('enabled', 1); 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. * Attach company_ids attribute to model.
* *
@ -216,6 +238,26 @@ class User extends Authenticatable implements HasLocalePreference
$this->offsetUnset('company_ids'); $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. * Get the user's preferred locale.
* *

View File

@ -100,7 +100,7 @@ trait Users
return route('login'); 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; $company_id = company_id() ?: optional($this->getFirstCompanyOfUser())->id;