Skip to content

Commit

Permalink
public function handlePermissionStateChange($groupId): ?array
Browse files Browse the repository at this point in the history
Code deduplication by introducing `PermissionManager::()`
  • Loading branch information
martin-rueegg committed Dec 16, 2023
1 parent 5c17cb6 commit 2fdef41
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 79 deletions.
89 changes: 89 additions & 0 deletions protected/humhub/libs/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use humhub\exceptions\InvalidArgumentClassException;
use humhub\exceptions\InvalidArgumentTypeException;
use humhub\exceptions\InvalidArgumentValueException;
use Stringable;
use Yii;
use yii\base\InvalidArgumentException;

Expand Down Expand Up @@ -187,6 +188,94 @@ public static function GetUniqeId()
return str_replace('.', '', uniqid('', true));
}

/**
* @param mixed $value value to be tested or converted
* @param bool $strict indicates if strict comparison should be performed:
* ``
* - if True, `$value` must already be of type `int`.
* - if False, a conversion to `int` is attempted.
* ``
*
* @since 1.16
*/
public static function checkBool($value, bool $strict = false): ?bool
{
// check if strict
if (($strict && !is_bool($value)) || !is_scalar($value)) {
return null;
}

return filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
}

/**
* @param mixed $value value to be tested or converted
* @param bool $strict indicates if strict comparison should be performed:
* ``
* - if True, `$value` must already be of type `float`.
* - if False, a conversion to `float` is attempted.
* ``
*
* @since 1.16
*/
public static function checkFloat($value, bool $strict = false): ?float
{
// check if strict
if (($strict && !is_float($value)) || !is_scalar($value)) {
return null;
}

return filter_var($value, FILTER_VALIDATE_FLOAT, FILTER_NULL_ON_FAILURE);
}

/**
* @param mixed $value value to be tested or converted
* @param bool $strict indicates if strict comparison should be performed:
* ``
* - if True, `$value` must already be of type `int`.
* - if False, a conversion to `int` is attempted.
* ``
*
* @since 1.16
*/
public static function checkInt($value, bool $strict = false): ?int
{
// check if strict
if (($strict && !is_int($value)) || !is_scalar($value)) {
return null;
}

return filter_var($value, FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
}

/**
* @param mixed $value value to be tested or converted
* @param bool $strict indicates if strict comparison should be performed:
* ``
* - if True, `$value` must already be of type `string`.
* - if False, a conversion to `string` is attempted.
* ``
*
* @since 1.16
*/
public static function checkString($value, bool $strict = false): ?string
{

if ($strict) {
return is_string($value) ? $value : null;
}

if ($value instanceof Stringable || (\is_object($value) && \method_exists($value, '__toString'))) {
return $value->__toString();
}

if (!is_scalar($value)) {
return null;
}

return (string)$value;
}

/**
* Checks if the class has this class as one of its parents
*
Expand Down
19 changes: 6 additions & 13 deletions protected/humhub/modules/admin/controllers/GroupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
*/
class GroupController extends Controller
{

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -112,24 +111,18 @@ public function actionEdit()
]);
}

public function actionManagePermissions()
public function actionManagePermissions(int $id)
{
$group = Group::findOne(['id' => Yii::$app->request->get('id')]);
$group = Group::findOne(['id' => $id]);

$this->checkGroupAccess($group);

// Save changed permission states
if (!$group->isNewRecord && Yii::$app->request->post('dropDownColumnSubmit')) {
$permission = Yii::$app->user->permissionManager->getById(Yii::$app->request->post('permissionId'), Yii::$app->request->post('moduleId'));
if ($permission === null) {
throw new HttpException(500, 'Could not find permission!');
}
Yii::$app->user->permissionManager->setGroupState($group->id, $permission, Yii::$app->request->post('state'));

return $this->asJson([]);
if (!$group->isNewRecord) {
// Save changed permission state
$return = Yii::$app->user->permissionManager->handlePermissionStateChange($group->id);
}

return $this->render('permissions', ['group' => $group]);
return $return ?? $this->render('permissions', ['group' => $group]);
}

public function actionManageGroupUsers()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,31 +55,22 @@ protected function getAccessRules()
/**
* Default User Permissions
*/
public function actionIndex()
public function actionIndex($groupId = User::USERGROUP_USER)
{
$defaultPermissionManager = new ContentContainerDefaultPermissionManager([
'contentContainerClass' => User::class,
]);

$groups = User::getUserGroups();

$groupId = Yii::$app->request->get('groupId', User::USERGROUP_USER);
if (!array_key_exists($groupId, $groups)) {
throw new HttpException(500, 'Invalid group id given!');
}

// Handle permission state change
if (Yii::$app->request->post('dropDownColumnSubmit')) {
Yii::$app->response->format = 'json';
$permission = $defaultPermissionManager->getById(Yii::$app->request->post('permissionId'), Yii::$app->request->post('moduleId'));
if ($permission === null) {
throw new HttpException(500, 'Could not find permission!');
}
$defaultPermissionManager->setGroupState($groupId, $permission, Yii::$app->request->post('state'));
return [];
}
$return = $defaultPermissionManager->handlePermissionStateChange($groupId);

return $this->render('default', [
return $return ?? $this->render('default', [
'defaultPermissionManager' => $defaultPermissionManager,
'groups' => $groups,
'groupId' => $groupId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,30 +68,21 @@ public function actionIndex()
}

/**
* Shows space permessions
* Shows space permissions
*/
public function actionPermissions()
public function actionPermissions($groupId = Space::USERGROUP_MEMBER)
{
$space = $this->getSpace();

$groups = $space->getUserGroups();
$groupId = Yii::$app->request->get('groupId', Space::USERGROUP_MEMBER);
if (!array_key_exists($groupId, $groups)) {
throw new HttpException(500, 'Invalid group id given!');
}

// Handle permission state change
if (Yii::$app->request->post('dropDownColumnSubmit')) {
Yii::$app->response->format = 'json';
$permission = $space->permissionManager->getById(Yii::$app->request->post('permissionId'), Yii::$app->request->post('moduleId'));
if ($permission === null) {
throw new HttpException(500, 'Could not find permission!');
}
$space->permissionManager->setGroupState($groupId, $permission, Yii::$app->request->post('state'));
return [];
}
$return = $space->permissionManager->handlePermissionStateChange($groupId);

return $this->render('permissions', [
return $return ?? $this->render('permissions', [
'space' => $space,
'groups' => $groups,
'groupId' => $groupId
Expand Down

0 comments on commit 2fdef41

Please sign in to comment.