1 | <?php |
||
17 | class Permission extends Model implements PermissionContract |
||
18 | { |
||
19 | use HasRoles; |
||
20 | use RefreshesPermissionCache; |
||
21 | |||
22 | protected $guarded = ['id']; |
||
23 | |||
24 | public function __construct(array $attributes = []) |
||
32 | |||
33 | public static function create(array $attributes = []) |
||
45 | |||
46 | /** |
||
47 | * A permission can be applied to roles. |
||
48 | */ |
||
49 | public function roles(): BelongsToMany |
||
58 | |||
59 | /** |
||
60 | * A permission belongs to some users of the model associated with its guard. |
||
61 | */ |
||
62 | public function users(): MorphToMany |
||
63 | { |
||
64 | return $this->morphedByMany( |
||
65 | getModelForGuard($this->attributes['guard_name']), |
||
66 | 'model', |
||
67 | config('permission.table_names.model_has_permissions'), |
||
68 | 'permission_id', |
||
69 | config('permission.column_names.model_morph_key') |
||
70 | ); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Find a permission by its name (and optionally guardName). |
||
75 | * |
||
76 | * @param string $name |
||
77 | * @param string|null $guardName |
||
78 | * |
||
79 | * @throws \Spatie\Permission\Exceptions\PermissionDoesNotExist |
||
80 | * |
||
81 | * @return \Spatie\Permission\Contracts\Permission |
||
82 | */ |
||
83 | public static function findByName(string $name, $guardName = null): PermissionContract |
||
84 | { |
||
85 | $guardName = $guardName ?? Guard::getDefaultName(static::class); |
||
86 | $permission = static::getPermission(['name' => $name, 'guard_name' => $guardName]); |
||
87 | if (! $permission) { |
||
88 | throw PermissionDoesNotExist::create($name, $guardName); |
||
89 | } |
||
90 | |||
91 | return $permission; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Find a permission by its id (and optionally guardName). |
||
96 | * |
||
97 | * @param int $id |
||
98 | * @param string|null $guardName |
||
99 | * |
||
100 | * @throws \Spatie\Permission\Exceptions\PermissionDoesNotExist |
||
101 | * |
||
102 | * @return \Spatie\Permission\Contracts\Permission |
||
103 | */ |
||
104 | public static function findById(int $id, $guardName = null): PermissionContract |
||
105 | { |
||
106 | $guardName = $guardName ?? Guard::getDefaultName(static::class); |
||
107 | $permission = static::getPermission(['id' => $id, 'guard_name' => $guardName]); |
||
108 | |||
109 | if (! $permission) { |
||
110 | throw PermissionDoesNotExist::withId($id, $guardName); |
||
111 | } |
||
112 | |||
113 | return $permission; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Find or create permission by its name (and optionally guardName). |
||
118 | * |
||
119 | * @param string $name |
||
120 | * @param string|null $guardName |
||
121 | * |
||
122 | * @return \Spatie\Permission\Contracts\Permission |
||
123 | */ |
||
124 | public static function findOrCreate(string $name, $guardName = null): PermissionContract |
||
135 | |||
136 | /** |
||
137 | * Get a cached permission. |
||
138 | */ |
||
139 | protected static function getPermission(array $params): ?PermissionContract |
||
145 | |||
146 | /** |
||
147 | * Get the current cached permissions. |
||
148 | */ |
||
149 | protected static function getPermissions(array $params = []): Collection |
||
155 | } |
||
156 |