2020-08-04 11:25:22 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Authorization;
|
2020-07-31 16:05:10 +00:00
|
|
|
|
|
|
|
class FlatAuthorization extends \Myth\Auth\Authorization\FlatAuthorization
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Checks a group to see if they have the specified permission.
|
|
|
|
*
|
|
|
|
* @param int|string $permission
|
|
|
|
*/
|
2021-05-06 14:00:48 +00:00
|
|
|
public function groupHasPermission($permission, int $groupId): bool
|
2020-07-31 16:05:10 +00:00
|
|
|
{
|
|
|
|
// Get the Permission ID
|
|
|
|
$permissionId = $this->getPermissionID($permission);
|
|
|
|
|
|
|
|
if (!is_numeric($permissionId)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
return (bool) $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-06 14:00:48 +00:00
|
|
|
* @param array $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-06 14:00:48 +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;
|
|
|
|
}
|
|
|
|
}
|