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!

Users

Your Eloquent User model should make use of the HasRoles trait. It enables assigning and un-assigning of roles. (See setup).

  • Assign Roles
  • Un-assign Roles
    • Un-assign all roles
  • Synchronise roles
  • Check user's roles
    • Has role
    • Has all roles
    • Has any roles
  • Check user's permissions
    • Cached permissions
    • Manual database check

Assign Roles

Use the assignRoles() method to assign one or more roles to a user. The method behaves similarly as the grantPermissions() method, in that it too accepts a variety of argument types:

  • slugs
  • ids
  • Role model instance
  • Collection of role model instances
  • array of slugs, ids or role model instances.
$role = Role::findBySlug('flight-manager');

// Assign single role
$user->assignRoles($role);

// ... Or via array...

$user->assignRoles([ 'editor', 'reviewer', 'flight-manager' ]);

// ... Or single role via slug

$user->assignRoles('flight-manager');

Un-assign Roles

When you need to un-assign roles, then use the unassignRoles(). It accepts the same type of arguments as the assignRoles() method.

$user->unassignRoles([ 'editor', 'flight-manager' ]);

Un-assign all roles

The unassignAllRoles() method can be used when you need to un-assign all roles for a user.

$user->unassignAllRoles();

Synchronise roles

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

To synchronise assigned roles, use the syncRoles() method.

// Regardless of what roles previously were assigned,
// the user will now only be assign to the given roles...
$user->syncRoles([
    'editor',
    'reviewer',
    'flight-manager',
]);

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

Check user's roles

Determining what roles are assigned to a given user, can be achieved via the following methods:

Has role

The hasRoles() method returns true, if given role is assigned to the user.

echo $user->hasRoles('editor'); // e.g. false (0)

Has all roles

To determine if a user has multiple roles assigned, use the hasAllRoles(). The method only returns true, if all given roles are assigned.

echo $user->hasAllRoles([ 'editor', 'reviewer' ]); // e.g. false (0)

Has any roles

To determine if a user is assigned either (one of) of given roles, use the hasAnyRoles() method.

// Returns true if either role is assigned
echo $user->hasAnyRoles([ 'editor', 'reviewer' ]); // e.g. true (1)

Check user's permissions

During runtime, if you have defined permissions in the AuthServiceProvider (See setup), you can use Laravel's builtin mechanisms to check a user's permissions.

<?php

namespace App\Http\Controllers;

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

class FlightController extends Controller
{

    public function update(Request $request, Flight $flight)
    {
        if (!$request->user()->can('flights.update', $flight)) {
            abort(403);
        }

        // The current user can update the flight post...
    }
}

Cached permissions

It is important to understand that when using the ACL Registrar in your AuthServiceProvider, all permissions will be cached. Unless you are aware of this, you can experience unexpected behavior, should you change a user's roles, permissions...etc.

See Cached Permissions section for additional information.

Manual database check

Should you require checking if a user is granted a specific permission, without using the cache, then you may use the hasPermission() method. It ONLY accepts a Permission model instance as argument and will perform a database query, to determine whether the user is granted the given permission or not.

$permission = Permission::findBySlug('flights.destroy');

echo $user->hasPermission($permission); // E.g. true (1)
Edit page
Last Updated: 16/02/2023, 09:10
Contributors: Alin Eugen Deac, alin
Prev
Roles
Next
Cached Permissions