/** * @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 getAssignablePermissionsByUserType(InstitutionUserType $userType) { $currentUserRoles = $userType->getInstitutionUserRoles(); $ids = array(); foreach ($currentUserRoles as $each) { $ids[] = $each->getId(); } $idsNotIn = "'" . \implode("', '", $ids) . "'"; $dql = "SELECT a FROM UserBundle:InstitutionUserRole a WHERE a.status = :active AND a.id NOT IN ({$idsNotIn})"; $query = $this->getEntityManager()->createQuery($dql)->setParameter('active', InstitutionUserRole::STATUS_ACTIVE); return $query->getResult(); }
public function testRemoveRoleFromUserType() { $uri = '/institution/staff/user-roles/remove-role-from-user-type'; $params = array('userRoleId' => $this->userRole->getId(), 'userTypeId' => $this->userType->getId()); // test that it will not accept a GET method $client = $this->getBrowserWithActualLoggedInUser(); $crawler = $client->request('GET', $uri, $params); $this->assertEquals(405, $client->getResponse()->getStatusCode(), 'Expecting method GET to be not accepted'); // test to remove invalid $client = $this->getBrowserWithActualLoggedInUser(); $crawler = $client->request('POST', $uri, array('userRoleId' => 99999, 'userTypeId' => 21312388324242399)); $this->assertEquals(404, $client->getResponse()->getStatusCode(), "Expecting error 404 after passing invalid user type and user role"); // test valid data post $crawler = $client->request('POST', $uri, $params); $this->assertEquals(200, $client->getResponse()->getStatusCode()); }
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); }