@param SubjectInterface $subject

@param bool $permissions Load permission sets along with roles
@return Role|\RBAC\Role\Role[]
330548
public function roleFetchSubjectRoles(SubjectInterface $subject, $permissions = true)
    {
        $query = "
            SELECT
                role_id
            FROM
                auth_subject_role
            WHERE
                subject_id = :subject_id
        ";
        $roles = [];
        $cur = $this->db->prepare($query);
        $cur->bindValue(":subject_id", $subject->id(), PDO::PARAM_INT);
        try {
            $cur->execute();
            $res = $cur->fetchAll(PDO::FETCH_OBJ);
            if (!$res) {
                return [];
            } else {
                $role_ids = array_map(
                    function ($row) {
                        return $row->role_id;
                    },
                    $res
                );
                $roles = $this->roleFetchById($role_ids);
            }
        } catch (PDOException $db_err) {
            if ($this->logger) {
                $this->logger->error("Failed to fetch roles for subject", ['exception' => $db_err]);
            }
        }
        return $roles;
    }
781943
@GET
    public Iterator<EmoRole> getAllRoles(final @Authenticated Subject subject) {
        return _uac.getAllRoles(subject);
    }
30272
private function resolvePermissions(Row $row, UserInterface $user = null)
    {
        $permissions = [];
        if (null !== $user) {
            foreach ($user->getRoleObjects() as $role) {
                foreach (array_filter(explode(' ', $row->getValue(sprintf('role%s', $role->getId())))) as $permission) {
                    $permissions[$role->getId()][$permission] = true;
                }
            }
        }

        return $permissions;
    }
4259
public function getAssignmentForSubject(Subject $subject)
    {
        return $this->assignmentStorage->getAssignment($subject->getSubjectId(), $subject->getSubjectName());
    }
371227
protected final function createRbacRole(array $permissions, array $roles = [])
    {
        if (!$this->_rbacRoleFactory) {
            throw new RequiredDependencyException(sprintf(
                'Rbac role factory is not injected in class "%s".',
                get_class($this)
            ));
        }

        return $this->_rbacRoleFactory->createRole($permissions, $roles);
    }
905379
public function subject($subject = null)
    {
        if ($subject !== null) {
            $this->_subject = $subject;

            return $this;
        }

        return $this->_subject;
    }
1217462
public function rolePermissions()
    {

        $permissions = new Collection();
        foreach ($this->getRoles() as $role)
            $permissions = $permissions->merge($role->permissions);
        return $permissions;
    }
831335
@Transactional(readOnly=true)
    public List<Permission> loadRolesAndPermissions() throws Exception {
        return _persistence.getResults(_qRoleAuthorizations, null);
    }
1009072
private function doLoadSharingPermissions(array $idSubjects, array $roles): void
    {
        /** @var RoleInterface[] $mapRoles */
        $mapRoles = [];
        $cRoles = $this->provider->getPermissionRoles($roles);

        foreach ($cRoles as $role) {
            $mapRoles[$role->getName()] = $role;
        }

        /** @var SubjectIdentityInterface $subject */
        foreach ($idSubjects as $id => $subject) {
            foreach ($this->cacheRoleSharing[$id] as $roleId) {
                if (isset($mapRoles[$roleId])) {
                    $cRole = $mapRoles[$roleId];

                    foreach ($cRole->getPermissions() as $perm) {
                        $class = $subject->getType();
                        $field = PermissionUtils::getMapAction($perm->getField());
                        $this->cacheSharing[$id][$class][$field][$perm->getOperation()] = true;
                    }
                }
            }
        }
    }
106773
protected function getPermissions()
    {
        try {
            return $this->cache->rememberForever('permissions.policies', function () {
                return Permission::with('roles')->get();
            });
        } catch (\Exception $exception) {
            $this->cache->forget('permissions.policies');

            return new Collection;
        }
    }
1335302
protected function getPermissionsForSubject(Subject $subject)
    {
        $permissions = [];

        foreach ($this->getRolesForSubject($subject) as $role) {
            if ($role->hasPermissions()) {
                $permissions = array_merge($this->getPermissionsByNames($role->getPermissions()), $permissions);
            }
        }

        return $permissions;
    }
1204582
public function hasPermission($permissionName, Subject $subject, Resource $resource = null, array $params = [])
    {
        return in_array($permissionName, $this->getPermissions($subject, $resource, $params));
    }
215045
public function syncPermissions($permissions)
    {
        $mappedPermissions = [];

        foreach ($permissions as $permission) {
            $mappedPermissions[] = Helper::getIdFor($permission, 'permission');
        }

        $changes = $this->permissions()->sync($mappedPermissions);
        $this->flushCache();
        $this->fireLaratrustEvent("permission.synced", [$this, $changes]);

        return $this;
    }
957569
private function getRealPermissions(array $permissions, $subject = null, $field = null)
    {
        if (null !== $subject && $this->hasConfig($subject->getType())) {
            $config = $this->getConfig($subject->getType());

            if (null !== $field && $config->hasField($field)) {
                $config = $config->getField($field);
            }

            foreach ($permissions as $key => &$permission) {
                $permission = $config->getMappingPermission($permission);
            }
        }

        return $permissions;
    }
174829
public function getIdentityRoles(UserInterface $user = null)
    {
        if ($user === null) {
            $user = $this->getDefaultIdentity();
            if (!$user) {
                return (array) $this->getModuleOptions()->getDefaultGuestRole();
            }
        }

        $resultSet = $this->getUserRoleLinkerMapper()->findByUser($user);

        if (count($resultSet) > 0) { // if exists in database
            $roles = array();
            foreach ($resultSet  as $userRoleLinker) {
                $roles[] = $userRoleLinker->getRoleId();
            }

            return $roles;
        } else {
            return (array) $this->getModuleOptions()->getDefaultUserRole();
        }

    }
564566
public function loadSubjectObjectives($subject)
    {
        $target = $this->getSubjectType($subject);
        $repoMethod = "findBy{$target}";

        return $this->objectiveRepo->{$repoMethod}($subject);
    }
718613
protected function mergeAuthorizationState($subject)
    {
        $currentRoles = $subject->getRoles();
        $session      = $subject->getSession(false);

        if ($session === null) {
            if (!empty($currentRoles)) {
                $session = $subject->getSession();
                $session->setAttribute(DefaultSubjectContext::AUTHORIZATION_SESSION_KEY, $currentRoles);
            }
        } else {    //otherwise no session and no roles - nothing to save
            $existingRoles =
                    $session->getAttribute(DefaultSubjectContext::AUTHORIZATION_SESSION_KEY);

            if (empty($currentRoles)) {
                if (!empty($existingRoles)) {
                    $session->removeAttribute(DefaultSubjectContext::AUTHORIZATION_SESSION_KEY);
                }
            } else {    //otherwise both are null or empty - no need to update the session
                if ($currentRoles !== $existingRoles) {
                    $session->setAttribute(DefaultSubjectContext::AUTHORIZATION_SESSION_KEY, $currentRoles);
                }
            }
        }

    }