2020-04-22 22:48:56 +03:00
|
|
|
|
<?php defined('BASEPATH') or exit('No direct script access allowed');
|
2016-01-09 23:29:28 +02:00
|
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------
|
2023-12-31 21:46:27 +03:00
|
|
|
|
* IFLRandevu - İzmir Fen Lisesi Randevu Portalı
|
2016-01-09 23:29:28 +02: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
|
2016-01-09 23:29:28 +02:00
|
|
|
|
* @since v1.0.0
|
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
/**
|
2021-11-06 18:21:27 +03:00
|
|
|
|
* Roles model.
|
2021-10-27 11:04:28 +03:00
|
|
|
|
*
|
|
|
|
|
* Handles all the database operations of the role resource.
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*
|
|
|
|
|
* @package Models
|
|
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
|
class Roles_model extends EA_Model
|
|
|
|
|
{
|
2021-10-29 13:39:08 +03:00
|
|
|
|
/**
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2023-03-13 11:06:18 +03:00
|
|
|
|
protected array $casts = [
|
2021-10-29 13:39:08 +03:00
|
|
|
|
'id' => 'integer',
|
|
|
|
|
'is_admin' => 'boolean',
|
|
|
|
|
'appointments' => 'integer',
|
|
|
|
|
'customers' => 'integer',
|
|
|
|
|
'services' => 'integer',
|
|
|
|
|
'users' => 'integer',
|
|
|
|
|
'system_settings' => 'integer',
|
2023-12-22 13:35:41 +03:00
|
|
|
|
'user_settings' => 'integer',
|
2021-10-29 13:39:08 +03:00
|
|
|
|
];
|
|
|
|
|
|
2016-01-09 23:29:28 +02:00
|
|
|
|
/**
|
2021-10-27 11:04:28 +03:00
|
|
|
|
* Save (insert or update) a role.
|
|
|
|
|
*
|
|
|
|
|
* @param array $role Associative array with the role data.
|
|
|
|
|
*
|
|
|
|
|
* @return int Returns the role ID.
|
|
|
|
|
*
|
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
|
*/
|
|
|
|
|
public function save(array $role): int
|
|
|
|
|
{
|
|
|
|
|
$this->validate($role);
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (empty($role['id'])) {
|
2021-10-27 11:04:28 +03:00
|
|
|
|
return $this->insert($role);
|
2023-11-29 12:24:09 +03:00
|
|
|
|
} else {
|
2021-10-27 11:04:28 +03:00
|
|
|
|
return $this->update($role);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Validate the role data.
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*
|
2021-10-27 11:04:28 +03:00
|
|
|
|
* @param array $role Associative array with the role data.
|
2017-09-08 15:03:45 +03:00
|
|
|
|
*
|
2021-10-27 11:04:28 +03:00
|
|
|
|
* @throws InvalidArgumentException
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*/
|
2021-10-27 11:49:42 +03:00
|
|
|
|
public function validate(array $role)
|
2017-09-15 14:36:37 +03:00
|
|
|
|
{
|
2021-10-27 11:04:28 +03:00
|
|
|
|
// If a role ID is provided then check whether the record really exists in the database.
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (!empty($role['id'])) {
|
2021-10-27 11:04:28 +03:00
|
|
|
|
$count = $this->db->get_where('roles', ['id' => $role['id']])->num_rows();
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (!$count) {
|
|
|
|
|
throw new InvalidArgumentException(
|
2023-12-22 13:35:41 +03:00
|
|
|
|
'The provided role ID does not exist in the database: ' . $role['id'],
|
2023-11-29 12:24:09 +03:00
|
|
|
|
);
|
2021-10-27 11:04:28 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Make sure all required fields are provided.
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (empty($role['name'])) {
|
|
|
|
|
throw new InvalidArgumentException('Not all required fields are provided: ' . print_r($role, true));
|
2021-10-27 11:04:28 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Insert a new role into the database.
|
|
|
|
|
*
|
|
|
|
|
* @param array $role Associative array with the role data.
|
|
|
|
|
*
|
|
|
|
|
* @return int Returns the role ID.
|
|
|
|
|
*
|
|
|
|
|
* @throws RuntimeException
|
|
|
|
|
*/
|
|
|
|
|
protected function insert(array $role): int
|
|
|
|
|
{
|
2022-01-25 01:42:13 +03:00
|
|
|
|
$role['create_datetime'] = date('Y-m-d H:i:s');
|
|
|
|
|
$role['update_datetime'] = date('Y-m-d H:i:s');
|
2023-10-19 17:25:45 +03:00
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (!$this->db->insert('roles', $role)) {
|
2021-10-27 11:04:28 +03:00
|
|
|
|
throw new RuntimeException('Could not insert role.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->db->insert_id();
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-10-27 11:04:28 +03:00
|
|
|
|
* Update an existing role.
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*
|
2021-10-27 11:04:28 +03:00
|
|
|
|
* @param array $role Associative array with the role data.
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*
|
2021-10-27 11:04:28 +03:00
|
|
|
|
* @return int Returns the role ID.
|
2017-09-08 15:03:45 +03:00
|
|
|
|
*
|
2021-10-27 11:04:28 +03:00
|
|
|
|
* @throws RuntimeException
|
2016-01-09 23:29:28 +02:00
|
|
|
|
*/
|
2021-10-27 11:04:28 +03:00
|
|
|
|
protected function update(array $role): int
|
2017-09-15 14:36:37 +03:00
|
|
|
|
{
|
2022-01-25 01:42:13 +03:00
|
|
|
|
$role['update_datetime'] = date('Y-m-d H:i:s');
|
2023-10-19 17:25:45 +03:00
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (!$this->db->update('roles', $role, ['id' => $role['id']])) {
|
2021-10-27 11:04:28 +03:00
|
|
|
|
throw new RuntimeException('Could not update role.');
|
|
|
|
|
}
|
2016-01-09 23:29:28 +02:00
|
|
|
|
|
2021-10-27 11:04:28 +03:00
|
|
|
|
return $role['id'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Remove an existing role from the database.
|
|
|
|
|
*
|
|
|
|
|
* @param int $role_id Role ID.
|
|
|
|
|
*
|
|
|
|
|
* @throws RuntimeException
|
|
|
|
|
*/
|
2023-10-19 17:25:45 +03:00
|
|
|
|
public function delete(int $role_id): void
|
2021-10-27 11:04:28 +03:00
|
|
|
|
{
|
2023-10-19 17:25:45 +03:00
|
|
|
|
$this->db->delete('roles', ['id' => $role_id]);
|
2021-10-27 11:04:28 +03:00
|
|
|
|
}
|
2016-01-09 23:29:28 +02:00
|
|
|
|
|
2021-10-27 11:04:28 +03:00
|
|
|
|
/**
|
|
|
|
|
* Get a specific role from the database.
|
|
|
|
|
*
|
|
|
|
|
* @param int $role_id The ID of the record to be returned.
|
|
|
|
|
*
|
|
|
|
|
* @return array Returns an array with the role data.
|
|
|
|
|
*
|
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
|
*/
|
2023-10-19 17:25:45 +03:00
|
|
|
|
public function find(int $role_id): array
|
2021-10-27 11:04:28 +03:00
|
|
|
|
{
|
2021-10-29 13:39:08 +03:00
|
|
|
|
$role = $this->db->get_where('roles', ['id' => $role_id])->row_array();
|
2022-01-25 01:32:42 +03:00
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (!$role) {
|
2022-01-25 01:32:42 +03:00
|
|
|
|
throw new InvalidArgumentException('The provided role ID was not found in the database: ' . $role_id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->cast($role);
|
|
|
|
|
|
|
|
|
|
return $role;
|
2021-10-27 11:04:28 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a specific field value from the database.
|
|
|
|
|
*
|
|
|
|
|
* @param int $role_id Role ID.
|
|
|
|
|
* @param string $field Name of the value to be returned.
|
|
|
|
|
*
|
2023-01-14 11:39:14 +03:00
|
|
|
|
* @return mixed Returns the selected role value from the database.
|
2021-10-27 11:04:28 +03:00
|
|
|
|
*
|
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
|
*/
|
2023-01-14 11:39:14 +03:00
|
|
|
|
public function value(int $role_id, string $field): mixed
|
2021-10-27 11:04:28 +03:00
|
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (empty($field)) {
|
2021-10-27 11:04:28 +03:00
|
|
|
|
throw new InvalidArgumentException('The field argument is cannot be empty.');
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (empty($role_id)) {
|
2021-10-27 11:04:28 +03:00
|
|
|
|
throw new InvalidArgumentException('The role ID argument cannot be empty.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check whether the role exists.
|
|
|
|
|
$query = $this->db->get_where('roles', ['id' => $role_id]);
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (!$query->num_rows()) {
|
2021-10-27 11:04:28 +03:00
|
|
|
|
throw new InvalidArgumentException('The provided role ID was not found in the database: ' . $role_id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if the required field is part of the role data.
|
|
|
|
|
$role = $query->row_array();
|
2022-01-25 01:32:42 +03:00
|
|
|
|
|
2021-10-29 13:39:08 +03:00
|
|
|
|
$this->cast($role);
|
2021-10-27 11:04:28 +03:00
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (!array_key_exists($field, $role)) {
|
2021-10-27 11:04:28 +03:00
|
|
|
|
throw new InvalidArgumentException('The requested field was not found in the role data: ' . $field);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $role[$field];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the permissions array by role slug.
|
|
|
|
|
*
|
|
|
|
|
* The permission numbers are converted into boolean values of the four main actions:
|
|
|
|
|
*
|
|
|
|
|
* - view
|
|
|
|
|
* - add
|
|
|
|
|
* - edit
|
|
|
|
|
* - delete
|
|
|
|
|
*
|
|
|
|
|
* After checking each individual value, you can make sure if the user is able to perform each action or not.
|
|
|
|
|
*
|
|
|
|
|
* @param string $slug Role slug.
|
|
|
|
|
*
|
|
|
|
|
* @return array Returns the permissions value.
|
|
|
|
|
*/
|
|
|
|
|
public function get_permissions_by_slug(string $slug): array
|
|
|
|
|
{
|
|
|
|
|
$role = $this->db->get_where('roles', ['slug' => $slug])->row_array();
|
2022-01-25 01:32:42 +03:00
|
|
|
|
|
2021-10-29 13:39:08 +03:00
|
|
|
|
$this->cast($role);
|
2021-10-27 11:04:28 +03:00
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
unset($role['id'], $role['name'], $role['slug'], $role['is_admin']);
|
2021-10-27 11:04:28 +03:00
|
|
|
|
|
|
|
|
|
// Convert the integer values to boolean.
|
|
|
|
|
|
|
|
|
|
$permissions = [];
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
foreach ($role as $resource => $value) {
|
2021-10-27 11:04:28 +03:00
|
|
|
|
$permissions[$resource] = [
|
2023-11-29 12:24:09 +03:00
|
|
|
|
'view' => false,
|
|
|
|
|
'add' => false,
|
|
|
|
|
'edit' => false,
|
2023-12-22 13:35:41 +03:00
|
|
|
|
'delete' => false,
|
2017-09-15 14:36:37 +03:00
|
|
|
|
];
|
2016-01-09 23:29:28 +02:00
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if ($value > 0) {
|
|
|
|
|
if ((int) ($value / PRIV_DELETE) === 1) {
|
|
|
|
|
$permissions[$resource]['delete'] = true;
|
2021-10-27 11:04:28 +03:00
|
|
|
|
$value -= PRIV_DELETE;
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if ((int) ($value / PRIV_EDIT) === 1) {
|
|
|
|
|
$permissions[$resource]['edit'] = true;
|
2021-10-27 11:04:28 +03:00
|
|
|
|
$value -= PRIV_EDIT;
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if ((int) ($value / PRIV_ADD) === 1) {
|
|
|
|
|
$permissions[$resource]['add'] = true;
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
$permissions[$resource]['view'] = true;
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-27 11:04:28 +03:00
|
|
|
|
return $permissions;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the query builder interface, configured for use with the roles table.
|
|
|
|
|
*
|
|
|
|
|
* @return CI_DB_query_builder
|
|
|
|
|
*/
|
|
|
|
|
public function query(): CI_DB_query_builder
|
|
|
|
|
{
|
|
|
|
|
return $this->db->from('roles');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Search roles by the provided keyword.
|
|
|
|
|
*
|
|
|
|
|
* @param string $keyword Search keyword.
|
|
|
|
|
* @param int|null $limit Record limit.
|
|
|
|
|
* @param int|null $offset Record offset.
|
|
|
|
|
* @param string|null $order_by Order by.
|
|
|
|
|
*
|
|
|
|
|
* @return array Returns an array of roles.
|
|
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
|
public function search(string $keyword, int $limit = null, int $offset = null, string $order_by = null): array
|
2021-10-27 11:04:28 +03:00
|
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
|
$roles = $this->db
|
2021-10-27 11:04:28 +03:00
|
|
|
|
->select()
|
|
|
|
|
->from('roles')
|
2022-07-07 16:27:10 +03:00
|
|
|
|
->group_start()
|
2021-10-27 11:04:28 +03:00
|
|
|
|
->like('name', $keyword)
|
|
|
|
|
->or_like('slug', $keyword)
|
2022-07-07 16:27:10 +03:00
|
|
|
|
->group_end()
|
2021-10-27 11:04:28 +03:00
|
|
|
|
->limit($limit)
|
|
|
|
|
->offset($offset)
|
|
|
|
|
->order_by($order_by)
|
|
|
|
|
->get()
|
|
|
|
|
->result_array();
|
2021-10-29 13:39:08 +03:00
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
|
foreach ($roles as &$role) {
|
|
|
|
|
$this->cast($role);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $roles;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get all roles that match the provided criteria.
|
|
|
|
|
*
|
|
|
|
|
* @param array|string|null $where Where conditions
|
|
|
|
|
* @param int|null $limit Record limit.
|
|
|
|
|
* @param int|null $offset Record offset.
|
|
|
|
|
* @param string|null $order_by Order by.
|
|
|
|
|
*
|
|
|
|
|
* @return array Returns an array of roles.
|
|
|
|
|
*/
|
|
|
|
|
public function get(
|
|
|
|
|
array|string $where = null,
|
|
|
|
|
int $limit = null,
|
|
|
|
|
int $offset = null,
|
2023-12-22 13:35:41 +03:00
|
|
|
|
string $order_by = null,
|
2023-11-29 12:24:09 +03:00
|
|
|
|
): array {
|
|
|
|
|
if ($where !== null) {
|
|
|
|
|
$this->db->where($where);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($order_by !== null) {
|
|
|
|
|
$this->db->order_by($order_by);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$roles = $this->db->get('roles', $limit, $offset)->result_array();
|
|
|
|
|
|
|
|
|
|
foreach ($roles as &$role) {
|
2021-10-29 13:39:08 +03:00
|
|
|
|
$this->cast($role);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $roles;
|
2021-10-27 11:04:28 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-11-06 17:40:55 +03:00
|
|
|
|
* Load related resources to a role.
|
2021-10-27 11:04:28 +03:00
|
|
|
|
*
|
|
|
|
|
* @param array $role Associative array with the role data.
|
|
|
|
|
* @param array $resources Resource names to be attached.
|
|
|
|
|
*
|
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
|
*/
|
2021-11-06 17:40:55 +03:00
|
|
|
|
public function load(array &$role, array $resources)
|
2021-10-27 11:04:28 +03:00
|
|
|
|
{
|
2023-10-19 17:25:45 +03:00
|
|
|
|
// Roles do not currently have any related resources.
|
2016-01-09 23:29:28 +02:00
|
|
|
|
}
|
|
|
|
|
}
|