coreMasqueradeService = $coreMasqueradeService; $this->em = $em; $this->tokenStorage = $tokenStorage; $this->groupRoleManager = $groupRoleManager; $this->class = $this->coreMasqueradeService->getUserGroupClass(); } /** * @return null|string */ public function getClassName(){ return $this->coreMasqueradeService->getUserGroupClass(); } /** * transform class name to repo's name * * @return null|string */ public function getRepoClass() { if (false !== strpos($this->class, ':')) { $metadata = $this->em->getClassMetadata($this->class); $this->class = $metadata->getName(); } return $this->class; } /** * shortcut for masquerade * * @param string $name * @return CustomUserGroupInterface|null */ public function generateGroupObject(string $name = ""){ try { $o = $this->coreMasqueradeService->getUserGroupObject(); $o->setDefault($name); return $o; } catch (\Exception $e) { return null; } } public function registerGroup(CustomUserGroupInterface $group , ?CustomUserInterface $user = null){ if(is_null($user)){ $user = $this->tokenStorage->getToken()->getUser(); if(!$user instanceof CustomUserInterface) return false; } $group->setName($this->sanitizeName($group->getName())); $r = $this->update($group); if(!$r) return false; $role = $this->groupRoleManager->setRoleInGroup($group,$user,CompanyRoles::OWNER); if(!$role instanceof CustomUserGroupRoleInterface) return false; return true; } /** * @param int $id * @return null|CustomUserGroupInterface */ public function getById(int $id){ $i = $this->em->getRepository($this->getRepoClass())->find($id); if(!$i instanceof CustomUserGroupInterface) return null; return $i; } /** * @param string $publicKey * @return null|CustomUserGroupInterface */ public function getByPublicKey(string $publicKey){ $i = $this->em->getRepository($this->getRepoClass())->findOneBy(['publicKey'=>$publicKey]); if(!$i instanceof CustomUserGroupInterface) return null; return $i; } /** * @param CustomUserGroupInterface $group * @param CustomUserInterface|null $user * @return bool */ public function isOwner(CustomUserGroupInterface $group, ?CustomUserInterface $user = null){ if(is_null($user)){ $user = $this->tokenStorage->getToken()->getUser(); if(!$user instanceof CustomUserInterface) return false; } $rs = $this->groupRoleManager->getUserRoleInGroup($group,$user); if($rs instanceof CustomUserGroupRoleInterface) return $rs->isOwner(); return false; } /** * @param CustomUserGroupInterface $group * @param CustomUserInterface|null $user * @return bool */ public function isAdmin(CustomUserGroupInterface $group, ?CustomUserInterface $user = null){ if(is_null($user)){ $user = $this->tokenStorage->getToken()->getUser(); if(!$user instanceof CustomUserInterface) return false; } $rs = $this->groupRoleManager->getUserRoleInGroup($group,$user); if($rs instanceof CustomUserGroupRoleInterface){ if($rs->isOwner()) return true; if($rs->getLevel() == CompanyRoles::ADMIN) return true; } return false; } /** * @param CustomUserGroupInterface $group * @param CustomUserInterface|null $user * @return bool */ public function canDelete(CustomUserGroupInterface $group, ?CustomUserInterface $user = null){ $v = $this->isOwner($group,$user); return $v; } /** * can edit some settings of this group * * @param CustomUserGroupInterface $group * @param CustomUserInterface|null $user * @return bool */ public function canEdit(CustomUserGroupInterface $group, ?CustomUserInterface $user = null){ $v = $this->isAdmin($group,$user); return $v; } /** * can view the public information of this group * * @param CustomUserGroupInterface $group * @param CustomUserInterface|null $user * @return bool */ public function canView(CustomUserGroupInterface $group, ?CustomUserInterface $user = null){ //if group is public if($group->isPublicVisible()) return true; //if user is at least guest view=true $r = $this->groupRoleManager->getUserRoleInGroup($group,$user); if(!$r instanceof CustomUserGroupRoleInterface) return false; if($r->getLevel() <= CompanyRoles::GUEST) return true; return false; } /** * can join a group if its public open or after an invitation * nb : invitation set status as "GUEST" * * @param CustomUserGroupInterface $group * @param CustomUserInterface|null $user * @return bool */ public function canJoin(CustomUserGroupInterface $group, ?CustomUserInterface $user = null){ if($group->isPublicOpen()) return true; $r = $this->groupRoleManager->getUserRoleInGroup($group,$user); if(!$r instanceof CustomUserGroupRoleInterface) return false; if($r->getLevel() <= CompanyRoles::GUEST) return true; return false; } /** * @param CustomUserGroupInterface $group * @param CustomUserInterface|null $user * @return bool */ public function canUse(CustomUserGroupInterface $group, ?CustomUserInterface $user = null){ //if user is at least guest view=true $r = $this->groupRoleManager->getUserRoleInGroup($group,$user); if(!$r instanceof CustomUserGroupRoleInterface) return false; if($r->getLevel() <= CompanyRoles::USER) return true; return false; } /** * @param CustomUserGroupInterface $group * @param CustomUserInterface|null $user * @param int $level * @return CustomUserGroupRoleInterface|null */ public function setUserRoleInGroup(CustomUserGroupInterface $group, ?CustomUserInterface $user = null, int $level = CompanyRoles::GUEST){ return $this->groupRoleManager->setRoleInGroup($group,$user,$level); } /** * @param CustomUserGroupInterface $group * @return CustomUserGroupRoleInterface[]|array */ public function getRolesInGroup(CustomUserGroupInterface $group){ return $this->groupRoleManager->getRolesInGroup($group); } /** * @param CustomUserInterface $user * @return CustomUserGroupRoleInterface[]|array */ public function getGroupForUser(CustomUserInterface $user){ return $this->groupRoleManager->getGroupForUser($user); } /** * @param CustomUserGroupInterface $element * @return bool */ public function update(CustomUserGroupInterface $element) { return $this->quickUpdate($element); } /** * @param CustomUserGroupInterface $element * @return bool */ public function quickUpdate(CustomUserGroupInterface $element) { try { if(!$this->em->contains($element)){ $r = $this->checkAndValidatePublicKey($element); if($r) $this->em->persist($element); else return false; } $this->em->flush(); } catch (OptimisticLockException $e) { return false; } catch (ORMException $e) { return false; } return true; } /** * @param CustomUserGroupInterface $group * @return bool */ public function checkAndValidatePublicKey(CustomUserGroupInterface $group){ $attempts = 0; while(true){ $u = $this->getByPublicKey($group->getPublicKey()); if(is_null($u)) return true; $group->regeneratePublicKey(); $attempts++; if($attempts >= 20) return false; } return false; } /** * @param CustomUserGroupInterface $element * @return bool */ public function delete(CustomUserGroupInterface $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; } public function sanitizeName($name){ $disallowed = array('"','\'','\\','/','|','[',']','(',')','{','}','=','$','%','@','>','<','?','!','?',',',';',':','*'); foreach($disallowed as $char){ $name = str_replace($char,'',$name); } return $this->str_to_noaccent($name); } public function sanitizeNameForZip($name){ $name = $this->str_to_noaccent($name); return $name; } public function str_to_noaccent($str) { $url = $str; $url = preg_replace('#Ç#', 'C', $url); $url = preg_replace('#ç#', 'c', $url); $url = preg_replace('#è|é|ê|ë#', 'e', $url); $url = preg_replace('#È|É|Ê|Ë#', 'E', $url); $url = preg_replace('#à|á|â|ã|ä|å#', 'a', $url); $url = preg_replace('#@|À|Á|Â|Ã|Ä|Å#', 'A', $url); $url = preg_replace('#ì|í|î|ï#', 'i', $url); $url = preg_replace('#Ì|Í|Î|Ï#', 'I', $url); $url = preg_replace('#ð|ò|ó|ô|õ|ö#', 'o', $url); $url = preg_replace('#Ò|Ó|Ô|Õ|Ö#', 'O', $url); $url = preg_replace('#ù|ú|û|ü#', 'u', $url); $url = preg_replace('#Ù|Ú|Û|Ü#', 'U', $url); $url = preg_replace('#ý|ÿ#', 'y', $url); $url = preg_replace('#Ý#', 'Y', $url); return ($url); } }