Find a user by its username
public findUserByUsername ( string $username ) : |
||
$username | string | |
Результат | or null if user does not exist |
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; }
/** * Finds a user by his username and throws an exception if we can't find it. * * @param string $username * * @throws \InvalidArgumentException When user does not exist * * @return UserInterface */ private function findUserByUsernameOrThrowException($username) { $user = $this->userManager->findUserByUsername($username); if (!$user) { throw new \InvalidArgumentException(sprintf('User identified by "%s" username does not exist.', $username)); } return $user; }
/** * {@inheritDoc} */ public function loadUserByUsername($username) { // Compatibility with FOSUserBundle < 2.0 if (class_exists('FOS\\UserBundle\\Form\\Handler\\RegistrationFormHandler')) { return $this->userManager->loadUserByUsername($username); } return $this->userManager->findUserByUsername($username); }
public function getNbUnreadByUsername($username) { $nb = apc_fetch('nbm.' . $username); if (false === $nb) { $user = $this->userManager->findUserByUsername($username); $nb = $this->updateNbUnread($participant); } return $nb; }
/** * Transforms a username string into a UserInterface instance. * * @param string $value Username * * @return UserInterface the corresponding UserInterface instance * * @throws UnexpectedTypeException if the given value is not a string */ public function reverseTransform($value) { if (null === $value || '' === $value) { return null; } if (!is_string($value)) { throw new UnexpectedTypeException($value, 'string'); } return $this->userManager->findUserByUsername($value); }
protected function createModelInstance() { $message = parent::createModelInstance(); if ($to = $this->request->query->get('to')) { if ($recipient = $this->userManager->findUserByUsername($to)) { $message->setRecipient($recipient); } } return $message; }
/** * @param string $un * @param string $pw * @return \FOS\UserBundle\Model\UserInterface|null */ function getUserByUnPw($un, $pw) { $result = null; if ($un && $pw) { $user = $this->_fosUserManager->findUserByUsername($un); if ($user && $this->isPasswordValid($user, $pw)) { $result = $user; } } return $result; }
/** * Returns a specific principal, specified by it's path. * The returned structure should be the exact same as from * getPrincipalsByPrefix. * * @param string $path * @param bool $getObject * * @return array|GroupInterface|UserInterface|void * * @throws Exception */ public function getPrincipalByPath($path, $getObject = false) { $name = str_replace('principals/', '', $path); // get username from path-string, if string contains additional slashes (e.g. admin/calendar-proxy-read) if (!(strpos($name, '/') === false)) { $name = substr($name, 0, strpos($name, '/')); } $user = $this->user_manager->findUserByUsername($name); if ($user === null) { if (!$this->group_manager) { return; } // search in group-manager $group = $this->group_manager->findGroupByName($name); if ($group === null) { return; } if ($getObject === true) { return $group; } return $this->getPrincipalArray($group, true); } if ($getObject === true) { return $user; } return $this->getPrincipalArray($user, true); }
/** * Attempts to get a unique username for the user. * * @param string $name * * @return string Name, or empty string if it failed after all the iterations. */ protected function getUniqueUserName($name) { $i = 0; $testName = $name; do { $user = $this->userManager->findUserByUsername($testName); } while ($user !== null && $i < $this->iterations && ($testName = $name . ++$i)); return $user !== null ? '' : $testName; }
/** * Demotes the given user. * * @param string $username */ public function demote($username) { $user = $this->userManager->findUserByUsername($username); if (!$user) { throw new \InvalidArgumentException(sprintf('User identified by "%s" username does not exist.', $username)); } $user->setSuperAdmin(false); $this->userManager->updateUser($user); }
/** * Deletes an user from the FOSUser system * @param User $user */ public function deleteFOSUser(User $user) { if ($user->getProvider()->getType() !== self::BUILTIN_PROVIDER) { return; } $FOSUser = $this->userManager->findUserByUsername($user->getUsername()); if ($FOSUser !== null) { $this->userManager->deleteUser($FOSUser); } }
/** * Removes the User from the database. * * @param \Hal\Resource $resource * * @throws NotFoundHttpException if no user with the given id could be found. * * @return void */ public function delete(Resource $resource) { $data = $resource->toArray(); $user = $this->userManager->findUserByUsername($data['username']); if (!$user) { $errorMessage = $this->translator->trans('error.user_not_found', array('%username%' => $data['username'])); throw new NotFoundHttpException($errorMessage); } $this->userManager->deleteUser($user); }
function it_load_user_by_username(SamlAuth $auth, UserInterface $user, UserManagerInterface $userManager) { $auth->getAttributes()->shouldBeCalled(); $auth->getUsername()->shouldBeCalled(); $auth->isAuthenticated()->shouldBeCalled(); $user->getUsername()->shouldBeCalled(); $user->getRoles()->shouldBeCalled(); $userManager->findUserByUsername(self::USERNAME)->shouldBeCalled(); $samlUser = new SamlUser('*****@*****.**', ['ROLE_USER'], []); $this->loadUserByUsername('*****@*****.**')->shouldBeLike($samlUser); }
/** * Checks if username and password are valid. (Checked by the FOSUserManager) * Returns. * * @param $username * @param $passwordHash * * @return bool */ public function validateUserPass($username, $passwordHash) { $user = $this->user_manager->findUserByUsername($username); if (is_null($user)) { return false; } if ($passwordHash === $user->getPassword()) { // $this->userLoginAction($user, $passwordHash); return true; } return false; }
/** * Removes role from the given user. * * @param string $username * @param string $role * * @return Boolean true if role was removed, false if user didn't have the role */ public function removeRole($username, $role) { $user = $this->userManager->findUserByUsername($username); if (!$user) { throw new \InvalidArgumentException(sprintf('User identified by "%s" username does not exist.', $username)); } if (!$user->hasRole($role)) { return false; } $user->removeRole($role); $this->userManager->updateUser($user); return true; }
/** * Finds a user by username. * * This method is meant to be an extension point for child classes. * * @param string $username * * @return UserInterface|null */ protected function findUser($username) { return $this->userManager->findUserByUsername($username); }
/** * find username in the user-manager. * * @param string $username * * @return \FOS\UserBundle\Model\UserInterface */ private function getUser($username) { $user = $this->user_manager->findUserByUsername($username); return $user; }
public function findUserBySamlId($samlId) { preg_match('#(?<email>(?<username>[^/]+)@(?<organization>[^/]+))#', $samlId, $matches); return $this->userManager->findUserByUsername($matches['username']); }