Skip to content

Commit 30d9030

Browse files
committed
feat: Add case-insensitive duplicate checks for permissions and roles
1 parent 70b12bf commit 30d9030

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/Models/Permission.php

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

52+
// Check for case-insensitive duplicate
53+
$existing = static::query()
54+
->whereRaw('LOWER(name) = ?', [strtolower($attributes['name'])])
55+
->where('guard_name', $attributes['guard_name'])
56+
->first();
57+
58+
if ($existing) {
59+
throw PermissionAlreadyExists::create($attributes['name'], $attributes['guard_name']);
60+
}
61+
5262
return static::query()->create($attributes);
5363
}
5464

src/Models/Role.php

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

65+
// Check for case-insensitive duplicate
66+
$query = static::query()
67+
->whereRaw('LOWER(name) = ?', [strtolower($attributes['name'])])
68+
->where('guard_name', $attributes['guard_name']);
69+
70+
if ($registrar->teams) {
71+
$teamsKey = $registrar->teamsKey;
72+
$query->where(fn ($q) => $q->whereNull($teamsKey)
73+
->orWhere($teamsKey, $params[$teamsKey] ?? getPermissionsTeamId())
74+
);
75+
}
76+
77+
$existing = $query->first();
78+
79+
if ($existing) {
80+
throw RoleAlreadyExists::create($attributes['name'], $attributes['guard_name']);
81+
}
82+
6583
return static::query()->create($attributes);
6684
}
6785

0 commit comments

Comments
 (0)