Skip to content

Commit 9c9a7dd

Browse files
committed
update regrading functionality
1 parent 6047a4c commit 9c9a7dd

File tree

2 files changed

+146
-31
lines changed

2 files changed

+146
-31
lines changed

src/PermissionsServiceProvider.php

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,57 @@
66

77
class PermissionsServiceProvider extends ServiceProvider
88
{
9-
public function boot()
9+
/**
10+
* Bootstrap the application services.
11+
*
12+
* This method is called after all other service providers have been registered,
13+
* allowing you to register things like Blade directives and publish files.
14+
*
15+
* @return void
16+
*/
17+
public function boot(): void
1018
{
1119
// Publish the configuration and migration files
1220
$this->publishes([
13-
__DIR__ . '\config\permissions.php' => config_path('permissions.php'), // Note the correct file name
14-
__DIR__ . '\database\migrations\create_permission_tables.php' => database_path('migrations/' . date('Y_m_d_His', time()) . '_create_permission_tables.php'),
21+
__DIR__ . '/config/permissions.php' => config_path('permissions.php'), // Note the correct file name
22+
__DIR__ . '/database/migrations/create_permission_tables.php' => database_path('migrations/' . date('Y_m_d_His', time()) . '_create_permission_tables.php'),
1523
], 'config');
1624

1725
// Load migrations
18-
$this->loadMigrationsFrom(__DIR__.'\database\migrations');
26+
$this->loadMigrationsFrom(__DIR__ . '/database/migrations');
27+
28+
$bladeCompiler = $this->app->make('blade.compiler');
29+
30+
$bladeMethodWrapper = function ($method, ...$args) {
31+
return "<?php if(auth()->user()->{$method}(" . implode(',', array_map(fn($arg) => var_export($arg, true), $args)) . ")): ?>";
32+
};
33+
34+
// Role checks
35+
$bladeCompiler->if('role', fn() => $bladeMethodWrapper('hasRole', ...func_get_args()));
36+
$bladeCompiler->if('hasrole', fn() => $bladeMethodWrapper('hasRole', ...func_get_args()));
37+
$bladeCompiler->if('hasanyrole', fn() => $bladeMethodWrapper('hasAnyRole', ...func_get_args()));
38+
39+
// Permission checks
40+
$bladeCompiler->if('permission', fn() => $bladeMethodWrapper('hasPermission', ...func_get_args()));
41+
$bladeCompiler->if('haspermission', fn() => $bladeMethodWrapper('hasPermission', ...func_get_args()));
42+
$bladeCompiler->if('hasanypermission', fn() => $bladeMethodWrapper('hasAnyPermission', ...func_get_args()));
43+
$bladeCompiler->if('hasallpermissions', fn() => $bladeMethodWrapper('hasAllPermissions', ...func_get_args()));
44+
$bladeCompiler->if('hasexactpermissions', fn() => $bladeMethodWrapper('hasExactPermissions', ...func_get_args()));
45+
46+
// Ending directive for permissions
47+
$bladeCompiler->directive('endunlesspermission', fn() => '<?php endif; ?>');
1948
}
2049

21-
public function register()
50+
/**
51+
* Register the application services.
52+
*
53+
* This method is called before the application is bootstrapped.
54+
*
55+
* @return void
56+
*/
57+
public function register(): void
2258
{
2359
// Merge configuration
24-
$this->mergeConfigFrom(__DIR__ . '\config\permissions.php', 'permissions'); // Ensure this matches the config file
60+
$this->mergeConfigFrom(__DIR__ . '/config/permissions.php', 'permissions'); // Ensure this matches the config file
2561
}
2662
}

src/Traits/HasRolesAndPermissions.php

Lines changed: 104 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
<?php
2+
23
namespace Lazycode\Permissions\Traits;
34

45
use Lazycode\Permissions\Models\Permission;
56
use Lazycode\Permissions\Models\Role;
67

78
trait HasRolesAndPermissions
89
{
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
1118
{
1219
$role = Role::where('name', $roleName)->first();
1320

@@ -20,14 +27,22 @@ public function assignRoleByName(string $roleName)
2027
return $this;
2128
}
2229

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
2536
{
2637
return $this->role;
2738
}
2839

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
3146
{
3247
if ($this->role) {
3348
$this->role()->dissociate();
@@ -37,22 +52,16 @@ public function removeRole()
3752
return $this;
3853
}
3954

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
4262
{
4363
$role = $this->role;
4464

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;
5665
if ($role) {
5766
$permission = Permission::where('name', $permissionName)->first();
5867

@@ -64,10 +73,16 @@ public function assignPermissionToRole(string $permissionName)
6473
return $this;
6574
}
6675

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
6983
{
7084
$role = $this->role;
85+
7186
if ($role) {
7287
$permission = Permission::where('name', $permissionName)->first();
7388

@@ -79,15 +94,79 @@ public function removePermissionFromRole(string $permissionName)
7994
return $this;
8095
}
8196

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
84104
{
85105
return $this->role && $this->role->name === $roleName;
86106
}
87107

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
90114
{
91115
return $this->belongsTo(Role::class);
92116
}
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+
}
93172
}

0 commit comments

Comments
 (0)