示例#1
0
 /**
  * Creates a new Member entity.
  *
  * Create a new member and add him to the team with the member role.
  * Only administrator could be invit/create new member.
  * 
  * @Route("/{id}/create", name="member_create")
  * @Method("post")
  * @Template("NeblionScrumBundle:Member:new.html.twig")
  * @param integer $id Project id
  */
 public function createAction($id)
 {
     // Check if user is authorized
     if (!$this->get('security.context')->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
         throw new AccessDeniedException();
     }
     $user = $this->get('security.context')->getToken()->getUser();
     $em = $this->getDoctrine()->getManager();
     $project = $em->getRepository('NeblionScrumBundle:Project')->find($id);
     if (!$project) {
         throw $this->createNotFoundException('Unable to find Project entity.');
     }
     // Check if user is really a member of this project
     $member = $em->getRepository('NeblionScrumBundle:Member')->isMemberOfProject($user->getId(), $project->getId());
     if (!$member or !$member->getAdmin()) {
         throw new AccessDeniedException();
     }
     $form = $this->createForm(new InvitationType());
     $request = $this->getRequest();
     $form->bindRequest($request);
     if ($form->isValid()) {
         // Load fos user manager
         $userManager = $this->container->get('fos_user.user_manager');
         // Retrieve the submited email
         $email = $request->request->get($form->getName() . '[email]', null, true);
         // Load account associated to the email address
         $account = $userManager->findUserByUsernameOrEmail($email);
         // Load necessary entites
         // Load default Role (member);
         $role = $em->getRepository('NeblionScrumBundle:Role')->find(4);
         // Load default status (pending confirmation);
         $status = $em->getRepository('NeblionScrumBundle:MemberStatus')->find(1);
         if (!$account) {
             // Create new Member
             $member = new Member();
             $member->setProject($project);
             $member->setAccount(null);
             $member->setEmail($email);
             $member->setRole($role);
             $member->setAdmin(false);
             $member->setStatus($status);
             $member->setSender($user);
             $em->persist($member);
         } else {
             $existAccount = true;
             // Create new Member
             $member = new Member();
             $member->setProject($project);
             $member->setAccount($account);
             $member->setEmail($email);
             $member->setSender($user);
             $member->setRole($role);
             $member->setAdmin(false);
             $member->setStatus($status);
             $em->persist($member);
         }
         $em->flush();
         // Send an email notification to the new member
         $this->get('neblion_mailer')->sendInvitationEmailMessage($email, $user, $project, $account);
         // Set flash message
         $this->get('session')->getFlashBag()->add('success', 'Your invitation was sent successfully!');
         return $this->redirect($this->generateUrl('member_list', array('id' => $project->getId())));
     }
     return array('project' => $project, 'form' => $form->createView());
 }
示例#2
0
 private function newEntity($params)
 {
     // Create account
     $userManager = $this->container->get('fos_user.user_manager');
     $user = $userManager->createUser();
     $user->setUsername($params['name']);
     $user->setEmail($params['name'] . '@test.com');
     $user->setPlainPassword('test');
     $user->setEnabled(true);
     $userManager->updateUser($user, false);
     $this->manager->persist($user);
     // Create profile
     $profile = new Profile();
     $profile->setAccount($user);
     $profile->setFirstname($params['name']);
     $profile->setLastname($params['name']);
     //$profile->setLocation($params['name']);
     $this->manager->persist($profile);
     // Create member
     $member = new Member();
     $member->setAccount($user);
     $member->setProject($params['project']);
     $member->setRole($params['role']);
     $member->setStatus($params['status']);
     $member->setEmail($params['name'] . '@test.com');
     if ($params['status']->getId() == 1) {
         $member->setSender($params['sender']->getAccount());
     }
     $member->setAdmin($params['admin']);
     $this->manager->persist($member);
 }