reconstruction of the project
This commit is contained in:
+35
-41
@@ -3,67 +3,61 @@
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\BaseModel;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class Permission extends BaseModel
|
||||
{
|
||||
protected $table = 'permissions'; // Define the table associated with this model
|
||||
protected $primaryKey = 'id'; // Define the primary key of the table
|
||||
protected $table = 'permissions';
|
||||
|
||||
// ✅ CI: useTimestamps = true (created_at/updated_at)
|
||||
public $timestamps = true;
|
||||
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'description',
|
||||
'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 permissions from the database
|
||||
*
|
||||
* @return array List of permissions
|
||||
* @throws \Exception If there is an error during the fetch
|
||||
/* =========================
|
||||
* CI method equivalents
|
||||
* ========================= */
|
||||
|
||||
/**
|
||||
* Fetch all permissions from the database.
|
||||
*/
|
||||
public function getPermissions()
|
||||
public static 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
|
||||
return static::query()->get();
|
||||
} catch (\Throwable $e) {
|
||||
Log::error('Error fetching permissions: ' . $e->getMessage());
|
||||
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
|
||||
* Fetch permissions assigned to a specific role.
|
||||
* Returns rows similar to CI:
|
||||
* role_permissions.* + permissions.name
|
||||
*/
|
||||
public function getRolePermissions($role_id)
|
||||
public static function getRolePermissions(int $roleId): array
|
||||
{
|
||||
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
|
||||
$rows = DB::table('role_permissions')
|
||||
->leftJoin('permissions', 'role_permissions.permission_id', '=', 'permissions.id')
|
||||
->where('role_permissions.role_id', $roleId)
|
||||
->select('role_permissions.*', 'permissions.name')
|
||||
->get()
|
||||
->map(fn ($r) => (array) $r)
|
||||
->all();
|
||||
|
||||
Log::info('Role permissions fetched: ' . print_r($rows, true));
|
||||
|
||||
return $rows;
|
||||
} catch (\Throwable $e) {
|
||||
Log::error("Error fetching role permissions for role ID {$roleId}: " . $e->getMessage());
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user