1
1
<?php
2
+
2
3
namespace Lazycode \Permissions \Traits ;
3
4
4
5
use Lazycode \Permissions \Models \Permission ;
5
6
use Lazycode \Permissions \Models \Role ;
6
7
7
8
trait HasRolesAndPermissions
8
9
{
9
- // Assign a role by name (one role per user)
10
- public function assignRoleByName (string $ roleName )
10
+ /**
11
+ * Assign a role to the user by role name.
12
+ * Each user can have only one role.
13
+ *
14
+ * @param string $roleName
15
+ * @return $this
16
+ */
17
+ public function assignRole (string $ roleName ): self
11
18
{
12
19
$ role = Role::where ('name ' , $ roleName )->first ();
13
20
@@ -20,14 +27,22 @@ public function assignRoleByName(string $roleName)
20
27
return $ this ;
21
28
}
22
29
23
- // Get the user's role
24
- public function getRole ()
30
+ /**
31
+ * Get the current role of the user.
32
+ *
33
+ * @return Role|null
34
+ */
35
+ public function getRole (): ?Role
25
36
{
26
37
return $ this ->role ;
27
38
}
28
39
29
- // Disassociate (remove) the current role from the user
30
- public function removeRole ()
40
+ /**
41
+ * Disassociate (remove) the current role from the user.
42
+ *
43
+ * @return $this
44
+ */
45
+ public function removeRole (): self
31
46
{
32
47
if ($ this ->role ) {
33
48
$ this ->role ()->dissociate ();
@@ -37,22 +52,16 @@ public function removeRole()
37
52
return $ this ;
38
53
}
39
54
40
- // Check if the user has a specific permission
41
- public function hasPermission (string $ permissionName )
55
+ /**
56
+ * Assign a permission to the user's role by permission name.
57
+ *
58
+ * @param string $permissionName
59
+ * @return $this
60
+ */
61
+ public function assignPermissionToRole (string $ permissionName ): self
42
62
{
43
63
$ role = $ this ->role ;
44
64
45
- if ($ role ) {
46
- return $ role ->permissions ()->where ('name ' , $ permissionName )->exists ();
47
- }
48
-
49
- return false ;
50
- }
51
-
52
- // Assign a permission to the role by permission name
53
- public function assignPermissionToRole (string $ permissionName )
54
- {
55
- $ role = $ this ->role ;
56
65
if ($ role ) {
57
66
$ permission = Permission::where ('name ' , $ permissionName )->first ();
58
67
@@ -64,10 +73,16 @@ public function assignPermissionToRole(string $permissionName)
64
73
return $ this ;
65
74
}
66
75
67
- // Remove a permission from the role by permission name
68
- public function removePermissionFromRole (string $ permissionName )
76
+ /**
77
+ * Remove a permission from the user's role by permission name.
78
+ *
79
+ * @param string $permissionName
80
+ * @return $this
81
+ */
82
+ public function removePermissionFromRole (string $ permissionName ): self
69
83
{
70
84
$ role = $ this ->role ;
85
+
71
86
if ($ role ) {
72
87
$ permission = Permission::where ('name ' , $ permissionName )->first ();
73
88
@@ -79,15 +94,79 @@ public function removePermissionFromRole(string $permissionName)
79
94
return $ this ;
80
95
}
81
96
82
- // Check if the user has the specified role
83
- public function hasRole (string $ roleName )
97
+ /**
98
+ * Check if the user has the specified role.
99
+ *
100
+ * @param string $roleName
101
+ * @return bool
102
+ */
103
+ public function hasRole (string $ roleName ): bool
84
104
{
85
105
return $ this ->role && $ this ->role ->name === $ roleName ;
86
106
}
87
107
88
- // Relationship for user role (one-to-one)
89
- public function role ()
108
+ /**
109
+ * Define a one-to-one relationship with the Role model.
110
+ *
111
+ * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
112
+ */
113
+ public function role (): \Illuminate \Database \Eloquent \Relations \BelongsTo
90
114
{
91
115
return $ this ->belongsTo (Role::class);
92
116
}
117
+
118
+ /**
119
+ * Check if the user has any of the specified roles.
120
+ *
121
+ * @param string ...$roles
122
+ * @return bool
123
+ */
124
+ public function hasAnyRole (string ...$ roles ): bool
125
+ {
126
+ return $ this ->roles ->whereIn ('name ' , $ roles )->isNotEmpty ();
127
+ }
128
+
129
+ /**
130
+ * Check if the user has a specific permission.
131
+ *
132
+ * @param string $permission
133
+ * @return bool
134
+ */
135
+ public function hasPermission (string $ permission ): bool
136
+ {
137
+ return $ this ->permissions ->contains ('name ' , $ permission );
138
+ }
139
+
140
+ /**
141
+ * Check if the user has any of the specified permissions.
142
+ *
143
+ * @param string ...$permissions
144
+ * @return bool
145
+ */
146
+ public function hasAnyPermission (string ...$ permissions ): bool
147
+ {
148
+ return $ this ->permissions ->whereIn ('name ' , $ permissions )->isNotEmpty ();
149
+ }
150
+
151
+ /**
152
+ * Check if the user has all of the specified permissions.
153
+ *
154
+ * @param string ...$permissions
155
+ * @return bool
156
+ */
157
+ public function hasAllPermissions (string ...$ permissions ): bool
158
+ {
159
+ return $ this ->permissions ->pluck ('name ' )->intersect ($ permissions )->count () == count ($ permissions );
160
+ }
161
+
162
+ /**
163
+ * Check if the user has exactly the specified permissions.
164
+ *
165
+ * @param string ...$permissions
166
+ * @return bool
167
+ */
168
+ public function hasExactPermissions (string ...$ permissions ): bool
169
+ {
170
+ return $ this ->permissions ->pluck ('name ' )->sort ()->values ()->all () === collect ($ permissions )->sort ()->values ()->all ();
171
+ }
93
172
}
0 commit comments