/** * Check if $user is the $organisation's manager * * @param User $user the user to check if he is $organisation's manager * @param Organisation $organisation the organisation to check if $user is * the manager * @return boolean true if $user is $organisation's manager, false else */ public function isOrganisationManager($user, $organisation) { //If $user in not a User (it may be equals to null for e.g) if (!$user instanceof User) { return false; } elseif (!$organisation instanceof Organisation) { //if $organisation is not an Organisation (it may be equals to null) return false; } elseif (empty($organisation->getManager())) { //If organisation has no manager return false; } return $user->getId() == $organisation->getManager()->getId(); }
/** * Update the user's camp * * @param \Xali\Bundle\UserBundle\Entity\User $volunteer * @param \Xali\Bundle\CampBundle\Entity\Camp $camp * @param \Xali\Bundle\OrganisationBundle\Entity\Organisation * @return integer -1 if parameters type are invalids, 0 if request failed * and 1 if it works, and 2 if volunteerd doesn't belong to this * organisation */ public function updateCamp($volunteer, $camp) { $return = 0; //If parameters are invalids (usually $volunteer) if ($volunteer instanceof User && $camp instanceof Camp) { //If user belong to an other organisation $volunteerOrganisation = $volunteer->getCamp() == null ? null : $volunteer->getCamp()->getOrganisation(); if ($volunteerOrganisation != null && $camp->getOrganisation()->getId() != $volunteerOrganisation->getId()) { return 2; } $queryBuilder = $this->createQueryBuilder('u'); $q = $queryBuilder->update('XaliUserBundle:User', 'u')->set('u.camp', ':camp')->setParameter('camp', $camp)->where('u.id = :user_id')->setParameter('user_id', $volunteer->getId())->getQuery(); $return = $q->execute(); } else { $return = -1; } return $return; }
/** * Update an organisation * * @param \Xali\Bundle\UserBundle\Entity\User $manager the manager we want * to assign * @param \Xali\Bundle\OrganisationBundle\Entity\Organisation $organisation * the organisation to update * @return string */ public function updateOrganisation($manager, $organisation) { if (!empty($manager) && $manager instanceof User) { $result = null; $managerOrganisation = $manager->getCamp() == null ? null : $manager->getCamp()->getOrganisation(); //user belong to an other organisation if ($managerOrganisation != null && $managerOrganisation->getId() != $organisation->getId()) { return "form.error.belong_other_org"; } $sql = "UPDATE organisation\n SET manager_id = ?, name = ?, dateofcreation = ?\n WHERE organisation.id = ?\n "; try { $stmt = $this->_em->getConnection()->prepare($sql); $stmt->execute(array($manager->getId(), $organisation->getName(), $organisation->getDateOfCreation()->format('Y-m-d'), $organisation->getId())); } catch (UniqueConstraintViolationException $e) { $result = "form.error.violation_key"; } catch (\Exception $e) { $result = "form.error.other_error"; } } else { //Manager's email adress is invalid $result = "form.error.invalid_email"; } return $result; }
/** * Check if two users are the same * * @param Xali\Bundle\UserBundle\Entity\User $loggedUser * @param Xali\Bundle\UserBundle\Entity\User $givenUser * @return boolean */ public function isSameUser($loggedUser, $givenUser) { //If at least one of users is invalid if (!$loggedUser instanceof User || !$givenUser instanceof User) { return false; } return $loggedUser->getId() == $givenUser->getId(); }