cccd8152c81b7ce5a43db9d7206aa63836880c31
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / editattributes / UserValidator.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.onap.aai.sparky.editattributes;
24
25 import java.io.File;
26 import java.io.IOException;
27 import java.util.List;
28
29 import org.onap.aai.cl.api.Logger;
30 import org.onap.aai.cl.eelf.LoggerFactory;
31 import org.onap.aai.sparky.logging.AaiUiMsgs;
32 import org.onap.aai.sparky.viewandinspect.config.TierSupportUiConstants;
33
34 /**
35  * Validates users against a user authorization file.
36  */
37 public class UserValidator {
38
39   private static final Logger LOG = LoggerFactory.getInstance().getLogger(UserValidator.class);
40   private static final String USER_AUTH_FILE =
41       TierSupportUiConstants.AUTHORIZED_USERS_FILE_LOCATION;
42
43   private UserAuthorizationReader userAuthorizationReader =
44       new UserAuthorizationReader(new File(USER_AUTH_FILE));
45
46   /**
47    * Returns true if the user is authorized.
48    *
49    * @param userId a user identifier
50    * @return true if the user ID is present in the user authorization file
51    */
52   public boolean isAuthorizedUser(String userId) {
53     if (userId != null && !userId.isEmpty()) {
54       try {
55         List<String> users = userAuthorizationReader.getUsers();
56         return users.contains(userId);
57       } catch (IOException exc) {
58         LOG.error(AaiUiMsgs.USER_AUTHORIZATION_FILE_UNAVAILABLE, userId);
59         return false;
60       }
61     } else {
62       return false;
63     }
64   }
65 }