/**
  * @param $username
  * @param $email
  * @param $galaxyRoles
  * @param $firstName
  * @param $lastName
  * @param $codeSection
  *
  * @return UserModel
  */
 public function createUser($username, $email, $galaxyRoles, $firstName, $lastName, $codeSection)
 {
     $user = new UserModel();
     $user->setUsername($username)->setEmail($email)->setGalaxyRoles(implode(",", $galaxyRoles))->setFirstName($firstName)->setLastName($lastName);
     if (in_array('Local.regularBoardMember', $galaxyRoles)) {
         $user->addRole(User::ROLE_BOARD);
     }
     $user->addRole(User::ROLE_NORMAL)->setCodeSection($codeSection);
     return $user;
 }
 public function saveUser(UserModel $userModel)
 {
     $userDb = $this->em->getRepository("MainBundle:User")->findOneBy(array("email" => $userModel->getEmail()));
     $sectionDb = $this->em->getRepository("MainBundle:Section")->findOneBy(array("codeSection" => $userModel->getCodeSection()));
     $persist = $userDb ? false : true;
     $userDb = !$userDb ? new EntityUser() : $userDb;
     $userDb->setUsername($userModel->getUsername())->setEmail($userModel->getEmail())->setGalaxyRoles($userModel->getGalaxyRoles())->setFirstName($userModel->getFirstName())->setLastName($userModel->getLastName())->setRandomPassword()->setSection($sectionDb);
     foreach ($userModel->getRoles() as $role) {
         if (!in_array($role, $userDb->getRoles())) {
             $userDb->addRole($role);
         }
     }
     if ($persist) {
         $this->em->persist($userDb);
     }
     $this->em->flush();
     return $userDb;
 }