Skip to content

Commit

Permalink
refactor: use GroupModel::isValidGroup()
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Aug 20, 2024
1 parent e79bf78 commit 7ae3ff5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 27 deletions.
22 changes: 8 additions & 14 deletions src/Authorization/Traits/Authorizable.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ public function addGroup(string ...$groups): self
continue;
}

/** @var GroupModel $groupModel */
$groupModel = model(GroupModel::class);

// make sure it's a valid group
if (! $this->isValidGroup($group)) {
if (! $groupModel->isValidGroup($group)) {
throw AuthorizationException::forUnknownGroup($group);
}

Expand All @@ -59,18 +62,6 @@ public function addGroup(string ...$groups): self
return $this;
}

/**
* @TODO duplicate of UserModel::isValidGroup()
*
* @param non-empty-string $group
*/
private function isValidGroup(string $group): bool
{
$allowedGroups = array_keys(setting('AuthGroups.groups'));

return (bool) (in_array($group, $allowedGroups, true));
}

/**
* Removes one or more groups from the user.
*
Expand Down Expand Up @@ -106,8 +97,11 @@ public function syncGroups(string ...$groups): self
{
$this->populateGroups();

/** @var GroupModel $groupModel */
$groupModel = model(GroupModel::class);

foreach ($groups as $group) {
if (! $this->isValidGroup($group)) {
if (! $groupModel->isValidGroup($group)) {
throw AuthorizationException::forUnknownGroup($group);
}
}
Expand Down
17 changes: 4 additions & 13 deletions src/Models/UserModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,25 +155,16 @@ public function addToDefaultGroup(User $user): void
{
$defaultGroup = setting('AuthGroups.defaultGroup');

if (empty($defaultGroup) || ! $this->isValidGroup($defaultGroup)) {
/** @var GroupModel $groupModel */
$groupModel = model(GroupModel::class);

if (empty($defaultGroup) || ! $groupModel->isValidGroup($defaultGroup)) {
throw new InvalidArgumentException(lang('Auth.unknownGroup', [$defaultGroup ?? '--not found--']));
}

$user->addGroup($defaultGroup);
}

/**
* @TODO duplicate of Authorizable::isValidGroup()
*
* @param non-empty-string $group
*/
private function isValidGroup(string $group): bool
{
$allowedGroups = array_keys(setting('AuthGroups.groups'));

return (bool) (in_array($group, $allowedGroups, true));
}

public function fake(Generator &$faker): User
{
$this->checkReturnType();
Expand Down

0 comments on commit 7ae3ff5

Please sign in to comment.