Skip to content

Commit 3fbdd3f

Browse files
committed
refactor: Implement case-insensitive comparison for permission name attribute in PermissionRegistrar
1 parent 58e34b9 commit 3fbdd3f

File tree

3 files changed

+8
-32
lines changed

3 files changed

+8
-32
lines changed

src/Models/Permission.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,6 @@ public static function create(array $attributes = [])
4949
throw PermissionAlreadyExists::create($attributes['name'], $attributes['guard_name']);
5050
}
5151

52-
// Check for case-insensitive duplicate
53-
$permissionClass = get_class(new static);
54-
$existing = app($permissionClass)->where('guard_name', $attributes['guard_name'])->get()
55-
->filter(function ($item) use ($attributes) {
56-
return strtolower($item->name) === strtolower($attributes['name']);
57-
})->first();
58-
59-
if ($existing) {
60-
throw PermissionAlreadyExists::create($attributes['name'], $attributes['guard_name']);
61-
}
62-
6352
return static::query()->create($attributes);
6453
}
6554

src/Models/Role.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -62,26 +62,6 @@ public static function create(array $attributes = [])
6262
throw RoleAlreadyExists::create($attributes['name'], $attributes['guard_name']);
6363
}
6464

65-
// Check for case-insensitive duplicate
66-
$roleClass = get_class(new static);
67-
$query = app($roleClass)->where('guard_name', $attributes['guard_name']);
68-
69-
if ($registrar->teams) {
70-
$teamsKey = $registrar->teamsKey;
71-
$teamId = $params[$teamsKey] ?? getPermissionsTeamId();
72-
$query = $query->where(function ($q) use ($teamsKey, $teamId) {
73-
$q->whereNull($teamsKey)->orWhere($teamsKey, $teamId);
74-
});
75-
}
76-
77-
$existing = $query->get()->filter(function ($item) use ($attributes) {
78-
return strtolower($item->name) === strtolower($attributes['name']);
79-
})->first();
80-
81-
if ($existing) {
82-
throw RoleAlreadyExists::create($attributes['name'], $attributes['guard_name']);
83-
}
84-
8565
return static::query()->create($attributes);
8666
}
8767

src/PermissionRegistrar.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,14 @@ public function getPermissions(array $params = [], bool $onlyOne = false): Colle
246246

247247
$permissions = $this->permissions->$method(static function ($permission) use ($params) {
248248
foreach ($params as $attr => $value) {
249-
if ($permission->getAttribute($attr) != $value) {
249+
$permissionValue = $permission->getAttribute($attr);
250+
251+
// Case-insensitive comparison for 'name' attribute
252+
if ($attr === 'name' && is_string($value) && is_string($permissionValue)) {
253+
if (strtolower($permissionValue) !== strtolower($value)) {
254+
return false;
255+
}
256+
} elseif ($permissionValue != $value) {
250257
return false;
251258
}
252259
}

0 commit comments

Comments
 (0)