83b393287960c5d603ef7fde447953aa50cec929
[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-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 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 package org.onap.aai.sparky.editattributes;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.util.List;
26
27 import org.onap.aai.cl.api.Logger;
28 import org.onap.aai.cl.eelf.LoggerFactory;
29 import org.onap.aai.sparky.logging.AaiUiMsgs;
30 import org.onap.aai.sparky.viewandinspect.config.SparkyConstants;
31
32 /**
33  * Validates users against a user authorization file.
34  */
35 public class UserValidator {
36
37   private static final Logger LOG = LoggerFactory.getInstance().getLogger(UserValidator.class);
38   private static final String USER_AUTH_FILE =
39       SparkyConstants.AUTHORIZED_USERS_FILE_LOCATION;
40
41   private UserAuthorizationReader userAuthorizationReader =
42       new UserAuthorizationReader(new File(USER_AUTH_FILE));
43
44   /**
45    * Returns true if the user is authorized.
46    *
47    * @param userId a user identifier
48    * @return true if the user ID is present in the user authorization file
49    */
50   public boolean isAuthorizedUser(String userId) {
51     if (userId != null && !userId.isEmpty()) {
52       try {
53         List<String> users = userAuthorizationReader.getUsers();
54         return users.contains(userId);
55       } catch (IOException exc) {
56         LOG.error(AaiUiMsgs.USER_AUTHORIZATION_FILE_UNAVAILABLE, userId);
57         return false;
58       }
59     } else {
60       return false;
61     }
62   }
63 }