2021-10-23 20:19:21 +03:00
|
|
|
<?php defined('BASEPATH') or exit('No direct script access allowed');
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------
|
2022-01-18 15:05:42 +03:00
|
|
|
* Easy!Appointments - Online Appointment Scheduler
|
2021-10-23 20:19:21 +03:00
|
|
|
*
|
|
|
|
* @package EasyAppointments
|
|
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
2021-12-18 19:43:45 +03:00
|
|
|
* @copyright Copyright (c) Alex Tselegidis
|
|
|
|
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
|
|
* @link https://easyappointments.org
|
2021-10-23 20:19:21 +03:00
|
|
|
* @since v1.5.0
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!function_exists('can')) {
|
2021-10-23 20:19:21 +03:00
|
|
|
/**
|
|
|
|
* Check if the currently logged-in user can perform an action
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* if (can('edit', 'appointments') === FALSE) abort(403);
|
|
|
|
*
|
|
|
|
* @param string $action
|
|
|
|
* @param string $resource
|
|
|
|
* @param int|null $user_id
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
function can(string $action, string $resource, int $user_id = null): bool
|
2021-10-23 20:19:21 +03:00
|
|
|
{
|
|
|
|
/** @var EA_Controller $CI */
|
|
|
|
$CI = &get_instance();
|
|
|
|
|
|
|
|
$CI->load->model('roles_model');
|
2021-10-28 14:59:27 +03:00
|
|
|
$CI->load->model('users_model');
|
2021-10-23 20:19:21 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (empty($user_id)) {
|
2021-10-23 20:19:21 +03:00
|
|
|
$role_slug = session('role_slug');
|
2023-11-29 12:24:09 +03:00
|
|
|
} else {
|
2021-10-28 14:59:27 +03:00
|
|
|
$user = $CI->users_model->find($user_id);
|
|
|
|
|
|
|
|
$role_slug = $CI->roles_model->value($user['id_roles'], 'slug');
|
2021-10-23 20:19:21 +03:00
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (empty($role_slug)) {
|
|
|
|
return false;
|
2021-10-23 20:19:21 +03:00
|
|
|
}
|
|
|
|
|
2021-10-28 14:59:27 +03:00
|
|
|
$permissions = $CI->roles_model->get_permissions_by_slug($role_slug);
|
2021-10-23 20:19:21 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
return $permissions[$resource][$action] ?? false;
|
2021-10-23 20:19:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!function_exists('cannot')) {
|
2021-10-23 20:19:21 +03:00
|
|
|
/**
|
|
|
|
* Check if the currently logged-in user can perform an action
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* if (cannot('edit', 'appointments')) abort(403);
|
|
|
|
*
|
|
|
|
* @param string $action
|
|
|
|
* @param string $resource
|
|
|
|
* @param int|null $user_id
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
function cannot(string $action, string $resource, int $user_id = null): bool
|
2021-10-23 20:19:21 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
return !can($action, $resource, $user_id);
|
2021-10-23 20:19:21 +03:00
|
|
|
}
|
|
|
|
}
|