/** * {@inheritDoc} */ public function load(ObjectManager $manager) { $username = '******'; $email = '*****@*****.**'; $password = '******'; if ($this->container->has('drafterbit_installer')) { $data = $this->container->get('drafterbit_installer')->getData(); if ($data) { $username = $data['username']; $email = $data['email']; $password = $data['password']; } } $userAdmin = new User(); $userAdmin->setUsername($username); $userAdmin->setRealname($username); $userAdmin->setEmail($email); $userAdmin->setPlainPassword($password); $userAdmin->setEnabled(1); $userAdmin->addRole('ROLE_ADMIN'); $userAdmin->getGroups()->add($this->getReference('admin-group')); $manager->persist($userAdmin); $manager->flush(); $this->addReference('admin-user', $userAdmin); }
/** * @Route("/user/save", name="drafterbit_user_save") * @Template() */ public function saveAction(Request $request) { $requestUser = $request->request->get('user'); $id = $requestUser['id']; $userManager = $this->get('fos_user.user_manager'); $user = $userManager->findUserBy(['id' => $id]); if (!$user) { $user = new User(); $user->addRole('ROlE_ADMIN'); } $form = $this->createForm(new UserType(), $user); $form->handleRequest($request); if ($form->isValid()) { //save data to database $user = $form->getData(); $password = $form->get('password')->getData(); if (trim($password) != '') { $user->setPlainPassword($password); } $userManager->updateUser($user); $id = $user->getId(); // @todo $logger = $this->get('logger'); $logger->info('%author% edited user %user%', ['author' => $this->getUser()->getId(), 'user' => $id]); $response = ['message' => 'User saved', 'status' => 'success', 'id' => $id]; } else { $errors = []; $formView = $form->createView(); // @todo clean this, make a recursive foreach ($formView as $inputName => $view) { if ($view->children) { foreach ($view->children as $name => $childView) { if (isset($childView->vars['errors'])) { foreach ($childView->vars['errors'] as $error) { $errors[$childView->vars['full_name']] = $error->getMessage(); } } } } if (isset($view->vars['errors'])) { foreach ($view->vars['errors'] as $error) { $errors[$view->vars['full_name']] = $error->getMessage(); } } } $response['error'] = ['type' => 'validation', 'messages' => $errors]; } return new JsonResponse($response); }