1 package org.openecomp.sdc.be.components.validation;
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;
16 import java.util.List;
18 @org.springframework.stereotype.Component
19 public class UserValidations {
21 private static final Logger log = LoggerFactory.getLogger(UserValidations.class);
22 private final IUserBusinessLogic userAdmin;
23 private final ComponentsUtils componentsUtils;
25 public UserValidations(IUserBusinessLogic userAdmin, ComponentsUtils componentsUtils) {
26 this.userAdmin = userAdmin;
27 this.componentsUtils = componentsUtils;
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);
38 responseFormat = componentsUtils.getResponseFormat(ActionStatus.AUTH_FAILED);
40 if (log.isDebugEnabled()) {
41 log.debug("validateUserExists - failed to authorize user, userId {}", userId);
43 responseFormat = componentsUtils.getResponseFormat(eitherCreator.right().value());
45 if (log.isDebugEnabled()) {
46 log.debug("User is not listed. userId {}", userId);
48 BeEcompErrorManager.getInstance().logBeUserMissingError(ecompErrorContext, userId);
49 return Either.right(responseFormat);
51 return Either.left(eitherCreator.left().value());
54 public Either<Boolean, ResponseFormat> validateUserRole(User user, List<Role> roles) {
55 Role userRole = Role.valueOf(user.getRole());
57 if (!roles.contains(userRole)) {
58 if (log.isDebugEnabled()) {
59 log.debug("user is not in appropriate role to perform action");
61 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION);
62 return Either.right(responseFormat);
64 return Either.left(Boolean.TRUE);
66 return Either.left(Boolean.FALSE);
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);
76 log.debug("validateUserExists - failed to authorize user, userId {}", userId);
78 log.debug("User is not listed. userId {}", userId);
79 BeEcompErrorManager.getInstance().logBeUserMissingError(ecompErrorContext, userId);
80 return Either.right(eitherCreator.right().value());
82 return Either.left(eitherCreator.left().value());
85 public Either<User, ResponseFormat> validateUserNotEmpty(User user, String ecompErrorContext) {
86 String userId = user.getUserId();
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);
94 return Either.left(user);
97 public Either<User, ResponseFormat> validateUserExists(User user, String ecompErrorContext, boolean inTransaction) {
98 return validateUserExists(user.getUserId(), ecompErrorContext, inTransaction);
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());