Skip to content

Commit e36ddb9

Browse files
authored
Add compatiblity with Laravel 5.5 (#433)
* make compatible with laravel 5.5 * Replaced created and updated event by saved event * Remove pivot entries on model delete * Minor cleanup * Updated deps for Laravel 5.4 * Apply fixes from StyleCI * Update README.md (#428) * prepare for release * prepare for release
1 parent 42025f0 commit e36ddb9

11 files changed

+36
-23
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ language: php
33
php:
44
- 7.0
55
- 7.1
6+
- 7.2
67

78
env:
89
matrix:

CHANGELOG.md

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

33
All notable changes to `laravel-permission` will be documented in this file
44

5+
## 2.5.0 - 2017-08-30
6+
- add compatiblity with Laravel 5.5
7+
58
## 2.4.2 - 2017-08-11
69
- automatically detach roles and permissions when a user gets deleted
710

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ You can install the package via composer:
4848
composer require spatie/laravel-permission
4949
```
5050

51-
Now add the service provider in `config/app.php` file:
51+
In Laravel 5.5 the service provider will automatically get registered. In older versions of the framework just add the service provider in `config/app.php` file:
5252

5353
```php
5454
'providers' => [

composer.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@
2020
],
2121
"require": {
2222
"php" : ">=7.0",
23-
"illuminate/auth": "~5.4.0",
24-
"illuminate/container": "~5.4.0",
25-
"illuminate/contracts": "~5.4.0",
26-
"illuminate/database": "~5.4.0"
23+
"illuminate/auth": "~5.4.0|~5.5.0",
24+
"illuminate/container": "~5.4.0|~5.5.0",
25+
"illuminate/contracts": "~5.4.0|~5.5.0",
26+
"illuminate/database": "~5.4.0|~5.5.0"
2727
},
2828
"require-dev": {
2929
"monolog/monolog": "^1.22",
30-
"orchestra/testbench": "~3.4.2",
31-
"phpunit/phpunit" : "^6.0"
30+
"orchestra/testbench": "~3.4.2|~3.5.0",
31+
"phpunit/phpunit" : "^6.2"
3232
},
3333
"autoload": {
3434
"psr-4": {

phpunit.xml.dist

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,4 @@
1919
<directory suffix=".php">src/</directory>
2020
</whitelist>
2121
</filter>
22-
<logging>
23-
<log type="tap" target="build/report.tap"/>
24-
<log type="junit" target="build/report.junit.xml"/>
25-
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
26-
<log type="coverage-text" target="build/coverage.txt"/>
27-
<log type="coverage-clover" target="build/logs/clover.xml"/>
28-
</logging>
2922
</phpunit>

src/Contracts/Role.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ public function permissions(): BelongsToMany;
2121
*
2222
* @return \Spatie\Permission\Contracts\Role
2323
*
24-
* @throws RoleDoesNotExist
24+
* @throws \Spatie\Permission\Exceptions\RoleDoesNotExist
2525
*/
2626
public static function findByName(string $name, $guardName): Role;
2727

2828
/**
2929
* Determine if the user may perform the given permission.
3030
*
31-
* @param string|Permission $permission
31+
* @param string|\Spatie\Permission\Contracts\Permission $permission
3232
*
3333
* @return bool
3434
*/

src/PermissionRegistrar.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function __construct(Gate $gate, Repository $cache, Log $logger)
3333
public function registerPermissions(): bool
3434
{
3535
try {
36-
$this->getPermissions()->map(function ($permission) {
36+
$this->getPermissions()->map(function (Permission $permission) {
3737
$this->gate->define($permission->name, function ($user) use ($permission) {
3838
return $user->hasPermissionTo($permission);
3939
});

src/Traits/HasPermissions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ protected function getStoredPermission($permissions): Permission
8989
/**
9090
* @param \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Role $roleOrPermission
9191
*
92-
* @throws \Spatie\Permission\Exceptions\GuardMismatch
92+
* @throws \Spatie\Permission\Exceptions\GuardDoesNotMatch
9393
*/
9494
protected function ensureModelSharesGuard($roleOrPermission)
9595
{

src/Traits/HasRoles.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function permissions(): MorphToMany
5252
* Scope the model query to certain roles only.
5353
*
5454
* @param \Illuminate\Database\Eloquent\Builder $query
55-
* @param string|array|Role|\Illuminate\Support\Collection $roles
55+
* @param string|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles
5656
*
5757
* @return \Illuminate\Database\Eloquent\Builder
5858
*/
@@ -122,7 +122,7 @@ public function removeRole($role)
122122
/**
123123
* Remove all current roles and set the given ones.
124124
*
125-
* @param array ...$roles
125+
* @param array|\Spatie\Permission\Contracts\Role|string ...$roles
126126
*
127127
* @return $this
128128
*/

src/Traits/RefreshesPermissionCache.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@
22

33
namespace Spatie\Permission\Traits;
44

5-
use Illuminate\Database\Eloquent\Model;
65
use Spatie\Permission\PermissionRegistrar;
76

87
trait RefreshesPermissionCache
98
{
109
public static function bootRefreshesPermissionCache()
1110
{
12-
static::saved(function (Model $model) {
11+
static::saved(function () {
1312
app(PermissionRegistrar::class)->forgetCachedPermissions();
1413
});
1514

16-
static::deleted(function (Model $model) {
15+
static::deleted(function () {
1716
app(PermissionRegistrar::class)->forgetCachedPermissions();
1817
});
1918
}

tests/HasRolesTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,23 @@ public function it_throws_an_exception_when_syncing_a_role_from_another_guard()
139139
$this->testUser->syncRoles('testRole', $this->testAdminRole);
140140
}
141141

142+
/** @test */
143+
public function it_deletes_pivot_table_entries_when_deleting_models()
144+
{
145+
$user = User::create(['email' => '[email protected]']);
146+
147+
$user->assignRole('testRole');
148+
$user->givePermissionTo('edit-articles');
149+
150+
$this->assertDatabaseHas('model_has_permissions', ['model_id' => $user->id]);
151+
$this->assertDatabaseHas('model_has_roles', ['model_id' => $user->id]);
152+
153+
$user->delete();
154+
155+
$this->assertDatabaseMissing('model_has_permissions', ['model_id' => $user->id]);
156+
$this->assertDatabaseMissing('model_has_roles', ['model_id' => $user->id]);
157+
}
158+
142159
/** @test */
143160
public function it_can_scope_users_using_a_string()
144161
{

0 commit comments

Comments
 (0)