Check if the user is correctly affiliated
*and* has the requested permission on that
affiliation.

@param $permission

@return bool
@throws \Seat\Web\Exceptions\BouncerException
770142
public function hasAffiliationAndPermission($permission)
    {

        // TODO: An annoying change in the 3x migration introduced
        // and array based permission, which is stupid. Remove that
        // or plan better for it in 3.1

        $array_permission = $permission;

        if (! is_array($permission))
            $array_permission = (array) $permission;

        // Process entries for character
        if (array_filter($array_permission, [$this, 'permissionCharacterLookup']))
            return $this->hasCharacterPermission($array_permission);

        // Process entries for corporations
        if (array_filter($array_permission, [$this, 'permissionCorporationLookup']))
            return $this->hasCorporationPermission($array_permission);

        return false;
    }
1058555
public function validatePermission(array $permission)
    {
        /** @var \Illuminate\Validation\Validator $validator */
        $validator = Validator::make($permission, $this->permissionValidationRules);

        if ($validator->passes()) {
            return true;
        }

        $message = "Invalid permission:\r\n".implode("\r\n", Arr::flatten($validator->errors()->messages()));

        throw new \Exception($message);
    }
846644
protected function checkPermission($permission = 'manage', $role = 'admin')
    {
        if ($this->auth->guest()) {
            return 401;
        }

        if ($this->auth->user()->can($permission) || $this->auth->user()->hasRole($role)) {
            return true;
        }

        return 403;
    }
432029
public function can($permission, $allowAny = false)
    {
        if ( ! ($user = $this->user())) {
            return false;
        }

        if ($user->isAdmin()) {
            return true;
        }

        return $user->can($permission, $allowAny);
    }
1102389
public function hasPermission($permission)
    {
        if (!$this->permissions->where('token', $permission)->isEmpty()) {
            return true;
        }

        foreach ($this->groups as $group) {
            foreach ($group->roles as $role) {
                if (!$role->permissions->where('token', $permission)->isEmpty()) {
                    return true;
                }
            }
        }

        foreach ($this->roles as $role) {
            if (!$role->permissions->where('token', $permission)->isEmpty()) {
                return true;
            }
        }

        return false;
    }
531218
public function hasPermission($recipient, $permission)
    {

        if ($recipient instanceof Group) {
            $check = true;
            foreach ($recipient->getLogins() as $login) {
                if ($this->hasLoginPermission($login, $permission) === false) {
                    $check = false;
                }
            }

            return $check;
        } else {
            return $this->hasLoginPermission($recipient, $permission);
        }
    }
425592
public function permissionDelete(Permission $permission)
    {
        if (!$permission->permission_id) {
            throw new ValidationError("Permission is in an invalid state");
        }
        $cur = $this->db->prepare("DELETE FROM auth_permission WHERE permission_id = :pid");
        $cur->bindParam(":pid", $permission->permission_id, PDO::PARAM_INT);
        $this->db->beginTransaction();
        try {
            $cur->execute();
            $this->db->commit();
        } catch (PDOException $db_err) {
            $this->db->rollBack();
            if ($this->logger) {
                $this->logger->error("Failed to delete permission", ['exception' => $db_err]);
            }
            return false;
        }
        return true;
    }
281138
public function hasPermission($permission)
    {
        if (!is_array($this->permissionsMap)) {
            $permissionsMap = cache()->get('permissions:' . $this->id, null);
            if ($permissionsMap === null) { // Not in cache
                $this->permissionsMap = $this->buildPermissionsMap();
                cache()->forever('permissions:' . $this->id, $this->permissionsMap);
            } else {
                $this->permissionsMap = $permissionsMap;
            }
        }
        return in_array($permission, $this->permissionsMap);
    }
93744
public function authorize($permission)
    {
        $this->requiredPermissions[] = $permission instanceof IPermission
            ? $permission
            : Permission::named($permission);
    }
948066
protected function checkPermission(array $prepared, $permission)
    {
        if (array_key_exists($permission, $prepared) && $prepared[$permission] === true) {
            return true;
        }

        $permission = (string) $permission;

        foreach ($prepared as $key => $value) {
            $key = (string) $key;

            if ((str_is($permission, $key) || str_is($key, $permission)) && $value === true) {
                return true;
            }
        }

        return false;
    }
1327108
public function findWithPermission($permission)
    {
        return array_filter(
            $this->findAll(),
            function ($role) use ($permission) {
                return $role->has($permission);
            }
        );
    }
110666
public function hasPermissionTo($permission): bool
    {
        // Check role flags
        if ($this->hasPermissionFlags()) {
            return $this->hasPermissionThroughFlag();
        }

        // Check role permissions
        if ($this->hasPermissionThroughRole($permission)) {
            return true;
        }

        // Check user permission
        if ($this->hasPermission($permission)) {
            return true;
        }

        return false;
    }
14896
public function hasPermissionThroughRole($permission)
    {
        if (! $this->isMethodRolesExists()) {
            return false;
        }

        if (! $permission = $this->preparePermission($permission)) {
            return false;   
        }

        foreach ($permission->roles as $role) { 
            if ($this->roles->contains($role)) {
                return true;
            }
        }

        return false;
    }
186763
public function hasPermission($permission)
    {
        foreach ($this->permissions()->get() as $p) {
            if ($p->name == $permission) {
                return true;
            }
        }
        return false;
    }
1343183
protected function hasPermissionThroughRole($permission): bool
    {
        foreach ($permission->roles as $role) {
            if ($this->roles->contains($role)) {
                return true;
            }
        }

        return false;
    }
1056629
public function hasAccess(UserInterface $user, string $permission) : bool
    {
        return $this->provider->hasRolePermission($user->getRole(), $permission);
    }
1283016
public function hasAllPermissions($permission)
    {
        foreach ($this->getArrayFrom($permission) as $permission) {
            if (!$this->checkPermission($permission)) {
                return false;
            }
        }

        return true;
    }