2020-08-04 11:25:22 +00:00
|
|
|
<?php
|
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2021-08-23 11:05:16 +00:00
|
|
|
namespace Modules\Auth\Authorization;
|
2020-07-31 16:05:10 +00:00
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
use Myth\Auth\Authorization\FlatAuthorization as MythAuthFlatAuthorization;
|
|
|
|
|
|
|
|
class FlatAuthorization extends MythAuthFlatAuthorization
|
2020-07-31 16:05:10 +00:00
|
|
|
{
|
2021-05-12 14:00:25 +00:00
|
|
|
/**
|
2021-05-19 16:35:13 +00:00
|
|
|
* The group model to use. Usually the class noted below (or an extension thereof) but can be any compatible
|
|
|
|
* CodeIgniter Model.
|
2021-05-12 14:00:25 +00:00
|
|
|
*
|
|
|
|
* @var PermissionModel
|
|
|
|
*/
|
|
|
|
protected $permissionModel;
|
|
|
|
|
2020-07-31 16:05:10 +00:00
|
|
|
/**
|
|
|
|
* Checks a group to see if they have the specified permission.
|
|
|
|
*/
|
2021-05-19 16:35:13 +00:00
|
|
|
public function groupHasPermission(int | string $permission, int $groupId): bool
|
2020-07-31 16:05:10 +00:00
|
|
|
{
|
|
|
|
// Get the Permission ID
|
|
|
|
$permissionId = $this->getPermissionID($permission);
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
if (! is_numeric($permissionId)) {
|
2020-07-31 16:05:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
return $this->permissionModel->doesGroupHavePermission($groupId, $permissionId);
|
2020-07-31 16:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-05 16:10:39 +00:00
|
|
|
* Makes user part of given groups.
|
2020-07-31 16:05:10 +00:00
|
|
|
*
|
2021-05-12 14:00:25 +00:00
|
|
|
* @param array<string, string> $groups Either collection of ID or names
|
2020-07-31 16:05:10 +00:00
|
|
|
*/
|
2021-05-06 14:00:48 +00:00
|
|
|
public function setUserGroups(int $userId, array $groups = []): bool
|
2020-07-31 16:05:10 +00:00
|
|
|
{
|
|
|
|
// remove user from all groups before resetting it in new groups
|
2020-08-04 11:25:22 +00:00
|
|
|
$this->groupModel->removeUserFromAllGroups($userId);
|
2020-07-31 16:05:10 +00:00
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
if ($groups === []) {
|
2020-07-31 16:05:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($groups as $group) {
|
2020-08-04 11:25:22 +00:00
|
|
|
$this->addUserToGroup($userId, $group);
|
2020-07-31 16:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|