Skip to content

Commit e74c7de

Browse files
authored
Merge pull request #243 from ajoy39/master
Laravel 6.0 compatibility updates
2 parents 640a48c + 433e504 commit e74c7de

14 files changed

+46
-145
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
8080

8181
6. Run the migrations to generate your roles and permissions tables
8282

83+
Please note that if you are upgrading to 6.0 from a previous version, the default column type for the id on the users table has changed. On certain databases foreign keys can only be defined with matching column types. As such, you will need to change the id column on your users table to bigInteger in to user this package.
84+
8385
```
8486
php artisan migrate
8587
```

src/Kodeine/Acl/AclServiceProvider.php

Lines changed: 3 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Blade;
66
use Illuminate\Support\ServiceProvider;
7-
use Illuminate\Support\Str;
87

98
class AclServiceProvider extends ServiceProvider
109
{
@@ -24,15 +23,7 @@ public function boot()
2423
{
2524
$this->publishConfig();
2625
$this->loadMigrationsFrom(__DIR__ . '/../../migrations');
27-
28-
$laravel = app();
29-
if ( substr($laravel::VERSION, 0, 2) === (string) "5.0" ) {
30-
$this->registerBlade5_0();
31-
} else if ( (substr($laravel::VERSION, 0, 2) === (string) "5.1") || (substr($laravel::VERSION, 0, 2) === (string) "5.2") ) {
32-
$this->registerBlade5_1();
33-
} else {
34-
$this->registerBlade5_3();
35-
}
26+
$this->registerBladeDirectives();
3627
}
3728

3829
/**
@@ -57,7 +48,7 @@ public function publishConfig()
5748
], 'config');
5849
}
5950

60-
protected function registerBlade5_3()
51+
public function registerBladeDirectives()
6152
{
6253
// role
6354
Blade::directive('role', function ($expression) {
@@ -70,62 +61,11 @@ protected function registerBlade5_3()
7061

7162
// permission
7263
Blade::directive('permission', function ($expression) {
73-
return "<?php if (Auth::check() && Auth::user()->can({$expression})): ?>";
74-
});
75-
76-
Blade::directive('endpermission', function () {
77-
return "<?php endif; ?>";
78-
});
79-
}
80-
81-
/**
82-
* Register Blade Template Extensions for >= L5.1
83-
*/
84-
protected function registerBlade5_1()
85-
{
86-
// role
87-
Blade::directive('role', function ($expression) {
88-
return "<?php if (Auth::check() && Auth::user()->is{$expression}): ?>";
89-
});
90-
91-
Blade::directive('endrole', function () {
92-
return "<?php endif; ?>";
93-
});
94-
95-
// permission
96-
Blade::directive('permission', function ($expression) {
97-
return "<?php if (Auth::check() && Auth::user()->can{$expression}): ?>";
64+
return "<?php if (Auth::check() && Auth::user()->hasPermission({$expression})): ?>";
9865
});
9966

10067
Blade::directive('endpermission', function () {
10168
return "<?php endif; ?>";
10269
});
10370
}
104-
105-
/**
106-
* Register Blade Template Extensions for <= L5.0
107-
*/
108-
protected function registerBlade5_0()
109-
{
110-
$blade = $this->app['view']->getEngineResolver()->resolve('blade')->getCompiler();
111-
$blade->extend(function ($view, $compiler) {
112-
$pattern = $compiler->createMatcher('role');
113-
return preg_replace($pattern, '<?php if (Auth::check() && Auth::user()->is$2): ?> ', $view);
114-
});
115-
116-
$blade->extend(function ($view, $compiler) {
117-
$pattern = $compiler->createPlainMatcher('endrole');
118-
return preg_replace($pattern, '<?php endif; ?>', $view);
119-
});
120-
121-
$blade->extend(function ($view, $compiler) {
122-
$pattern = $compiler->createMatcher('permission');
123-
return preg_replace($pattern, '<?php if (Auth::check() && Auth::user()->can$2): ?> ', $view);
124-
});
125-
126-
$blade->extend(function ($view, $compiler) {
127-
$pattern = $compiler->createPlainMatcher('endpermission');
128-
return preg_replace($pattern, '<?php endif; ?>', $view);
129-
});
130-
}
13171
}

src/Kodeine/Acl/Helper/Helper.php

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,6 @@
66

77
trait Helper
88
{
9-
/*
10-
|----------------------------------------------------------------------
11-
| Collection methods compatible with L5.1.
12-
|----------------------------------------------------------------------
13-
|
14-
*/
15-
16-
/**
17-
* Lists() method in l5.1 returns collection.
18-
* This method fixes that issue for backward
19-
* compatibility.
20-
*
21-
* @param $data
22-
* @return mixed
23-
*/
24-
protected function collectionAsArray($data)
25-
{
26-
return ($data instanceof Collection)
27-
? $data->toArray()
28-
: $data;
29-
}
30-
319
/*
3210
|----------------------------------------------------------------------
3311
| Slug Permission Related Protected Methods
@@ -104,9 +82,7 @@ protected function mapArray($item, \Closure $closure)
10482

10583
// item is a collection.
10684
if ($item instanceof Collection) {
107-
$item = $this->collectionAsArray(
108-
method_exists($item, 'pluck') ? $item->pluck('name') : $item->lists('name')
109-
);
85+
$item = $item->pluck('name')->all();
11086
}
11187

11288
// multiple items

src/Kodeine/Acl/Middleware/HasPermission.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,6 @@ private function filterMethods($methods, $callback) {
155155
*/
156156
protected function forbiddenRoute()
157157
{
158-
/*$action = $request->route()->getAction();
159-
if ( isset($action['except']) ) {
160-
dd($request->user()->roles->lists('slug'));
161-
dd($request->user()->getPermissions());
162-
//return $action['except'] == $request->user()->role->slug;
163-
}*/
164-
165158
return false;
166159
}
167160

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,20 @@
1-
<?php namespace Kodeine\Acl\Models\Eloquent;
2-
3-
use Illuminate\Database\Eloquent\Model;
4-
use Illuminate\Auth\Authenticatable;
1+
<?php
2+
namespace Kodeine\Acl\Models\Eloquent;
53

4+
use Illuminate\Contracts\Auth\MustVerifyEmail;
65
use Illuminate\Database\Eloquent\SoftDeletes;
7-
8-
use Illuminate\Auth\Passwords\CanResetPassword;
6+
use Illuminate\Foundation\Auth\User as Authenticatable;
7+
use Illuminate\Notifications\Notifiable;
98
use Kodeine\Acl\Traits\HasRole;
109

11-
class User extends Model
10+
class User extends Authenticatable
1211
{
13-
use Authenticatable, CanResetPassword, HasRole, SoftDeletes;
12+
use HasRole, SoftDeletes;
1413

1514
/**
1615
* The attributes that are fillable via mass assignment.
1716
*
1817
* @var array
1918
*/
20-
protected $fillable = ['username', 'first_name', 'last_name', 'email', 'password',];
21-
22-
/**
23-
* The database table used by the model.
24-
*
25-
* @var string
26-
*/
27-
protected $table = 'users';
19+
protected $fillable = ['name', 'email', 'password',];
2820
}

src/Kodeine/Acl/Traits/HasPermissionInheritance.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,7 @@ public function getPermissionsInherited()
4747
// process inherit_id recursively
4848
$inherited = $this->getRecursiveInherit($row->inherit_id, $row->slug);
4949
$merge = $permissions->where('name', $row->name);
50-
$merge = method_exists($merge, 'pluck') ? $merge->pluck('slug', 'name') : $merge->lists('slug', 'name');
51-
52-
// fix for l5.1 and backward compatibility.
53-
// lists() method should return as an array.
54-
$merge = $this->collectionAsArray($merge);
50+
$merge = $merge->pluck('slug', 'name')->all();
5551

5652
// replace and merge permissions
5753
$rights = array_replace_recursive($rights, $inherited, $merge);

src/Kodeine/Acl/Traits/HasRole.php

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
namespace Kodeine\Acl\Traits;
44

55
use Kodeine\Acl\Traits\HasPermission;
6+
use Illuminat\Support\Str;
7+
68
/**
79
* Class HasRoleImplementation
810
* @package Kodeine\Acl\Traits
911
*
1012
* @method static Builder|Collection|\Eloquent role($role, $column = null)
1113
*/
12-
trait HasRoleImplementation
14+
trait HasRole
1315
{
1416
use HasPermission;
1517

@@ -48,10 +50,10 @@ function () {
4850
}
4951
);
5052

51-
$slugs = method_exists($this_roles, 'pluck') ? $this_roles->pluck('slug','id') : $this_roles->lists('slug','id');
53+
$slugs = $this_roles->pluck('slug','id');
5254
return is_null($this_roles)
5355
? []
54-
: $this->collectionAsArray($slugs);
56+
: $slugs->all();
5557
}
5658

5759
/**
@@ -266,13 +268,13 @@ protected function parseRoleId($role)
266268
public function __call($method, $arguments)
267269
{
268270
// Handle isRoleSlug() methods
269-
if ( starts_with($method, 'is') and $method !== 'is' and ! starts_with($method, 'isWith') ) {
271+
if ( str::startsWith($method, 'is') and $method !== 'is' and ! str::startsWith($method, 'isWith') ) {
270272
$role = substr($method, 2);
271273
return $this->hasRole($role);
272274
}
273275

274276
// Handle canDoSomething() methods
275-
if ( starts_with($method, 'can') and $method !== 'can' and ! starts_with($method, 'canWith') ) {
277+
if ( str::startsWith($method, 'can') and $method !== 'can' and ! str::startsWith($method, 'canWith') ) {
276278
$permission = substr($method, 3);
277279
$permission = snake_case($permission, '.');
278280

@@ -282,18 +284,3 @@ public function __call($method, $arguments)
282284
return parent::__call($method, $arguments);
283285
}
284286
}
285-
286-
$laravel = app();
287-
if ($laravel instanceof \Illuminate\Foundation\Application && version_compare($laravel::VERSION, '5.3', '<')) {
288-
trait HasRole
289-
{
290-
use HasRoleImplementation {
291-
hasRole as is;
292-
}
293-
}
294-
} else {
295-
trait HasRole
296-
{
297-
use HasRoleImplementation;
298-
}
299-
}

src/Kodeine/Acl/Traits/HasUserPermission.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ protected function extractAlias($str)
119119
*/
120120
protected function addSlug($alias, array $permissions)
121121
{
122-
$slugs = method_exists($this->permissions, 'pluck') ? $this->permissions->pluck('slug', 'name') : $this->permissions->lists('slug', 'name');
122+
$slugs = $this->permissions->pluck('slug', 'name');
123123
$collection = new Collection($slugs);
124124

125125
if ( $collection->has($alias) ) {
@@ -140,7 +140,7 @@ protected function addSlug($alias, array $permissions)
140140
*/
141141
protected function removeSlug($alias, array $permissions)
142142
{
143-
$slugs = method_exists($this->permissions, 'pluck') ? $this->permissions->pluck('slug', 'name') : $this->permissions->lists('slug', 'name');
143+
$slugs = $this->permissions->pluck('slug', 'name');
144144
$collection = new Collection($slugs);
145145

146146
if ( $collection->has($alias) ) {

src/config/acl.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
<?php
22

33
return [
4+
5+
/*
6+
/--------------------------------------------------------------------------
7+
/ Custom Database Options
8+
/--------------------------------------------------------------------------
9+
/
10+
/ If you want to add a prefix to your acl tables, or if you use a different
11+
/ table for your user class, define it here
12+
*/
13+
14+
'db_prefix' => '',
15+
'users_table' => '',
16+
417
/*
518
|--------------------------------------------------------------------------
619
| Model Definitions

src/migrations/2015_02_07_172633_create_role_user_table.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class CreateRoleUserTable extends Migration
1313
public function __construct()
1414
{
1515
$this->prefix = config('acl.db_prefix');
16+
$this->users_table = config('acl.users_table') === '' ? 'users' : config('acl.users_table');
1617
}
1718

1819
/**
@@ -26,7 +27,7 @@ public function up()
2627
$table->increments('id');
2728

2829
$table->integer('role_id')->unsigned()->index()->foreign()->references("id")->on("roles")->onDelete("cascade");
29-
$table->integer('user_id')->unsigned()->index()->foreign()->references("id")->on("users")->onDelete("cascade");
30+
$table->bigInteger('user_id')->unsigned()->index()->foreign()->references("id")->on("users")->onDelete("cascade");
3031

3132
$table->timestamps();
3233

@@ -37,7 +38,7 @@ public function up()
3738

3839
$table->foreign('user_id')
3940
->references('id')
40-
->on(config('acl.users_table'))
41+
->on()
4142
->onDelete('cascade');
4243
});
4344
}

src/migrations/2015_02_17_152439_create_permission_user_table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function up()
2525
Schema::create($this->prefix . 'permission_user', function (Blueprint $table) {
2626
$table->increments('id');
2727
$table->integer('permission_id')->unsigned()->index()->references('id')->on('permissions')->onDelete('cascade');
28-
$table->integer('user_id')->unsigned()->index()->references('id')->on('users')->onDelete('cascade');
28+
$table->bigInteger('user_id')->unsigned()->index()->references('id')->on('users')->onDelete('cascade');
2929
$table->timestamps();
3030
});
3131
}

src/migrations/2016_02_06_172606_create_users_table.php renamed to src/migrations/2016_02_06_172606_create_users_table_if_doesnt_exist.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use Illuminate\Database\Schema\Blueprint;
44
use Illuminate\Database\Migrations\Migration;
55

6-
class CreateUsersTable extends Migration
6+
class CreateUsersTableIfDoesntExist extends Migration
77
{
88

99
/**

tests/Models/UserTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function itCanAttachRole()
6060
]);
6161

6262
$user = new User();
63-
$user->username = 'Role test';
63+
$user->name = 'Role test';
6464
$user->email = '[email protected]';
6565
$user->password = 'RoleTest';
6666
$user->save();
@@ -97,7 +97,7 @@ public function itCanAttachRoleAndPermission()
9797
$role->syncPermissions($permission);
9898

9999
$user = new User();
100-
$user->username = 'Role test';
100+
$user->name = 'Role test';
101101
$user->email = '[email protected]';
102102
$user->password = 'RoleTest';
103103
$user->save();
@@ -134,7 +134,7 @@ public function cacheTest()
134134
$role->syncPermissions($permission);
135135

136136
$user = new User();
137-
$user->username = 'Cache test';
137+
$user->name = 'Cache test';
138138
$user->email = '[email protected]';
139139
$user->password = 'CacheTest';
140140
$user->save();

tests/TestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ protected function getMigrationsDestPath()
119119
*/
120120
protected function migrate()
121121
{
122+
$this->loadLaravelMigrations();
122123
$this->loadMigrationsFrom($this->getMigrationsSrcPath());
123124
}
124125

0 commit comments

Comments
 (0)