Пример #1
0
 /**
  * {@inheritDoc}
  */
 public function load(ObjectManager $manager)
 {
     $usersNumber = 10000;
     $firstnamesMale = array('John', 'Luke', 'Mike', 'Anakin', 'Bruce', 'Clark', 'Peet', 'Jack', 'Harry', 'Peter', 'Ron', 'Chuck', 'Cordel', 'Aladdin');
     $firstnamesFemale = array('Hermione', 'Sarah', 'Jasmine', 'Taslima', 'Alix', 'Padme', 'Rachel', 'Rose', 'Jane');
     $lastnamesMale = array('Connor', 'Skywalker', 'Lee', 'Wayne', 'Kent', 'Sparrow', 'Potter', 'Parker', 'Weasley', 'Norris', 'Walker');
     $lastnamesFemale = array('Granger', 'Connor', 'Amidala', 'Kent', 'Parker', 'Potter');
     $genders = array('m', 'f');
     $firstnames = array($firstnamesMale, $firstnamesFemale);
     $lastnames = array($lastnamesMale, $lastnamesFemale);
     //Create users
     for ($i = 0; $i < $usersNumber; $i++) {
         $user = new User();
         $maleOrFemale = rand(0, count($genders) - 1);
         $user->setGender($genders[$maleOrFemale]);
         $firstname = $i != 0 ? $firstnames[$maleOrFemale][rand(0, count($firstnames[$maleOrFemale]) - 1)] : "root";
         $user->setFirstname($firstname);
         $lastname = $i != 0 ? $lastnames[$maleOrFemale][rand(0, count($lastnames[$maleOrFemale]) - 1)] : "root";
         $user->setLastname($lastname);
         $user->setEmail(strtolower($user->getFirstname()) . '@' . strtolower($user->getLastname() . $i) . '.com');
         //First user is root
         $roles = $i != 0 ? array('ROLE_USER') : array('ROLE_USER', 'ROLE_SUPER_ADMIN');
         $user->setRoles($roles);
         $user->setSalt(md5(uniqid()));
         $encoder = $this->container->get('security.encoder_factory')->getEncoder($user);
         $user->setPassword($encoder->encodePassword(strtolower($user->getFirstname()), $user->getSalt()));
         $user->setUsername(strtolower($user->getFirstname() . '-' . $user->getLastname() . $i));
         $user->setCamp(null);
         $manager->persist($user);
     }
     $manager->flush();
 }
Пример #2
0
 /**
  * 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();
 }
Пример #3
0
 /**
  * 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;
 }
Пример #4
0
 /**
  * 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;
 }
Пример #5
0
 /**
  * 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();
 }