reconstruction of the project
This commit is contained in:
+127
-32
@@ -2,12 +2,15 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use App\Models\BaseModel;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class RolePermission extends BaseModel
|
||||
{
|
||||
protected $table = 'role_permissions'; // Define the table associated with this model
|
||||
protected $primaryKey = 'id'; // Define the primary key of the table
|
||||
protected $table = 'role_permissions';
|
||||
|
||||
protected $fillable = [
|
||||
'role_id',
|
||||
'permission_id',
|
||||
@@ -18,47 +21,139 @@ class RolePermission extends BaseModel
|
||||
'can_manage',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
]; // Define the fields that can be set using insert/update methods
|
||||
];
|
||||
|
||||
public $timestamps = true;
|
||||
const CREATED_AT = 'created_at';
|
||||
const UPDATED_AT = 'updated_at';
|
||||
|
||||
|
||||
/**
|
||||
* Fetch all role permissions with optional limit and offset
|
||||
*
|
||||
* @param int $limit Number of records to return
|
||||
* @param int $offset Number of records to skip
|
||||
* @return array List of role permissions
|
||||
* @throws \Exception If there is an error during the fetch
|
||||
|
||||
protected $casts = [
|
||||
'role_id' => 'integer',
|
||||
'permission_id' => 'integer',
|
||||
'can_create' => 'boolean',
|
||||
'can_read' => 'boolean',
|
||||
'can_update' => 'boolean',
|
||||
'can_delete' => 'boolean',
|
||||
'can_manage' => 'boolean',
|
||||
'created_at' => 'datetime',
|
||||
'updated_at' => 'datetime',
|
||||
];
|
||||
|
||||
/* ============================================================
|
||||
* Relationships
|
||||
* ============================================================
|
||||
*/
|
||||
/**
|
||||
* Fetch permissions assigned to a specific role
|
||||
*
|
||||
* @param int $role_id The ID of the role
|
||||
* @return array List of permissions associated with the role
|
||||
* @throws \Exception If there is an error during the fetch
|
||||
|
||||
public function role(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Role::class, 'role_id');
|
||||
}
|
||||
|
||||
public function permission(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Permission::class, 'permission_id');
|
||||
}
|
||||
|
||||
/* ============================================================
|
||||
* Scopes
|
||||
* ============================================================
|
||||
*/
|
||||
public function getRolePermissions($role_id)
|
||||
|
||||
public function scopeForRole(Builder $q, int $roleId): Builder
|
||||
{
|
||||
return $q->where('role_id', $roleId);
|
||||
}
|
||||
|
||||
public function scopeForPermission(Builder $q, int $permissionId): Builder
|
||||
{
|
||||
return $q->where('permission_id', $permissionId);
|
||||
}
|
||||
|
||||
/* ============================================================
|
||||
* CI-compatible methods
|
||||
* ============================================================
|
||||
*/
|
||||
|
||||
/**
|
||||
* Fetch permissions assigned to a specific role.
|
||||
* (CI: getRolePermissions)
|
||||
*/
|
||||
public static function getRolePermissions(int $roleId)
|
||||
{
|
||||
try {
|
||||
// Query to get role permissions based on role ID
|
||||
return $this->where('role_id', $role_id)->findAll();
|
||||
} catch (\Exception $e) {
|
||||
// Log the error message if the query fails
|
||||
log_message('error', 'Error fetching role permissions for role ID: ' . $role_id . ': ' . $e->getMessage());
|
||||
// Rethrow the exception to be handled by the caller
|
||||
return static::query()->forRole($roleId)->get();
|
||||
} catch (\Throwable $e) {
|
||||
Log::error("Error fetching role permissions for role ID {$roleId}: {$e->getMessage()}");
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
public function getPermissionsByRole($roleId)
|
||||
/**
|
||||
* Alias (CI: getPermissionsByRole)
|
||||
*/
|
||||
public static function getPermissionsByRole(int $roleId)
|
||||
{
|
||||
return $this->where('role_id', $roleId)->findAll();
|
||||
return static::query()->forRole($roleId)->get();
|
||||
}
|
||||
|
||||
public function deleteByRole($roleId)
|
||||
/**
|
||||
* Delete all role_permissions rows for role.
|
||||
* (CI: deleteByRole)
|
||||
*/
|
||||
public static function deleteByRole(int $roleId): int
|
||||
{
|
||||
return $this->where('role_id', $roleId)->delete();
|
||||
return static::query()->forRole($roleId)->delete(); // returns deleted rows count
|
||||
}
|
||||
}
|
||||
|
||||
/* ============================================================
|
||||
* Enhancements / helpers
|
||||
* ============================================================
|
||||
*/
|
||||
|
||||
/**
|
||||
* Compute effective permission flags.
|
||||
* If can_manage is true, treat all CRUD as true.
|
||||
*/
|
||||
public function effectiveAbilities(): array
|
||||
{
|
||||
$manage = (bool) $this->can_manage;
|
||||
|
||||
return [
|
||||
'can_create' => $manage ? true : (bool) $this->can_create,
|
||||
'can_read' => $manage ? true : (bool) $this->can_read,
|
||||
'can_update' => $manage ? true : (bool) $this->can_update,
|
||||
'can_delete' => $manage ? true : (bool) $this->can_delete,
|
||||
'can_manage' => $manage,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience: set CRUD flags quickly.
|
||||
*/
|
||||
public function setCrud(bool $create, bool $read, bool $update, bool $delete, bool $manage = false): self
|
||||
{
|
||||
$this->can_create = $create;
|
||||
$this->can_read = $read;
|
||||
$this->can_update = $update;
|
||||
$this->can_delete = $delete;
|
||||
$this->can_manage = $manage;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/* ============================================================
|
||||
* Optional validation rules helper (FormRequest/controller)
|
||||
* ============================================================
|
||||
*/
|
||||
|
||||
public static function rules(bool $updating = false): array
|
||||
{
|
||||
return [
|
||||
'role_id' => [$updating ? 'sometimes' : 'required', 'integer', 'min:1', 'exists:roles,id'],
|
||||
'permission_id' => [$updating ? 'sometimes' : 'required', 'integer', 'min:1', 'exists:permissions,id'],
|
||||
|
||||
'can_create' => ['nullable', 'boolean'],
|
||||
'can_read' => ['nullable', 'boolean'],
|
||||
'can_update' => ['nullable', 'boolean'],
|
||||
'can_delete' => ['nullable', 'boolean'],
|
||||
'can_manage' => ['nullable', 'boolean'],
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user