<?php
// \App\Config\Services
// /**
// * The Auth service.
// *
// * @param bool $getShared
// *
// * @return CustomAuthService|mixed
// */
// public static function auth($getShared = true)
// {
// if ($getShared) {
// return self::getSharedInstance('auth');
// }
// return new \App\Libraries\CustomAuthService(); // Use your custom service
// }
namespace App\Libraries;
use CodeIgniter\Shield\Config\Auth as AuthConfig;
class CustomAuthService extends \CodeIgniter\Shield\Auth
{
function __construct()
{
parent::__construct(new AuthConfig());
}
/**
* Create a new group and save it to the settings.
*
* @param string $group
* @param string $title
* @param string $description
* @return bool
*/
public function createGroup(string $group, string $title = '', string $description = '')
{
// Fetch the existing groups from the settings
$groups = setting('AuthGroups.groups') ?? [];
// Check if the group already exists
if ($this->existedGroup($group)) {
throw new \RuntimeException("Group '{$group}' already exists.");
}
// Add the new group
$groups[$group] = [
'title' => $title,
'description' => $description
];
// Save the updated groups to the settings
setting()->set('AuthGroups.groups', $groups);
return true;
}
/**
* Delete a group and remove it from the settings.
* Also remove the group from the users store in table 'auth_groups_users'.
*
* @param string $group
* @return bool
*/
function deleteGroup(string $group)
{
// Fetch the existing groups from the settings
$groups = setting('AuthGroups.groups') ?? [];
// Check if the group already exists
if (!$this->existedGroup($group)) {
throw new \RuntimeException("Group '{$group}' does not exist.");
}
// Remove the group
unset($groups[$group]);
// Remove the group from the matrix
$matrix = setting('AuthGroups.matrix') ?? [];
unset($matrix[$group]);
// Remove the group from the users store in table 'auth_groups_users'
$db = db_connect();
if (!$db->table('auth_groups_users')->where('group', $group)->delete()) {
throw new \RuntimeException("Failed to delete group '{$group}'.");
}
// Save the updated groups to the settings
setting()->set('AuthGroups.groups', $groups);
return true;
}
/**
* Update a group and save it to the settings.
* Also update the group in the users store in table 'auth_groups_users'.
*
* @param string $group
* @param string $groupNew
* @param string $titleNew
* @param string $descriptionNew
*
* @return bool
*/
function updateGroup(string $group, string $groupNew, string $titleNew = '', string $descriptionNew = '')
{
// Fetch the existing groups from the settings
$groups = setting('AuthGroups.groups') ?? [];
// Check if the group already exists
if (!$this->existedGroup($group)) {
throw new \RuntimeException("Group '{$group}' does not exist.");
}
// Update the group
$groups[$group] = [
'title' => $titleNew,
'description' => $descriptionNew
];
// Rename the group
$groups[$groupNew] = $groups[$group];
unset($groups[$group]);
// Update the group in the matrix
$matrix = setting('AuthGroups.matrix') ?? [];
$matrix[$groupNew] = $matrix[$group];
unset($matrix[$group]);
// Update the group in the users store in table 'auth_groups_users'
$db = db_connect();
if (!$db->table('auth_groups_users')->where('group', $group)->update(['group' => $groupNew])) {
throw new \RuntimeException("Failed to update group '{$group}' to '{$groupNew}'.");
}
// Save the updated groups to the settings
setting()->set('AuthGroups.groups', $groups);
return true;
}
/**
* Check if a group already exists.
*
* @param string $group
* @return bool
*/
function existedGroup(string $group)
{
// Fetch the existing groups from the settings
$groups = setting('AuthGroups.groups') ?? [];
// Check if the group already exists
if (isset($groups[$group])) {
return true;
}
return false;
}
/**
* Create a new permission and save it to the settings.
*
* @param string $permissionName
* @param string $description
*
* @return bool
*/
public function createPermission(string $permissionName, string $description = '')
{
// Fetch existing permissions from the settings
$permissions = setting('AuthGroups.permissions') ?? [];
// Check if the permission already exists
if ($this->existedPermission($permissionName)) {
throw new \RuntimeException("Permission '{$permissionName}' already exists.");
}
// Add the new permission
$permissions[$permissionName] = $description;
// Save the updated permissions to the settings
setting()->set('AuthGroups.permissions', $permissions);
return "Permission '{$permissionName}' created successfully.";
}
/**
* Update a permission and save it to the settings.
* Also update the permission in the users store in table 'auth_permissions_users'.
*
* @param string $permission
* @param string $permissionNew
* @param string $descriptionNew
*
* @return bool
*/
function updatePermission(string $permission, string $permissionNew, string $descriptionNew = '')
{
// Fetch the existing permissions from the settings
$permissions = setting('AuthGroups.permissions') ?? [];
// Check if the permission already exists
if (!$this->existedPermission($permission)) {
throw new \RuntimeException("Permission '{$permission}' does not exist.");
}
// Update the permission
$permissions[$permission] = $descriptionNew;
// Rename the permission
$permissions[$permissionNew] = $permissions[$permission];
unset($permissions[$permission]);
// Update the permission in the matrix
$matrix = setting('AuthGroups.matrix') ?? [];
foreach ($matrix as $group => $perms) {
if (($key = array_search($permission, $perms)) !== false) {
$matrix[$group][$key] = $permissionNew;
}
}
// Update the permission in the users store in table 'auth_permissions_users'
$db = db_connect();
if (!$db->table('auth_permissions_users')->where('permission', $permission)->update(['permission' => $permissionNew])) {
throw new \RuntimeException("Failed to update permission '{$permission}' to '{$permissionNew}'.");
}
// Save the updated permissions to the settings
setting()->set('AuthGroups.permissions', $permissions);
return true;
}
/**
* Delete a permission and remove it from the settings.
* Also remove the permission from the users store in table 'auth_permissions_users'.
*
* @param string $permission
* @return bool
*/
function deletePermission(string $permission)
{
// Fetch the existing permissions from the settings
$permissions = setting('AuthGroups.permissions') ?? [];
// Check if the permission already exists
if (!$this->existedPermission($permission)) {
throw new \RuntimeException("Permission '{$permission}' does not exist.");
}
// Remove the permission from the matrix
$matrix = setting('AuthGroups.matrix') ?? [];
foreach ($matrix as $group => $perms) {
if (($key = array_search($permission, $perms)) !== false) {
unset($matrix[$group][$key]);
}
}
// Remove the permission from the users store in table 'auth_permissions_users'
$db = db_connect();
if ($db->table('auth_permissions_users')->where('permission', $permission)->delete()) {
throw new \RuntimeException("Failed to delete permission '{$permission}'.");
}
// Remove the permission
unset($permissions[$permission]);
// Save the updated permissions to the settings
setting()->set('AuthGroups.permissions', $permissions);
return true;
}
/**
* Check if a permission already exists.
*
* @param string $permission
* @return bool
*/
function existedPermission(string $permission)
{
// Fetch the existing permissions from the settings
$permissions = setting('AuthGroups.permissions') ?? [];
// Check if the permission already exists
if (isset($permissions[$permission])) {
return true;
}
return false;
}
/**
* Create or update a rule (permission matrix) that assigns permissions to groups.
*
* @param string $groups
* @param string $permissionName
* @return bool
*/
public function createRule(string $groupName, string $permissionName)
{
// Fetch the existing groups from the settings
$groups = setting('AuthGroups.groups') ?? [];
// Check if the group already exists
if (!isset($groups[$groupName])) {
$this->createGroup($groupName);
}
// Fetch the existing permissions from the settings
$permissions = setting('AuthGroups.permissions') ?? [];
// Check if the permission already exists
if (!isset($permissions[$permissionName])) {
$this->createPermission($permissionName);
}
// Fetch the existing matrix from the settings
$matrix = setting('AuthGroups.matrix') ?? [];
if (!isset($matrix[$groupName])) {
$matrix[$groupName] = [];
}
// Add the permission to the group's permissions if not already present
if (!in_array($permissionName, $matrix[$groupName])) {
$matrix[$groupName][] = $permissionName;
}
// Save the updated matrix to the settings
setting()->set('AuthGroups.matrix', $matrix);
log_message('info', "Rule '{$groupName}' => '{$permissionName}' created successfully.");
log_message('info', db_connect()->getLastQuery());
return true;
}
/**
* Create or update multiple rules (permission matrix) that assigns permissions to groups.
*
* @param array $groups
* @param array $permissionNames
* @return bool
*/
function createRules(array $groupNames, array $permissionNames)
{
foreach ($groupNames as $groupName) {
foreach ($permissionNames as $permissionName) {
$this->createRule($groupName, $permissionName);
}
}
return true;
}
/**
* Delete a rule (permission matrix) that assigns permissions to groups.
*
* @param string $groupName
* @param string $permissionName
* @return bool
*/
public function deleteRule(string $groupName, string $permissionName)
{
// Fetch the existing groups from the settings
$groups = setting('AuthGroups.groups') ?? [];
// Check if the group already exists
if (!isset($groups[$groupName])) {
throw new \RuntimeException("Group '{$groupName}' does not exist.");
}
// Fetch the existing permissions from the settings
$permissions = setting('AuthGroups.permissions') ?? [];
// Check if the permission already exists
if (!isset($permissions[$permissionName])) {
throw new \RuntimeException("Permission '{$permissionName}' does not exist.");
}
// Fetch the existing matrix from the settings
$matrix = setting('AuthGroups.matrix') ?? [];
// Check if the group already exists
if (!isset($matrix[$groupName])) {
throw new \RuntimeException("Group '{$groupName}' does not have rule.");
}
// Remove the permission from the group's permissions
if (($key = array_search($permissionName, $matrix[$groupName])) !== false) {
unset($matrix[$groupName][$key]);
}
// Save the updated matrix to the settings
setting()->set('AuthGroups.matrix', $matrix);
return true;
}
}