Skip to content

Commit

Permalink
Merge a064b2f into 40f2e2f
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafamaklad committed Jan 25, 2018
2 parents 40f2e2f + a064b2f commit 287b302
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 24 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

All Notable changes to `laravel-permission-mongodb` will be documented in this file.

## 1.5.2 - 2018-01-25

### Added
- Added multiple Revoke Permissions
- Added multiple Remove Roles
- Remove SensioLabsInsight badge


## 1.5.1 - 2018-01-22

### Added
Expand Down
16 changes: 5 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
[![StyleCI][ico-styleci]][link-styleci]
[![Coverage Status][ico-coveralls]][link-coveralls]
[![Dependency Status][ico-gemnasium]][link-gemnasium]
[![SensioLabsInsight][ico-sensiolabs]][link-sensiolabs]
[![Total Downloads][ico-downloads]][link-packagist]
[![Laravel 5.3.x][ico-laravel-5.2]][link-laravel-5.2]
[![Laravel 5.3.x][ico-laravel-5.3]][link-laravel-5.3]
Expand Down Expand Up @@ -41,7 +40,7 @@ $user->can('edit articles');
## Table of contents
* [Installation](#installation)
* [Laravel](#laravel)
* [Lumen](#Lumen)
* [Lumen](#lumen)
* [Usage](#usage)
* [Using "direct" permissions](#using-direct-permissions)
* [Using permissions via roles](#using-permissions-via-roles)
Expand All @@ -52,6 +51,7 @@ $user->can('edit articles');
* [Using blade directives with multiple guards](#using-blade-directives-with-multiple-guards)
* [Using a middleware](#using-a-middleware)
* [Using artisan commands](#using-artisan-commands)
* [Unit Testing](#unit-testing)
* [Database Seeding](#database-seeding)
* [Extending](#extending)
* [Cache](#cache)
Expand Down Expand Up @@ -176,12 +176,6 @@ $app->register(Maklad\Permission\PermissionServiceProvider::class);
$app->configure('permission');
```

Then, run your migrations:

```bash
php artisan migrate
```

## Usage

First, add the `Maklad\Permission\Traits\HasRoles` trait to your `User` model(s):
Expand Down Expand Up @@ -560,7 +554,7 @@ You can add something in Laravel exception handler:
```php
public function render($request, Exception $exception)
{
if ($exception instanceof \Spatie\Permission\Exceptions\UnauthorizedException) {
if ($exception instanceof \Maklad\Permission\Exceptions\UnauthorizedException) {
// Code here ...
}

Expand Down Expand Up @@ -610,7 +604,7 @@ public function setUp()

Two notes about Database Seeding:

1. It is best to flush the `Maklad.permission.cache` before seeding, to avoid cache conflict errors. This can be done from an Artisan command (see Troubleshooting: Cache section, later) or directly in a seeder class (see example below).
1. It is best to flush the `maklad.permission.cache` before seeding, to avoid cache conflict errors. This can be done from an Artisan command (see Troubleshooting: Cache section, later) or directly in a seeder class (see example below).

2. Here's a sample seeder, which clears the cache, creates permissions, and then assigns permissions to roles:
```php
Expand Down Expand Up @@ -676,7 +670,7 @@ HOWEVER, if you manipulate permission/role data directly in the database instead
### Manual cache reset
To manually reset the cache for this package, run:
```bash
php artisan cache:forget spatie.permission.cache
php artisan cache:forget maklad.permission.cache
```

### Cache Identifier
Expand Down
28 changes: 18 additions & 10 deletions src/Traits/HasPermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,31 +61,39 @@ public function syncPermissions(...$permissions)
/**
* Revoke the given permission.
*
* @param Permission|string $permission
* @param string|array|Permission|\Illuminate\Support\Collection $permissions
*
* @return $this
* @throws \Maklad\Permission\Exceptions\GuardDoesNotMatch
*/
public function revokePermissionTo($permission)
public function revokePermissionTo(...$permissions)
{
$this->permissions()->detach($this->getStoredPermission($permission));
\collect($permissions)
->flatten()
->map(function ($permission) {
$permission = $this->getStoredPermission($permission);
$this->permissions()->detach($permission);

return $permission;
});

$this->forgetCachedPermissions();

return $this;
}

/**
* @param string|Permission $permissions
* @param string|Permission $permission
*
* @return Permission
*/
protected function getStoredPermission($permissions): Permission
protected function getStoredPermission($permission): Permission
{
if (\is_string($permissions)) {
return \app(Permission::class)->findByName($permissions, $this->getDefaultGuardName());
if (\is_string($permission)) {
return \app(Permission::class)->findByName($permission, $this->getDefaultGuardName());
}

return $permissions;
return $permission;
}

/**
Expand All @@ -97,8 +105,8 @@ protected function ensureModelSharesGuard(Model $roleOrPermission)
{
if (! $this->getGuardNames()->contains($roleOrPermission->guard_name)) {
$expected = $this->getGuardNames();
$given = $roleOrPermission->guard_name;
$helpers = new Helpers();
$given = $roleOrPermission->guard_name;
$helpers = new Helpers();

throw new GuardDoesNotMatch($helpers->getGuardDoesNotMatchMessage($expected, $given));
}
Expand Down
18 changes: 15 additions & 3 deletions src/Traits/HasRoles.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,23 @@ public function assignRole(...$roles)
/**
* Revoke the given role from the model.
*
* @param string|Role $role
* @param array|string|Role ...$roles
*
* @return HasRoles
*/
public function removeRole($role)
public function removeRole(...$roles)
{
$this->roles()->detach($this->getStoredRole($role));
\collect($roles)
->flatten()
->map(function ($role) {
$role = $this->getStoredRole($role);
$this->roles()->detach($this->getStoredRole($role));
return $role;
});

$this->forgetCachedPermissions();

return $this;
}

/**
Expand Down
9 changes: 9 additions & 0 deletions tests/HasPermissionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,13 @@ public function it_doesnt_detach_permissions_when_soft_deleting()
$user = SoftDeletingUser::withTrashed()->find($user->id);
$this->assertTrue($user->hasPermissionTo('edit-news'));
}

/** @test */
public function it_can_give_and_revoke_multiple_permissions()
{
$this->testUserRole->givePermissionTo(['edit-articles', 'edit-news']);
$this->assertEquals(2, $this->testUserRole->permissions()->count());
$this->testUserRole->revokePermissionTo(['edit-articles', 'edit-news']);
$this->assertEquals(0, $this->testUserRole->permissions()->count());
}
}
13 changes: 13 additions & 0 deletions tests/HasRolesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -495,4 +495,17 @@ public function it_does_not_detach_roles_when_soft_deleting()
$user = SoftDeletingUser::withTrashed()->find($user->id);
$this->assertTrue($user->hasRole('testRole'));
}

/** @test */
public function it_can_give_and_revoke_multiple_roles()
{
$this->testUser->assignRole('testRole');
$this->testUser->assignRole('testRole2');

$this->testUser->removeRole('testRole', 'testRole2');

$this->assertFalse($this->testUser->hasRole('testRole'));

$this->assertFalse($this->testUser->hasRole('testRole2'));
}
}

0 comments on commit 287b302

Please sign in to comment.