26
26
import de .rwth .idsg .steve .service .dto .WebUserOverview ;
27
27
import de .rwth .idsg .steve .web .dto .WebUserForm ;
28
28
import de .rwth .idsg .steve .web .dto .WebUserQueryForm ;
29
- import java .util .ArrayList ;
30
29
import jooq .steve .db .tables .records .WebUserRecord ;
31
30
import lombok .RequiredArgsConstructor ;
32
31
import org .jooq .JSON ;
40
39
import org .springframework .security .core .userdetails .User ;
41
40
import org .springframework .security .core .userdetails .UserDetails ;
42
41
import org .springframework .security .core .userdetails .UsernameNotFoundException ;
42
+ import org .springframework .security .crypto .password .PasswordEncoder ;
43
43
import org .springframework .security .provisioning .JdbcUserDetailsManager ;
44
44
import org .springframework .security .provisioning .UserDetailsManager ;
45
45
import org .springframework .stereotype .Service ;
51
51
import java .util .stream .Collectors ;
52
52
53
53
import static org .springframework .security .authentication .UsernamePasswordAuthenticationToken .authenticated ;
54
- import org .springframework .security .core .authority .SimpleGrantedAuthority ;
55
54
import static org .springframework .security .core .context .SecurityContextHolder .getContextHolderStrategy ;
56
- import org .springframework .security .crypto .password .PasswordEncoder ;
57
55
58
56
/**
59
57
* Inspired by {@link org.springframework.security.provisioning.JdbcUserDetailsManager}
@@ -167,11 +165,10 @@ public void add(WebUserForm form) {
167
165
createUser (toUserDetails (form ));
168
166
}
169
167
170
-
171
168
public void update (WebUserForm form ) {
172
169
updateUser (toUserDetails (form ));
173
170
}
174
-
171
+
175
172
public List <WebUserOverview > getOverview (WebUserQueryForm form ) {
176
173
return webUserRepository .getOverview (form )
177
174
.map (r -> WebUserOverview .builder ()
@@ -191,31 +188,16 @@ public WebUserForm getDetails(Integer webUserPk) {
191
188
throw new SteveException ("There is no user with id '%d'" , webUserPk );
192
189
}
193
190
194
- WebUserForm form = new WebUserForm ();
191
+ String [] authValues = fromJson (ur .getAuthorities ());
192
+ String joinedAuthValues = String .join (", " , authValues );
195
193
194
+ WebUserForm form = new WebUserForm ();
196
195
form .setEnabled (ur .getEnabled ());
197
196
form .setWebUsername (ur .getUsername ());
198
- form .setPassword ("" ); // don't expose the pw
199
- form .setApiToken ("" ); // ur.getApiToken()
200
- form .setAuthorities (rolesStr (fromJson (ur .getAuthorities ())));
201
-
197
+ form .setAuthorities (joinedAuthValues );
202
198
return form ;
203
199
}
204
200
205
- private static String rolesStr (String [] authorities ) {
206
- String roles = "" ;
207
-
208
- for (String ar : authorities ) {
209
- roles = roles + ar + ", " ;
210
- }
211
- roles = roles .strip ();
212
- if (!roles .isBlank ()) { //(roles.endsWith(","))
213
- roles = roles .substring (0 , roles .length () - 1 );
214
- }
215
-
216
- return roles ;
217
- }
218
-
219
201
// Helpers
220
202
private WebUserRecord toWebUserRecord (UserDetails user ) {
221
203
return new WebUserRecord ()
@@ -227,17 +209,16 @@ private WebUserRecord toWebUserRecord(UserDetails user) {
227
209
228
210
private UserDetails toUserDetails (WebUserForm form ) {
229
211
String encPw = "" ;
230
- if (form .getPassword ()!= null ) {
212
+ if (form .getPassword () != null ) {
231
213
//encPw = form.getPassword();
232
214
encPw = encoder .encode (form .getPassword ());
233
215
}
234
- var user = User
235
- .withUsername (form .getWebUsername ())
236
- .password (encPw )
237
- .disabled (!form .getEnabled ())
238
- .authorities (toAuthorities (form .getAuthorities ()))
239
- .build ();
240
- return user ;
216
+ return User
217
+ .withUsername (form .getWebUsername ())
218
+ .password (encPw )
219
+ .disabled (!form .getEnabled ())
220
+ .authorities (form .getAuthorities ().split ("," ))
221
+ .build ();
241
222
}
242
223
243
224
private String [] fromJson (JSON jsonArray ) {
@@ -248,15 +229,6 @@ private String[] fromJson(JSON jsonArray) {
248
229
}
249
230
}
250
231
251
- private Collection <? extends GrantedAuthority > toAuthorities (String authoritiesStr ) {
252
- String [] authoritiesList = authoritiesStr .split ("," );
253
- List <GrantedAuthority > authorities = new ArrayList <>();
254
- for (String authStr : authoritiesList ) {
255
- authorities .add (new SimpleGrantedAuthority (authStr .strip ()));
256
- }
257
- return authorities ;
258
- }
259
-
260
232
private JSON toJson (Collection <? extends GrantedAuthority > authorities ) {
261
233
Collection <String > auths = authorities .stream ()
262
234
.map (GrantedAuthority ::getAuthority )
0 commit comments