Now your all the accessibility is set and now you can use AuthUser in your project for show, hide buttons or any element in html or get a role or user level permissions or create dynamic access permissions. To do all these things AuthUser provide you a facade, helpers for instant access.

Facade

Before started If you added the alias to config/app.php then you can access AuthUser from any Controller, anywhere else in your laravel application. Following some facade method are available in AuthUser.

before use facade method please go through following permission levels.

/*[
    'CONFIG_ROLE'   => 1, // Config Role
    'CONFIG_USER'   => 2, // Config User
    'DB_ROLE'       => 3, // DB Role
    'DB_USER'       => 4, // DB User
    'CONDITIONS'    => 5, // Conditions
]*/

availableRoutes

availableRoutes method used to return array of allowed and public routes and its require two argument.

  • isUriRequired - default this argument set to false, if you required uri along with route names then pass a first argument as true.
  • requestForUserId - default is logged in user id, if you pass other than logged in user id then it will return array of that user allow / public route.
AuthUser::availableRoutes(false, null);

getRoutes

This method returns array of all routes. You may also pass arguments as per availableRoutes.

AuthUser::getRoutes(false, null);

If you wish to return only allowed, denied or public route then add chaining method to getRoutes.

// For Allowed
AuthUser::takeAllowed()->getRoutes();

// For Denied
AuthUser::takeDenied()->getRoutes();

// For Public
AuthUser::takePublic()->getRoutes(); 

If you wish to get routes according to levels.

// Get some levels, you can use array also for multiple levels
AuthUser::checkOnly('CONFIG_ROLE')->getRoutes();

// Get Except, you can use array also for multiple levels
AuthUser::checkExcept('CONFIG_ROLE')->getRoutes();

// Get up to levels, you can use array also for multiple levels
AuthUser::checkUpto('CONFIG_ROLE')->getRoutes();

If you wish to get routes according to role_id then use viaRole

AuthUser::checkOnly(['CONFIG_ROLE', 'DB_ROLE'])->viaRole()->getRoutes(false, roleId);

availableZones

availableZones method used to return array of allowed and public zones. You can also pass userId as argument for get zones of particular user other than logged in user.

AuthUser::availableZones(false, null);

getZones

This method returns array of all zones. You may also pass arguments as per availableRoutes.

Authority::getZones(false, null);

If you wish to return only allowed, denied or public route then add chaining method to getZones.

// For Allowed
AuthUser::takeAllowed()->getZones();

// For Denied
AuthUser::takeDenied()->getZones();

// For Public
AuthUser::takePublic()->getZones(); 

If you wish to get zones according to levels.

// Get some levels, you can use array also for multiple levels
AuthUser::checkOnly('CONFIG_ROLE')->getZones();

// Get Except, you can use array also for multiple levels
AuthUser::checkExcept('CONFIG_ROLE')->getZones();

// Get up to levels, you can use array also for multiple levels
AuthUser::checkUpto('CONFIG_ROLE')->getZones();

If you wish to get zones according to role_id then use viaRole

AuthUser::checkOnly([your_level_here])->viaRole()->getZones(false, roleId);

Its all output is in collection objects as following

/*[
    0 => AuthUserResult {#208 ▼
    #originalResult: array:12 [▼
      "response_code" => 200
      "message" => "OK"
      "is_access" => true
      "result_by" => "CONFIG_USER"
      "upper_level" => "CONFIG_ROLE"
      "condition_result_by" => null
      "conditions_checked" => null
      "levels_checked" => array:2 [▶]
      "access_id_key" => "demo.1"
      "title" => null
      "is_public" => false
      "is_zone" => false
    ]
    #options: array:1 [▶]
    #checkLevels: array:5 [▶]
    +"uri": "demo-1"
    }
    1 => AuthUserResult {#198 ▶}
    2 => AuthUserResult {#209 ▶}
    .
    .
]*/

isPublicAccess

isPublicAccess is used for to check if given route id have public access or not. If it is have public access then return true otherwise return false. Its require two argument which is explain below.

  • routeName - it is not required, if route name is not given then it will take current route id.
  • requestForUserId - it is not required, if this is given then its check route id for given user id.
AuthUser::isPublicAccess('home', 1);

// true

Methods

There are 2 basic functions that you need to be aware of to utilize AuthUser.

canAccess

Check if user can access a resource

canAccess('home');

// true

You can also use wild card string to check access a resource.

canAccess('manage.*');

// true

canPublicAccess

Check if if given route have public access.

canPublicAccess('public.app');

// true