Example #1
0
 /**
  * Has Permission.
  *
  * @param string|object|null $permission_name
  * @param object|null        $resource
  *
  * @return bool
  */
 public function hasPermission($permission_name = null, $resource = null)
 {
     if (is_object($permission_name) && is_null($resource)) {
         $resource = $permission_name;
         $permission_name = debug_backtrace(false, 3)[2]['function'];
     }
     $check_roles = [];
     if (is_string($permission_name) && is_null($resource)) {
         $roles_tmp = $this->fortress->getGlobalRoles();
     } else {
         $policy = $this->gate->getPolicyFor($resource);
         if (!method_exists($policy, 'fortress_roles')) {
             return false;
         }
         $roles_tmp = $policy->fortress_roles();
     }
     foreach ($roles_tmp as $role_name => $permissions) {
         if (in_array($permission_name, $permissions)) {
             $check_roles[] = $role_name;
         }
     }
     foreach ($check_roles as $role_name) {
         if ($this->hasRole($role_name, $resource)) {
             return true;
         }
     }
     return false;
 }
Example #2
0
 /**
  * Which Models have the Permission for a resource?
  *
  * @param string       $permission_name
  * @param object       $resource
  * @param Closure|null $resolver
  *
  * @return Collection
  */
 public function allowedModels($permission_name, $resource, Closure $resolver = null)
 {
     $policy = $this->gate->getPolicyFor($resource);
     $policy_roles = $policy->fortress_roles();
     $check_roles = [];
     foreach ($policy_roles as $role_name => $permissions) {
         if (in_array($permission_name, $permissions)) {
             $check_roles[] = $role_name;
         }
     }
     $roles = Role::whereIn('role_name', $check_roles)->where('resource_type', get_class($resource))->where('resource_id', $resource->getKey())->get();
     if ($resolver) {
         return $resolver($roles);
     }
     $collection = collect();
     foreach ($roles as $role) {
         $tmp_instance = app($role->getModelType());
         $tmp_instance->find($role->getModelId());
         $collection->push($tmp_instance);
     }
     return $collection;
 }