re base code
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / user / UserBusinessLogic.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.user;
22
23 import fj.data.Either;
24 import org.apache.tinkerpop.gremlin.structure.Edge;
25 import org.openecomp.sdc.be.config.BeEcompErrorManager;
26 import org.openecomp.sdc.be.dao.api.ActionStatus;
27 import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
28 import org.openecomp.sdc.be.dao.titan.TitanGenericDao;
29 import org.openecomp.sdc.be.dao.utils.UserStatusEnum;
30 import org.openecomp.sdc.be.impl.ComponentsUtils;
31 import org.openecomp.sdc.be.model.LifecycleStateEnum;
32 import org.openecomp.sdc.be.model.User;
33 import org.openecomp.sdc.be.model.operations.api.IUserAdminOperation;
34 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
35 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
36 import org.openecomp.sdc.common.api.UserRoleEnum;
37 import org.openecomp.sdc.common.log.wrappers.Logger;
38 import org.openecomp.sdc.common.kpi.api.ASDCKpiApi;
39 import org.openecomp.sdc.exception.ResponseFormat;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.stereotype.Component;
42
43 import javax.annotation.Resource;
44 import java.util.ArrayList;
45 import java.util.HashMap;
46 import java.util.List;
47 import java.util.Map;
48
49 @Component("userBusinessLogic")
50 public class UserBusinessLogic implements IUserBusinessLogic {
51
52     private static final Logger log = Logger.getLogger(UserBusinessLogic.class);
53     private static UserAdminValidator userAdminValidator = UserAdminValidator.getInstance();
54
55     @Resource
56     private IUserAdminOperation userAdminOperation;
57     @Resource
58     private ComponentsUtils componentsUtils;
59     @Autowired
60     private TitanGenericDao titanDao;
61
62     @Override
63     public Either<User, ActionStatus> getUser(String userId, boolean inTransaction) {
64         return userAdminOperation.getUserData(userId, inTransaction);
65     }
66
67     @Override
68     public Either<User, ResponseFormat> createUser(User modifier, User newUser) {
69
70         ResponseFormat responseFormat;
71         String modifierUserId = modifier.getUserId();
72
73         if (modifierUserId == null) {
74             modifier.setUserId("UNKNOWN");
75             log.debug("createUser method -  user header is missing");
76             responseFormat = componentsUtils.getResponseFormat(ActionStatus.MISSING_INFORMATION);
77             handleAuditing(modifier, null, null, responseFormat, AuditingActionEnum.ADD_USER);
78             return Either.right(responseFormat);
79         }
80
81         Either<User, ActionStatus> eitherCreator = getUser(modifierUserId, false);
82         if (eitherCreator.isRight() || eitherCreator.left().value() == null) {
83             log.debug("createUser method - user is not listed. userId = {}", modifier.getUserId());
84             responseFormat = componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION);
85             handleAuditing(modifier, null, null, responseFormat, AuditingActionEnum.ADD_USER);
86             return Either.right(responseFormat);
87         }
88
89         modifier = eitherCreator.left().value();
90         if (!modifier.getRole().equals(UserRoleEnum.ADMIN.getName())) {
91             log.debug("createUser method - user is not admin = {}", modifier.getUserId());
92             responseFormat = componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION);
93             handleAuditing(modifier, null, null, responseFormat, AuditingActionEnum.ADD_USER);
94             return Either.right(responseFormat);
95         }
96
97         // verify user not exist
98         User userFromDb = new User();
99         Either<User, ActionStatus> eitherUserInDB = getUser(newUser.getUserId(), false);
100         if (eitherUserInDB.isRight()) {
101             ActionStatus status = eitherUserInDB.right().value();
102             if (!ActionStatus.USER_NOT_FOUND.equals(status) && !ActionStatus.USER_INACTIVE.equals(status)) {
103                 responseFormat = componentsUtils.getResponseFormat(eitherUserInDB.right().value(), newUser.getUserId());
104                 handleAuditing(modifier, null, null, responseFormat, AuditingActionEnum.ADD_USER);
105                 return Either.right(responseFormat);
106             }
107         } else {// User exist in DB
108             userFromDb = eitherUserInDB.left().value();
109             if (userFromDb.getStatus() == UserStatusEnum.ACTIVE) {
110                 responseFormat = componentsUtils.getResponseFormatByUserId(ActionStatus.USER_ALREADY_EXIST, newUser.getUserId());
111                 log.debug("createUser method - user with id {} already exist with id: {}", modifier.getUserId(), userFromDb.getUserId());
112                 handleAuditing(modifier, null, null, responseFormat, AuditingActionEnum.ADD_USER);
113                 return Either.right(responseFormat);
114             }
115         }
116
117         newUser.setStatus(UserStatusEnum.ACTIVE);
118
119         // validate Email
120         if (newUser.getEmail() != null && !userAdminValidator.validateEmail(newUser.getEmail())) {
121             log.debug("createUser method - user has invalid email = {}", modifier.getUserId());
122             responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_EMAIL_ADDRESS, newUser.getEmail());
123             handleAuditing(modifier, null, null, responseFormat, AuditingActionEnum.ADD_USER);
124             return Either.right(responseFormat);
125         }
126
127         // validate Role
128         if (newUser.getRole() == null || newUser.getRole().length() == 0) {
129             newUser.setRole(Role.DESIGNER.name());
130         } else {
131             if (!userAdminValidator.validateRole(newUser.getRole())) {
132                 log.debug("createUser method - user has invalid role = {}", modifier.getUserId());
133                 responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_ROLE, newUser.getRole());
134                 handleAuditing(modifier, null, null, responseFormat, AuditingActionEnum.ADD_USER);
135                 return Either.right(responseFormat);
136             }
137         }
138
139         // handle last login if user is import
140         if (newUser.getLastLoginTime() == null) {
141             newUser.setLastLoginTime(0L);
142         }
143
144         Either<User, StorageOperationStatus> addOrUpdateUserReq;
145
146         if (ActionStatus.USER_INACTIVE.equals(eitherUserInDB.right().value())) { // user
147                                                                                     // exist
148                                                                                     // with
149                                                                                     // inactive
150                                                                                     // state
151                                                                                     // -
152                                                                                     // update
153                                                                                     // user
154                                                                                     // data
155             newUser.setLastLoginTime(0L);
156             addOrUpdateUserReq = userAdminOperation.updateUserData(newUser);
157
158         } else { // user not exist - create new user
159
160             if (newUser.getUserId() != null && !userAdminValidator.validateUserId(newUser.getUserId())) {
161                 log.debug("createUser method - user has invalid userId = {}", modifier.getUserId());
162                 responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_USER_ID, newUser.getUserId());
163                 handleAuditing(modifier, null, null, responseFormat, AuditingActionEnum.ADD_USER);
164                 return Either.right(responseFormat);
165             }
166             addOrUpdateUserReq = userAdminOperation.saveUserData(newUser);
167         }
168
169         if (addOrUpdateUserReq.isRight() || addOrUpdateUserReq.left().value() == null) {
170             log.debug("createUser method - failed to create user");
171             Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(addOrUpdateUserReq.right().value())));
172         }
173         log.debug("createUser method - user created");
174         User createdUser = addOrUpdateUserReq.left().value();
175         responseFormat = componentsUtils.getResponseFormat(ActionStatus.CREATED);
176         handleAuditing(modifier, null, createdUser, responseFormat, AuditingActionEnum.ADD_USER);
177         return Either.left(createdUser);
178     }
179
180     @Override
181     public Either<User, ResponseFormat> updateUserRole(User modifier, String userIdToUpdate, String userRole) {
182
183         ResponseFormat responseFormat;
184         String modifierUserId = modifier.getUserId();
185
186         if (modifierUserId == null) {
187             modifier.setUserId("UNKNOWN");
188             log.debug("updateUserRole method -  user header is missing");
189             responseFormat = componentsUtils.getResponseFormat(ActionStatus.MISSING_INFORMATION);
190             handleAuditing(modifier, null, null, responseFormat, AuditingActionEnum.UPDATE_USER);
191             return Either.right(responseFormat);
192         }
193
194         Either<User, ActionStatus> eitherCreator = getUser(modifierUserId, false);
195         if (eitherCreator.isRight() || eitherCreator.left().value() == null) {
196             log.debug("updateUserRole method - user is not listed. userId = {}", modifier.getUserId());
197             responseFormat = componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION);
198             handleAuditing(modifier, null, null, responseFormat, AuditingActionEnum.UPDATE_USER);
199             return Either.right(responseFormat);
200         }
201
202         modifier = eitherCreator.left().value();
203         if (!modifier.getRole().equals(UserRoleEnum.ADMIN.getName())) {
204             log.debug("updateUserRole method - user is not admin. userId = {}", modifier.getUserId());
205             responseFormat = componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION);
206             handleAuditing(modifier, null, null, responseFormat, AuditingActionEnum.UPDATE_USER);
207             return Either.right(responseFormat);
208         }
209
210         if (modifier.getUserId().equals(userIdToUpdate)) {
211             log.debug("updateUserRole method - admin role can only be updated by other admin. userId = {}", modifier.getUserId());
212             responseFormat = componentsUtils.getResponseFormat(ActionStatus.UPDATE_USER_ADMIN_CONFLICT);
213             handleAuditing(modifier, null, null, responseFormat, AuditingActionEnum.UPDATE_USER);
214             return Either.right(responseFormat);
215         }
216
217         Either<User, ActionStatus> userToUpdateReq = getUser(userIdToUpdate, false);
218         if (userToUpdateReq.isRight() || userToUpdateReq.left().value() == null) {
219             log.debug("updateUserRole method - user not found. userId = {}", modifier.getUserId());
220             responseFormat = componentsUtils.getResponseFormat(ActionStatus.USER_NOT_FOUND, userIdToUpdate);
221             handleAuditing(modifier, null, null, responseFormat, AuditingActionEnum.UPDATE_USER);
222             return Either.right(responseFormat);
223         }
224
225         if (!userAdminValidator.validateRole(userRole)) {
226             log.debug("updateUserRole method - user has invalid role = {}", modifier.getUserId());
227             responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_ROLE, userRole);
228             handleAuditing(modifier, null, null, responseFormat, AuditingActionEnum.UPDATE_USER);
229             return Either.right(responseFormat);
230         }
231
232         User newUser = new User();
233         newUser.setRole(userRole);
234         newUser.setUserId(userIdToUpdate);
235         User userToUpdate = userToUpdateReq.left().value();
236         // if(!userRole.equals(UserRoleEnum.ADMIN.getName())){ //this is in
237         // comment until admin will be able to do do check-in/check-out from the
238         // UI
239
240         Either<List<Edge>, StorageOperationStatus> userPendingTasksReq = getPendingUserPendingTasksWithCommit(userToUpdate);
241         if (userPendingTasksReq.isRight()) {
242             log.debug("updateUserRole method - failed to get user pending tasks list userId {}", userIdToUpdate);
243             return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(userPendingTasksReq.right().value())));
244         }
245
246         List<Edge> userPendingTasks = userPendingTasksReq.left().value();
247         if (!userPendingTasks.isEmpty()) {
248             log.debug("updateUserRole method - User canot be updated, user have pending projects userId {}", userIdToUpdate);
249
250             String userTasksStatusForErrorMessage = getUserPendingTaskStatusByRole(UserRoleEnum.valueOf(userToUpdate.getRole()));
251             String userInfo = userToUpdate.getFirstName() + " " + userToUpdate.getLastName() + '(' + userToUpdate.getUserId() + ')';
252             responseFormat = componentsUtils.getResponseFormat(ActionStatus.CANNOT_UPDATE_USER_WITH_ACTIVE_ELEMENTS, userInfo, userTasksStatusForErrorMessage);
253             handleAuditing(modifier, userToUpdate, userToUpdate, responseFormat, AuditingActionEnum.UPDATE_USER);
254             return Either.right(responseFormat);
255         }
256         // }
257         Either<User, StorageOperationStatus> updateUserReq = userAdminOperation.updateUserData(newUser);
258
259         if (updateUserReq.isRight() || updateUserReq.left().value() == null) {
260             log.debug("updateUser method - failed to update user data. userId = {}", modifier.getUserId());
261             return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(updateUserReq.right().value())));
262         }
263
264         responseFormat = componentsUtils.getResponseFormat(ActionStatus.OK);
265         User updatedUser = updateUserReq.left().value();
266         handleAuditing(modifier, userToUpdate, updatedUser, responseFormat, AuditingActionEnum.UPDATE_USER);
267         return Either.left(updatedUser);
268     }
269
270     public Either<List<User>, ResponseFormat> getAllAdminUsers() {
271         Either<List<User>, ActionStatus> response = userAdminOperation.getAllUsersWithRole(Role.ADMIN.name(), null);
272
273         if (response.isRight()) {
274             ResponseFormat responseFormat = componentsUtils.getResponseFormat(response.right().value());
275             return Either.right(responseFormat);
276         }
277         return Either.left(response.left().value());
278     }
279
280     @Override
281     public Either<List<User>, ResponseFormat> getUsersList(String modifierAttId, List<String> roles, String rolesStr) {
282         ResponseFormat responseFormat;
283         User user = new User();
284         if (modifierAttId == null) {
285             user.setUserId("UNKNOWN");
286             responseFormat = componentsUtils.getResponseFormat(ActionStatus.MISSING_INFORMATION);
287             handleGetUsersListAuditing(user, responseFormat, rolesStr);
288             return Either.right(responseFormat);
289         }
290         Either<User, ActionStatus> userResult = getUser(modifierAttId, false);
291         if (userResult.isRight()) {
292             user.setUserId(modifierAttId);
293             if (userResult.right().value().equals(ActionStatus.USER_NOT_FOUND)) {
294                 responseFormat = componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION);
295             } else {
296                 responseFormat = componentsUtils.getResponseFormat(userResult.right().value());
297             }
298             BeEcompErrorManager.getInstance().logBeUserMissingError("Get users per roles", modifierAttId);
299
300             handleGetUsersListAuditing(user, responseFormat, rolesStr);
301             return Either.right(responseFormat);
302         }
303         user = userResult.left().value();
304         Either<List<User>, ResponseFormat> getResponse = null;
305         List<User> resultList = new ArrayList<>();
306         if (roles != null && !roles.isEmpty()) {
307             for (String role : roles) {
308                 if (!userAdminValidator.validateRole(role)) {
309                     responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_ROLE, role);
310                     handleGetUsersListAuditing(user, responseFormat, rolesStr);
311                     return Either.right(responseFormat);
312                 }
313                 getResponse = getUsersPerRole(role, user, rolesStr);
314                 resultList.addAll(getResponse.left().value());
315             }
316         } else {
317             rolesStr = "All";
318             getResponse = getUsersPerRole(null, user, rolesStr);
319             resultList.addAll(getResponse.left().value());
320         }
321         responseFormat = componentsUtils.getResponseFormat(ActionStatus.OK);
322         handleGetUsersListAuditing(user, responseFormat, rolesStr);
323         return Either.left(resultList);
324     }
325
326     private Either<List<User>, ResponseFormat> getUsersPerRole(String role, User user, String rolesStr) {
327         ResponseFormat responseFormat;
328         Either<List<User>, ActionStatus> response = userAdminOperation.getAllUsersWithRole(role, UserStatusEnum.ACTIVE.name());
329         if (response.isRight()) {
330             responseFormat = componentsUtils.getResponseFormat(response.right().value());
331             handleGetUsersListAuditing(user, responseFormat, rolesStr);
332             return Either.right(responseFormat);
333         }
334         return Either.left(response.left().value());
335     }
336
337     private void handleGetUsersListAuditing(User user, ResponseFormat responseFormat, String details) {
338         componentsUtils.auditGetUsersList(user, details, responseFormat);
339     }
340
341     private void handleAuditing(User modifier, User userBefor, User userAfter, ResponseFormat responseFormat, AuditingActionEnum actionName) {
342         componentsUtils.auditAdminUserAction(actionName, modifier, userBefor, userAfter, responseFormat);
343     }
344
345     private void handleUserAccessAuditing(User user, ResponseFormat responseFormat) {
346         componentsUtils.auditUserAccess(user, responseFormat);
347     }
348
349     @Override
350     public Either<User, ResponseFormat> deActivateUser(User modifier, String userUniuqeIdToDeactive) {
351
352         ResponseFormat responseFormat;
353         String userId = modifier.getUserId();
354
355         if (userId == null) {
356             modifier.setUserId("UNKNOWN");
357             log.debug("deActivateUser method -  user header is missing");
358             responseFormat = componentsUtils.getResponseFormat(ActionStatus.MISSING_INFORMATION);
359             handleAuditing(modifier, null, null, responseFormat, AuditingActionEnum.DELETE_USER);
360             return Either.right(responseFormat);
361         }
362
363         Either<User, ActionStatus> eitherCreator = getUser(userId, false);
364         if (eitherCreator.isRight() || eitherCreator.left().value() == null) {
365             log.debug("deActivateUser method - user is not listed. userId = {}", modifier.getUserId());
366             responseFormat = componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION);
367             handleAuditing(modifier, null, null, responseFormat, AuditingActionEnum.DELETE_USER);
368             return Either.right(responseFormat);
369         }
370
371         modifier = eitherCreator.left().value();
372
373         if (!modifier.getRole().equals(UserRoleEnum.ADMIN.getName())) {
374             log.debug("deActivateUser method - user is not admin. userId = {}", modifier.getUserId());
375             responseFormat = componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION);
376             handleAuditing(modifier, null, null, responseFormat, AuditingActionEnum.DELETE_USER);
377             return Either.right(responseFormat);
378         }
379
380         if (modifier.getUserId().equals(userUniuqeIdToDeactive)) {
381             log.debug("deActivateUser deActivateUser - admin can only be deactivate by other admin. userId = {}", modifier.getUserId());
382             responseFormat = componentsUtils.getResponseFormat(ActionStatus.DELETE_USER_ADMIN_CONFLICT);
383             handleAuditing(modifier, null, null, responseFormat, AuditingActionEnum.DELETE_USER);
384             return Either.right(responseFormat);
385         }
386
387         Either<User, ActionStatus> getUserToDeleteResponse = getUser(userUniuqeIdToDeactive, false);
388         if (getUserToDeleteResponse.isRight() || getUserToDeleteResponse.left().value() == null) {
389             log.debug("deActivateUser method - failed to get user by id {}", userUniuqeIdToDeactive);
390             responseFormat = componentsUtils.getResponseFormat(ActionStatus.USER_NOT_FOUND, userUniuqeIdToDeactive);
391             handleAuditing(modifier, null, null, responseFormat, AuditingActionEnum.DELETE_USER);
392             return Either.right(componentsUtils.getResponseFormat(getUserToDeleteResponse.right().value(), userUniuqeIdToDeactive));
393         }
394
395         User userToDeactivate = getUserToDeleteResponse.left().value();
396         if (userToDeactivate.getStatus().equals(UserStatusEnum.INACTIVE)) {
397             log.debug("deActivateUser method - User already inactive", userUniuqeIdToDeactive);
398             responseFormat = componentsUtils.getResponseFormat(ActionStatus.USER_NOT_FOUND, userUniuqeIdToDeactive);
399             handleAuditing(modifier, null, null, responseFormat, AuditingActionEnum.DELETE_USER);
400             return Either.right(responseFormat);
401         }
402
403         Either<List<Edge>, StorageOperationStatus> userPendingTasksReq = getPendingUserPendingTasksWithCommit(userToDeactivate);
404         if (userPendingTasksReq.isRight()) {
405             log.debug("deActivateUser method - failed to get user pending tasks list", userUniuqeIdToDeactive);
406             return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(userPendingTasksReq.right().value())));
407         }
408
409         List<Edge> userPendingTasks = userPendingTasksReq.left().value();
410         if (userPendingTasks.size() > 0) {
411             log.debug("deActivateUser method - User canot be deleted, user have pending projects", userUniuqeIdToDeactive);
412
413             String userTasksStatusForErrorMessage = getUserPendingTaskStatusByRole(UserRoleEnum.valueOf(userToDeactivate.getRole()));
414             String userInfo = userToDeactivate.getFirstName() + " " + userToDeactivate.getLastName() + '(' + userToDeactivate.getUserId() + ')';
415             responseFormat = componentsUtils.getResponseFormat(ActionStatus.CANNOT_DELETE_USER_WITH_ACTIVE_ELEMENTS, userInfo, userTasksStatusForErrorMessage);
416             handleAuditing(modifier, userToDeactivate, userToDeactivate, responseFormat, AuditingActionEnum.DELETE_USER);
417             return Either.right(responseFormat);
418         }
419
420         Either<User, StorageOperationStatus> deactivateUserReq = userAdminOperation.deActivateUser(userToDeactivate);
421         if (deactivateUserReq.isRight()) {
422             log.debug("deActivateUser method - failed to deactivate user", userUniuqeIdToDeactive);
423             return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(deactivateUserReq.right().value())));
424         }
425         User deactivateUser = deactivateUserReq.left().value();
426         responseFormat = componentsUtils.getResponseFormat(ActionStatus.OK);
427         handleAuditing(modifier, userToDeactivate, null, responseFormat, AuditingActionEnum.DELETE_USER);
428         return Either.left(deactivateUser);
429     }
430
431     @Override
432     public Either<User, ResponseFormat> authorize(User authUser) {
433
434         ResponseFormat responseFormat;
435
436         String userId = authUser.getUserId();
437
438         if (userId == null) {
439             authUser.setUserId("UNKNOWN");
440             log.debug("deActivateUser method -  user header is missing");
441             responseFormat = componentsUtils.getResponseFormat(ActionStatus.MISSING_INFORMATION);
442             handleUserAccessAuditing(authUser, responseFormat);
443             return Either.right(responseFormat);
444         }
445
446         Either<User, ActionStatus> eitherCreator = getUser(userId, false);
447         if (eitherCreator.isRight()) {
448             if (eitherCreator.right().value() == ActionStatus.USER_NOT_FOUND || eitherCreator.right().value() == ActionStatus.USER_INACTIVE) {
449                 responseFormat = componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_ACCESS);
450                 handleUserAccessAuditing(authUser, responseFormat);
451                 return Either.right(responseFormat);
452             } else {
453                 return Either.right(componentsUtils.getResponseFormatByUser(eitherCreator.right().value(), authUser));
454             }
455         } else {
456             if (eitherCreator.left().value() == null) {
457                 responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR);
458                 return Either.right(responseFormat);
459             }
460         }
461
462         User user = eitherCreator.left().value();
463
464         String firstName = authUser.getFirstName();
465         if (firstName != null && !firstName.isEmpty() && !firstName.equals(user.getFirstName())) {
466             user.setFirstName(firstName);
467         }
468
469         String lastName = authUser.getLastName();
470         if (lastName != null && !lastName.isEmpty() && !lastName.equals(user.getLastName())) {
471             user.setLastName(lastName);
472         }
473
474         String email = authUser.getEmail();
475         if (email != null && !email.isEmpty() && !email.equals(user.getEmail())) {
476             user.setEmail(email);
477         }
478
479         // last login time stamp handle
480         user.setLastLoginTime();
481
482         Either<User, StorageOperationStatus> updateUserReq = userAdminOperation.updateUserData(user);
483
484         if (updateUserReq.isRight()) {
485             responseFormat = componentsUtils.getResponseFormatByUser(eitherCreator.right().value(), user);
486             handleUserAccessAuditing(user, responseFormat);
487             return Either.right(componentsUtils.getResponseFormatByUser(eitherCreator.right().value(), user));
488         }
489
490         User updatedUser = updateUserReq.left().value();
491
492         Long lastLoginTime = user.getLastLoginTime();
493         if (lastLoginTime != null) {
494             updatedUser.setLastLoginTime(lastLoginTime);
495         } else {
496             updatedUser.setLastLoginTime(new Long(0));
497         }
498
499         responseFormat = componentsUtils.getResponseFormat(ActionStatus.OK);
500         handleUserAccessAuditing(updatedUser, responseFormat);
501         ASDCKpiApi.countUsersAuthorizations();
502         return Either.left(updatedUser);
503     }
504
505     /*
506      * The method updates user credentials only, the role is neglected The role updated through updateRole method
507      */
508     public Either<User, ResponseFormat> updateUserCredentials(User updatedUserCred) {
509
510         ResponseFormat responseFormat;
511
512         String userId = updatedUserCred.getUserId();
513
514         if (userId == null) {
515             updatedUserCred.setUserId("UNKNOWN");
516             log.debug("updateUserCredentials method - user header is missing");
517             responseFormat = componentsUtils.getResponseFormat(ActionStatus.MISSING_INFORMATION);
518             handleUserAccessAuditing(updatedUserCred, responseFormat);
519             return Either.right(responseFormat);
520         }
521
522         Either<User, ActionStatus> eitherCreator = getUser(userId, false);
523         if (eitherCreator.isRight()) {
524             ActionStatus status = eitherCreator.right().value();
525             if (status == ActionStatus.USER_NOT_FOUND || status == ActionStatus.USER_INACTIVE) {
526                 responseFormat = componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_ACCESS);
527                 handleUserAccessAuditing(updatedUserCred, responseFormat);
528                 return Either.right(responseFormat);
529             } else {
530                 return Either.right(componentsUtils.getResponseFormatByUser(status, updatedUserCred));
531             }
532         } else {
533             if (eitherCreator.left().value() == null) {
534                 responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR);
535                 return Either.right(responseFormat);
536             }
537         }
538
539         User user = eitherCreator.left().value();
540
541         String firstName = updatedUserCred.getFirstName();
542         if (firstName != null && !firstName.isEmpty() && !firstName.equals(user.getFirstName())) {
543             user.setFirstName(firstName);
544         }
545
546         String lastName = updatedUserCred.getLastName();
547         if (lastName != null && !lastName.isEmpty() && !lastName.equals(user.getLastName())) {
548             user.setLastName(lastName);
549         }
550
551         String email = updatedUserCred.getEmail();
552         if (email != null && !email.isEmpty() && !email.equals(user.getEmail())) {
553             user.setEmail(email);
554         }
555
556         if (updatedUserCred.getLastLoginTime() != null && user.getLastLoginTime() != null) {
557             if (updatedUserCred.getLastLoginTime() > user.getLastLoginTime()) {
558                 user.setLastLoginTime(updatedUserCred.getLastLoginTime());
559             }
560         } else if (updatedUserCred.getLastLoginTime() != null && user.getLastLoginTime() == null) {
561             user.setLastLoginTime(updatedUserCred.getLastLoginTime());
562         }
563
564         Either<User, StorageOperationStatus> updateUserReq = userAdminOperation.updateUserData(user);
565
566         if (updateUserReq.isRight()) {
567             responseFormat = componentsUtils.getResponseFormatByUser(eitherCreator.right().value(), user);
568             handleUserAccessAuditing(user, responseFormat);
569             return Either.right(componentsUtils.getResponseFormatByUser(eitherCreator.right().value(), user));
570         }
571
572         User updatedUser = updateUserReq.left().value();
573
574         responseFormat = componentsUtils.getResponseFormat(ActionStatus.OK);
575         handleUserAccessAuditing(updatedUser, responseFormat);
576         return Either.left(updatedUser);
577     }
578
579     private Either<List<Edge>, StorageOperationStatus> getPendingUserPendingTasksWithCommit(User user) {
580
581         Either<List<Edge>, StorageOperationStatus> result = null;
582
583         try {
584             UserRoleEnum userRole = UserRoleEnum.valueOf(user.getRole());
585             Map<String, Object> properties = new HashMap<>();
586             switch (userRole) {
587             case DESIGNER:
588             case PRODUCT_STRATEGIST:
589             case PRODUCT_MANAGER:
590                 properties.put(GraphPropertiesDictionary.STATE.getProperty(), LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name());
591                 return userAdminOperation.getUserPendingTasksList(user, properties);
592             case TESTER:
593                 properties.put(GraphPropertiesDictionary.STATE.getProperty(), LifecycleStateEnum.CERTIFICATION_IN_PROGRESS.name());
594                 return userAdminOperation.getUserPendingTasksList(user, properties);
595             case ADMIN:
596                 properties.put(GraphPropertiesDictionary.STATE.getProperty(), LifecycleStateEnum.CERTIFICATION_IN_PROGRESS.name());
597                 properties.put(GraphPropertiesDictionary.STATE.getProperty(), LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name());
598                 return userAdminOperation.getUserPendingTasksList(user, properties);
599             default:
600                 return Either.left(new ArrayList<>());
601             }
602         } finally {
603             // commit will be perform outside!!!
604             if (result == null || result.isRight()) {
605                 log.debug("getUserPendingTasksList failed to perform fetching");
606                 titanDao.rollback();
607             } else {
608                 titanDao.commit();
609             }
610         }
611     }
612
613     private String getUserPendingTaskStatusByRole(UserRoleEnum role) {
614
615         switch (role) {
616         case DESIGNER:
617         case PRODUCT_STRATEGIST:
618         case PRODUCT_MANAGER:
619             return "checked-out";
620
621         case TESTER:
622             return "in-certification";
623         case ADMIN:
624             return "in-certification/checked-out";
625         default:
626             return "";
627         }
628     }
629 }