Skip to content

Commit de7e689

Browse files
authored
Merge branch 'master' into l9-compatibility
2 parents 4d73a65 + f16effc commit de7e689

File tree

4 files changed

+65
-36
lines changed

4 files changed

+65
-36
lines changed

.travis.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
language: php
22
sudo: required
3-
dist: trusty
3+
dist: focal
44
group: edge
55

66
php:
7-
- 7.3
8-
- 7.4
7+
- 8.1
98

109
sudo: false
1110

composer.json

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jeremykenedy/laravel-roles",
3-
"description": "A Powerful package for handling roles and permissions in Laravel. Supports Laravel 5.3, 5.4, 5.5, 5.6, 5.7, 5.8, 6.0, and 7.0.",
3+
"description": "A Powerful package for handling roles and permissions in Laravel. Supports Laravel 5.3, 5.4, 5.5, 5.6, 5.7, 5.8, 6.0, 7.0, 8.0 and 9.0.",
44
"keywords": [
55
"laravel-roles",
66
"laravel-permissions",
@@ -24,17 +24,23 @@
2424
}
2525
],
2626
"require": {
27-
"php": "^7.2|^8.0",
27+
"php": "^7.2|^8.0.2|^8.1",
2828
"laravel/framework": "5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|6.*|7.*|8.*|^9.0",
29-
"eklundkristoffer/seedster": "^4.0",
30-
"laravel/helpers": "^1.3"
29+
"eklundkristoffer/seedster": "dev-l9-support",
30+
"laravel/helpers": "^1.5"
3131
},
3232
"require-dev": {
3333
"orchestra/testbench": "^6.0|^7.0",
34-
"laravel/tinker": "^2.4",
34+
"laravel/tinker": "^2.7",
3535
"illuminate/support": "^8.5|^9.0",
36-
"laravel/laravel": "^8.0"
36+
"laravel/laravel": "^8.0|^9.0"
3737
},
38+
"repositories": [
39+
{
40+
"type": "vcs",
41+
"url": "https://github.com/rafaelqm/seedster.git"
42+
}
43+
],
3844
"autoload": {
3945
"psr-4": {
4046
"jeremykenedy\\LaravelRoles\\": "src/"

readme.md

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -547,30 +547,29 @@ You can catch these exceptions inside `app/Exceptions/Handler.php` file and do w
547547
* Render an exception into an HTTP response.
548548
*
549549
* @param \Illuminate\Http\Request $request
550-
* @param \Exception $exception
550+
* @param \Throwable $e
551551
* @return \Illuminate\Http\Response
552552
*/
553-
public function render($request, Exception $exception)
554-
{
555-
556-
$userLevelCheck = $exception instanceof \jeremykenedy\LaravelRoles\App\Exceptions\RoleDeniedException ||
557-
$exception instanceof \jeremykenedy\LaravelRoles\App\Exceptions\PermissionDeniedException ||
558-
$exception instanceof \jeremykenedy\LaravelRoles\App\Exceptions\LevelDeniedException;
559-
560-
if ($userLevelCheck) {
561-
562-
if ($request->expectsJson()) {
563-
return Response::json(array(
564-
'error' => 403,
565-
'message' => 'Unauthorized.'
566-
), 403);
567-
}
568-
569-
abort(403);
570-
}
571-
572-
return parent::render($request, $exception);
573-
}
553+
public function render($request, Throwable $e)
554+
{
555+
$userLevelCheck = $e instanceof \jeremykenedy\LaravelRoles\App\Exceptions\RoleDeniedException ||
556+
$e instanceof \jeremykenedy\LaravelRoles\App\Exceptions\PermissionDeniedException ||
557+
$e instanceof \jeremykenedy\LaravelRoles\App\Exceptions\LevelDeniedException;
558+
559+
if ($userLevelCheck) {
560+
561+
if ($request->expectsJson()) {
562+
return Response::json(array(
563+
'error' => 403,
564+
'message' => 'Unauthorized.'
565+
), 403);
566+
}
567+
568+
abort(403);
569+
}
570+
571+
return parent::render($request, $e);
572+
}
574573
```
575574

576575
---

src/Traits/HasRoleAndPermission.php

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,22 @@ public function roles()
4444
*/
4545
public function getRoles()
4646
{
47-
return (!$this->roles) ? $this->roles = $this->roles()->get() : $this->roles;
47+
if (!$this->roles) {
48+
if (method_exists($this, 'loadMissing')) {
49+
$this->loadMissing('roles');
50+
} else {
51+
if (!array_key_exists('roles', $this->relations)) {
52+
$this->load('roles');
53+
}
54+
}
55+
if (method_exists($this, 'getRelation')) {
56+
$this->roles = $this->getRelation('roles');
57+
} else {
58+
$this->roles = $this->relations['roles'];
59+
}
60+
}
61+
62+
return $this->roles;
4863
}
4964

5065
/**
@@ -130,7 +145,7 @@ public function attachRole($role)
130145
if ($this->getRoles()->contains($role)) {
131146
return true;
132147
}
133-
$this->roles = null;
148+
$this->resetRoles();
134149

135150
return $this->roles()->attach($role);
136151
}
@@ -144,7 +159,7 @@ public function attachRole($role)
144159
*/
145160
public function detachRole($role)
146161
{
147-
$this->roles = null;
162+
$this->resetRoles();
148163

149164
return $this->roles()->detach($role);
150165
}
@@ -156,7 +171,7 @@ public function detachRole($role)
156171
*/
157172
public function detachAllRoles()
158173
{
159-
$this->roles = null;
174+
$this->resetRoles();
160175

161176
return $this->roles()->detach();
162177
}
@@ -170,7 +185,7 @@ public function detachAllRoles()
170185
*/
171186
public function syncRoles($roles)
172187
{
173-
$this->roles = null;
188+
$this->resetRoles();
174189

175190
return $this->roles()->sync($roles);
176191
}
@@ -442,6 +457,16 @@ private function getArrayFrom($argument)
442457
return (!is_array($argument)) ? preg_split('/ ?[,|] ?/', $argument) : $argument;
443458
}
444459

460+
protected function resetRoles()
461+
{
462+
$this->roles = null;
463+
if (method_exists($this, 'unsetRelation')) {
464+
$this->unsetRelation('roles');
465+
} else {
466+
unset($this->relations['roles']);
467+
}
468+
}
469+
445470
public function callMagic($method, $parameters)
446471
{
447472
if (starts_with($method, 'is')) {

0 commit comments

Comments
 (0)