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 6.x

    • Release Notes
    • Upgrade Guide
    • New to this...
    • Contribution Guide
    • Security Policy
    • Code of Conduct
    • Origin
  • ACL

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

    • Audit
    • How to install
    • Setup
    • Recording
    • Events
  • 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
    • How to install
    • Setup
    • 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
  • ETags

    • ETags
    • How to install
    • Setup
    • Usage
    • Generators

      • Default Generator
      • Custom Generator
    • Eloquent Models
    • Macros
  • 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
  • Flysystem

    • Introduction
    • Database Adapter

      • Introduction
      • How to install
      • Setup
      • Data Deduplication
      • MIME-Type Detection
  • Http

    • Api

      • Http API
      • How to install
      • Setup
      • Resources

        • Introduction
        • Timestamps
        • Self-Link
        • Relations
        • Registrar
      • Middleware

        • Introduction
        • Request Must Be Json
        • Capture Fields To Select
    • 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
  • Maintenance

    • Modes

      • Maintenance Modes
      • How to install
      • Setup
      • Basic Usage
      • Available Drivers
  • Mime Types

    • MIME-Types
    • How to install
    • Setup
    • Usage
    • Drivers

      • Available Drivers
      • File Info
  • 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
  • Streams

    • Streams
    • How to install
    • Setup
    • How to use

      • Introduction
      • Open and Close
      • Raw Resource
      • Seeking
      • Reading
      • Writing
      • Size
      • Truncate
      • Flush
      • Hash
      • MIME-Type
      • Output
      • Locking
      • Transactions
      • Meta
      • Misc
  • 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
    • Memory
    • Method Helper
    • Invoker
    • Populatable
    • String
    • Version
  • Validation

    • Introduction
    • How to install
    • Setup
    • Rules

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

Roles

Roles are the responsible for grouping granted permissions. A role has be assigned to one or more users and thereby granting the users whatever permissions are granted to the role itself.

  • Creating a new role
    • New role with permissions
  • Grant permissions
  • Revoke permissions
    • Revoking all permissions
  • Synchronise permissions
  • Update role with permissions
    • Update and synchronise permissions
  • Check role's permissions
    • Has permission
    • Has all permissions
    • Has any permissions

Creating a new role

To create a new role, you can use the static create() method, as you normally would in your Eloquent model.

use Aedart\Acl\Models\Role;

$manager = Role::create([
    'slug' => 'flight-manager',
    'name' => 'Flight manager',
    'description' => 'Responsible for managing flight records'
]);

See Laravel's documentation for additional information on how to create new records.

New role with permissions

If you already know which permissions a new role should be granted, then you can define these right away, during the role's creation. This is done so, via the createWithPermissions() method.

use Aedart\Acl\Models\Permissions\Group;
use Aedart\Acl\Models\Role; 

$permissions = Group::findBySlug('flights')->permissions;

$manager = Role::create([
    'slug' => 'flight-manager',
    'name' => 'Flight manager',
    'description' => 'Responsible for managing flight records'
], $permissions);

Grant permissions

The grantPermissions() method allows you to grant one or more permissions to the given role. The method can accept multiple types of permission identifiers:

  • string: permission's unique slug
  • int: permissions unique id (integer)
  • Permission: instance of Permission model
  • Collection: a collection of permission models
  • array: list of permission slugs, ids or models
$role = Role::findBySlug('flight-manager');
$role->grantPermissions($permissions);

// ... Or via array...

$role->grantPermissions([ 'flights.index', 'flights.show', 'flights.store' ]);

// ... Or single permission via slug

$role->grantPermissions('flights.destroy');

Revoke permissions

To revoke permissions from an existing role, use the revokePermissions() method. It accepts the same type of arguments as the grantPermissions() method.

$role->revokePermissions([ 'flights.update', 'flights.destroy' ]);

Revoking all permissions

Use the revokeAllPermissions() method to revoke all permissions for given role.

$role->revokeAllPermissions();

Synchronise permissions

If you require synchronising granted permissions, then use the syncPermissions() method.

// Regardless of what permissions previously were granted,
// the role will now only be granted the given permissions...
$role->syncPermissions([
    'flights.index',
    'flights.show',
    'flights.store',
]);

For additional information about relations synchronisation, please review Laravel's documentation.

Update role with permissions

To update a role's attributes and also grant it permissions, use the updateAndGrantPermissions() method.

$permissions = [ 'flights.index', 'flights.show' ];

$role = Role::findBySlug('flight-manager');
$role->updateAndGrantPermissions([
    'description' => 'Responsible for supervising flight records'
], $permissions);

Update and synchronise permissions

Alternatively, you may also synchronise permissions when updating an existing role.

$permissions = [ 'flights.index', 'flights.show' ];

$role = Role::findBySlug('flight-manager');
$role->updateAndSyncPermissions([
    'description' => 'Responsible for supervising flight records'
], $permissions);

Check role's permissions

When you need to determine whether a role has specific permissions or not, then the following methods can be used.

Has permission

The hasPermission() method returns true, if given permission are granted to the role.

echo $role->hasPermission('flights.update'); // e.g. false (0)

Has all permissions

To determine if a role has multiple permissions granted, use the hasAllPermissions(). The method only returns true, if all given permissions are granted.

echo $role->hasAllPermissions([ 'flights.destroy', 'flights.store' ]); // e.g. true (1)

Has any permissions

Lastly, to determine if a role has either (one of) of given permissions, use the hasAnyPermissions() method.

// Returns true if either permission is granted
echo $role->hasAnyPermissions([ 'flights.index', 'flights.show' ]); // e.g. true (1)
Edit page
Last Updated: 16/02/2023, 09:10
Contributors: Alin Eugen Deac, alin
Prev
Permissions
Next
Users