Return all reachable circle for a given user, center and role

@param User $user
@param Role $role
@param Center $center
@return Scope[]
545920
public function getReachableCircles(User $user, Role $role, Center $center)
    {
        $scopes = array();
        
        foreach ($user->getGroupCenters() as $groupCenter){
            if ($center->getId() === $groupCenter->getCenter()->getId()) {
                //iterate on permissionGroup
                $permissionGroup = $groupCenter->getPermissionsGroup();
                //iterate on roleScopes
                foreach($permissionGroup->getRoleScopes() as $roleScope) {
                    //check that the role is in the reachable roles
                    if ($this->isRoleReached($role, 
                          new Role($roleScope->getRole()))) {

                        $scopes[] = $roleScope->getScope();
                    }
                }
            }
        }
        
        return $scopes;
    }
271206
protected function getImpersonatingUserFromRole($token, $user = null)
    {
        foreach ($token->getRoles() as $role) {
            if ($role instanceof SwitchUserRole) {
                $user = $role->getSource()->getUser();
                break;
            }
        }

        return $user;
    }
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() ?? [];
    }
193132
public function getUserMainDomain(User $user) {
    if($this->domainEnabled) {
      $groups = Group::getUserGroup($user);
      if (is_array($groups)) {
        foreach ($groups as $group) {
          if ($domaine = $group->getDomain()) {
            if ($domaine->status()) {
              return $domaine;
            }
          }
        }
      }
    }
    return NULL;
  }
942099
public function grantAccessByRoles($roles, UserInterface $user = null)
    {
        $user = $user ? : $this->currentUser();
        return count(array_diff($roles, $user->getRoles())) === 0;
    }
910829
private function createDeleteLinkGroupCenterForm(User $user, GroupCenter $groupCenter)
    {
        return $this->createFormBuilder()
            ->setAction($this->generateUrl('admin_user_delete_group_center', 
                    array('uid' => $user->getId(), 'gcid' => $groupCenter->getId())))
            ->setMethod('DELETE')
            ->add('submit', 'submit', array('label' => 'Delete'))
            ->getForm()
        ;
    }
851114
public function roles(User $user)
    {
        if ($this->session->hasValue(Roles::SESSION_KEY)) {
            return $this->session->value(Roles::SESSION_KEY);
        }

        $roles = $this->authorizationProvider->roles($user);
        $this->session->putValue(Roles::SESSION_KEY, $roles);
        return $roles;
    }
364976
public static function userCan(int $user_id, string $role, string $permission, $callback = null) : bool
    {
        return (new Services\UsersService)
            ->userCan($user_id, $role, $permission, $callback);
    }
322445
public function hasRole($role, $user = null)
    {
        $roleName = $role instanceof Role ? $role->getRole() : $role;

        $user = $user ?: $this->getUser();

        if (!($user instanceof UserInterface)) {
            return false;
        }

        if (null === $this->roleHierarchy) {
            return in_array($roleName, $user->getRoles(), true);
        }

        $userRoles = $this->roleHierarchy->getReachableRoles($this->getUserRolesAsObjects($user));
        foreach ($userRoles as $userRole) {
            if ($roleName === $userRole->getRole()) {
                return true;
            }
        }

        return false;
    }
1181112
public function indexAction(User $user = null)
    {
        return [
            'isAdmin' => !is_null($user) ? $user->hasRole('ROLE_ADMIN') : false,
            'resourceTypes' => array_map(function (ResourceType $type) {
                return $this->resourceTypeSerializer->serialize($type);
            }, $this->resourceTypeRepo->findBy([], ['name' => 'ASC'])),
        ];
    }
345921
public function getResourcesPermissions(User $user, $resources)
    {
        $permissions = [];
        if(!is_null($user)){
            try {
                $permissionManager = $this->getServiceManager()->get(PermissionInterface::SERVICE_ID);
                $supportedRights   = $permissionManager->getSupportedRights();
                $permissions['supportedRights'] = $supportedRights;

                if(count($supportedRights) > 0){
                    $uris = $this->getUris($resources);

                    $permissions['data'] = $permissionManager->getPermissions($user, $uris);
                }
            } catch(\Exception $e){
                \common_Logger::w('Unable to retrieve permssions ' . $e->getMessage());
            }
        }
        return $permissions;

    }
879077
function sync_roles($user) {
        global $DB;

        $roles = get_ldap_assignable_role_names(2); // Admin user.

        foreach ($roles as $role) {
            $isrole = $this->is_role($user->username, $role);
            if ($isrole === null) {
                continue; // Nothing to sync - role/LDAP contexts not configured.
            }

            // Sync user.
            $systemcontext = context_system::instance();
            if ($isrole) {
                // Following calls will not create duplicates.
                role_assign($role['id'], $user->id, $systemcontext->id, $this->roleauth);
            } else {
                // Unassign only if previously assigned by this plugin.
                role_unassign($role['id'], $user->id, $systemcontext->id, $this->roleauth);
            }
        }
    }
177802
public Set<Role> findRoles(final User user)
    {
        final Set<Role> result = new HashSet<Role>();
        
        Object source;
        for(final RoleMapping mapping : this.getRoleMappings())
        {
            source = mapping.getSource();
            
            if((user != null) && user.equals(source))
            {
                result.add(mapping.getTarget());
            }
        }
        
        return result;
    }
141914
public function userCan($user_id, string $role, string $permission, $callback = null) : bool
    {
        // Usuário permamentemente liberado
        $root_user = config('acl.root_user');
        if ($user_id == $root_user) {
            \Acl\Core::traceCurrentAbilityOrigin('config');
            \Acl\Core::traceCurrentAbility($role, $permission, true);
            return true;
        }

        // Existem permissões setadas?
        $user_abilities = (new UsersPermissionsService)->getPermissionsByUserID($user_id, $role);
        if ($user_abilities === null) {
            \Acl\Core::traceCurrentAbility($role, $permission, false);
            return false;
        }

        // create,read,update ou delete == yes?
        $result = (isset($user_abilities['permissions']) && $user_abilities['permissions'][$permission] == 'yes');

        // Existe uma verificação adicional
        if ($result == true && $callback != null && is_callable($callback) && $callback() !== true) {
            \Acl\Core::traceCurrentAbilityOrigin('callback');
            \Acl\Core::traceCurrentAbility($role, $permission, false);
            return false;
        }

        \Acl\Core::traceCurrentAbility($role, $permission, $result);

        return $result;
    }
749784
public function getRolesByUser($userId)
	{
		$roles = $this->getDefaultRoleInstances();
		foreach ($this->domain->assignment->getAssignments($userId) as $name => $assignment) {
			$role = $this->items[$assignment->roleName];
			if ($role->type === Item::TYPE_ROLE) {
				$roles[$name] = $role;
			}
		}
		
		return $roles;
	}
54428
public function all(User $user = null)
    {
        $query = Tag::query();

        return $this->scopeVisibleTo($query, $user)->get();
    }
881987
protected function loadUserRolesViaManyToMany($user)
    {
        $roles = ['ROLE_USER'];
        if (is_string($this->userRoleIdentifier)) {
            foreach ($user->get($this->userRoleIdentifier) as $role) {
                $roles[] = $role['name'];
            }
        }
        return $roles;
    }