Ejemplo n.º 1
0
 public function run()
 {
     DB::table('cms_role_permissions')->delete();
     $create_permission = Permission::where('alias', 'create')->first();
     $read_permission = Permission::where('alias', 'read')->first();
     $update_permission = Permission::where('alias', 'update')->first();
     $delete_permission = Permission::where('alias', 'delete')->first();
     $publish_permission = Permission::where('alias', 'publish')->first();
     // define permissions for each of the roles
     $admin_role = Role::where('alias', 'admin')->first();
     $manager_role = Role::where('alias', 'manager')->first();
     $editor_role = Role::where('alias', 'editor')->first();
     $admin_role->permissions()->attach([$create_permission->id, $read_permission->id, $update_permission->id, $delete_permission->id, $publish_permission->id]);
     $manager_role->permissions()->attach([$read_permission->id]);
     $editor_role->permissions()->attach([$create_permission->id, $read_permission->id, $update_permission->id, $delete_permission->id]);
 }
Ejemplo n.º 2
0
 /**
  * Validate access to a resource through premissions
  *
  * @param  string $permission_alias
  * @param  string $resource
  * @return boolean
  */
 public function validate($permission_alias, $resource)
 {
     // verify permission alias
     $permission = Permission::where('alias', $permission_alias)->first();
     if (!$permission) {
         throw new Exceptions\PermissionNotFoundException($permission_alias);
     }
     $admin_id = $this->authorable->identifier();
     $resource_type = get_class($resource);
     $privilege = Privilege::where('admin_id', $admin_id)->where('resource_type', $resource_type)->first();
     if (!$privilege) {
         // user has no privilege to access this resource
         return false;
     }
     // privilege exists, we check to see
     // whether the role has the requested permission
     /**
      * @todo Improve this by finding the Role with
      *       its id and that the permission id
      *       exists for that role using whereHas.
      *       Finding that role confirms the validation.
      */
     $role = Role::findOrFail($privilege->role_id);
     $permissions = $role->permissions()->get();
     if (!count($permissions) > 0) {
         // this role has no permissions set
         return false;
     }
     $permissions = $permissions->toArray();
     $permission_found = array_filter($permissions, function ($permission) use($permission_alias) {
         return $permission['alias'] == $permission_alias;
     });
     // there should be only one match.
     // otherwise something must have gone wrong somewhere
     if (count($permission_found) === 1) {
         return true;
     }
     return false;
 }