/**
  * Gets all the roles associated with an user
  * @param User $user
  * @param Organization $organization
  * @return mixed
  */
 public function rolesOfUser(User $user, Organization $organization)
 {
     $qb = $this->em->createQueryBuilder();
     $qb->select('r')->from($this->roleClass, 'r')->join('r.user_roles', 'ur')->where($qb->expr()->eq('ur.user', '?1'), $qb->expr()->eq('ur.organization', '?2'))->setParameter(1, $user->id())->setParameter(2, $organization->id());
     return $qb->getQuery()->getResult();
 }
예제 #2
0
 /**
  * Finds the organizations in which the given user is registered.
  *
  * @param User $user
  * @return ArrayCollection
  */
 public function organizationsOfUser(User $user)
 {
     $qb = $this->em->createQueryBuilder();
     $qb->select('ur, o')->from($this->urClass, 'ur')->join('ur.organization', 'o')->join('ur.role', 'r')->where($qb->expr()->eq('ur.user', '?1'), $qb->expr()->eq('r.slug', '?2'))->setParameter(1, $user->id())->setParameter(2, 'user');
     $userRoles = $qb->getQuery()->getResult();
     //convert to only organizations
     $result = [];
     foreach ($userRoles as $userRole) {
         if (!in_array($userRole->organization(), $result)) {
             $result[] = $userRole->organization();
         }
     }
     return $result;
 }