Skip to content

Commit

Permalink
added customer scopes to user model
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdulici committed Sep 1, 2021
1 parent 980fd9a commit 1b648a2
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/Http/Controllers/Auth/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function store(Request $request)
}

// 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
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Auth/Reset.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ protected function sendResetResponse($response)
}

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

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Auth/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public function edit(User $user)

$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');
Expand Down
2 changes: 1 addition & 1 deletion app/Http/ViewComposers/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function compose(View $view)

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'),
Expand Down
2 changes: 1 addition & 1 deletion app/Listeners/Document/CreateDocumentTransaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function handle(Event $event)
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);
Expand Down
44 changes: 43 additions & 1 deletion app/Models/Auth/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public function scopeCollect($query, $sort = 'name')
}

/**
* Scope to only include active currencies.
* Scope to only include active users.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
Expand All @@ -192,6 +192,28 @@ public function scopeEnabled($query)
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.
*
Expand All @@ -216,6 +238,26 @@ public function unsetCompanyIds()
$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.
*
Expand Down
2 changes: 1 addition & 1 deletion app/Traits/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function getLandingPageOfUser()
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;

Expand Down

0 comments on commit 1b648a2

Please sign in to comment.