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