em = $em; $this->coreMasqueradeService = $coreMasqueradeService; $this->class = $this->coreMasqueradeService->getUserGroupRoleClass(); } /** * @param CustomUserGroupInterface $group * @param CustomUserInterface $user * @return null|CustomUserGroupRoleInterface */ public function getUserRoleInGroup(CustomUserGroupInterface $group, CustomUserInterface $user){ $repo = $this->em->getRepository($this->getRepoClass()); $r = $repo->findOneBy(['user' => $user, 'group' => $group]); if(!$r instanceof CustomUserGroupRoleInterface) return null; return $r; } /** * @return null|string */ public function getRepoClass() { if (false !== strpos($this->class, ':')) { $metadata = $this->em->getClassMetadata($this->class); $this->class = $metadata->getName(); } return $this->class; } /** * Create a role with given parameters, update if its already exist * * @param CustomUserGroupInterface $group * @param CustomUserInterface $user * @param int|null $level * @return CustomUserGroupRoleInterface|null */ public function setRoleInGroup(CustomUserGroupInterface $group, CustomUserInterface $user, ?int $level = null) { $tr = $this->getUserRoleInGroup($group,$user); if($tr instanceof CustomUserGroupRoleInterface){ if($level == CompanyRoles::OWNER) $isOwner = true; else $isOwner = false; $tr->setOwner($isOwner); $tr->setLevel($level); $r = $this->quickUpdate($tr); if(!$r) return null; return $tr; } try { $role = $this->coreMasqueradeService->getUserGroupRoleObject(); } catch (\Exception $e) { return null; } if($level == CompanyRoles::OWNER) $isOwner = true; else $isOwner = false; if(is_null($level)) $level = CompanyRoles::NOBODY; $role->setDefault($user,$group,$level); $role->setOwner($isOwner); $r = $this->update($role); if(!$r) return null; return $role; } /** * @param CustomUserGroupRoleInterface $element * @return CustomUserGroupRoleInterface|null */ public function update(CustomUserGroupRoleInterface $element) { return $this->quickUpdate($element); } /** * @param CustomUserGroupRoleInterface $element * @return CustomUserGroupRoleInterface|null */ public function quickUpdate(CustomUserGroupRoleInterface $element) { if (!$this->em->contains($element)) { try { $this->em->persist($element); } catch (ORMException $e) { return null; } } try { $this->em->flush(); } catch (OptimisticLockException $e) { return null; } catch (ORMException $e) { return null; } return $element; } /** * @param CustomUserGroupInterface $group * @return CustomUserGroupRoleInterface[]|array */ public function getRolesInGroup(CustomUserGroupInterface $group){ $repo = $this->em->getRepository($this->getRepoClass()); return $repo->findBy(['group' => $group]); } /** * @param CustomUserInterface $user * @return CustomUserGroupRoleInterface[]|array */ public function getGroupForUser(CustomUserInterface $user){ $repo = $this->em->getRepository($this->getRepoClass()); return $repo->findBy(['user' => $user]); } /** * @param CustomUserGroupRoleInterface $element * @return bool */ public function delete(CustomUserGroupRoleInterface $element) { try { $this->em->remove($element); } catch (ORMException $e) { return false; } try { $this->em->flush(); } catch (OptimisticLockException $e) { return false; } catch (ORMException $e) { return false; } return true; } }