AthenaeumAthenaeum
Packages
  • next
  • current
  • v9.x
  • v8.x
  • v7.x
  • v6.x
  • v5.x
  • v4.x
  • v3.x
  • v2.x
  • v1.x
Changelog
GitHub
Packages
  • next
  • current
  • v9.x
  • v8.x
  • v7.x
  • v6.x
  • v5.x
  • v4.x
  • v3.x
  • v2.x
  • v1.x
Changelog
GitHub
  • Version 5.x

    • Release Notes
    • Upgrade Guide
    • New to this...
    • Origin
  • ACL

    • Introduction
    • How to install
    • Setup
    • Permissions
    • Roles
    • Users
    • Cached Permissions
  • Circuits

    • Circuits
    • How to install
    • Setup
    • Usage
    • Events
  • Collections

    • Collections
    • How to install
    • Summation

      • Summation Collection
      • Items Processor
  • Config

    • Configuration Loader
    • How to install
    • Setup
    • Load Configuration Files
    • Custom File Parsers
  • Console

    • Command and Schedule Registration
    • How to install
    • Setup
    • Commands
    • Schedules
  • Container

    • IoC Service Container
    • How to install
    • Container
    • List Resolver
  • Core

    • Athenaeum Core Application
    • Prerequisite
    • How to install
    • Integration
    • Usage

      • Configuration
      • Service Providers
      • Service Container
      • Events
      • Caching
      • Logging
      • Console
      • Task Scheduling
      • Exception Handling
      • Extending Core Application
      • Testing
  • Database

    • Introduction
    • How to install
    • Models

      • Instantiatable
      • Sluggable
    • Query

      • Criteria (Query Filter)
  • Dto

    • Data Transfer Object (DTO)
    • How to install
    • Create Interface
    • Implement DTO
    • How to use
    • Populate
    • Export
    • Json
    • Serialization
    • Nested DTOs
    • Array DTO
  • Events

    • Register Listeners and Subscribers
    • How to install
    • Setup
    • Listeners
    • Subscribers
  • Filters

    • Search Filter Utilities
    • Prerequisites
    • How to install
    • Setup
    • Processor
    • Filters Builder
    • Predefined Resources

      • Search Processor
      • Sorting Processor
      • Constraints Processor
      • Matching Processor
    • Tip: Create a base builder
  • Http

    • Clients

      • Http Clients
      • How to install
      • Setup
      • Basic Usage
      • Available Methods

        • Fluent Api
        • Protocol Version
        • Base Uri
        • Http Method and Uri
        • Headers
        • Accept & Content-Type
        • Authentication
        • Http Query
        • Payload Format
        • Payload
        • Attachments
        • Cookies
        • Response Expectations
        • Middleware
        • Conditions
        • Criteria
        • Redirects
        • Timeout
        • Debugging
        • Logging
        • Driver Options
        • Driver
      • Http Query Builder

        • Introduction
        • Select
        • Where
        • Dates
        • Include
        • Pagination
        • Sorting
        • Raw Expressions
        • Custom Grammar
    • Cookies

      • Http Cookies
      • How to install
      • Usage
    • Messages

      • Http Messages
      • How to install
      • Serializers
  • Properties

    • Properties Overload
    • How to install
    • Usage
    • Naming Convention
    • Properties Visibility
  • Redmine

    • Redmine Api Client
    • How to install
    • Setup
    • General Usage

      • Supported Operations
      • Fetch list of resources
      • Find
      • Fetch
      • Create new record
      • Update existing record
      • Delete existing record
      • Relations
    • Available Resources

      • Predefined Resources
      • Attachments
      • Enumerations
      • Issue Relations
      • Users
      • User Groups
      • Roles
      • Project Memberships
      • Versions (Milestones)
      • Issue Categories
      • Trackers
  • Service

    • Service Registrar
    • How to install
    • How to use
  • Support

    • Introduction
    • How to install
    • Laravel Aware-of Helpers

      • How to use
      • Enforce Via Interface
      • Custom Default
      • Pros and Cons
      • Available Helpers
    • Aware-of Properties

      • Generator
      • Available Aware-of Helpers
    • Live Templates
  • Testing

    • Introduction
    • How to install
    • Test Cases
    • Testing Aware-of Helpers
  • Utils

    • Introduction
    • How to install
    • Array
    • Duration
    • Json
    • Math
    • Method Helper
    • Populatable
    • Version
  • Validation

    • Introduction
    • How to install
    • Setup
    • Rules

      • Alpha-Dash-Dot
You are viewing documentation for an outdated version. It is no longer supported!

Permissions

Before you are able to grant permissions to roles, they must first be created. But, as you might have noticed, each permission must belong to a permission group. This makes creating permissions slightly cumbersome. Therefore, to ease permissions creation, you can make use of the createWithPermissions() method, in the permissions group model.

  • Create new migration
  • Create Permissions and Group
  • Permission slug prefixes
    • Disable prefixes
  • Find or create behaviour

Create new migration

Your application SHOULD be coded against it's available permissions. It would therefore be beneficial to install them via database migrations.

php artisan make:migration installs_flight_permissions

Create Permissions and Group

Inside your migration class, use the createWithPermissions() method to create a new permissions group, with it's desired permissions. The method accepts a unique slug identifier, along with an array of permissions. The array has to be formatted accordingly:

  • key: unique permission slug (prefixed with group's slug)
  • value: array containing permission's name and description (optional)
<?php

use Aedart\Acl\Models\Permissions\Group;
use Illuminate\Database\Migrations\Migration;

class InstallsFlightPermissions extends Migration
{
    public function up()
    {
        $name = 'Flight permissions'; 
        $description = 'Permissions related to flight records';

        Group::createWithPermissions('flights', [
            'index' => [
                'name' => 'List flights',
                'description' => 'Ability to view list of flights'
            ],
            'show' => [
                'name' => 'Show flight',
                'description' => 'Ability to view a single flight'
            ],
            
            // ... remaining not shown ...
        ], $name, $description);
    }

    public function down()
    {
        Group::findBySlugOrFail('flights')->forceDelete();
    }
}

Permission slug prefixes

In the above example, a new permission group is created, using flights as it's slug identifier. Each permission's slug is prefixed with the group's slug, separated by a dot (.). Thus, from the above example, the following permission slugs are inserted into the database:

  • flights.index
  • flights.show

Later in you application, you will be able to check against these permissions:

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class FlightController extends Controller
{

    public function index(Request $request)
    {
        if ($request->user()->cannot('flights.index')) {
            abort(403);
        }

        // ...remaining not shown
    }
}

Disable prefixes

If you do not wish your permission's slugs to be prefixed, then you can disable this behaviour by setting the $prefix argument to false, when using the createWithPermissions() method.

$name = 'Flight permissions'; 
$description = 'Permissions related to flight records';
$prefix = false;

Group::createWithPermissions('flights', [
    'show-flights-list' => [
        'name' => 'List flights',
        'description' => 'Ability to view list of flights'
    ],
    
    // ... remaining not shown ...
], $name, $description, $prefix);

From the above example, the following permission slug is inserted into the database:

  • show-flights-list

How should you name your permissions?

If you find yourself wondering how you should name your permission slugs, perhaps you can use the same names as for your routes.

See Laravel's resource routes documentation for inspiration.

Find or create behaviour

The createWithPermissions() method attempts to find a permissions group with the requested slug. Only if the group does not exist, then it will be created. This allows you to add more permissions to the same group, at a later point.

Caution

The "find or create" behaviour does NOT apply to the permissions. Each given permission is attempted created. This means that if you provide a permission slug that already exists, then createWithPermissions() will fails with a "unique key constraint violation" database exception.

Should you wish to change a permission, then you will have to do so manually, e.g. by using the Permission Eloquent model.

Edit page
Last Updated: 05/04/2022, 21:01
Contributors: Alin Eugen Deac
Prev
Setup
Next
Roles