| @@ 33-44 (lines=12) @@ | ||
| 30 | $this->setTable(config('permission.table_names.permissions')); |
|
| 31 | } |
|
| 32 | ||
| 33 | public static function create(array $attributes = []) |
|
| 34 | { |
|
| 35 | $attributes['guard_name'] = $attributes['guard_name'] ?? Guard::getDefaultName(static::class); |
|
| 36 | ||
| 37 | $permission = static::getPermissions(['name' => $attributes['name'], 'guard_name' => $attributes['guard_name']])->first(); |
|
| 38 | ||
| 39 | if ($permission) { |
|
| 40 | throw PermissionAlreadyExists::create($attributes['name'], $attributes['guard_name']); |
|
| 41 | } |
|
| 42 | ||
| 43 | return static::query()->create($attributes); |
|
| 44 | } |
|
| 45 | ||
| 46 | /** |
|
| 47 | * A permission can be applied to roles. |
|
| @@ 32-47 (lines=16) @@ | ||
| 29 | $this->setTable(config('permission.table_names.roles')); |
|
| 30 | } |
|
| 31 | ||
| 32 | public static function create(array $attributes = [], $withException = true) |
|
| 33 | { |
|
| 34 | $attributes['guard_name'] = $attributes['guard_name'] ?? Guard::getDefaultName(static::class); |
|
| 35 | ||
| 36 | $existingRole = static::where([ |
|
| 37 | 'name' => $attributes['name'], |
|
| 38 | 'guard_name' => $attributes['guard_name'], |
|
| 39 | ])->first(); |
|
| 40 | ||
| 41 | if ($existingRole) { |
|
| 42 | if ($withException) throw RoleAlreadyExists::named($attributes['name'], $attributes['guard_name']); |
|
| 43 | return $existingRole; |
|
| 44 | } |
|
| 45 | ||
| 46 | return static::query()->create($attributes); |
|
| 47 | } |
|
| 48 | ||
| 49 | /** |
|
| 50 | * Create many roles. |
|