Skip to content

Commit 6f75fcc

Browse files
committed
adding functionality
1 parent 4d3d1d7 commit 6f75fcc

File tree

4 files changed

+106
-3
lines changed

4 files changed

+106
-3
lines changed

src/Models/Permission.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
class Permission extends Model
88
{
9-
protected $guarded = [];
10-
9+
protected $fillable = ['name'];
10+
1111
public function roles()
1212
{
1313
return $this->belongsToMany(Role::class);

src/Models/Role.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class Role extends Model
88
{
9-
protected $guarded = [];
9+
protected $fillable = ['name'];
1010

1111
public function permissions()
1212
{

src/Traits/HasRoleAndPermissions.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
namespace Lazycode\Permissions\Traits;
3+
4+
use Lazycode\Permissions\Models\Permission;
5+
use Lazycode\Permissions\Models\Role;
6+
7+
trait HasRolesAndPermissions
8+
{
9+
// Assign a role by name (one role per user)
10+
public function assignRoleByName(string $roleName)
11+
{
12+
$role = Role::where('name', $roleName)->first();
13+
14+
if ($role) {
15+
// Ensure the user only has one role
16+
$this->role()->associate($role);
17+
$this->save();
18+
}
19+
20+
return $this;
21+
}
22+
23+
// Get the user's role
24+
public function getRole()
25+
{
26+
return $this->role;
27+
}
28+
29+
// Disassociate (remove) the current role from the user
30+
public function removeRole()
31+
{
32+
if ($this->role) {
33+
$this->role()->dissociate();
34+
$this->save();
35+
}
36+
37+
return $this;
38+
}
39+
40+
// Check if the user has a specific permission
41+
public function hasPermission(string $permissionName)
42+
{
43+
$role = $this->role;
44+
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+
if ($role) {
57+
$permission = Permission::where('name', $permissionName)->first();
58+
59+
if ($permission) {
60+
$role->permissions()->syncWithoutDetaching([$permission->id]);
61+
}
62+
}
63+
64+
return $this;
65+
}
66+
67+
// Remove a permission from the role by permission name
68+
public function removePermissionFromRole(string $permissionName)
69+
{
70+
$role = $this->role;
71+
if ($role) {
72+
$permission = Permission::where('name', $permissionName)->first();
73+
74+
if ($permission) {
75+
$role->permissions()->detach($permission->id);
76+
}
77+
}
78+
79+
return $this;
80+
}
81+
82+
// Check if the user has the specified role
83+
public function hasRole(string $roleName)
84+
{
85+
return $this->role && $this->role->name === $roleName;
86+
}
87+
88+
// Relationship for user role (one-to-one)
89+
public function role()
90+
{
91+
return $this->belongsTo(Role::class);
92+
}
93+
}

src/database/migrations/create_permission_tables.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,22 @@ public function up()
3131
$table->foreignId('permission_id')->constrained()->onDelete('cascade');
3232
});
3333
}
34+
35+
if (!Schema::hasTable('users')) {
36+
Schema::table('users', function (Blueprint $table) {
37+
$table->foreignId('role_id')->nullable()->constrained('roles')->onDelete('set null');
38+
});
39+
}
40+
3441
}
3542

3643
public function down()
3744
{
3845
Schema::dropIfExists('role_permission');
3946
Schema::dropIfExists('permissions');
4047
Schema::dropIfExists('roles');
48+
Schema::table('users', function (Blueprint $table) {
49+
$table->dropForeign(['role_id']);
50+
});
4151
}
4252
};

0 commit comments

Comments
 (0)