Checks the roles and returns only the valid ones
@param array $roles
@return array $checkedRoles
963210
protected function checkRoles(array $roles)
    {
        $checkedRoles = array();
        foreach ($roles as $role) {
            if (true === is_string($role) && false === in_array($role, $checkedRoles)) {
                $checkedRoles[] = (string)$role;
            }
        }
        return $checkedRoles;
    }
1260722
protected function flattenRoles(array $roles)
    {
        $roleNames = [];
        $iterator  = $this->traversalStrategy->getRolesIterator($roles);

        foreach ($iterator as $role) {
            $roleNames[] = $role->getName();
        }

        return array_unique($roleNames);
    }
240921
protected function hasRole( array $roles )
	{
		if( $this->has( 'security.authorization_checker' ) && $this->get( 'security.token_storage' )->getToken() )
		{
			$checker = $this->get( 'security.authorization_checker' );

			foreach( $roles as $role )
			{
				if( $checker->isGranted( $role ) ) {
					return true;
				}
			}
		}

		return false;
	}
1002302
public function getRoleKeys($roles)
    {
        $roles = Helpers::groupModelsAndIdentifiersByType($roles);

        $roles['strings'] = $this->getKeysByName($roles['strings']);

        $roles['models'] = Arr::pluck($roles['models'], $this->getKeyName());

        return Arr::collapse($roles);
    }
699582
public function findUsersByRolesIncludingGroups(
        array $roles
    ) {
        //very slow otherwise. If we want to do it properly, the OR clause won't do it.
        //we must use UNION wich is not supported by Doctrine
        $dql = '
            SELECT u, r1, ws
            From Claroline\\CoreBundle\\Entity\\User u
            LEFT JOIN u.roles r1
            LEFT JOIN u.personalWorkspace ws
            WHERE r1 in (:roles)
            AND u.isRemoved = false
            ORDER BY u.lastName, u.firstName ASC
        ';

        $query = $this->_em->createQuery($dql);
        $query->setParameter('roles', $roles);

        $resA = $query->getResult();
        $resA = $resA ? $resA : [];

        $dql = '
            SELECT u, g, r2, ws
            From Claroline\\CoreBundle\\Entity\\User u
            LEFT JOIN u.personalWorkspace ws
            LEFT JOIN u.groups g
            LEFT JOIN g.roles r2
            WHERE r2 in (:roles)
            AND u.isRemoved = false
            ORDER BY u.lastName, u.firstName ASC
        ';

        $query = $this->_em->createQuery($dql);
        $query->setParameter('roles', $roles);

        $resB = $query->getResult();
        $resB = $resB ? $resB : [];

        return array_unique(array_merge($resA, $resB));
    }
459017
public function hasRole(...$roles)
    {
        $roles = array_flatten($roles);

        foreach ($roles as $role) {
            $name = $role instanceof Role ? $role->name : $role;

            if ($this->roles->contains('name', $name)) {
                return true;
            }
        }

        return false;
    }
1227636
public function getReachableRoles($roles)
  {
    if (!is_array($roles)) $roles = [$roles];
    
    $reachableRoles = [];
    foreach ($roles as $role) 
    {
      $reachableRoles[$role] = $role;
      if (!isset($this->map[$role])) continue;
      
      foreach ($this->map[$role] as $role) 
      {
        $reachableRoles[$role] = $role;
      }
    }
    return $reachableRoles;
  }
1120871
public function getRelativeRoleName($role)
    {
        $roles = $this->collectRelativeRoles();
        if (isset($roles[$role])) {
            return $roles[$role];
        }
    }
1105980
public function getUsersWithRole(Role $role): array
    {
        $query = $this->getRepository()->createQueryBuilder('u')
            ->select('u')
            ->where(':role MEMBER OF u.roles')
            ->setParameter('role', $role)
            ->getQuery();

        return $query->getResult() ?? [];
    }
501249
private function normalizeRoleList (array $roles)
    {
        $normalized = [];

        foreach ($roles as $role)
        {
            $roleKey = (is_object($role) && ($role instanceof BaseRole))
                ? $role->getRole()
                : (string) $role;

            if (isset($this->roleCollection[$roleKey]))
            {
                $normalized[] = $this->roleCollection[$roleKey];
            }
        }

        return $normalized;
    }
237406
public function hasRoles($roles)
    {
        $roles = is_array($roles) ? $roles : func_get_args();

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

        return false;
    }
72554
public function intersect(RolesStorageInterface $roles)
    {
        $res = array_intersect($roles->getArrayCopy(), $this->getArrayCopy());
        return (!empty($res));
    }
625957
protected function _addRoles($roles)
    {
        foreach($roles as $role) {
            if (!$this->hasRole($role->getName())) {
                $parents = $role->getParents()->toArray();
                $parentNames = array();
                foreach($parents as $parent) {
                    $parentNames[] = $parent->getName();
                }
                $this->addRole(new Role($role->getName()), $parentNames);
            }
        }

        return $this;
    }
6077
final public function isGranted(array $roles, array $resources, array $modules, array $accesses)
    {
        // Find a user which matches the required roles.
        $user = $this->checkRole($roles);

        if (null === $user) {
            return false;
        } else {
            return $this->isUserGranted($roles, $resources, $modules, $accesses, $user);
        }
    }
961594
public function assignRole(...$roles)
    {
        $roles = collect($roles)
            ->flatten()
            ->map(function ($role) {
                if (empty($role)) {
                    return false;
                }

                return $this->getStoredRole($role);
            })
            ->filter(function ($role) {
                return $role instanceof Role;
            })
            ->each(function ($role) {
                $this->ensureModelSharesGuard($role);
            })
            ->map->id
            ->all();

        $model = $this->getModel();

        if ($model->exists) {
            $this->roles()->sync($roles, false);
            $model->load('roles');
        } else {
            $class = \get_class($model);

            $class::saved(
                function ($object) use ($roles, $model) {
                    static $modelLastFiredOn;
                    if ($modelLastFiredOn !== null && $modelLastFiredOn === $model) {
                        return;
                    }
                    $object->roles()->sync($roles, false);
                    $object->load('roles');
                    $modelLastFiredOn = $object;
                });
        }

        $this->forgetCachedPermissions();

        return $this;
    }
874923
public function is($roles): bool
    {
        $userRoles = $this->roles()->all();

        // For a pre-caution, we should return false in events where user
        // roles not an array.
        if (! \is_array($userRoles)) {
            return false;
        }

        // We should ensure that all given roles match the current user,
        // consider it as a AND condition instead of OR.
        foreach ((array) $roles as $role) {
            if (! \in_array($role, $userRoles)) {
                return false;
            }
        }

        return true;
    }
74311
public function detachRoles($roles)
    {
        if ($roles instanceof Role) {
            $roles = [$roles->getKey()];
        }

        $this->roles()->detach($roles);

        unset($this->relations['roles']);

        return $this;
    }