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
|
|
|
|
{
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
// Actions
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks a group to see if they have the specified permission.
|
|
|
|
*
|
|
|
|
* @param int|string $permission
|
|
|
|
* @param int $groupId
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function groupHasPermission($permission, int $groupId)
|
|
|
|
{
|
|
|
|
if (
|
|
|
|
empty($permission) ||
|
|
|
|
(!is_string($permission) && !is_numeric($permission))
|
|
|
|
) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($groupId) || !is_numeric($groupId)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the Permission ID
|
|
|
|
$permissionId = $this->getPermissionID($permission);
|
|
|
|
|
|
|
|
if (!is_numeric($permissionId)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
$this->permissionModel->doesGroupHavePermission(
|
|
|
|
$groupId,
|
|
|
|
(int) $permissionId
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-05 16:10:39 +00:00
|
|
|
* Makes user part of given groups.
|
2020-07-31 16:05:10 +00:00
|
|
|
*
|
2020-08-04 11:25:22 +00:00
|
|
|
* @param $userId
|
2020-07-31 16:05:10 +00:00
|
|
|
* @param array|null $groups // Either collection of ID or names
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2020-08-04 11:25:22 +00:00
|
|
|
public function setUserGroups(int $userId, $groups)
|
2020-07-31 16:05:10 +00:00
|
|
|
{
|
2020-08-04 11:25:22 +00:00
|
|
|
if (empty($userId) || !is_numeric($userId)) {
|
2020-07-31 16:05:10 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
if (empty($groups)) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|