em = $em; $this->managerService = $managerService; $this->coreMasqueradeService = $coreMasqueradeService; $this->class = $this->coreMasqueradeService->getCompanyRoleClass(); } public function getUserRoleInCompany(CustomCompanyInterface $company, CustomUserInterface $user){ $repo = $this->em->getRepository($this->getRepoClass()); $r = $repo->findBy(['user' => $user, 'company' => $company]); return $r; } /** * check if user is admin (or owner) in, at least, one service * * @param CustomCompanyInterface $company * @param CustomUserInterface $user * @return bool */ public function isAdminInCompany(CustomCompanyInterface $company, CustomUserInterface $user){ $rs = $this->getUserRoleInCompany($company,$user); foreach($rs as $r){ if($r instanceof CustomCompanyUserRoleInterface) { if($r->getLevel() == CompanyRoles::ADMIN || $r->getLevel() == CompanyRoles::OWNER){ return true; } } } return false; } /** * @param CustomCompanyInterface $company * @param CustomUserInterface $user * @param int|null $level * @param bool|null $selected * @param bool|null $selfReplicated * @return bool */ public function updateAllRoles(CustomCompanyInterface $company, CustomUserInterface $user, ?int $level = null, ?bool $selected = null, ?bool $selfReplicated = null){ $rs = $this->getUserRoleInCompany($company,$user); foreach($rs as $r){ if($r instanceof CustomCompanyUserRoleInterface){ /** * @var $r CustomCompanyUserRoleInterface */ if(!is_null($level)) $r->setLevel($level); if(!is_null($selected)) $r->setSelected($selected); if(!is_null($selfReplicated)) $r->setSelfCompany($selfReplicated); $this->quickUpdate($r); } } return true; } /** * @param CustomCompanyInterface $company * @param CustomUserInterface $user * @param CustomCompanyServiceInterface|null $service * @param string $position * @param int|null $level * @param bool $selfCompany * @param bool $selected * @return CustomCompanyUserRoleInterface|null */ public function setRoleInCompany(CustomCompanyInterface $company, CustomUserInterface $user, ?CustomCompanyServiceInterface $service = null, string $position = "", ?int $level = null, bool $selfCompany = false, bool $selected = false){ try { $role = $this->coreMasqueradeService->getCompanyRoleObject(); } catch (\Exception $e) { return null; } if($level == CompanyRoles::OWNER) $isOwner = true; else $isOwner = false; $role->setDefault($user,$company); $role->setOwner($isOwner) ->setSelfCompany($selfCompany) ->setSelected($selected) ->setPositionInService($position); if(is_null($level)) $level = CompanyRoles::NOBODY; $role->setLevel($level); if(!is_null($service)) $role->setService($service); $r = $this->update($role); if(!$r) return null; return $role; } /** * @param CustomUserInterface $user * @return array */ public function getAllUserRoles(CustomUserInterface $user){ $repo = $this->em->getRepository($this->getRepoClass()); return $repo->findBy(['user' => $user]); } /** * @param CustomCompanyServiceInterface $service * @return CustomCompanyUserRoleInterface[] */ public function getWhereServiceIs(CustomCompanyServiceInterface $service){ return $this->getRepositoryService()->findBy(['service' => $service]); } /** * meant to transform namespace to repository * * @return null|string */ public function getRepoClass() { if (false !== strpos($this->class, ':')) { $metadata = $this->em->getClassMetadata($this->class); $this->class = $metadata->getName(); } return $this->class; } /** * @return \Doctrine\Common\Persistence\ObjectRepository|\Doctrine\ORM\EntityRepository */ public function getRepositoryService(){ $repo = $this->em->getRepository($this->getRepoClass()); return $repo; } /** * @param CustomCompanyUserRoleInterface $element * @return CustomCompanyUserRoleInterface|null */ public function update(CustomCompanyUserRoleInterface $element) { return $this->quickUpdate($element); } /** * @param CustomCompanyUserRoleInterface $element * @return CustomCompanyUserRoleInterface|null */ public function quickUpdate(CustomCompanyUserRoleInterface $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 CustomCompanyUserRoleInterface $element * @return boolean */ public function delete(CustomCompanyUserRoleInterface $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; } }