/**
  * @Route("/signup", name="signup_route")
  */
 public function signupAction(Request $request)
 {
     $user = new User();
     $form = $this->createForm(SignupType::class, $user);
     $form->handleRequest($request);
     if ($form->isSubmitted() && $form->isValid()) {
         $em = $this->getDoctrine()->getManager();
         $password = $this->get('security.password_encoder')->encodePassword($user, $user->getPlainPassword());
         $user->setPassword($password);
         $adminRole = $this->getDoctrine()->getRepository('AppBundle:Role')->findOneBy(['name' => 'Admin']);
         $adminUserRole = new UserRole();
         $adminUserRole->setRole($adminRole);
         $adminUserRole->setUser($user);
         $em->persist($adminUserRole);
         $user->addUserRole($adminUserRole);
         $em->persist($user);
         $organization = new Organization();
         $organization->setName($form->get('organizationName')->getData());
         $em->persist($organization);
         $user->setOrganization($organization);
         $account = new Account();
         $em->persist($account);
         $accountOwnerChange = new AccountOwnerChange();
         $accountOwnerChange->setChangedBy($user);
         $accountOwnerChange->setChangedAt(new \DateTime());
         $accountOwnerChange->setAccount($account);
         $accountOwnerChange->setNewOwner($user);
         $em->persist($accountOwnerChange);
         $accountOwnerChange->updateAccount();
         $trialSubscription = $this->getDoctrine()->getRepository('AppBundle:Subscription')->findOneBy(['name' => 'Trial']);
         $accountSubscriptionChange = new AccountSubscriptionChange();
         $accountSubscriptionChange->setChangedBy($user);
         $accountSubscriptionChange->setChangedAt(new \DateTime());
         $accountSubscriptionChange->setAccount($account);
         $accountSubscriptionChange->setNewSubscription($trialSubscription);
         $em->persist($accountSubscriptionChange);
         $accountSubscriptionChange->updateAccount();
         $organization->setAccount($account);
         $em->flush();
         $this->updateAclByRoles($user, ['ROLE_USER' => 'view', 'ROLE_ADMIN' => 'operator']);
         foreach ($user->getUserRoles() as $userRole) {
             $this->updateAclByRoles($userRole, ['ROLE_USER' => 'view', 'ROLE_ADMIN' => 'operator']);
         }
         $this->updateAclByRoles($organization, ['ROLE_USER' => 'view', 'ROLE_ADMIN' => 'operator']);
         return $this->redirectToRoute('login_route');
     }
     return $this->render('security/signup.html.twig', ['form' => $form->createView()]);
 }
 /**
  * {@inheritDoc}
  */
 public function load(ObjectManager $manager)
 {
     $belacUser = new User();
     $belacUser->setUsername('belac');
     $belacUser->setEmail('*****@*****.**');
     $belacUser->setFirstName('Belac');
     $belacUser->setLastName('Kriv');
     $belacUser->setIsActive(true);
     $belacUser->setDefaultDepartment($this->getReference('stepDept'));
     $belacUser->setOrganization($this->getReference('stepOrg'));
     $belacUserRole = new UserRole();
     $belacUserRole->setUser($belacUser);
     $belacUserRole->setRole($this->getReference('ROLE_DEV'));
     $belacUser->addUserRole($belacUserRole);
     $belacPassword = '******';
     $encoder = $this->container->get('security.password_encoder');
     $encoded = $encoder->encodePassword($belacUser, $belacPassword);
     $belacUser->setPassword($encoded);
     $manager->persist($belacUser);
     $plainUser = new User();
     $plainUser->setUsername('usertest');
     $plainUser->setEmail('user@none');
     $plainUser->setFirstName('User');
     $plainUser->setLastName('Test');
     $plainUser->setIsActive(true);
     $plainUser->setDefaultDepartment($this->getReference('oneOne'));
     $plainUser->setOrganization($this->getReference('demoOrg'));
     $plainUserRole = new UserRole();
     $plainUserRole->setUser($plainUser);
     $plainUserRole->setRole($this->getReference('ROLE_USER'));
     $plainUser->addUserRole($plainUserRole);
     $plainPassword = '******';
     $encoder = $this->container->get('security.password_encoder');
     $encoded = $encoder->encodePassword($plainUser, $plainPassword);
     $plainUser->setPassword($encoded);
     $manager->persist($plainUser);
     $leadUser = new User();
     $leadUser->setUsername('leadtest');
     $leadUser->setEmail('lead@none');
     $leadUser->setFirstName('Lead');
     $leadUser->setLastName('Test');
     $leadUser->setIsActive(true);
     $leadUser->setDefaultDepartment($this->getReference('oneOne'));
     $leadUser->setOrganization($this->getReference('demoOrg'));
     $leadUserRole = new UserRole();
     $leadUserRole->setUser($leadUser);
     $leadUserRole->setRole($this->getReference('ROLE_LEAD'));
     $leadUser->addUserRole($leadUserRole);
     $leadPassword = '******';
     $encoder = $this->container->get('security.password_encoder');
     $encoded = $encoder->encodePassword($leadUser, $leadPassword);
     $leadUser->setPassword($encoded);
     $manager->persist($leadUser);
     $adminUser = new User();
     $adminUser->setUsername('admintest');
     $adminUser->setEmail('admin@none');
     $adminUser->setFirstName('Admin');
     $adminUser->setLastName('Test');
     $adminUser->setIsActive(true);
     $adminUser->setDefaultDepartment($this->getReference('oneOne'));
     $adminUser->setOrganization($this->getReference('demoOrg'));
     $adminUserRole = new UserRole();
     $adminUserRole->setUser($adminUser);
     $adminUserRole->setRole($this->getReference('ROLE_ADMIN'));
     $adminUser->addUserRole($adminUserRole);
     $adminPassword = '******';
     $encoder = $this->container->get('security.password_encoder');
     $encoded = $encoder->encodePassword($adminUser, $adminPassword);
     $adminUser->setPassword($encoded);
     $manager->persist($adminUser);
     $manager->flush();
     $aclProvider = $this->container->get('security.acl.provider');
     $devRoleSecurityIdentity = new RoleSecurityIdentity('ROLE_DEV');
     $adminRoleSecurityIdentity = new RoleSecurityIdentity('ROLE_ADMIN');
     $leadRoleSecurityIdentity = new RoleSecurityIdentity('ROLE_LEAD');
     $userRoleSecurityIdentity = new RoleSecurityIdentity('ROLE_USER');
     $objectIdentity = ObjectIdentity::fromDomainObject($belacUser);
     $acl = $aclProvider->createAcl($objectIdentity);
     $acl->insertObjectAce($userRoleSecurityIdentity, MaskBuilder::MASK_VIEW);
     $acl->insertObjectAce($devRoleSecurityIdentity, MaskBuilder::MASK_OPERATOR);
     $aclProvider->updateAcl($acl);
     $objectIdentity = ObjectIdentity::fromDomainObject($belacUserRole);
     $acl = $aclProvider->createAcl($objectIdentity);
     $acl->insertObjectAce($userRoleSecurityIdentity, MaskBuilder::MASK_VIEW);
     $acl->insertObjectAce($devRoleSecurityIdentity, MaskBuilder::MASK_OPERATOR);
     $aclProvider->updateAcl($acl);
     $objectIdentity = ObjectIdentity::fromDomainObject($plainUser);
     $acl = $aclProvider->createAcl($objectIdentity);
     $acl->insertObjectAce($userRoleSecurityIdentity, MaskBuilder::MASK_VIEW);
     $acl->insertObjectAce($adminRoleSecurityIdentity, MaskBuilder::MASK_OPERATOR);
     $aclProvider->updateAcl($acl);
     $objectIdentity = ObjectIdentity::fromDomainObject($plainUserRole);
     $acl = $aclProvider->createAcl($objectIdentity);
     $acl->insertObjectAce($userRoleSecurityIdentity, MaskBuilder::MASK_VIEW);
     $acl->insertObjectAce($adminRoleSecurityIdentity, MaskBuilder::MASK_OPERATOR);
     $aclProvider->updateAcl($acl);
     $objectIdentity = ObjectIdentity::fromDomainObject($leadUser);
     $acl = $aclProvider->createAcl($objectIdentity);
     $acl->insertObjectAce($userRoleSecurityIdentity, MaskBuilder::MASK_VIEW);
     $acl->insertObjectAce($adminRoleSecurityIdentity, MaskBuilder::MASK_OPERATOR);
     $aclProvider->updateAcl($acl);
     $objectIdentity = ObjectIdentity::fromDomainObject($leadUserRole);
     $acl = $aclProvider->createAcl($objectIdentity);
     $acl->insertObjectAce($userRoleSecurityIdentity, MaskBuilder::MASK_VIEW);
     $acl->insertObjectAce($adminRoleSecurityIdentity, MaskBuilder::MASK_OPERATOR);
     $aclProvider->updateAcl($acl);
     $objectIdentity = ObjectIdentity::fromDomainObject($adminUser);
     $acl = $aclProvider->createAcl($objectIdentity);
     $acl->insertObjectAce($userRoleSecurityIdentity, MaskBuilder::MASK_VIEW);
     $acl->insertObjectAce($adminRoleSecurityIdentity, MaskBuilder::MASK_OPERATOR);
     $aclProvider->updateAcl($acl);
     $objectIdentity = ObjectIdentity::fromDomainObject($adminUserRole);
     $acl = $aclProvider->createAcl($objectIdentity);
     $acl->insertObjectAce($userRoleSecurityIdentity, MaskBuilder::MASK_VIEW);
     $acl->insertObjectAce($adminRoleSecurityIdentity, MaskBuilder::MASK_OPERATOR);
     $aclProvider->updateAcl($acl);
 }