1. Go to this page and download the library: Download machuga/authority-l4 library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
//app/models/User.php
public function roles() {
return $this->belongsToMany('Role');
}
public function permissions() {
return $this->hasMany('Permission');
}
public function hasRole($key) {
foreach($this->roles as $role){
if($role->name === $key)
{
return true;
}
}
return false;
}
//app/models/Role.php
class Role extends Eloquent {}
//app/models/Permission.php
class Permission extends Eloquent {}
//app/config/packages/machuga/authority-l4
return array(
'initialize' => function($authority) {
$user = $authority->getCurrentUser();
//action aliases
$authority->addAlias('manage', array('create', 'read', 'update', 'delete'));
$authority->addAlias('moderate', array('read', 'update', 'delete'));
//an example using the `hasRole` function, see below examples for more details
if($user->hasRole('admin')) {
$authority->allow('manage', 'all');
}
// loop through each of the users permissions, and create rules
foreach($user->permissions as $perm) {
if($perm->type == 'allow') {
$authority->allow($perm->action, $perm->resource);
} else {
$authority->deny($perm->action, $perm->resource);
}
}
}
);
//If you added the alias to `app/config/app.php` then you can access Authority, from any Controller, View, or anywhere else in your Laravel app like so:
if( Authority::can('create', 'User') ) {
User::create(array(
'username' => '[email protected]'
));
}
//If you just chose to use the service provider, you can use the IoC container to resolve your instance
$authority = App::make('authority');