Extensions (PHP) · Forum permissions
Forum permissions
An extension can add its own permissions per forum, like who may add a pin in which forum. Admins set them in Groups, on the forum's Access tab, in the Extension access card.
Declare them
public function forumPermissions(): array
{
return [
'add_pins' => ['label' => 'Add pins', 'follows' => 'reply'],
];
}
follows names the core permission it copies until an admin sets it: view, read, reply or start. A new permission then behaves sensibly on day one, without admins having to visit every group.
Check them
if (! $this->allows($request->user(), $forum, 'add_pins')) {
abort(403);
}
Guests are passed as null and get what the guest group is allowed.
Member permissions
For something a member can do anywhere on the board, not per forum, list it in permissions(). It shows on your extension's own tab when an admin edits a group, and it is granted to the groups you name when the extension is installed.
public function permissions(): array
{
return [
['name' => 'add map pins', 'label' => 'Add pins to the map', 'description' => 'Put their own pins on the member map.', 'groups' => ['Members']],
];
}
Check it like any other permission:
if (! $request->user()?->can('add map pins')) {
abort(403);
}
Give your permission a name of its own, in lowercase words. Developer, All permissions in the admin panel lists every permission on the board, so you can see which names are taken. Only admins pass a check for a name nobody registered, so test with a member account to catch a typo.
Deciding a permission yourself
To answer an existing permission with your own rule, for example to let members who joined over a month ago use private messages, register a decision in boot(). Return true or false to decide, or null to leave it to the board.
$this->permission('use direct messaging', fn (?User $user) => $user?->created_at?->lt(now()->subMonth()) ? true : null);