Skip to content

Commit

Permalink
Merge pull request #36 from mostafamaklad/v1.3
Browse files Browse the repository at this point in the history
V1.3.5
  • Loading branch information
mostafamaklad committed Oct 18, 2017
2 parents c690a07 + 6666dfc commit 49bc7df
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 16 deletions.
9 changes: 7 additions & 2 deletions CHANGELOG.md
Expand Up @@ -2,14 +2,19 @@

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

## 1.3.5 - 2017-10-18

### Fixed
- Fixed a bug where `Role`s and `Permission`s got detached when soft deleting a model

## 1.3.4 - 2017-09-28

### added
### Added
- Add the support of `laravel 5.2`

## 1.3.3 - 2017-09-27

### added
### Added
- Add the support of `laravel 5.3`


Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -529,7 +529,7 @@ Route::group(['middleware' => ['role:super-admin','permission:publish articles']
You can protect your controllers similarly, by setting desired middleware in the constructor:

```php
public function __construct
public function __construct()
{
$this->middleware(['role:super-admin','permission:publish articles|edit articles']);
}
Expand Down
12 changes: 6 additions & 6 deletions composer.json
Expand Up @@ -31,16 +31,16 @@
],
"require": {
"php": ">=7.0",
"illuminate/auth": "~5.2.0|~5.3.0|~5.4.0|~5.5.0",
"illuminate/container": "~5.2.0|~5.3.0|~5.4.0|~5.5.0",
"illuminate/contracts": "~5.2.0|~5.3.0|~5.4.0|~5.5.0",
"jenssegers/mongodb": "3.0.*|3.1.*|3.2.*|3.3.0-alpha"
"illuminate/auth": "^5.2.0",
"illuminate/container": "^5.2.0",
"illuminate/contracts": "^5.2.0",
"jenssegers/mongodb": "^3.0|3.3.0-alpha"
},
"require-dev": {
"codeclimate/php-test-reporter": "^0.4.4",
"monolog/monolog": "^1.23",
"orchestra/testbench": "~3.2.0|~3.3.0|~3.4.2|~3.5.0",
"phpunit/phpunit": "^5.7|^6.3",
"orchestra/testbench": "^3.2.0",
"phpunit/phpunit": "^5.7|^6.0",
"squizlabs/php_codesniffer": "^3.1"
},
"autoload": {
Expand Down
18 changes: 14 additions & 4 deletions src/Commands/CreateRole.php
Expand Up @@ -4,6 +4,7 @@
namespace Maklad\Permission\Commands;

use Illuminate\Console\Command;
use Maklad\Permission\Contracts\PermissionInterface as Permission;
use Maklad\Permission\Contracts\RoleInterface as Role;

/**
Expand All @@ -14,19 +15,28 @@ class CreateRole extends Command
{
protected $signature = 'permission:create-role
{name : The name of the role}
{guard? : The name of the guard}';
{guard? : The name of the guard}
{--permission=* : The name of the permission}';

protected $description = 'Create a role';

public function handle()
{
$roleClass = \app(Role::class);
$roleClass = \app(Role::class);

$name = $this->argument('name');
$guard = $this->argument('guard');
$permissions = $this->option('permission');

$role = $roleClass::create([
'name' => $this->argument('name'),
'guard_name' => $this->argument('guard')
'name' => $name,
'guard_name' => $guard
]);

$this->info("Role `{$role->name}` created");

$role->givePermissionTo($permissions);
$permissionsStr = $role->permissions->implode('name', '`, `');
$this->info("Permissions `{$permissionsStr}` has been given to role `{$role->name}`");
}
}
37 changes: 36 additions & 1 deletion tests/CommandTest.php
Expand Up @@ -3,8 +3,8 @@
namespace Maklad\Permission\Test;

use Artisan;
use Maklad\Permission\Models\Role;
use Maklad\Permission\Models\Permission;
use Maklad\Permission\Models\Role;

class CommandTest extends TestCase
{
Expand Down Expand Up @@ -49,4 +49,39 @@ public function it_can_create_a_permission_with_a_specific_guard()
->where('guard_name', 'api')
->get());
}

/** @test */
public function it_can_create_a_role_and_assign_permissions()
{
Artisan::call('permission:create-permission', ['name' => 'test1',]);
Artisan::call('permission:create-permission', ['name' => 'test2',]);
Artisan::call('permission:create-role', [
'name' => 'new-role',
'--permission' => ['test1', 'test2']
]);

$role = Role::findByName('new-role');
$this->assertCount(2, $role->permissions);
}

/** @test */
public function it_can_create_a_role_with_guard_and_assign_permissions()
{
Artisan::call('permission:create-permission', [
'name' => 'test1',
'guard' => 'api',
]);
Artisan::call('permission:create-permission', [
'name' => 'test2',
'guard' => 'api',
]);
Artisan::call('permission:create-role', [
'name' => 'new-role',
'guard' => 'api',
'--permission' => ['test1', 'test2']
]);

$role = Role::findByName('new-role', 'api');
$this->assertCount(2, $role->permissions);
}
}
10 changes: 10 additions & 0 deletions tests/HasPermissionsTest.php
Expand Up @@ -157,4 +157,14 @@ public function it_throws_an_exception_when_trying_to_scope_a_permission_from_an
$this->expectException(GuardDoesNotMatch::class);
User::permission($this->testAdminPermission)->get();
}

/** @test */
public function it_doesnt_detach_permissions_when_soft_deleting()
{
$user = SoftDeletingUser::create(['email' => 'test@example.com']);
$user->givePermissionTo(['edit-news']);
$user->delete();
$user = SoftDeletingUser::withTrashed()->find($user->id);
$this->assertTrue($user->hasPermissionTo('edit-news'));
}
}
14 changes: 12 additions & 2 deletions tests/HasRolesTest.php
Expand Up @@ -2,10 +2,10 @@

namespace Maklad\Permission\Test;

use Maklad\Permission\Models\Role;
use Maklad\Permission\Exceptions\RoleDoesNotExist;
use Maklad\Permission\Exceptions\GuardDoesNotMatch;
use Maklad\Permission\Exceptions\PermissionDoesNotExist;
use Maklad\Permission\Exceptions\RoleDoesNotExist;
use Maklad\Permission\Models\Role;
use Monolog\Logger;

class HasRolesTest extends TestCase
Expand Down Expand Up @@ -485,4 +485,14 @@ public function it_can_retrieve_permission_names()
$this->testUser->getPermissionNames()
);
}

/** @test */
public function it_does_not_detach_roles_when_soft_deleting()
{
$user = SoftDeletingUser::create(['email' => 'test@example.com']);
$user->assignRole('testRole');
$user->delete();
$user = SoftDeletingUser::withTrashed()->find($user->id);
$this->assertTrue($user->hasRole('testRole'));
}
}
12 changes: 12 additions & 0 deletions tests/SoftDeletingUser.php
@@ -0,0 +1,12 @@
<?php

namespace Maklad\Permission\Test;

use Illuminate\Database\Eloquent\SoftDeletes;

class SoftDeletingUser extends User
{
use SoftDeletes;

protected $guard_name = 'web';
}

0 comments on commit 49bc7df

Please sign in to comment.