Skip to content

Commit f9aee90

Browse files
Merge pull request #80 from mostafamaklad/v1.10
V1.10.0
2 parents be9ce9b + ecda13a commit f9aee90

File tree

8 files changed

+110
-43
lines changed

8 files changed

+110
-43
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22

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

5+
## 1.10.0 - 2018-09-15
6+
7+
### Added
8+
- Add migration files
9+
10+
### Changed
11+
- Update PermissionRegistrar to use Authorizable
12+
- Improve readme description of how defaults work with multiple guards
13+
- Replacing static Permission::class and Role::class with dynamic value
14+
- Improve speed of findByName
15+
516
## 1.9.0 - 2018-09-14
617

718
### Fixed

README.md

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# laravel-permission-mongodb
22

3-
[![Latest Version on Packagist][ico-version]][link-packagist]
3+
[![Latest Version on Packagist][ico-version]][link-releases]
44
[![Software License][ico-license]](LICENSE.md)
55
[![Build Status][ico-travis]][link-travis]
66
[![Scrutinizer][ico-scrutinizer]][link-scrutinizer]
@@ -540,25 +540,16 @@ However when using multiple guards they will act like namespaces for your permis
540540

541541
### Using permissions and roles with multiple guards
542542

543-
By default the default guard (`config('auth.defaults.guard')`) will be used as the guard for new permissions and roles. When creating permissions and roles for specific guards you'll have to specify their `guard_name` on the model:
543+
When creating new permissions and roles, if no guard is specified, then the **first** defined guard in `auth.guards` config array will be used. When creating permissions and roles for specific guards you'll have to specify their `guard_name` on the model:
544544

545545
```php
546546
// Create a superadmin role for the admin users
547-
$role = Role::create(['guard_name' => 'admin', 'name' => 'superadmin']);
548547

549-
// Define a `publish articles` permission for the admin users belonging to the admin guard
550-
$permission = Permission::create(['guard_name' => 'admin', 'name' => 'publish articles']);
551-
552-
// Define a *different* `publish articles` permission for the regular users belonging to the web guard
553-
$permission = Permission::create(['guard_name' => 'web', 'name' => 'publish articles']);
554-
```
555-
556-
To check if a user has permission for a specific guard:
557-
558-
```php
559548
$user->hasPermissionTo('publish articles', 'admin');
560549
```
561550

551+
> **Note**: When determining whether a role/permission is valid on a given model, it chooses the guard in this order: first the `$guard_name` property of the model; then the guard in the config (through a provider); then the first-defined guard in the `auth.guards` config array; then the `auth.defaults.guard` config.
552+
562553
### Assigning permissions and roles to guard users
563554

564555
You can use the same methods to assign permissions and roles to users as described above in [using permissions via roles](#using-permissions-via-roles). Just make sure the `guard_name` on the permission or role matches the guard of the user, otherwise a `GuardDoesNotMatch` exception will be thrown.
@@ -826,6 +817,7 @@ The MIT License (MIT). Please see [License File](LICENSE.md) for more informatio
826817

827818
[link-author]: https://github.com/mostafamaklad
828819
[link-contributors]: ../../contributors
820+
[link-releases]: ../../releases
829821
[link-laravel-permission]: https://github.com/spatie/laravel-permission
830822
[link-laravel-mongodb]: https://github.com/jenssegers/laravel-mongodb
831823
[link-freekmurze]: https://github.com/freekmurze

src/Contracts/PermissionInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ public function roles(): BelongsToMany;
2121
* Find a permission by its name.
2222
*
2323
* @param string $name
24-
* @param string|null $guardName
24+
* @param string $guardName
2525
*
2626
* @throws PermissionDoesNotExist
2727
*
2828
* @return PermissionInterface
2929
*/
30-
public static function findByName(string $name, $guardName): PermissionInterface;
30+
public static function findByName(string $name, string $guardName): PermissionInterface;
3131
}

src/Models/Permission.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,15 @@ public function __construct(array $attributes = [])
5555
*/
5656
public static function create(array $attributes = [])
5757
{
58-
$helpers = new Helpers();
58+
$helpers = new Helpers();
5959
$attributes['guard_name'] = $attributes['guard_name'] ?? (new Guard())->getDefaultName(static::class);
6060

6161
if (static::getPermissions()->where('name', $attributes['name'])->where(
6262
'guard_name',
6363
$attributes['guard_name']
6464
)->first()) {
65-
$name = (string) $attributes['name'];
66-
$guardName = (string) $attributes['guard_name'];
65+
$name = (string)$attributes['name'];
66+
$guardName = (string)$attributes['guard_name'];
6767
throw new PermissionAlreadyExists($helpers->getPermissionAlreadyExistsMessage($name, $guardName));
6868
}
6969

@@ -78,22 +78,21 @@ public static function create(array $attributes = [])
7878
* Find or create permission by its name (and optionally guardName).
7979
*
8080
* @param string $name
81-
* @param string|null $guardName
81+
* @param string $guardName
8282
*
8383
* @return PermissionInterface
8484
* @throws \Maklad\Permission\Exceptions\PermissionAlreadyExists
8585
* @throws \ReflectionException
8686
*/
87-
public static function findOrCreate(string $name, $guardName = null): PermissionInterface
87+
public static function findOrCreate(string $name, string $guardName = null): PermissionInterface
8888
{
8989
$guardName = $guardName ?? (new Guard())->getDefaultName(static::class);
9090

91-
$permission = static::getPermissions()
92-
->where('name', $name)
93-
->where('guard_name', $guardName)
94-
->first();
91+
$permission = static::getPermissions()->filter(function ($permission) use ($name, $guardName) {
92+
return $permission->name === $name && $permission->guard_name === $guardName;
93+
})->first();
9594

96-
if (! $permission) {
95+
if (!$permission) {
9796
$permission = static::create(['name' => $name, 'guard_name' => $guardName]);
9897
}
9998

@@ -122,19 +121,21 @@ public function users()
122121
* Find a permission by its name (and optionally guardName).
123122
*
124123
* @param string $name
125-
* @param string|null $guardName
124+
* @param string $guardName
126125
*
127126
* @return PermissionInterface
128127
* @throws PermissionDoesNotExist
129128
* @throws \ReflectionException
130129
*/
131-
public static function findByName(string $name, $guardName = null): PermissionInterface
130+
public static function findByName(string $name, string $guardName = null): PermissionInterface
132131
{
133132
$guardName = $guardName ?? (new Guard())->getDefaultName(static::class);
134133

135-
$permission = static::getPermissions()->where('name', $name)->where('guard_name', $guardName)->first();
134+
$permission = static::getPermissions()->filter(function ($permission) use ($name, $guardName) {
135+
return $permission->name === $name && $permission->guard_name === $guardName;
136+
})->first();
136137

137-
if (! $permission) {
138+
if (!$permission) {
138139
$helpers = new Helpers();
139140
throw new PermissionDoesNotExist($helpers->getPermissionDoesNotExistMessage($name, $guardName));
140141
}

src/Models/Role.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public static function findByName(string $name, $guardName = null): RoleInterfac
134134
public function hasPermissionTo($permission): bool
135135
{
136136
if (\is_string($permission)) {
137-
$permission = app(Permission::class)->findByName($permission, $this->getDefaultGuardName());
137+
$permission = $this->getPermissionClass()->findByName($permission, $this->getDefaultGuardName());
138138
}
139139

140140
if (! $this->getGuardNames()->contains($permission->guard_name)) {

src/PermissionRegistrar.php

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace Maklad\Permission;
44

5+
use Illuminate\Contracts\Auth\Access\Authorizable;
56
use Illuminate\Contracts\Auth\Access\Gate;
67
use Illuminate\Contracts\Cache\Repository;
78
use Illuminate\Support\Collection;
8-
use Jenssegers\Mongodb\Eloquent\Model;
99
use Maklad\Permission\Contracts\PermissionInterface as Permission;
1010

1111
/**
@@ -23,32 +23,78 @@ class PermissionRegistrar
2323
/** @var string */
2424
protected $cacheKey = 'maklad.permission.cache';
2525

26+
/** @var string */
27+
protected $permissionClass;
28+
29+
/** @var string */
30+
protected $roleClass;
31+
32+
/**
33+
* PermissionRegistrar constructor.
34+
* @param Gate $gate
35+
* @param Repository $cache
36+
*/
2637
public function __construct(Gate $gate, Repository $cache)
2738
{
28-
$this->gate = $gate;
39+
$this->gate = $gate;
2940
$this->cache = $cache;
41+
$this->permissionClass = config('permission.models.permission');
42+
$this->roleClass = config('permission.models.role');
3043
}
3144

45+
/**
46+
* Register Permissions
47+
*
48+
* @return bool
49+
*/
3250
public function registerPermissions(): bool
3351
{
3452
$this->getPermissions()->map(function (Permission $permission) {
35-
$this->gate->define($permission->name, function (Model $user) use ($permission) {
53+
$this->gate->define($permission->name, function (Authorizable $user) use ($permission) {
3654
return $user->hasPermissionTo($permission) ?: null;
3755
});
3856
});
3957

4058
return true;
4159
}
4260

61+
/**
62+
* Forget cached permission
63+
*/
4364
public function forgetCachedPermissions()
4465
{
4566
$this->cache->forget($this->cacheKey);
4667
}
4768

69+
/**
70+
* Get Permissions
71+
*
72+
* @return Collection
73+
*/
4874
public function getPermissions(): Collection
4975
{
50-
return $this->cache->remember($this->cacheKey, \config('permission.cache_expiration_time'), function () {
51-
return \app(config('permission.models.permission'))->with('roles')->get();
76+
return $this->cache->remember($this->cacheKey, config('permission.cache_expiration_time'), function () {
77+
return $this->getPermissionClass()->with('roles')->get();
5278
});
5379
}
80+
81+
/**
82+
* Get Permission class
83+
*
84+
* @return \Illuminate\Foundation\Application|mixed
85+
*/
86+
public function getPermissionClass()
87+
{
88+
return app($this->permissionClass);
89+
}
90+
91+
/**
92+
* Get Role class
93+
*
94+
* @return \Illuminate\Foundation\Application|mixed
95+
*/
96+
public function getRoleClass()
97+
{
98+
return app($this->roleClass);
99+
}
54100
}

src/Traits/HasPermissions.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
*/
2020
trait HasPermissions
2121
{
22+
private $permissionClass;
23+
2224
public static function bootHasPermissions()
2325
{
2426
static::deleting(function (Model $model) {
@@ -30,6 +32,14 @@ public static function bootHasPermissions()
3032
});
3133
}
3234

35+
public function getPermissionClass()
36+
{
37+
if ($this->permissionClass === null) {
38+
$this->permissionClass = app(PermissionRegistrar::class)->getPermissionClass();
39+
}
40+
return $this->permissionClass;
41+
}
42+
3343
/**
3444
* A role may be given various permissions.
3545
* @return BelongsToMany
@@ -122,7 +132,7 @@ public function revokePermissionTo(...$permissions): self
122132
protected function getStoredPermission($permission): Permission
123133
{
124134
if (\is_string($permission)) {
125-
return \app(config('permission.models.permission'))->findByName($permission, $this->getDefaultGuardName());
135+
return $this->getPermissionClass()->findByName($permission, $this->getDefaultGuardName());
126136
}
127137

128138
return $permission;
@@ -214,7 +224,6 @@ public function getPermissionsViaRoles(): Collection
214224
->roles->flatMap(function (Role $role) {
215225
return $role->permissions;
216226
})->sort()->values();
217-
//return \app(\config('permission.models.permission'))->whereIn('role_id', $this->role_ids)->get();
218227
}
219228

220229
/**
@@ -240,7 +249,7 @@ public function getAllPermissions(): Collection
240249
public function hasPermissionTo($permission, $guardName = null): bool
241250
{
242251
if (\is_string($permission)) {
243-
$permission = \app(\config('permission.models.permission'))->findByName(
252+
$permission = $this->getPermissionClass()->findByName(
244253
$permission,
245254
$guardName ?? $this->getDefaultGuardName()
246255
);
@@ -295,10 +304,7 @@ protected function hasPermissionViaRole(Permission $permission): bool
295304
public function hasDirectPermission($permission): bool
296305
{
297306
if (\is_string($permission)) {
298-
$permission = \app(
299-
\config('permission.models.permission')
300-
)
301-
->findByName($permission, $this->getDefaultGuardName());
307+
$permission = $this->getPermissionClass()->findByName($permission, $this->getDefaultGuardName());
302308
}
303309

304310
return $this->permissions->contains('id', $permission->id);

src/Traits/HasRoles.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Jenssegers\Mongodb\Eloquent\Builder;
77
use Jenssegers\Mongodb\Eloquent\Model;
88
use Maklad\Permission\Contracts\RoleInterface as Role;
9+
use Maklad\Permission\PermissionRegistrar;
910
use ReflectionException;
1011

1112
/**
@@ -16,6 +17,8 @@ trait HasRoles
1617
{
1718
use HasPermissions;
1819

20+
private $roleClass;
21+
1922
public static function bootHasRoles()
2023
{
2124
static::deleting(function (Model $model) {
@@ -27,6 +30,14 @@ public static function bootHasRoles()
2730
});
2831
}
2932

33+
public function getRoleClass()
34+
{
35+
if ($this->roleClass === null) {
36+
$this->roleClass = app(PermissionRegistrar::class)->getRoleClass();
37+
}
38+
return $this->roleClass;
39+
}
40+
3041
/**
3142
* A model may have multiple roles.
3243
*/
@@ -184,7 +195,7 @@ public function hasAllRoles($roles): bool
184195
protected function getStoredRole($role): Role
185196
{
186197
if (\is_string($role)) {
187-
return \app(\config('permission.models.role'))->findByName($role, $this->getDefaultGuardName());
198+
return $this->getRoleClass()->findByName($role, $this->getDefaultGuardName());
188199
}
189200

190201
return $role;

0 commit comments

Comments
 (0)