Skip to content

Commit

Permalink
Fix Trails
Browse files Browse the repository at this point in the history
  • Loading branch information
carloscgo committed Jun 26, 2018
1 parent 644693e commit b3d61ea
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/Traits/PermissionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected function getFreshPermissions()
public function getPermissions()
{
$primaryKey = $this[$this->primaryKey];
$cacheKey = 'caffeinated.'.substr(static::getShinobiTag(), 0, -1).'.permissions.'.$primaryKey;
$cacheKey = 'shinobi.'.substr(static::getShinobiTag(), 0, -1).'.permissions.'.$primaryKey;

if (method_exists(app()->make('cache')->getStore(), 'tags')) {
return app()->make('cache')->tags(static::getShinobiTag())->remember($cacheKey, 60, function () {
Expand Down
52 changes: 17 additions & 35 deletions src/Traits/ShinobiTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,6 @@ trait ShinobiTrait
{
use PermissionTrait;

public function setRoles()
{
$this->roles = \CarlosCGO\Shinobi\Models\Role::get();
}

public function setPermissions()
{
$this->permissions = \CarlosCGO\Shinobi\Models\Permission::get();
}

/**
* The shinobi cache tag used by the user model.
*
Expand Down Expand Up @@ -50,10 +40,8 @@ public function roles()
*/
public function getRoles()
{
$this->setRoles();

if (!is_null($this->roles)) {
return $this->roles->pluck('slug')->all();
if (!is_null($this->roles())) {
return $this->roles()->pluck('slug')->all();
}
}

Expand All @@ -68,12 +56,10 @@ public function isRole($slug)
{
$slug = strtolower($slug);

$this->setRoles();
$roles = $this->RoleUsers()->with('Roles')->get();

foreach ($this->roles as $role) {
if ($role->slug == $slug) {
return true;
}
if ($roles[0]->Roles->slug == $slug) {
return true;
}

return false;
Expand All @@ -94,9 +80,7 @@ public function assignRole($roleId = null)
$roleId = \CarlosCGO\Shinobi\Models\Role::where('slug', $roleId)->pluck('id')->first();
}

$this->setRoles();

$roles = $this->roles;
$roles = $this->roles()->get();

if (!$roles->contains($roleId)) {
return $this->roles()->attach($roleId);
Expand Down Expand Up @@ -163,9 +147,7 @@ public function revokeAllRoles()
*/
public function getUserPermissions()
{
$this->setPermissions();

return $this->permissions->pluck('slug')->all();
return $this->permissions()->pluck('slug')->all();
}

/**
Expand All @@ -177,13 +159,17 @@ protected function getFreshPermissions()
{
$permissions = [[], $this->getUserPermissions()];

$this->setRoles();
$roles = $this->RoleUsers()->with(['Roles' => function ($query) {
return $query->with(['PermissionRoles' => function ($query) {
return $query->with('Permissions')->get();
}])->get();
}])->get();

foreach ($this->roles as $role) {
$permissions[] = $role->getPermissions();
foreach ($roles[0]->Roles->PermissionRoles as $item) {
$per[] = $item->Permissions->slug;
}

return call_user_func_array('array_merge', $permissions);
return call_user_func_array('array_merge', [[], $per]);
}

/**
Expand All @@ -196,9 +182,7 @@ protected function getFreshPermissions()
*/
public function can($permission, $arguments = [])
{
$this->setRoles();

foreach ($this->roles as $role) {
foreach ($this->roles()->get() as $role) {
if ($role->special === 'no-access') {
return false;
}
Expand All @@ -220,9 +204,7 @@ public function can($permission, $arguments = [])
*/
public function canAtLeast(array $permissions)
{
$this->setRoles();

foreach ($this->roles as $role) {
foreach ($this->roles()->get() as $role) {
if ($role->special === 'no-access') {
return false;
}
Expand Down

0 comments on commit b3d61ea

Please sign in to comment.