示例#1
0
 public static function hasPermission($permissions, $all = true)
 {
     $return = true;
     $user = Auth::user();
     $mergedPermissions = Group::getMergedPermissions($user);
     if (isset($mergedPermissions['superuser']) && $mergedPermissions['superuser'] == 1) {
         return true;
     }
     if (!is_array($permissions)) {
         $permissions = (array) $permissions;
     }
     foreach ($permissions as $permission) {
         // We will set a flag now for whether this permission was
         // matched at all.
         $matched = true;
         // Now, let's check if the permission ends in a wildcard "*" symbol.
         // If it does, we'll check through all the merged permissions to see
         // if a permission exists which matches the wildcard.
         if (strlen($permission) > 1 && ends_with($permission, '*')) {
             $matched = static::format_perm1($mergedPermissions, $permission);
             break;
         } elseif (strlen($permission) > 1 && starts_with($permission, '*')) {
             $matched = static::format_perm($mergedPermissions, $permission);
             break;
         } else {
             $matched = static::format_permission($mergedPermissions, $permission);
             break;
         }
         // Now, we will check if we have to match all
         // permissions or any permission and return
         // accordingly.
         if ($all === true && $matched === false) {
             $return = false;
         } elseif ($all === false && $matched === true) {
             $return = true;
         }
     }
     if ($all === false) {
         $return = false;
     }
     return $return;
 }