File tree Expand file tree Collapse file tree 5 files changed +43
-25
lines changed Expand file tree Collapse file tree 5 files changed +43
-25
lines changed Original file line number Diff line number Diff line change @@ -55,10 +55,6 @@ To check for a role:
55
55
``` php
56
56
$user->hasRole('admin');
57
57
```
58
- or
59
- ``` php
60
- $user->is('admin');
61
- ```
62
58
63
59
To check for permissions:
64
60
``` php
Original file line number Diff line number Diff line change @@ -15,7 +15,7 @@ public function up()
15
15
{
16
16
Schema::create ('roles ' , function (Blueprint $ table ) {
17
17
$ table ->increments ('id ' );
18
- $ table ->string ('name ' );
18
+ $ table ->string ('name ' )-> unique () ;
19
19
$ table ->timestamps ();
20
20
});
21
21
}
Original file line number Diff line number Diff line change 4
4
use Illuminate \Database \Schema \Blueprint ;
5
5
use Illuminate \Database \Migrations \Migration ;
6
6
7
- class CreateUsersRolesTable extends Migration
7
+ class CreateRoleUserTable extends Migration
8
8
{
9
9
/**
10
10
* Run the migrations.
@@ -13,14 +13,16 @@ class CreateUsersRolesTable extends Migration
13
13
*/
14
14
public function up ()
15
15
{
16
- Schema::create ('users_roles ' , function (Blueprint $ table ) {
17
- $ table ->integer ('user_id ' )->unsigned ();
18
- $ table ->integer ('role_id ' )->unsigned ();
16
+ Schema::create ('role_user ' , function (Blueprint $ table ) {
17
+ $ table ->increments ('id ' );
18
+ $ table ->integer ('user_id ' )->unsigned ()->index ();
19
+ $ table ->integer ('role_id ' )->unsigned ()->index ();
20
+ $ table ->timestamps ();
19
21
20
22
$ table ->foreign ('user_id ' )->references ('id ' )->on ('users ' )->onDelete ('cascade ' );
21
23
$ table ->foreign ('role_id ' )->references ('id ' )->on ('roles ' )->onDelete ('cascade ' );
22
24
23
- $ table ->primary (['user_id ' , 'role_id ' ]);
25
+ $ table ->unique (['user_id ' , 'role_id ' ]);
24
26
});
25
27
}
26
28
Original file line number Diff line number Diff line change 2
2
3
3
namespace Denismitr \Permissions ;
4
4
5
+ use App \User ;
5
6
use Denismitr \Permissions \Models \Permission ;
6
7
use Denismitr \Permissions \Models \Role ;
7
8
@@ -90,26 +91,17 @@ public function assignRole(...$roles)
90
91
{
91
92
foreach ($ roles as $ role ) {
92
93
if ( ! $ this ->hasRole ($ role ) ) {
93
- $ this ->roles ()->create ([
94
- 'name ' => $ role
95
- ]);
94
+ $ role = Role::fromName ($ role );
95
+
96
+ $ this ->roles ()->attach ($ role );
97
+
98
+ $ this ->load ('roles ' );
96
99
}
97
100
}
98
101
99
102
return $ this ;
100
103
}
101
104
102
- /**
103
- * Alias for hasRole
104
- *
105
- * @param string $roles
106
- * @return bool
107
- */
108
- public function is (...$ roles )
109
- {
110
- return $ this ->hasRole (...$ roles );
111
- }
112
-
113
105
/**
114
106
* Check if user has role
115
107
*
@@ -127,11 +119,13 @@ public function hasRole(...$roles)
127
119
return false ;
128
120
}
129
121
122
+
130
123
public function roles ()
131
124
{
132
- return $ this ->belongsToMany (Role::class, ' users_roles ' );
125
+ return $ this ->belongsToMany (Role::class);
133
126
}
134
127
128
+
135
129
public function permissions ()
136
130
{
137
131
return $ this ->belongsToMany (Permission::class, 'users_permissions ' );
Original file line number Diff line number Diff line change 2
2
3
3
namespace Denismitr \Permissions \Models ;
4
4
5
+ use App \User ;
5
6
use Illuminate \Database \Eloquent \Model ;
6
7
7
8
class Role extends Model
8
9
{
9
10
protected $ guarded = [];
10
11
12
+ /**
13
+ * Create new instance of Role with new name or old
14
+ * one if not unique
15
+ *
16
+ * @param string $name
17
+ * @return Illuminate\Database\Eloquent\Model
18
+ */
19
+ public static function fromName ($ name )
20
+ {
21
+ return self ::updateOrCreate ([
22
+ 'name ' => $ name
23
+ ]);
24
+ }
25
+
26
+ /**
27
+ * Belongs to many permissions
28
+ *
29
+ * @return Illuminate\Database\Eloquent\Relations\BelongsToMany
30
+ */
11
31
public function permissions ()
12
32
{
13
33
return $ this ->belongsToMany (Permission::class, 'roles_permissions ' );
14
34
}
35
+
36
+
37
+ public function users ()
38
+ {
39
+ return $ this ->belongsToMany (User::class);
40
+ }
15
41
}
You can’t perform that action at this time.
0 commit comments