Users & Permissions

Pagekit comes with a prepared signup procedure and a powerful user manager. With all of these users in your system, your extension can easily make use of roles and permissions.

Concepts

A user is a representation of a person registered with your site and identified by their username. The status of a user account can be active, blocked or new. Users can log in to your site or the admin area. Not all users' accounts are allowed to access the admin area.

Permissions define the actions a user can perform . A permission is identified by a name, for example user: access admin area. Permission names should be descriptive and start with the name of the according module, e.g. user: for the user module.

Roles group together several user accounts. All users with the same role share the same permissions. Roles are also used to manage access to elements of your site's content. A user can belong to zero, one or multiple roles. A role can have any number of users assigned to it. Pagekit comes with the default roles Anonymous, Authenticated and Administrator and allows you to create as many more as you need.

Show content to specific role only

Roles are very flexible in how they can be used. You can create specific content that is only accessible by selected users.

  • Create a new role called Premium in Users > Roles. Don't assign any permissions to this role.
  • In Users > List, click a user account to edit their profile and enable the new Premium role for this user.
  • On every page in the Site area, you can see a Restrict access section in the sidebar. Make sure to select the Premium role and nothing else.This item will now be visible only to logged in users of the Premium role.

Note Your administrator account won't be able to see this content either, unless you add the user to the Premium role as well or enable Administrator in the Restrict access settings.

Register permissions from a module definition

To add a permission to the system area, which can then be assigned to a role, use the permissions keyword in the index.php file of an extension.

Use speaking permission names. The convention is to start with the name of the extension and then have a short phrase describing the permission, all lowercase. The title is the string displayed in the browser. The _() call makes sure this string is translatable.

  1. 'permissions' => [
  2. 'hello: manage settings' => [
  3. 'title' => _('Manage settings')
  4. ],
  5. ],

Check, if the user has a role of a specific ID.

  1. $role_id = 4;
  2. App::user()->hasRole($role_id);

Check, if the user has a role of a specified name.

  1. $role_name = "Editor";
  2. $role = Role::where('name = ?', [$role_name])->first();
  3. App::user()->hasRole($role->id);