/** * @PreAuthorize("hasAnyRole('SUPER_ADMIN', 'CAN_MANAGE_STAFF')") * */ public function saveAction() { $request = $this->getRequest(); $institutionId = $this->institution->getId(); $id = $request->get('id', 0); $userType = $this->getDoctrine()->getRepository('UserBundle:InstitutionUserType')->find($id); if ($id && !$userType) { throw $this->createNotFoundException(); } elseif (!$id) { $userType = new InstitutionUserType(); $userType->setStatus(InstitutionUserType::STATUS_ACTIVE); } //assign institution to userType $userType->setInstitution($this->institution); $form = $this->createForm(new InstitutionUserTypeFormType(), $userType); $form->bind($request); if ($form->isValid()) { //persist data,create institution usertypes $em = $this->getDoctrine()->getEntityManager(); $em->persist($userType); $em->flush(); // create event on edit and create userTypes and dispatch $this->get('event_dispatcher')->dispatch(InstitutionBundleEvents::ON_ADD_INSTITUTION_USER_TYPE, $this->get('evens.factory')->create(InstitutionBundleEvents::ON_ADD_INSTITUTION_USER_TYPE, $userType)); $request->getSession()->setFlash("success", "{$userType->getName()} user type saved."); return $this->redirect($this->generateUrl('institution_userType_index')); } else { return $this->render('InstitutionBundle:InstitutionUserType:add.html.twig', array('form' => $form->createView(), 'userType' => $userType)); } }
public function onAdd(CreateInstitutionEvent $event) { $institution = $event->getInstitution(); $institutionUser = $event->getInstitutionUser(); if (!$institution instanceof Institution || !$institutionUser instanceof InstitutionUser) { throw new \Exception("{$event->getName()} handled by " . __CLASS__ . "::onAdd listener has invalid data."); } //persist data to create institutionUserTypes $institutionUserType = new InstitutionUserType(); $institutionUserType->setInstitution($institution); $institutionUserType->setName('ADMIN'); $institutionUserType->setStatus(InstitutionUserTypeStatuses::getBitValueForBuiltInUserType()); // add role to this first user type as super admin for this institution $adminInstitutionRole = $this->em->getRepository('UserBundle:InstitutionUserRole')->findOneBy(array('name' => InstitutionUserRole::SUPER_ADMIN)); if ($adminInstitutionRole) { $institutionUserType->addInstitutionUserRole($adminInstitutionRole); } $this->em->persist($institutionUserType); $this->em->flush(); //create institutionUser account and global account $this->createInstitutionUser($institutionUserType, $institutionUser); }