Skip to content

Commit

Permalink
Merge pull request #76 from mostafamaklad/v1.9
Browse files Browse the repository at this point in the history
V1.9
  • Loading branch information
mostafamaklad committed Nov 13, 2018
2 parents d2d7449 + 24825bd commit 3ee339b
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 30 deletions.
8 changes: 7 additions & 1 deletion .scrutinizer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ build:
coverage:
file: 'coverage'
format: 'clover'
nodes:
analysis:
tests:
override:
- php-scrutinizer-run

filter:
excluded_paths: [tests/*]
excluded_paths:
- "tests/"

checks:
php:
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/CreatePermission.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class CreatePermission extends Command

public function handle()
{
$permissionClass = \app(Permission::class);
$permissionClass = \app(\config('permission.models.permission'));

$permission = $permissionClass::create([
'name' => $this->argument('name'),
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/CreateRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class CreateRole extends Command

public function handle()
{
$roleClass = \app(Role::class);
$roleClass = \app(\config('permission.models.role'));

$name = $this->argument('name');
$guard = $this->argument('guard');
Expand Down
6 changes: 5 additions & 1 deletion src/Guard.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,22 @@ class Guard
*/
public function getNames($model) : Collection
{
$guardName = null;
$class = null;

if (\is_object($model)) {
$guardName = $model->guard_name ?? null;
}

if (! isset($guardName)) {
if ($guardName === null) {
$class = \is_object($model) ? \get_class($model) : $model;
$guardName = (new \ReflectionClass($class))->getDefaultProperties()['guard_name'] ?? null;
}

if ($guardName) {
return collect($guardName);
}

return collect(config('auth.guards'))
->map(function ($guard) {
if (! isset($guard['provider'])) {
Expand Down
2 changes: 1 addition & 1 deletion src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,6 @@ public function getUserNotLoggedINMessage(): string

public function isNotLumen(): bool
{
return ! stripos(app()->version(), 'lumen');
return ! (stripos(app()->version(), 'lumen') !== false);
}
}
2 changes: 1 addition & 1 deletion src/Models/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function roles(): BelongsToMany
* A permission belongs to some users of the model associated with its guard.
* @return BelongsToMany
*/
public function users(): BelongsToMany
public function users()
{
return $this->belongsToMany($this->helpers->getModelForGuard($this->attributes['guard_name']));
}
Expand Down
2 changes: 1 addition & 1 deletion src/PermissionRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function forgetCachedPermissions()
public function getPermissions(): Collection
{
return $this->cache->remember($this->cacheKey, \config('permission.cache_expiration_time'), function () {
return \app(Permission::class)->with('roles')->get();
return \app(config('permission.models.permission'))->with('roles')->get();
});
}
}
17 changes: 11 additions & 6 deletions src/Traits/HasPermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Maklad\Permission\Guard;
use Maklad\Permission\Helpers;
use Maklad\Permission\Models\Role;
use Maklad\Permission\Models\PermissionRole;
use Maklad\Permission\PermissionRegistrar;

/**
Expand Down Expand Up @@ -55,7 +56,7 @@ public function users(): BelongsToMany
* @return $this
* @throws GuardDoesNotMatch
*/
public function givePermissionTo(...$permissions)
public function givePermissionTo(...$permissions): self
{
$permissions = collect($permissions)
->flatten()
Expand All @@ -82,7 +83,7 @@ public function givePermissionTo(...$permissions)
* @return $this
* @throws GuardDoesNotMatch
*/
public function syncPermissions(...$permissions)
public function syncPermissions(...$permissions): self
{
$this->permissions()->sync([]);

Expand All @@ -97,7 +98,7 @@ public function syncPermissions(...$permissions)
* @return $this
* @throws \Maklad\Permission\Exceptions\GuardDoesNotMatch
*/
public function revokePermissionTo(...$permissions)
public function revokePermissionTo(...$permissions): self
{
collect($permissions)
->flatten()
Expand All @@ -122,7 +123,7 @@ public function revokePermissionTo(...$permissions)
protected function getStoredPermission($permission): Permission
{
if (\is_string($permission)) {
return \app(Permission::class)->findByName($permission, $this->getDefaultGuardName());
return \app(config('permission.models.permission'))->findByName($permission, $this->getDefaultGuardName());
}

return $permission;
Expand Down Expand Up @@ -214,6 +215,7 @@ public function getPermissionsViaRoles(): Collection
->roles->flatMap(function (Role $role) {
return $role->permissions;
})->sort()->values();
//return \app(\config('permission.models.permission'))->whereIn('role_id', $this->role_ids)->get();
}

/**
Expand All @@ -239,7 +241,7 @@ public function getAllPermissions(): Collection
public function hasPermissionTo($permission, $guardName = null): bool
{
if (\is_string($permission)) {
$permission = \app(Permission::class)->findByName(
$permission = \app(\config('permission.models.permission'))->findByName(
$permission,
$guardName ?? $this->getDefaultGuardName()
);
Expand Down Expand Up @@ -294,7 +296,10 @@ protected function hasPermissionViaRole(Permission $permission): bool
public function hasDirectPermission($permission): bool
{
if (\is_string($permission)) {
$permission = \app(Permission::class)->findByName($permission, $this->getDefaultGuardName());
$permission = \app(
\config('permission.models.permission')
)
->findByName($permission, $this->getDefaultGuardName());
}

return $this->permissions->contains('id', $permission->id);
Expand Down
9 changes: 4 additions & 5 deletions src/Traits/HasRoles.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Illuminate\Support\Collection;
use Jenssegers\Mongodb\Eloquent\Builder;
use Jenssegers\Mongodb\Eloquent\Model;
use Jenssegers\Mongodb\Relations\BelongsToMany;
use Maklad\Permission\Contracts\RoleInterface as Role;
use ReflectionException;

Expand All @@ -31,9 +30,9 @@ public static function bootHasRoles()
/**
* A model may have multiple roles.
*/
public function roles(): BelongsToMany
public function roles()
{
return $this->belongsToMany(\config('permission.models.role'))->withTimestamps();
return $this->belongsToMany(config('permission.models.role'));
}

/**
Expand Down Expand Up @@ -105,7 +104,7 @@ public function removeRole(...$roles)
*
* @param array ...$roles
*
* @return $this
* @return array|Role|string
*/
public function syncRoles(...$roles)
{
Expand Down Expand Up @@ -185,7 +184,7 @@ public function hasAllRoles($roles): bool
protected function getStoredRole($role): Role
{
if (\is_string($role)) {
return \app(Role::class)->findByName($role, $this->getDefaultGuardName());
return \app(\config('permission.models.role'))->findByName($role, $this->getDefaultGuardName());
}

return $role;
Expand Down
4 changes: 2 additions & 2 deletions src/Traits/RefreshesPermissionCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ trait RefreshesPermissionCache
public static function bootRefreshesPermissionCache()
{
static::saved(function () {
\app(PermissionRegistrar::class)->forgetCachedPermissions();
\app(\config('permission.models.permission'))->forgetCachedPermissions();
});

static::deleted(function () {
\app(PermissionRegistrar::class)->forgetCachedPermissions();
\app(\config('permission.models.permission'))->forgetCachedPermissions();
});
}
}
6 changes: 3 additions & 3 deletions tests/PermissionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public function it_throws_an_exception_when_the_permission_already_exists()
try {
$this->expectException(PermissionAlreadyExists::class);

\app(Permission::class)->create(['name' => 'test-permission']);
\app(Permission::class)->create(['name' => 'test-permission']);
\app(\config('permission.models.permission'))->create(['name' => 'test-permission']);
\app(\config('permission.models.permission'))->create(['name' => 'test-permission']);
} finally {
$message = $this->helpers->getPermissionAlreadyExistsMessage('test-permission', 'web');
$this->assertLogMessage($message, Logger::ALERT);
Expand All @@ -31,7 +31,7 @@ public function it_throws_an_exception_when_the_permission_already_exists()
/** @test */
public function it_belongs_to_a_guard()
{
$permission = \app(Permission::class)->create(['name' => 'can-edit', 'guard_name' => 'admin']);
$permission = \app(\config('permission.models.permission'))->create(['name' => 'can-edit', 'guard_name' => 'admin']);

$this->assertEquals('admin', $permission->guard_name);
}
Expand Down
6 changes: 3 additions & 3 deletions tests/RoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public function it_throws_an_exception_when_the_role_already_exists()
try {
$this->expectException(RoleAlreadyExists::class);

\app(Role::class)->create(['name' => 'test-role']);
\app(Role::class)->create(['name' => 'test-role']);
\app(\config('permission.models.role'))->create(['name' => 'test-role']);
\app(\config('permission.models.role'))->create(['name' => 'test-role']);
} finally {
$message = $this->helpers->getRoleAlreadyExistsMessage('test-role', 'web');
$this->assertLogMessage($message, Logger::ALERT);
Expand Down Expand Up @@ -264,7 +264,7 @@ public function it_throws_an_exception_when_a_permission_of_the_wrong_guard_is_p
/** @test */
public function it_belongs_to_a_guard()
{
$role = \app(Role::class)->create(['name' => 'admin', 'guard_name' => 'admin']);
$role = \app(\config('permission.models.role'))->create(['name' => 'admin', 'guard_name' => 'admin']);

$this->assertEquals('admin', $role->guard_name);
}
Expand Down
8 changes: 4 additions & 4 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ public function setUp()
$this->reloadPermissions();

$this->testUser = User::first();
$this->testUserRole = \app(Role::class)->where('name', 'testRole')->first();
$this->testUserPermission = \app(Permission::class)->where('name', 'edit-articles')->first();
$this->testUserRole = \app(\config('permission.models.role'))->where('name', 'testRole')->first();
$this->testUserPermission = \app(\config('permission.models.permission'))->where('name', 'edit-articles')->first();

$this->testAdmin = Admin::first();
$this->testAdminRole = \app(Role::class)->where('name', 'testAdminRole')->first();
$this->testAdminPermission = \app(Permission::class)->where('name', 'admin-permission')->first();
$this->testAdminRole = \app(\config('permission.models.role'))->where('name', 'testAdminRole')->first();
$this->testAdminPermission = \app(\config('permission.models.permission'))->where('name', 'admin-permission')->first();

$this->clearLogTestHandler();

Expand Down

0 comments on commit 3ee339b

Please sign in to comment.