Skip to content

Commit

Permalink
Permission check refactoring and add two methods for role checking
Browse files Browse the repository at this point in the history
  • Loading branch information
santigarcor committed Sep 1, 2017
1 parent 717cdfc commit c1d6515
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 20 deletions.
6 changes: 5 additions & 1 deletion src/Laratrust/Traits/LaratrustRoleTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static function bootLaratrustRoleTrait()
$flushCache = function ($role) {
$role->flushCache();
};

// If the role doesn't use SoftDeletes.
if (method_exists(static::class, 'restored')) {
static::restored($flushCache);
Expand Down Expand Up @@ -111,6 +111,10 @@ public static function bootLaratrustRoleTrait()
public function hasPermission($permission, $requireAll = false)
{
if (is_array($permission)) {
if (empty($permission)) {
return true;
}

foreach ($permission as $permissionName) {
$hasPermission = $this->hasPermission($permissionName);

Expand Down
45 changes: 32 additions & 13 deletions src/Laratrust/Traits/LaratrustUserTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,32 @@ public function hasRole($name, $team = null, $requireAll = false)
return false;
}

/**
* Checks if the user has a role by its name.
*
* @param string|array $name Role name or array of role names.
* @param string|bool $team Team name or requiredAll roles.
* @param bool $requireAll All roles in the array are required.
* @return bool
*/
public function isA($role, $team = null, $requireAll = false)
{
return $this->hasRole($role, $team, $requireAll);
}

/**
* Checks if the user has a role by its name.
*
* @param string|array $name Role name or array of role names.
* @param string|bool $team Team name or requiredAll roles.
* @param bool $requireAll All roles in the array are required.
* @return bool
*/
public function isAn($role, $team = null, $requireAll = false)
{
return $this->hasRole($role, $team, $requireAll);
}

/**
* Check if user has a permission by its name.
*
Expand Down Expand Up @@ -206,24 +232,17 @@ public function hasPermission($permission, $team = null, $requireAll = false)
$team = $this->fetchTeam($team);

foreach ($this->cachedPermissions() as $perm) {
if (!$this->isInSameTeam($perm, $team)) {
continue;
}

if (str_is($permission, $perm->name)) {
if ($this->isInSameTeam($perm, $team)
&& str_is($permission, $perm->name)) {
return true;
}
}

foreach ($this->cachedRoles() as $role) {
if (!$this->isInSameTeam($role, $team)) {
continue;
}

foreach ($role->cachedPermissions() as $perm) {
if (str_is($permission, $perm->name)) {
return true;
}
if ($this->isInSameTeam($role, $team)
&& $role->hasPermission($permission)
) {
return true;
}
}

Expand Down
10 changes: 5 additions & 5 deletions tests/LaratrustUserTest.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Mockery as m;
use Laratrust\Contracts\Ownable;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Config;
use Laratrust\Contracts\LaratrustUserInterface;
use Laratrust\Contracts\Ownable;
use Illuminate\Database\Eloquent\Model;
use Laratrust\Traits\LaratrustUserTrait;
use Mockery as m;
use Illuminate\Database\Eloquent\SoftDeletes;
use Laratrust\Contracts\LaratrustUserInterface;

class LaratrustUserTest extends UserTest
{
Expand Down
4 changes: 3 additions & 1 deletion tests/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ protected function mockPermission($permName, $team_id = null)

protected function mockRole($roleName, $team_id = null)
{
$roleMock = m::mock('Laratrust\Role');
$roleMock = m::mock('Laratrust\LaratrustRole')->makePartial();

Config::shouldReceive('get')->with('laratrust.user_models')->andReturn([]);
$roleMock->name = $roleName;
$roleMock->perms = [];
$roleMock->permissions = [];
Expand Down

0 comments on commit c1d6515

Please sign in to comment.