public function __invoke(Request $request) { if ($this->container->hasParameter('partkeepr.auth.allow_password_change') && $this->container->getParameter('partkeepr.auth.allow_password_change') === false) { throw new PasswordChangeNotAllowedException(); } $user = $this->userService->getUser(); if (!$request->request->has('oldpassword') && !$request->request->has('newpassword')) { throw new \Exception('old password and new password need to be specified'); } $FOSUser = $this->userManager->findUserByUsername($user->getUsername()); if ($FOSUser !== null) { $encoder = $this->encoderFactory->getEncoder($FOSUser); $encoded_pass = $encoder->encodePassword($request->request->get('oldpassword'), $FOSUser->getSalt()); if ($FOSUser->getPassword() != $encoded_pass) { throw new OldPasswordWrongException(); } $this->userManipulator->changePassword($user->getUsername(), $request->request->get('newpassword')); } else { if ($user->isLegacy()) { if ($user->getPassword() !== md5($request->request->get('oldpassword'))) { throw new OldPasswordWrongException(); } $user->setNewPassword($request->request->get('newpassword')); $this->userService->syncData($user); } else { throw new \Exception('Cannot change password for LDAP users'); } } $user->setPassword(''); $user->setNewPassword(''); return $user; }
/** * function description * Activation de l'Agent pour sa connexion */ public function ActivateAgentAction($username, $enable) { $userManager = $this->get('fos_user.user_manager'); $userManipulator = new UserManipulator($userManager); if ($enable == 1) { $active = $userManipulator->deactivate($username); } else { $active = $userManipulator->activate($username); } return $this->ShowAgentAction($default = "Default"); }
protected function executeRoleCommand(UserManipulator $manipulator, OutputInterface $output, $username, $super, $role) { if ($super) { $manipulator->promote($username); $output->writeln(sprintf('User "%s" has been promoted as a super administrator.', $username)); } else { if ($manipulator->addRole($username, $role)) { $output->writeln(sprintf('Role "%s" has been added to user "%s".', $role, $username)); } else { $output->writeln(sprintf('User "%s" did already have "%s" role.', $username, $role)); } } }
protected function executeRoleCommand(UserManipulator $manipulator, OutputInterface $output, $username, $super, $role) { if ($super) { $manipulator->demote($username); $output->writeln(sprintf('User "%s" has been demoted as a simple user.', $username)); } else { if ($manipulator->removeRole($username, $role)) { $output->writeln(sprintf('Role "%s" has been removed from user "%s".', $role, $username)); } else { $output->writeln(sprintf('User "%s" didn\'t have "%s" role.', $username, $role)); } } }
/** * * @Route("/StatusUser",name="StatusUser") * * */ public function StatusUsersAction(Request $request) { $enable = $request->get('enable'); $username = $request->get('username'); $userManager = $this->get('fos_user.user_manager'); $userManipulator = new UserManipulator($userManager); if ($enable == 1) { $active = $userManipulator->deactivate($username); } else { $active = $userManipulator->activate($username); } return $this->redirect($this->generateUrl("listusers")); }
/** * {@inheritdoc} */ public function create($username, $password, $email, $active, $superadmin) { $user = parent::create($username, $password, $email, $active, $superadmin); $apiToken = substr($this->tokenGenerator->generateToken(), 0, 20); $user->setApiToken($apiToken); $this->userManager->updateUser($user); return $user; }
/** * @inheritdoc */ public function getUserFromOAuthResponse($providerName, array $data) { $field = $providerName . 'Id'; if ($user = $this->userManager->findUserBy([$field => $data['id']])) { if (isset($data['data']['email'])) { $user->setEmail($data['data']['email']); } return $user; } if (isset($data['data']['email'])) { $user->setEmail($data['data']['email']); } $user = $this->userManipulator->create($data['data']['name'], 'secret', '', true, false); $setter = "set" . ucfirst($providerName) . 'Id'; $user->{$setter}($data['id']); $this->userManager->updateUser($user); return $user; }
/** * Syncronizes the data of the given user with the FOSRestBundle * * @throws \Exception If the password was not set * * @param $user */ public function syncData(User $user) { if ($user->getProvider()->getType() !== self::BUILTIN_PROVIDER) { return; } $FOSUser = $this->userManager->findUserByUsername($user->getUsername()); if ($FOSUser === null) { if ($user->getNewPassword() == "") { throw new \Exception("Password must be set"); } $FOSUser = $this->userManipulator->create($user->getUsername(), $user->getNewPassword(), "", true, false); $user->setLegacy(false); } if ($user->getNewPassword() != "") { $this->userManipulator->changePassword($user->getUsername(), $user->getNewPassword()); } $FOSUser->setEmail($user->getEmail()); $FOSUser->setEnabled($user->isActive()); }
/** * Displays a form to create a new User. * * @Route("/user/new", name="donate_admin_user_new") */ public function newAction(Request $request) { // @since 2.3 we user voters to check authorization instead of being ROLE based if (false === $this->get('security.authorization_checker')->isGranted('create users')) { throw new AccessDeniedException('Unauthorised access!'); } $form = $this->createForm(new AccountType(), new User(), array('roles' => $this->getAvailabledRoles(), 'action' => 'new')); $form->handleRequest($request); if ($form->isValid()) { $data = $form->getData(); $userManager = $this->get('fos_user.user_manager'); if (!$this->userAlreadyExist($userManager, $data->getUsername(), $data->getEmail())) { $userManipulator = new UserManipulator($userManager); $user = $userManipulator->create($data->getUsername(), $data->getPassword(), $data->getEmail(), true, false); $this->get('session')->getFlashBag()->add('notice', "L'utilisateur " . $user->getUsername() . " a été enregistré"); return $this->redirect($this->generateUrl('donate_admin_users')); } } return $this->render('DonateAdminBundle:Account:new.html.twig', ['form' => $form->createView()]); }
/** * @expectedException \InvalidArgumentException */ public function testChangePasswordWithInvalidUsername() { $userManagerMock = $this->createMock('FOS\\UserBundle\\Model\\UserManagerInterface'); $invalidusername = '******'; $password = '******'; $userManagerMock->expects($this->once())->method('findUserByUsername')->will($this->returnValue(null))->with($this->equalTo($invalidusername)); $userManagerMock->expects($this->never())->method('updateUser'); $manipulator = new UserManipulator($userManagerMock); $manipulator->changePassword($invalidusername, $password); }
/** * Displays a form to create a new User. * * @Route("/user/new", name="donate_admin_user_new") * @Security("is_granted('ROLE_ADMIN')") * @since 2.4.7 we use ROLE_ADMIN as User Manager */ public function newAction(Request $request) { $form = $this->createForm(AccountType::class, new User(), array('roles' => $this->getAvailabledRoles(), 'action' => 'new')); $form->handleRequest($request); if ($form->isValid()) { $data = $form->getData(); $userManager = $this->get('fos_user.user_manager'); if (!$this->userAlreadyExist($userManager, $data->getUsername(), $data->getEmail())) { $userManipulator = new UserManipulator($userManager); $user = $userManipulator->create($data->getUsername(), $data->getPassword(), $data->getEmail(), true, false); $this->get('session')->getFlashBag()->add('notice', "L'utilisateur " . $user->getUsername() . " a été enregistré"); return $this->redirect($this->generateUrl('donate_admin_users')); } } return $this->render('DonateAdminBundle:Account:new.html.twig', ['form' => $form->createView()]); }