@@ -64,11 +64,7 @@ public function create($username, $password, $email, $active, $superadmin)
64
64
*/
65
65
public function activate ($ username )
66
66
{
67
- $ user = $ this ->userManager ->findUserByUsername ($ username );
68
-
69
- if (!$ user ) {
70
- throw new \InvalidArgumentException (sprintf ('User identified by "%s" username does not exist. ' , $ username ));
71
- }
67
+ $ user = $ this ->findUserByUsernameOrThrowException ($ username );
72
68
$ user ->setEnabled (true );
73
69
$ this ->userManager ->updateUser ($ user );
74
70
}
@@ -80,11 +76,7 @@ public function activate($username)
80
76
*/
81
77
public function deactivate ($ username )
82
78
{
83
- $ user = $ this ->userManager ->findUserByUsername ($ username );
84
-
85
- if (!$ user ) {
86
- throw new \InvalidArgumentException (sprintf ('User identified by "%s" username does not exist. ' , $ username ));
87
- }
79
+ $ user = $ this ->findUserByUsernameOrThrowException ($ username );
88
80
$ user ->setEnabled (false );
89
81
$ this ->userManager ->updateUser ($ user );
90
82
}
@@ -97,11 +89,7 @@ public function deactivate($username)
97
89
*/
98
90
public function changePassword ($ username , $ password )
99
91
{
100
- $ user = $ this ->userManager ->findUserByUsername ($ username );
101
-
102
- if (!$ user ) {
103
- throw new \InvalidArgumentException (sprintf ('User identified by "%s" username does not exist. ' , $ username ));
104
- }
92
+ $ user = $ this ->findUserByUsernameOrThrowException ($ username );
105
93
$ user ->setPlainPassword ($ password );
106
94
$ this ->userManager ->updateUser ($ user );
107
95
}
@@ -113,11 +101,7 @@ public function changePassword($username, $password)
113
101
*/
114
102
public function promote ($ username )
115
103
{
116
- $ user = $ this ->userManager ->findUserByUsername ($ username );
117
-
118
- if (!$ user ) {
119
- throw new \InvalidArgumentException (sprintf ('User identified by "%s" username does not exist. ' , $ username ));
120
- }
104
+ $ user = $ this ->findUserByUsernameOrThrowException ($ username );
121
105
$ user ->setSuperAdmin (true );
122
106
$ this ->userManager ->updateUser ($ user );
123
107
}
@@ -129,11 +113,7 @@ public function promote($username)
129
113
*/
130
114
public function demote ($ username )
131
115
{
132
- $ user = $ this ->userManager ->findUserByUsername ($ username );
133
-
134
- if (!$ user ) {
135
- throw new \InvalidArgumentException (sprintf ('User identified by "%s" username does not exist. ' , $ username ));
136
- }
116
+ $ user = $ this ->findUserByUsernameOrThrowException ($ username );
137
117
$ user ->setSuperAdmin (false );
138
118
$ this ->userManager ->updateUser ($ user );
139
119
}
@@ -148,11 +128,7 @@ public function demote($username)
148
128
*/
149
129
public function addRole ($ username , $ role )
150
130
{
151
- $ user = $ this ->userManager ->findUserByUsername ($ username );
152
-
153
- if (!$ user ) {
154
- throw new \InvalidArgumentException (sprintf ('User identified by "%s" username does not exist. ' , $ username ));
155
- }
131
+ $ user = $ this ->findUserByUsernameOrThrowException ($ username );
156
132
if ($ user ->hasRole ($ role )) {
157
133
return false ;
158
134
}
@@ -161,6 +137,7 @@ public function addRole($username, $role)
161
137
162
138
return true ;
163
139
}
140
+
164
141
/**
165
142
* Removes role from the given user.
166
143
*
@@ -171,11 +148,7 @@ public function addRole($username, $role)
171
148
*/
172
149
public function removeRole ($ username , $ role )
173
150
{
174
- $ user = $ this ->userManager ->findUserByUsername ($ username );
175
-
176
- if (!$ user ) {
177
- throw new \InvalidArgumentException (sprintf ('User identified by "%s" username does not exist. ' , $ username ));
178
- }
151
+ $ user = $ this ->findUserByUsernameOrThrowException ($ username );
179
152
if (!$ user ->hasRole ($ role )) {
180
153
return false ;
181
154
}
@@ -184,4 +157,22 @@ public function removeRole($username, $role)
184
157
185
158
return true ;
186
159
}
160
+
161
+ /**
162
+ * Finds a user by his username and throws an exception if we can't find it.
163
+ *
164
+ * @param string $username
165
+ *
166
+ * @return UserInterface
167
+ */
168
+ private function findUserByUsernameOrThrowException ($ username )
169
+ {
170
+ $ user = $ this ->userManager ->findUserByUsername ($ username );
171
+
172
+ if (!$ user ) {
173
+ throw new \InvalidArgumentException (sprintf ('User identified by "%s" username does not exist. ' , $ username ));
174
+ }
175
+
176
+ return $ user ;
177
+ }
187
178
}
0 commit comments