Skip to content

Extracting logic to a method in UserManipulator #1247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from Sep 10, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 26 additions & 35 deletions Util/UserManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,7 @@ public function create($username, $password, $email, $active, $superadmin)
*/
public function activate($username)
{
$user = $this->userManager->findUserByUsername($username);

if (!$user) {
throw new \InvalidArgumentException(sprintf('User identified by "%s" username does not exist.', $username));
}
$user = $this->findUserByUsernameOrThrowException($username);
$user->setEnabled(true);
$this->userManager->updateUser($user);
}
Expand All @@ -80,11 +76,7 @@ public function activate($username)
*/
public function deactivate($username)
{
$user = $this->userManager->findUserByUsername($username);

if (!$user) {
throw new \InvalidArgumentException(sprintf('User identified by "%s" username does not exist.', $username));
}
$user = $this->findUserByUsernameOrThrowException($username);
$user->setEnabled(false);
$this->userManager->updateUser($user);
}
Expand All @@ -97,11 +89,7 @@ public function deactivate($username)
*/
public function changePassword($username, $password)
{
$user = $this->userManager->findUserByUsername($username);

if (!$user) {
throw new \InvalidArgumentException(sprintf('User identified by "%s" username does not exist.', $username));
}
$user = $this->findUserByUsernameOrThrowException($username);
$user->setPlainPassword($password);
$this->userManager->updateUser($user);
}
Expand All @@ -113,11 +101,7 @@ public function changePassword($username, $password)
*/
public function promote($username)
{
$user = $this->userManager->findUserByUsername($username);

if (!$user) {
throw new \InvalidArgumentException(sprintf('User identified by "%s" username does not exist.', $username));
}
$user = $this->findUserByUsernameOrThrowException($username);
$user->setSuperAdmin(true);
$this->userManager->updateUser($user);
}
Expand All @@ -129,11 +113,7 @@ public function promote($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 = $this->findUserByUsernameOrThrowException($username);
$user->setSuperAdmin(false);
$this->userManager->updateUser($user);
}
Expand All @@ -148,11 +128,7 @@ public function demote($username)
*/
public function addRole($username, $role)
{
$user = $this->userManager->findUserByUsername($username);

if (!$user) {
throw new \InvalidArgumentException(sprintf('User identified by "%s" username does not exist.', $username));
}
$user = $this->findUserByUsernameOrThrowException($username);
if ($user->hasRole($role)) {
return false;
}
Expand All @@ -161,6 +137,7 @@ public function addRole($username, $role)

return true;
}

/**
* Removes role from the given user.
*
Expand All @@ -171,11 +148,7 @@ public function addRole($username, $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));
}
$user = $this->findUserByUsernameOrThrowException($username);
if (!$user->hasRole($role)) {
return false;
}
Expand All @@ -184,4 +157,22 @@ public function removeRole($username, $role)

return true;
}

/**
* Finds a user by his username and throws an exception if we can't find it.
*
* @param string $username
*
* @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;
}
}