Sync Integ to Master
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / validation / UserValidations.java
1 package org.openecomp.sdc.be.components.validation;
2
3 import fj.data.Either;
4 import org.apache.commons.lang3.StringUtils;
5 import org.openecomp.sdc.be.config.BeEcompErrorManager;
6 import org.openecomp.sdc.be.dao.api.ActionStatus;
7 import org.openecomp.sdc.be.impl.ComponentsUtils;
8 import org.openecomp.sdc.be.model.User;
9 import org.openecomp.sdc.be.user.IUserBusinessLogic;
10 import org.openecomp.sdc.be.user.Role;
11 import org.openecomp.sdc.common.datastructure.Wrapper;
12 import org.openecomp.sdc.exception.ResponseFormat;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15
16 import java.util.List;
17
18 @org.springframework.stereotype.Component
19 public class UserValidations {
20
21     private static final Logger log = LoggerFactory.getLogger(UserValidations.class);
22     private final IUserBusinessLogic userAdmin;
23     private final ComponentsUtils componentsUtils;
24
25     public UserValidations(IUserBusinessLogic userAdmin, ComponentsUtils componentsUtils) {
26         this.userAdmin = userAdmin;
27         this.componentsUtils = componentsUtils;
28     }
29
30     public Either<User, ResponseFormat> validateUserExists(String userId, String ecompErrorContext, boolean inTransaction) {
31         Either<User, ActionStatus> eitherCreator = userAdmin.getUser(userId, inTransaction);
32         if (eitherCreator.isRight() || eitherCreator.left().value() == null) {
33             ResponseFormat responseFormat;
34             if (eitherCreator.right().value().equals(ActionStatus.USER_NOT_FOUND)) {
35                 if (log.isDebugEnabled()) {
36                     log.debug("validateUserExists - not authorized user, userId {}", userId);
37                 }
38                 responseFormat = componentsUtils.getResponseFormat(ActionStatus.AUTH_FAILED);
39             } else {
40                 if (log.isDebugEnabled()) {
41                     log.debug("validateUserExists - failed to authorize user, userId {}", userId);
42                 }
43                 responseFormat = componentsUtils.getResponseFormat(eitherCreator.right().value());
44             }
45             if (log.isDebugEnabled()) {
46                 log.debug("User is not listed. userId {}", userId);
47             }
48             BeEcompErrorManager.getInstance().logBeUserMissingError(ecompErrorContext, userId);
49             return Either.right(responseFormat);
50         }
51         return Either.left(eitherCreator.left().value());
52     }
53
54     public Either<Boolean, ResponseFormat> validateUserRole(User user, List<Role> roles) {
55         Role userRole = Role.valueOf(user.getRole());
56         if (roles != null) {
57             if (!roles.contains(userRole)) {
58                 if (log.isDebugEnabled()) {
59                     log.debug("user is not in appropriate role to perform action");
60                 }
61                 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION);
62                 return Either.right(responseFormat);
63             }
64             return Either.left(Boolean.TRUE);
65         }
66         return Either.left(Boolean.FALSE);
67     }
68
69     public Either<User, ActionStatus> validateUserExistsActionStatus(String userId, String ecompErrorContext) {
70         Either<User, ActionStatus> eitherCreator = userAdmin.getUser(userId, false);
71         if (eitherCreator.isRight() || eitherCreator.left().value() == null) {
72             if (eitherCreator.right().value().equals(ActionStatus.USER_NOT_FOUND)) {
73                 log.debug("validateUserExists - not authorized user, userId {}", userId);
74                 Either.right(ActionStatus.RESTRICTED_OPERATION);
75             } else {
76                 log.debug("validateUserExists - failed to authorize user, userId {}", userId);
77             }
78             log.debug("User is not listed. userId {}", userId);
79             BeEcompErrorManager.getInstance().logBeUserMissingError(ecompErrorContext, userId);
80             return Either.right(eitherCreator.right().value());
81         }
82         return Either.left(eitherCreator.left().value());
83     }
84
85     public Either<User, ResponseFormat> validateUserNotEmpty(User user, String ecompErrorContext) {
86         String userId = user.getUserId();
87
88         if (StringUtils.isEmpty(userId)) {
89             log.debug("User header is missing ");
90             BeEcompErrorManager.getInstance().logBeUserMissingError(ecompErrorContext, user.getUserId());
91             ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.MISSING_INFORMATION);
92             return Either.right(responseFormat);
93         }
94         return Either.left(user);
95     }
96
97     public Either<User, ResponseFormat> validateUserExists(User user, String ecompErrorContext, boolean inTransaction) {
98         return validateUserExists(user.getUserId(), ecompErrorContext, inTransaction);
99     }
100
101     public void validateUserExist(String userId, String ecompErrorContext, Wrapper<ResponseFormat> errorWrapper) {
102         Either<User, ResponseFormat> resp = validateUserExists(userId, ecompErrorContext, false);
103         if (resp.isRight()) {
104             errorWrapper.setInnerElement(resp.right().value());
105         }
106     }
107
108
109
110 }