| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 | 
							- <?php
 - 
 - /*
 -  * This file is part of the FOSUserBundle package.
 -  *
 -  * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
 -  *
 -  * For the full copyright and license information, please view the LICENSE
 -  * file that was distributed with this source code.
 -  */
 - 
 - namespace FOS\UserBundle\Util;
 - 
 - use FOS\UserBundle\Model\UserManagerInterface;
 - 
 - /**
 -  * Executes some manipulations on the users
 -  *
 -  * @author Christophe Coevoet <stof@notk.org>
 -  * @author Luis Cordova <cordoval@gmail.com>
 -  */
 - class UserManipulator
 - {
 -     /**
 -      * User manager
 -      *
 -      * @var UserManagerInterface
 -      */
 -     private $userManager;
 - 
 -     public function __construct(UserManagerInterface $userManager)
 -     {
 -         $this->userManager = $userManager;
 -     }
 - 
 -     /**
 -      * Creates a user and returns it.
 -      *
 -      * @param string  $username
 -      * @param string  $password
 -      * @param string  $email
 -      * @param Boolean $active
 -      * @param Boolean $superadmin
 -      *
 -      * @return \FOS\UserBundle\Model\UserInterface
 -      */
 -     public function create($username, $password, $email, $active, $superadmin)
 -     {
 -         $user = $this->userManager->createUser();
 -         $user->setUsername($username);
 -         $user->setEmail($email);
 -         $user->setPlainPassword($password);
 -         $user->setEnabled((Boolean) $active);
 -         $user->setSuperAdmin((Boolean) $superadmin);
 -         $this->userManager->updateUser($user);
 - 
 -         return $user;
 -     }
 - 
 -     /**
 -      * Activates the given user.
 -      *
 -      * @param string $username
 -      */
 -     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->setEnabled(true);
 -         $this->userManager->updateUser($user);
 -     }
 - 
 -     /**
 -      * Deactivates the given user.
 -      *
 -      * @param string $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->setEnabled(false);
 -         $this->userManager->updateUser($user);
 -     }
 - 
 -     /**
 -      * Changes the password for the given user.
 -      *
 -      * @param string $username
 -      * @param string $password
 -      */
 -     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->setPlainPassword($password);
 -         $this->userManager->updateUser($user);
 -     }
 - 
 -     /**
 -      * Promotes the given user.
 -      *
 -      * @param string $username
 -      */
 -     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->setSuperAdmin(true);
 -         $this->userManager->updateUser($user);
 -     }
 - 
 -     /**
 -      * 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);
 -     }
 - 
 -     /**
 -      * Adds role to the given user.
 -      *
 -      * @param string $username
 -      * @param string $role
 -      *
 -      * @return Boolean true if role was added, false if user already had the role
 -      */
 -     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));
 -         }
 -         if ($user->hasRole($role)) {
 -             return false;
 -         }
 -         $user->addRole($role);
 -         $this->userManager->updateUser($user);
 - 
 -         return true;
 -     }
 -     /**
 -      * 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;
 -     }
 - }
 
 
  |