coreMasqueradeService = $coreMasqueradeService; $this->em = $em; $this->managerService = $managerService; $this->tokenStorage = $tokenStorage; $this->companyUserRoleManager = $companyUserRoleManager; $this->class = $this->coreMasqueradeService->getCompanyClass(); } /** * @param int $id * @return null|\App\Entity\Company\Company */ public function getById(int $id): ?Company { return $this->em->getRepository($this->class)->find($id); } /** * @param string $publicKey * @return null|\App\Entity\Company\Company */ public function getByPublicKey(string $publicKey): ?Company { return $this->em->getRepository($this->class)->findOneBy(['publicKey'=>$publicKey]); } /** * can edit some settings of this company * * @param CustomCompanyInterface $company * @param CustomUserInterface|null $user * @return bool */ public function canEdit(CustomCompanyInterface $company, ?CustomUserInterface $user = null): bool { $v = $this->isAdmin($company,$user); return $v; } /** * can view the public information of this company * * @param CustomCompanyInterface $company * @param CustomUserInterface|null $user * @return bool */ public function canView(CustomCompanyInterface $company, ?CustomUserInterface $user = null){ //if set to visible we agree to show the company //otherwise we check if the user is a member of the company if($company->isPublicVisible()) return true; return $this->canUse($company,$user); } /** * User can create action for this company * * @param CustomCompanyInterface $company * @param CustomUserInterface|null $user * @return bool */ public function canUse(CustomCompanyInterface $company, ?CustomUserInterface $user = null){ if(is_null($user)){ $user = $this->tokenStorage->getToken()->getUser(); if(!$user instanceof CustomUserInterface) return false; } $rs = $this->companyUserRoleManager->getUserRoleInCompany($company,$user); if(!empty($rs)) return true; return false; } /** * can delete and do important stuff for this company * * @param CustomCompanyInterface $company * @param CustomUserInterface|null $user * @return bool */ public function canDelete(CustomCompanyInterface $company, ?CustomUserInterface $user = null){ return $this->isOwner($company,$user); } /** * check if an user is at least admin in a company * * @param CustomCompanyInterface $company * @param CustomUserInterface|null $user * @return bool */ public function isAdmin(CustomCompanyInterface $company, ?CustomUserInterface $user = null){ // if(is_null($user)){ $user = $this->tokenStorage->getToken()->getUser(); if(!$user instanceof CustomUserInterface) return false; } $rs = $this->companyUserRoleManager->getUserRoleInCompany($company,$user); foreach($rs as $role){ /** * @var $role CustomCompanyUserRoleInterface */ if($role->getLevel() == CompanyRoles::ADMIN || $role->getLevel() == CompanyRoles::OWNER) return true; } return false; } /** * check if an user is owner in a company * * @param CustomCompanyInterface $company * @param CustomUserInterface|null $user * @return bool */ public function isOwner(CustomCompanyInterface $company, ?CustomUserInterface $user = null){ // if(is_null($user)){ $user = $this->tokenStorage->getToken()->getUser(); if(!$user instanceof CustomUserInterface) return false; } $rs = $this->companyUserRoleManager->getUserRoleInCompany($company,$user); foreach($rs as $role){ /** * @var $role CustomCompanyUserRoleInterface */ if($role->getLevel() == CompanyRoles::OWNER) return true; } return false; } /** * @param CustomCompanyInterface $company * @return bool */ public function registerCompany(CustomCompanyInterface $company){ //we may add something in the future $user = $this->tokenStorage->getToken()->getUser(); if(!$user instanceof CustomUserInterface) return false; $this->companyUserRoleManager->setRoleInCompany($company,$user,null, "Owner",CompanyRoles::OWNER,false); return $this->quickUpdate($company); } /** * @param CustomCompanyInterface $company * @param CustomUserInterface|null $user * @return bool */ public function setActiveCompany(CustomCompanyInterface $company, ?CustomUserInterface $user = null){ if(is_null($user)) $user = $this->tokenStorage->getToken()->getUser(); if(!$user instanceof CustomUserInterface) return false; $cs = $this->getCompaniesForUser($user); foreach($cs as $otherCompany){ /** * @var $otherCompany CustomCompanyInterface */ $this->companyUserRoleManager->updateAllRoles($otherCompany,$user,null,false,null); } return $this->companyUserRoleManager->updateAllRoles($company,$user,null,true,null); } /** * Return a persisted CustomCompanyUserRoleInterface object to deal with, if any error occurs it return null * * @param CustomCompanyInterface $company * @param CustomUserInterface $user * @param CustomCompanyServiceInterface|null $service * @param null|string $position * @return CustomCompanyUserRoleInterface|null */ public function addUserToCompany(CustomCompanyInterface $company, CustomUserInterface $user, ?CustomCompanyServiceInterface $service = null, ?string $position = null){ try { $role = $this->coreMasqueradeService->getCompanyRoleObject(); } catch (\Exception $e) { return null; } $role->setDefault($user,$company,$service,$position); $f = $this->companyUserRoleManager->quickUpdate($role); if(!$f) return null; return $role; } /** * return an array of company for the given user (current if not specified) * * @param CustomUserInterface|null $user * @return array */ public function getCompaniesForUser(?CustomUserInterface $user = null){ if(is_null($user)) $user = $this->tokenStorage->getToken()->getUser(); if(!$user instanceof CustomUserInterface) return array(); $allRoles = $this->companyUserRoleManager->getAllUserRoles($user); $companies = []; foreach($allRoles as $roles){ /** * @var $roles CustomCompanyUserRoleInterface */ $c = $roles->getCompany(); if($c instanceof CustomCompanyInterface) $companies[] = $c; } return $companies; } public function updateUserService(CustomCompanyInterface $company, CustomUserInterface $user, ?CustomCompanyServiceInterface $service = null, ?string $position = null){ //TODO } public function getClass() { if (false !== strpos($this->class, ':')) { $metadata = $this->em->getClassMetadata($this->class); $this->class = $metadata->getName(); } return $this->class; } /** * @param CustomCompanyInterface $company * @return bool */ public function update(CustomCompanyInterface $company){ if(empty($company)) return false; return $this->quickUpdate($company); } /** * @param CustomCompanyInterface $company * @return bool */ public function quickUpdate(CustomCompanyInterface $company){ if(empty($company)) return false; try { if(!$this->em->contains($company)) $this->em->persist($company); $this->em->flush(); } catch (OptimisticLockException $e) { //die('Exception : '.$e->getMessage()); return false; } catch (ORMException $e) { return false; } return true; } /** * @param CustomCompanyInterface $element * @return bool */ public function delete(CustomCompanyInterface $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; } }