3
3
import io .micronaut .http .HttpResponse ;
4
4
import io .micronaut .http .HttpStatus ;
5
5
import io .micronaut .http .annotation .*;
6
+ import io .micronaut .http .exceptions .HttpStatusException ;
6
7
import io .micronaut .security .annotation .Secured ;
7
8
import io .micronaut .security .authentication .Authentication ;
8
9
import io .micronaut .security .rules .SecurityRule ;
@@ -24,22 +25,24 @@ public class UserController {
24
25
private final UserRepo userRepo ;
25
26
private final TenantRepo tenantRepo ;
26
27
private final RoleRepo roleRepo ;
28
+ private final PasswordEncoder passwordEncoder ;
27
29
28
- public UserController (UserRepo userRepo , TenantRepo tenantRepo , RoleRepo roleRepo ) {
30
+ public UserController (UserRepo userRepo , TenantRepo tenantRepo , RoleRepo roleRepo , PasswordEncoder passwordEncoder ) {
29
31
this .userRepo = userRepo ;
30
32
this .tenantRepo = tenantRepo ;
31
33
this .roleRepo = roleRepo ;
34
+ this .passwordEncoder = passwordEncoder ;
32
35
}
33
36
34
37
@ Post
35
- public HttpResponse <? > createUser (@ Body AddUserRequest requestDTO ,
38
+ public HttpResponse <UserResponse > createUser (@ Body AddUserRequest requestDTO ,
36
39
Authentication authentication ) {
37
40
38
41
Long requestTenantId = requestDTO .tenantId ();
39
42
40
43
// reject if the declared tenant does not exist
41
- if (tenantRepo .existsById (requestTenantId )) {
42
- return HttpResponse . notFound ( "Tenant does not exist " );
44
+ if (! tenantRepo .existsById (requestTenantId )) {
45
+ throw new HttpStatusException ( HttpStatus . NOT_FOUND , "Tenant not found " );
43
46
}
44
47
45
48
Role unityAdministrator = roleRepo .findByName ("Unity Administrator" );
@@ -62,7 +65,7 @@ public HttpResponse<?> createUser(@Body AddUserRequest requestDTO,
62
65
63
66
// reject if new user already exists under a tenant
64
67
if (userRepo .existsByEmailAndTenantId (requestDTO .email (), requestTenantId )) {
65
- return HttpResponse . badRequest ( "User already exists under declared tenant. " );
68
+ throw new HttpStatusException ( HttpStatus . BAD_REQUEST , "User already exists" );
66
69
}
67
70
68
71
// if the new user exists, create a new user-role entry
@@ -72,9 +75,9 @@ public HttpResponse<?> createUser(@Body AddUserRequest requestDTO,
72
75
if (userOptional .isEmpty ()) {
73
76
User newUser = new User ();
74
77
newUser .setEmail (requestDTO .email ());
75
- newUser .setPassword (requestDTO .password ());
76
- newUser .setFirstName (requestDTO .firstName );
77
- newUser .setLastName (requestDTO .lastName );
78
+ newUser .setPassword (passwordEncoder . encode ( requestDTO .password () ));
79
+ newUser .setFirstName (requestDTO .firstName () );
80
+ newUser .setLastName (requestDTO .lastName () );
78
81
newUser .setStatus (User .UserStatus .ENABLED );
79
82
user = userRepo .save (newUser );
80
83
} else {
@@ -91,18 +94,18 @@ public HttpResponse<?> createUser(@Body AddUserRequest requestDTO,
91
94
}
92
95
93
96
@ Patch ("{id}/roles" )
94
- public HttpResponse <? > updateUserRoles (@ PathVariable Long id , @ Body UpdateUserRolesRequest requestDTO ,
97
+ public HttpResponse <UserResponse > updateUserRoles (@ PathVariable Long id , @ Body UpdateUserRolesRequest requestDTO ,
95
98
Authentication authentication ) {
96
99
Long requestTenantId = requestDTO .tenantId ();
97
100
98
101
// reject if the declared tenant does not exist
99
- if (tenantRepo .existsById (requestTenantId )) {
100
- return HttpResponse . notFound ( "Tenant does not exist " );
102
+ if (! tenantRepo .existsById (requestTenantId )) {
103
+ throw new HttpStatusException ( HttpStatus . NOT_FOUND , "Tenant not found " );
101
104
}
102
105
103
106
Optional <User > userOptional = userRepo .findById (id );
104
107
if (userOptional .isEmpty ()) {
105
- return HttpResponse . notFound ( "User not found. " );
108
+ throw new HttpStatusException ( HttpStatus . NOT_FOUND , "User not found" );
106
109
}
107
110
108
111
User user = userOptional .get ();
@@ -126,7 +129,6 @@ public HttpResponse<?> updateUserRoles(@PathVariable Long id, @Body UpdateUserRo
126
129
127
130
applyRolesPatch (rolesIntersection , requestTenantId , user .getId ());
128
131
129
- // return updated user
130
132
return HttpResponse .created (new UserResponse (user .getId (),
131
133
user .getEmail (),
132
134
user .getFirstName (),
@@ -150,17 +152,17 @@ public void applyRolesPatch(List<Long> requestRoles, Long requestTenantId, Long
150
152
}
151
153
152
154
@ Patch ("{id}" )
153
- public HttpResponse <? > selfPatch (@ PathVariable Long id , @ Body UpdateSelfRequest requestDTO ,
155
+ public HttpResponse <UserResponse > selfPatch (@ PathVariable Long id , @ Body UpdateSelfRequest requestDTO ,
154
156
Authentication authentication ) {
155
157
156
158
Optional <User > userOptional = userRepo .findByEmail (authentication .getName ());
157
159
if (userOptional .isEmpty ()) {
158
- return HttpResponse . notFound ( "User not found. " );
160
+ throw new HttpStatusException ( HttpStatus . NOT_FOUND , "User not found" );
159
161
}
160
162
161
163
User user = userOptional .get ();
162
164
if (!Objects .equals (user .getId (), id )) {
163
- return HttpResponse . badRequest ( "User id mismatch." );
165
+ throw new HttpStatusException ( HttpStatus . BAD_REQUEST , "User id mismatch." );
164
166
}
165
167
166
168
if (requestDTO .firstName != null ) {
@@ -170,10 +172,10 @@ public HttpResponse<?> selfPatch(@PathVariable Long id, @Body UpdateSelfRequest
170
172
user .setLastName (requestDTO .lastName );
171
173
}
172
174
if (requestDTO .password != null ) {
173
- user .setPassword (requestDTO .password );
175
+ user .setPassword (passwordEncoder . encode ( requestDTO .password ()) );
174
176
}
175
177
176
- User saved = userRepo .save (user );
178
+ User saved = userRepo .update (user );
177
179
return HttpResponse .ok (new UserResponse (saved .getId (), saved .getEmail (), saved .getFirstName (), saved .getLastName (),
178
180
userRepo .getUserRolesByUserId (saved .getId ())));
179
181
}
@@ -196,8 +198,8 @@ public record AddUserRequest(
196
198
197
199
@ Serdeable
198
200
public record UpdateSelfRequest (
199
- @ NotBlank String firstName ,
200
- @ NotBlank String lastName ,
201
- @ NotBlank String password ) {
201
+ @ NullOrNotBlank String firstName ,
202
+ @ NullOrNotBlank String lastName ,
203
+ @ NullOrNotBlank String password ) {
202
204
}
203
205
}
0 commit comments