Skip to content

Commit 4cac2d8

Browse files
committed
refactor: extract isValidGroup()
1 parent 56662ca commit 4cac2d8

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

src/Authorization/Traits/Authorizable.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ public function addGroup(string ...$groups): self
3333
{
3434
$this->populateGroups();
3535

36-
$configGroups = $this->getConfigGroups();
37-
3836
$groupCount = count($this->groupCache);
3937

4038
foreach ($groups as $group) {
@@ -46,7 +44,7 @@ public function addGroup(string ...$groups): self
4644
}
4745

4846
// make sure it's a valid group
49-
if (! in_array($group, $configGroups, true)) {
47+
if (! $this->isValidGroup($group)) {
5048
throw AuthorizationException::forUnknownGroup($group);
5149
}
5250

@@ -61,6 +59,18 @@ public function addGroup(string ...$groups): self
6159
return $this;
6260
}
6361

62+
/**
63+
* @TODO duplicate of UserModel::isValidGroup()
64+
*
65+
* @param non-empty-string $group
66+
*/
67+
private function isValidGroup(string $group): bool
68+
{
69+
$allowedGroups = array_keys(setting('AuthGroups.groups'));
70+
71+
return (bool) (in_array($group, $allowedGroups, true));
72+
}
73+
6474
/**
6575
* Removes one or more groups from the user.
6676
*
@@ -96,10 +106,8 @@ public function syncGroups(string ...$groups): self
96106
{
97107
$this->populateGroups();
98108

99-
$configGroups = $this->getConfigGroups();
100-
101109
foreach ($groups as $group) {
102-
if (! in_array($group, $configGroups, true)) {
110+
if (! $this->isValidGroup($group)) {
103111
throw AuthorizationException::forUnknownGroup($group);
104112
}
105113
}
@@ -398,14 +406,6 @@ private function saveGroupsOrPermissions(string $type, $model, array $cache): vo
398406
}
399407
}
400408

401-
/**
402-
* @return list<string>
403-
*/
404-
private function getConfigGroups(): array
405-
{
406-
return array_keys(setting('AuthGroups.groups'));
407-
}
408-
409409
/**
410410
* @return list<string>
411411
*/

src/Models/UserModel.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,16 +153,27 @@ private function assignIdentities(array $data, array $identities): array
153153
*/
154154
public function addToDefaultGroup(User $user): void
155155
{
156-
$defaultGroup = setting('AuthGroups.defaultGroup');
157-
$allowedGroups = array_keys(setting('AuthGroups.groups'));
156+
$defaultGroup = setting('AuthGroups.defaultGroup');
158157

159-
if (empty($defaultGroup) || ! in_array($defaultGroup, $allowedGroups, true)) {
158+
if (empty($defaultGroup) || ! $this->isValidGroup($defaultGroup)) {
160159
throw new InvalidArgumentException(lang('Auth.unknownGroup', [$defaultGroup ?? '--not found--']));
161160
}
162161

163162
$user->addGroup($defaultGroup);
164163
}
165164

165+
/**
166+
* @TODO duplicate of Authorizable::isValidGroup()
167+
*
168+
* @param non-empty-string $group
169+
*/
170+
private function isValidGroup(string $group): bool
171+
{
172+
$allowedGroups = array_keys(setting('AuthGroups.groups'));
173+
174+
return (bool) (in_array($group, $allowedGroups, true));
175+
}
176+
166177
public function fake(Generator &$faker): User
167178
{
168179
$this->checkReturnType();

0 commit comments

Comments
 (0)