Use data owner attribute instead of owning entity for OwnerCheck
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / introspection / sideeffect / OwnerCheck.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 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.onap.aai.introspection.sideeffect;
22
23 import org.apache.commons.lang3.ObjectUtils;
24 import org.apache.tinkerpop.gremlin.structure.Vertex;
25 import org.onap.aai.exceptions.AAIException;
26 import org.onap.aai.introspection.Introspector;
27 import org.onap.aai.schema.enums.PropertyMetadata;
28 import org.onap.aai.serialization.db.DBSerializer;
29 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
30 import org.springframework.util.CollectionUtils;
31
32 import java.util.Map.Entry;
33 import java.util.Optional;
34
35 public class OwnerCheck extends SideEffect {
36
37     public static final String READ_ONLY_SUFFIX = "_readOnly";
38     private static final String DATA_OWNER = "data-owner";
39
40     public OwnerCheck(Introspector obj, Vertex self, TransactionalGraphEngine dbEngine, DBSerializer serializer) {
41         super(obj, self, dbEngine, serializer);
42     }
43
44     @Override
45     protected void processURI(Optional<String> completeUri, Entry<String, String> entry)
46         throws AAIException {
47         if (!isAuthorized(serializer.getGroups(), self)) {
48
49             throw new AAIException("AAI_3304",
50                 "Group(s) :" + serializer.getGroups() + " not authorized to perform function");
51
52         } //else skip processing because no required properties were specified
53
54     }
55
56     public static boolean isAuthorized(java.util.Set<String> groups, Vertex vertex) {
57         if (!CollectionUtils.isEmpty(groups)) {
58             Object dataOwnerProperty = vertex.property(DATA_OWNER).orElse(null);
59             if (ObjectUtils.isNotEmpty(dataOwnerProperty)) {
60                 String dataOwner = dataOwnerProperty.toString();
61                 String dataOwnerWithReadAccess = dataOwner + READ_ONLY_SUFFIX;
62                 return groups.stream()
63                     .anyMatch(group -> group.equals(dataOwner) || group.equals(dataOwnerWithReadAccess));
64             }
65         }
66         return true;
67     }
68
69     @Override
70     protected PropertyMetadata getPropertyMetadata() {
71         return PropertyMetadata.OWNER_CHECK;
72     }
73
74     @Override
75     protected boolean replaceWithWildcard() {
76         return false;
77     }
78
79 }