Skip to content

Commit

Permalink
Merge 501f039 into d05514b
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafamaklad committed Feb 5, 2018
2 parents d05514b + 501f039 commit 49866ea
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 15 deletions.
29 changes: 27 additions & 2 deletions src/Models/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public static function create(array $attributes = [])
'guard_name',
$attributes['guard_name']
)->first()) {
$name = $attributes['name'];
$guardName = $attributes['guard_name'];
$name = (string) $attributes['name'];
$guardName = (string) $attributes['guard_name'];
throw new PermissionAlreadyExists($helpers->getPermissionAlreadyExistsMessage($name, $guardName));
}

Expand All @@ -69,6 +69,31 @@ public static function create(array $attributes = [])
return static::query()->create($attributes);
}

/**
* Find or create permission by its name (and optionally guardName).
*
* @param string $name
* @param string|null $guardName
*
* @return PermissionInterface
* @throws \Maklad\Permission\Exceptions\PermissionAlreadyExists
*/
public static function findOrCreate(string $name, $guardName = null): PermissionInterface
{
$guardName = $guardName ?? config('auth.defaults.guard');

$permission = static::getPermissions()
->where('name', $name)
->where('guard_name', $guardName)
->first();

if (! $permission) {
$permission = static::create(['name' => $name, 'guard_name' => $guardName]);
}

return $permission;
}

/**
* A permission can be applied to roles.
* @return BelongsToMany
Expand Down
33 changes: 30 additions & 3 deletions src/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use Jenssegers\Mongodb\Eloquent\Model;
use Jenssegers\Mongodb\Relations\BelongsToMany;
use Maklad\Permission\Contracts\PermissionInterface;
use Maklad\Permission\Contracts\RoleInterface;
use Maklad\Permission\Exceptions\GuardDoesNotMatch;
use Maklad\Permission\Exceptions\RoleAlreadyExists;
Expand Down Expand Up @@ -55,8 +56,8 @@ public static function create(array $attributes = [])
$helpers = new Helpers();

if (static::where('name', $attributes['name'])->where('guard_name', $attributes['guard_name'])->first()) {
$name = $attributes['name'];
$guardName = $attributes['guard_name'];
$name = (string) $attributes['name'];
$guardName = (string) $attributes['guard_name'];
throw new RoleAlreadyExists($helpers->getRoleAlreadyExistsMessage($name, $guardName));
}

Expand All @@ -67,6 +68,30 @@ public static function create(array $attributes = [])
return static::query()->create($attributes);
}

/**
* Find or create role by its name (and optionally guardName).
*
* @param string $name
* @param string|null $guardName
*
* @return RoleInterface
* @throws \Maklad\Permission\Exceptions\RoleAlreadyExists
*/
public static function findOrCreate(string $name, $guardName = null): RoleInterface
{
$guardName = $guardName ?? config('auth.defaults.guard');

$role = static::where('name', $name)
->where('guard_name', $guardName)
->first();

if (! $role) {
$role = static::create(['name' => $name, 'guard_name' => $guardName]);
}

return $role;
}

/**
* A role may be given various permissions.
* @return BelongsToMany
Expand Down Expand Up @@ -100,7 +125,9 @@ public static function findByName(string $name, $guardName = null): RoleInterfac
{
$guardName = $guardName ?? \config('auth.defaults.guard');

$role = static::where('name', $name)->where('guard_name', $guardName)->first();
$role = static::where('name', $name)
->where('guard_name', $guardName)
->first();

if (! $role) {
$helpers = new Helpers();
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/HasPermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function givePermissionTo(...$permissions)
*/
public function syncPermissions(...$permissions)
{
$this->permissions()->detach();
$this->permissions()->sync([]);

return $this->givePermissionTo($permissions);
}
Expand Down
17 changes: 9 additions & 8 deletions src/Traits/HasRoles.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ trait HasRoles
public static function bootHasRoles()
{
static::deleting(function (Model $model) {
foreach ($model->roles as $role) {
$role->users()->detach($model);
}
foreach ($model->permissions as $permission) {
$permission->users()->detach($model);
if (isset($model->forceDeleting) && !$model->forceDeleting) {
return;
}

$model->roles()->sync([]);
$model->permissions()->sync([]);
});
}

Expand Down Expand Up @@ -123,7 +123,8 @@ public function removeRole(...$roles)
->flatten()
->map(function ($role) {
$role = $this->getStoredRole($role);
$this->roles()->detach($this->getStoredRole($role));
$this->roles()->detach($role);

return $role;
});

Expand All @@ -141,7 +142,7 @@ public function removeRole(...$roles)
*/
public function syncRoles(...$roles)
{
$this->roles()->detach();
$this->roles()->sync([]);

return $this->assignRole($roles);
}
Expand Down Expand Up @@ -343,7 +344,7 @@ protected function getStoredRole($role): Role
*/
public function getRoleNames(): Collection
{
return $this->roles->pluck('name');
return $this->roles()->pluck('name');
}


Expand Down
14 changes: 13 additions & 1 deletion tests/PermissionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class PermissionTest extends TestCase
/** @test */
public function it_throws_an_exception_when_the_permission_already_exists()
{
$can_logs = [true, false];
$can_logs = [true, false];

foreach ($can_logs as $can_log) {
$this->app['config']->set('permission.log_registration_exception', $can_log);
Expand Down Expand Up @@ -56,4 +56,16 @@ public function it_has_user_models_of_the_right_class()
$this->testUser->delete();
$this->assertEquals(0, $this->testUserPermission->users()->count());
}

/** @test */
public function it_creates_permission_object_with_findOrCreate_if_it_does_not_have_a_permission_object()
{
$permission = app(Permission::class)->findOrCreate('another-permission');
$this->assertFalse($this->testUserRole->hasPermissionTo($permission));

$this->testUserRole->givePermissionTo($permission);

$this->testUserRole = $this->testUserRole->fresh();
$this->assertTrue($this->testUserRole->hasPermissionTo('another-permission'));
}
}
14 changes: 14 additions & 0 deletions tests/RoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,4 +274,18 @@ public function it_belongs_to_the_default_guard_by_default()
{
$this->assertEquals($this->app['config']->get('auth.defaults.guard'), $this->testUserRole->guard_name);
}

/** @test */
public function it_creates_role_object_with_findOrCreate_if_it_does_not_have_a_role_object()
{
$role = app(Role::class)->findOrCreate('another-role');

$this->assertFalse($this->testUser->hasRole($role));

$this->testUser->assignRole($role);

$this->testUser = $this->testUser->fresh();

$this->assertTrue($this->testUser->hasRole('another-role'));
}
}

0 comments on commit 49866ea

Please sign in to comment.