33
33
import org .silverpeas .core .admin .user .model .UserFull ;
34
34
import org .silverpeas .core .annotation .WebService ;
35
35
import org .silverpeas .core .security .authentication .AuthenticationCredential ;
36
+ import org .silverpeas .core .security .authentication .AuthenticationResponse ;
36
37
import org .silverpeas .core .security .authentication .AuthenticationServiceProvider ;
37
- import org .silverpeas .core .security .authentication .exception .AuthenticationPasswordExpired ;
38
- import org .silverpeas .core .security .authentication .exception .AuthenticationPasswordMustBeChangedAtNextLogon ;
39
- import org .silverpeas .core .security .authentication .exception .AuthenticationPasswordMustBeChangedOnFirstLogin ;
40
- import org .silverpeas .core .security .authentication .exception .AuthenticationPwdNotAvailException ;
41
- import org .silverpeas .core .security .authentication .exception .AuthenticationUserAccountBlockedException ;
42
- import org .silverpeas .core .security .authentication .exception .AuthenticationUserAccountDeactivatedException ;
38
+ import org .silverpeas .core .security .authentication .exception .AuthenticationException ;
43
39
import org .silverpeas .core .web .chat .listeners .ChatUserAuthenticationListener ;
44
40
import org .silverpeas .core .web .rs .UserPrivilegeValidation ;
45
41
import org .silverpeas .mobile .server .helpers .DataURLHelper ;
46
42
import org .silverpeas .mobile .server .services .helpers .UserHelper ;
47
43
import org .silverpeas .mobile .shared .dto .DetailUserDTO ;
48
44
import org .silverpeas .mobile .shared .dto .DomainDTO ;
49
- import org .silverpeas .mobile .shared .exceptions .AuthenticationException ;
50
45
import org .silverpeas .mobile .shared .exceptions .AuthenticationException .AuthenticationError ;
51
46
52
47
import javax .inject .Inject ;
@@ -79,7 +74,8 @@ public class ServiceConnection extends AbstractRestWebService {
79
74
@ Context
80
75
HttpServletRequest request ;
81
76
82
- private OrganizationController organizationController = OrganizationController .get ();
77
+ @ Inject
78
+ private OrganizationController organizationController ;
83
79
84
80
static final String PATH = "mobile/connection" ;
85
81
@@ -98,27 +94,37 @@ public DetailUserDTO login(List<String> ids) {
98
94
String domainId = ids .get (2 );
99
95
100
96
// vérification
101
- AuthenticationCredential credential =
102
- AuthenticationCredential .newWithAsLogin (login ).withAsPassword (password )
103
- .withAsDomainId (domainId );
104
- String key = AuthenticationServiceProvider .getService ().authenticate (credential );
105
- //SilverLogger.getLogger(this).debug("mobile authentification : {0} {1}", login, key);
106
- if (key == null || key .startsWith ("Error_" )) {
107
- if (key .equals ("Error_5" )) {
108
- throw new WebApplicationException (AuthenticationError .PwdNotAvailable .name ());
109
- } else if (key .equals ("Error_PwdExpired" )) {
110
- throw new WebApplicationException (AuthenticationError .PwdExpired .name ());
111
- } else if (key .equals ("Error_PwdMustBeChanged" )) {
112
- throw new WebApplicationException (AuthenticationError .PwdMustBeChanged .name ());
113
- } else if (key .equals ("Error_PwdMustBeChangedOnFirstLogin" )) {
114
- throw new WebApplicationException (AuthenticationError .PwdMustBeChangedOnFirstLogin .name ());
115
- } else if (key .equals ("Error_UserAccountBlocked" )) {
116
- throw new WebApplicationException (AuthenticationError .UserAccountBlocked .name ());
117
- } else if (key .equals ("Error_UserAccountDeactivated" )) {
118
- throw new WebApplicationException (AuthenticationError .UserAccountDeactivated .name ());
119
- } else {
120
- throw new WebApplicationException (AuthenticationError .BadCredential .name ());
97
+ AuthenticationCredential credential = getCredentials (login , password , domainId );
98
+ AuthenticationResponse result =
99
+ AuthenticationServiceProvider .getService ().authenticate (credential );
100
+ if (result == null || result .getStatus ().isInError ()) {
101
+ AuthenticationResponse .Status status =
102
+ result == null ? AuthenticationResponse .Status .BAD_LOGIN_PASSWORD : result .getStatus ();
103
+ WebApplicationException e ;
104
+ switch (status ) {
105
+ case NO_PASSWORD :
106
+ e = new WebApplicationException (AuthenticationError .PwdNotAvailable .name ());
107
+ break ;
108
+ case PASSWORD_EXPIRED :
109
+ e = new WebApplicationException (AuthenticationError .PwdExpired .name ());
110
+ break ;
111
+ case PASSWORD_TO_CHANGE :
112
+ e = new WebApplicationException (AuthenticationError .PwdMustBeChanged .name ());
113
+ break ;
114
+ case PASSWORD_EMAIL_TO_CHANGE_ON_FIRST_LOGIN :
115
+ e = new WebApplicationException (AuthenticationError .PwdMustBeChangedOnFirstLogin .name ());
116
+ break ;
117
+ case USER_ACCOUNT_BLOCKED :
118
+ e = new WebApplicationException (AuthenticationError .UserAccountBlocked .name ());
119
+ break ;
120
+ case USER_ACCOUNT_DEACTIVATED :
121
+ e = new WebApplicationException (AuthenticationError .UserAccountDeactivated .name ());
122
+ break ;
123
+ default :
124
+ e = new WebApplicationException (AuthenticationError .BadCredential .name ());
125
+ break ;
121
126
}
127
+ throw e ;
122
128
}
123
129
124
130
// récupération des informations de l'utilisateur
@@ -137,10 +143,10 @@ public DetailUserDTO login(List<String> ids) {
137
143
throw new WebApplicationException (AuthenticationError .CanCreateMainSessionController .name ());
138
144
}
139
145
140
- DetailUserDTO userDTO = new DetailUserDTO ();
141
- userDTO = UserHelper .getInstance ().populate (user );
146
+ DetailUserDTO userDTO = UserHelper .getInstance ().populate (user );
142
147
143
- String avatar = DataURLHelper .convertAvatarToUrlData (user .getAvatarFileName (), getSettings ().getString ("big.avatar.size" , "40x" ));
148
+ String avatar = DataURLHelper .convertAvatarToUrlData (user .getAvatarFileName (),
149
+ getSettings ().getString ("big.avatar.size" , "40x" ));
144
150
userDTO .setAvatar (avatar );
145
151
try {
146
152
userDTO .setStatus (new ServiceRSE ().getStatus ().getDescription ());
@@ -157,10 +163,11 @@ public DetailUserDTO login(List<String> ids) {
157
163
@ GET
158
164
@ Produces (MediaType .APPLICATION_JSON )
159
165
@ Path ("userExist/{login}/{domainId}" )
160
- public Boolean userExist (@ PathParam ("login" ) String login , @ PathParam ("domainId" ) String domainId ) {
166
+ public Boolean userExist (@ PathParam ("login" ) String login ,
167
+ @ PathParam ("domainId" ) String domainId ) {
161
168
try {
162
169
String id = getUserId (login , domainId );
163
- return !( id == null ) ;
170
+ return id != null ;
164
171
} catch (Exception e ) {
165
172
return false ;
166
173
}
@@ -171,7 +178,7 @@ public Boolean userExist(@PathParam("login") String login, @PathParam("domainId"
171
178
@ Path ("setTabletMode" )
172
179
public Boolean setTabletMode () {
173
180
if (!isUserGUIMobileForTablets ()) {
174
- request .getSession ().setAttribute ("tablet" , Boolean .valueOf ( true ) );
181
+ request .getSession ().setAttribute ("tablet" , Boolean .TRUE );
175
182
return true ;
176
183
}
177
184
return false ;
@@ -189,7 +196,7 @@ public List<DomainDTO> getDomains() {
189
196
return domains ;
190
197
}
191
198
192
- private String getUserId (String login , String domainId ) throws Exception {
199
+ private String getUserId (String login , String domainId ) throws AdminException {
193
200
return Administration .get ().getUserIdByLoginAndDomain (login , domainId );
194
201
}
195
202
@@ -209,14 +216,16 @@ private DomainDTO populate(Domain domain) {
209
216
@ Produces (MediaType .APPLICATION_JSON )
210
217
@ Path ("changePwd/" )
211
218
public void changePwd (String newPwd ) {
212
- if (getUserInSession () == null ) throw new NotAuthorizedException (getHttpServletResponse ());
213
- UserFull user = null ;
219
+ if (getUserInSession () == null ) {
220
+ throw new NotAuthorizedException (getHttpServletResponse ());
221
+ }
222
+ UserFull user ;
214
223
try {
215
224
user = Administration .get ().getUserFull (getUserInSession ().getId ());
216
225
user .setPassword (newPwd );
217
226
Administration .get ().updateUserFull (user );
218
227
} catch (AdminException e ) {
219
- throw new WebApplicationException (e );
228
+ throw new WebApplicationException (e );
220
229
}
221
230
}
222
231
@@ -236,7 +245,8 @@ protected void setUserInSession(UserDetail user) {
236
245
}
237
246
238
247
protected UserDetail getUserInSession () {
239
- return (UserDetail ) request .getSession ().getAttribute (AbstractAuthenticateService .USER_ATTRIBUT_NAME );
248
+ return (UserDetail ) request .getSession ()
249
+ .getAttribute (AbstractAuthenticateService .USER_ATTRIBUT_NAME );
240
250
}
241
251
242
252
@ Override
@@ -251,5 +261,16 @@ public String getComponentId() {
251
261
252
262
@ Override
253
263
public void validateUserAuthorization (final UserPrivilegeValidation validation ) {
264
+ // no need to validate the authorization
265
+ }
266
+
267
+ private AuthenticationCredential getCredentials (String login , String password , String domainId ) {
268
+ try {
269
+ return AuthenticationCredential .newWithAsLogin (login )
270
+ .withAsPassword (password )
271
+ .withAsDomainId (domainId );
272
+ } catch (AuthenticationException e ) {
273
+ throw new WebApplicationException (AuthenticationError .BadCredential .name ());
274
+ }
254
275
}
255
276
}
0 commit comments