Skip to content

Commit

Permalink
adding functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
subhobiswas committed Oct 7, 2024
1 parent 4d3d1d7 commit 6f75fcc
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/Models/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

class Permission extends Model
{
protected $guarded = [];

protected $fillable = ['name'];
public function roles()
{
return $this->belongsToMany(Role::class);
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class Role extends Model
{
protected $guarded = [];
protected $fillable = ['name'];

public function permissions()
{
Expand Down
93 changes: 93 additions & 0 deletions src/Traits/HasRoleAndPermissions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
namespace Lazycode\Permissions\Traits;

use Lazycode\Permissions\Models\Permission;
use Lazycode\Permissions\Models\Role;

trait HasRolesAndPermissions
{
// Assign a role by name (one role per user)
public function assignRoleByName(string $roleName)
{
$role = Role::where('name', $roleName)->first();

if ($role) {
// Ensure the user only has one role
$this->role()->associate($role);
$this->save();
}

return $this;
}

// Get the user's role
public function getRole()
{
return $this->role;
}

// Disassociate (remove) the current role from the user
public function removeRole()
{
if ($this->role) {
$this->role()->dissociate();
$this->save();
}

return $this;
}

// Check if the user has a specific permission
public function hasPermission(string $permissionName)
{
$role = $this->role;

if ($role) {
return $role->permissions()->where('name', $permissionName)->exists();
}

return false;
}

// Assign a permission to the role by permission name
public function assignPermissionToRole(string $permissionName)
{
$role = $this->role;
if ($role) {
$permission = Permission::where('name', $permissionName)->first();

if ($permission) {
$role->permissions()->syncWithoutDetaching([$permission->id]);
}
}

return $this;
}

// Remove a permission from the role by permission name
public function removePermissionFromRole(string $permissionName)
{
$role = $this->role;
if ($role) {
$permission = Permission::where('name', $permissionName)->first();

if ($permission) {
$role->permissions()->detach($permission->id);
}
}

return $this;
}

// Check if the user has the specified role
public function hasRole(string $roleName)
{
return $this->role && $this->role->name === $roleName;
}

// Relationship for user role (one-to-one)
public function role()
{
return $this->belongsTo(Role::class);
}
}
10 changes: 10 additions & 0 deletions src/database/migrations/create_permission_tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,22 @@ public function up()
$table->foreignId('permission_id')->constrained()->onDelete('cascade');
});
}

if (!Schema::hasTable('users')) {
Schema::table('users', function (Blueprint $table) {
$table->foreignId('role_id')->nullable()->constrained('roles')->onDelete('set null');
});
}

}

public function down()
{
Schema::dropIfExists('role_permission');
Schema::dropIfExists('permissions');
Schema::dropIfExists('roles');
Schema::table('users', function (Blueprint $table) {
$table->dropForeign(['role_id']);
});
}
};

0 comments on commit 6f75fcc

Please sign in to comment.