71 lines
2.7 KiB
PHP
71 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use CodeIgniter\Model;
|
|
|
|
class PermissionModel extends Model
|
|
{
|
|
protected $table = 'permissions'; // Define the table associated with this model
|
|
protected $primaryKey = 'id'; // Define the primary key of the table
|
|
protected $allowedFields = [
|
|
'name',
|
|
'description',
|
|
'created_at',
|
|
'updated_at'
|
|
]; // Define the fields that can be set using insert/update methods
|
|
|
|
protected $useTimestamps = true;
|
|
protected $createdField = 'created_at';
|
|
protected $updatedField = 'updated_at';
|
|
|
|
/**
|
|
* Fetch all permissions from the database
|
|
*
|
|
* @return array List of permissions
|
|
* @throws \Exception If there is an error during the fetch
|
|
*/
|
|
public function getPermissions()
|
|
{
|
|
try {
|
|
// Query to get all permissions from the 'permissions' table
|
|
return $this->findAll();
|
|
} catch (\Exception $e) {
|
|
// Log the error message if the query fails
|
|
log_message('error', 'Error fetching permissions: ' . $e->getMessage());
|
|
// Rethrow the exception to be handled by the caller
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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 getRolePermissions($role_id)
|
|
{
|
|
try {
|
|
// Query to get role permissions along with default permissions
|
|
$rolePermissions = $this->db->table('role_permissions')
|
|
->select('role_permissions.*, permissions.name')
|
|
// Join with 'permissions' table to get permission details
|
|
->join('permissions', 'role_permissions.permission_id = permissions.id', 'left')
|
|
// Filter by the specified role ID
|
|
->where('role_permissions.role_id', $role_id)
|
|
->get()
|
|
// Get the result as an array
|
|
->getResultArray();
|
|
// Log the fetched role permissions for debugging
|
|
log_message('info', 'Role permissions fetched: ' . print_r($rolePermissions, true));
|
|
return $rolePermissions;
|
|
} 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
|
|
throw $e;
|
|
}
|
|
}
|
|
} |