Skip to content

Commit 7ae3ff5

Browse files
committed
refactor: use GroupModel::isValidGroup()
1 parent e79bf78 commit 7ae3ff5

File tree

2 files changed

+12
-27
lines changed

2 files changed

+12
-27
lines changed

src/Authorization/Traits/Authorizable.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,11 @@ public function addGroup(string ...$groups): self
4343
continue;
4444
}
4545

46+
/** @var GroupModel $groupModel */
47+
$groupModel = model(GroupModel::class);
48+
4649
// make sure it's a valid group
47-
if (! $this->isValidGroup($group)) {
50+
if (! $groupModel->isValidGroup($group)) {
4851
throw AuthorizationException::forUnknownGroup($group);
4952
}
5053

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

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-
7465
/**
7566
* Removes one or more groups from the user.
7667
*
@@ -106,8 +97,11 @@ public function syncGroups(string ...$groups): self
10697
{
10798
$this->populateGroups();
10899

100+
/** @var GroupModel $groupModel */
101+
$groupModel = model(GroupModel::class);
102+
109103
foreach ($groups as $group) {
110-
if (! $this->isValidGroup($group)) {
104+
if (! $groupModel->isValidGroup($group)) {
111105
throw AuthorizationException::forUnknownGroup($group);
112106
}
113107
}

src/Models/UserModel.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -155,25 +155,16 @@ public function addToDefaultGroup(User $user): void
155155
{
156156
$defaultGroup = setting('AuthGroups.defaultGroup');
157157

158-
if (empty($defaultGroup) || ! $this->isValidGroup($defaultGroup)) {
158+
/** @var GroupModel $groupModel */
159+
$groupModel = model(GroupModel::class);
160+
161+
if (empty($defaultGroup) || ! $groupModel->isValidGroup($defaultGroup)) {
159162
throw new InvalidArgumentException(lang('Auth.unknownGroup', [$defaultGroup ?? '--not found--']));
160163
}
161164

162165
$user->addGroup($defaultGroup);
163166
}
164167

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-
177168
public function fake(Generator &$faker): User
178169
{
179170
$this->checkReturnType();

0 commit comments

Comments
 (0)