Now your all the accessibility is set and now you can use YesAuthority 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 YesAuthority provide you a facade, helpers for instant access.

Facade

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

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.
YesAuthority::availableRoutes(false, null);

getRoutes

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

YesAuthority::getRoutes(false, null);

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

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

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

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

If you wish to get routes according to levels.

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

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

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

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

YesAuthority::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.

YesAuthority::availableZones(false, null);

getZones

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

YesAuthority::getZones(false, null);

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

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

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

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

If you wish to get zones according to levels.

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

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

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

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

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

Its all output is in collection objects as following

/*[
    0 => YesAuthorityResult {#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 => YesAuthorityResult {#198 ▶}
    2 => YesAuthorityResult {#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.
YesAuthority::isPublicAccess('home', 1);

// true

Methods

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

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