UserManipulator.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /*
  3. * This file is part of the FOSUserBundle package.
  4. *
  5. * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace FOS\UserBundle\Util;
  11. use FOS\UserBundle\Model\UserManagerInterface;
  12. /**
  13. * Executes some manipulations on the users
  14. *
  15. * @author Christophe Coevoet <stof@notk.org>
  16. * @author Luis Cordova <cordoval@gmail.com>
  17. */
  18. class UserManipulator
  19. {
  20. /**
  21. * User manager
  22. *
  23. * @var UserManagerInterface
  24. */
  25. private $userManager;
  26. public function __construct(UserManagerInterface $userManager)
  27. {
  28. $this->userManager = $userManager;
  29. }
  30. /**
  31. * Creates a user and returns it.
  32. *
  33. * @param string $username
  34. * @param string $password
  35. * @param string $email
  36. * @param Boolean $active
  37. * @param Boolean $superadmin
  38. *
  39. * @return \FOS\UserBundle\Model\UserInterface
  40. */
  41. public function create($username, $password, $email, $active, $superadmin)
  42. {
  43. $user = $this->userManager->createUser();
  44. $user->setUsername($username);
  45. $user->setEmail($email);
  46. $user->setPlainPassword($password);
  47. $user->setEnabled((Boolean) $active);
  48. $user->setSuperAdmin((Boolean) $superadmin);
  49. $this->userManager->updateUser($user);
  50. return $user;
  51. }
  52. /**
  53. * Activates the given user.
  54. *
  55. * @param string $username
  56. */
  57. public function activate($username)
  58. {
  59. $user = $this->userManager->findUserByUsername($username);
  60. if (!$user) {
  61. throw new \InvalidArgumentException(sprintf('User identified by "%s" username does not exist.', $username));
  62. }
  63. $user->setEnabled(true);
  64. $this->userManager->updateUser($user);
  65. }
  66. /**
  67. * Deactivates the given user.
  68. *
  69. * @param string $username
  70. */
  71. public function deactivate($username)
  72. {
  73. $user = $this->userManager->findUserByUsername($username);
  74. if (!$user) {
  75. throw new \InvalidArgumentException(sprintf('User identified by "%s" username does not exist.', $username));
  76. }
  77. $user->setEnabled(false);
  78. $this->userManager->updateUser($user);
  79. }
  80. /**
  81. * Changes the password for the given user.
  82. *
  83. * @param string $username
  84. * @param string $password
  85. */
  86. public function changePassword($username, $password)
  87. {
  88. $user = $this->userManager->findUserByUsername($username);
  89. if (!$user) {
  90. throw new \InvalidArgumentException(sprintf('User identified by "%s" username does not exist.', $username));
  91. }
  92. $user->setPlainPassword($password);
  93. $this->userManager->updateUser($user);
  94. }
  95. /**
  96. * Promotes the given user.
  97. *
  98. * @param string $username
  99. */
  100. public function promote($username)
  101. {
  102. $user = $this->userManager->findUserByUsername($username);
  103. if (!$user) {
  104. throw new \InvalidArgumentException(sprintf('User identified by "%s" username does not exist.', $username));
  105. }
  106. $user->setSuperAdmin(true);
  107. $this->userManager->updateUser($user);
  108. }
  109. /**
  110. * Demotes the given user.
  111. *
  112. * @param string $username
  113. */
  114. public function demote($username)
  115. {
  116. $user = $this->userManager->findUserByUsername($username);
  117. if (!$user) {
  118. throw new \InvalidArgumentException(sprintf('User identified by "%s" username does not exist.', $username));
  119. }
  120. $user->setSuperAdmin(false);
  121. $this->userManager->updateUser($user);
  122. }
  123. /**
  124. * Adds role to the given user.
  125. *
  126. * @param string $username
  127. * @param string $role
  128. *
  129. * @return Boolean true if role was added, false if user already had the role
  130. */
  131. public function addRole($username, $role)
  132. {
  133. $user = $this->userManager->findUserByUsername($username);
  134. if (!$user) {
  135. throw new \InvalidArgumentException(sprintf('User identified by "%s" username does not exist.', $username));
  136. }
  137. if ($user->hasRole($role)) {
  138. return false;
  139. }
  140. $user->addRole($role);
  141. $this->userManager->updateUser($user);
  142. return true;
  143. }
  144. /**
  145. * Removes role from the given user.
  146. *
  147. * @param string $username
  148. * @param string $role
  149. *
  150. * @return Boolean true if role was removed, false if user didn't have the role
  151. */
  152. public function removeRole($username, $role)
  153. {
  154. $user = $this->userManager->findUserByUsername($username);
  155. if (!$user) {
  156. throw new \InvalidArgumentException(sprintf('User identified by "%s" username does not exist.', $username));
  157. }
  158. if (!$user->hasRole($role)) {
  159. return false;
  160. }
  161. $user->removeRole($role);
  162. $this->userManager->updateUser($user);
  163. return true;
  164. }
  165. }