222ed000ae79a534bd3a5e3fac8c380c9acf046c
[sdc.git] /
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.openecomp.sdc.itempermissions.impl;
17
18 import org.apache.commons.collections.CollectionUtils;
19 import org.openecomp.sdc.common.errors.CoreException;
20 import org.openecomp.sdc.itempermissions.PermissionsRules;
21 import org.openecomp.sdc.itempermissions.PermissionsServices;
22 import org.openecomp.sdc.itempermissions.PermissionsServicesFactory;
23 import org.openecomp.sdc.itempermissions.errors.PermissionsErrorMessagesBuilder;
24 import org.openecomp.sdc.itempermissions.impl.types.PermissionActionTypes;
25 import org.openecomp.sdc.itempermissions.impl.types.PermissionTypes;
26
27 import java.util.Collections;
28 import java.util.HashSet;
29 import java.util.Set;
30
31 import static org.openecomp.sdc.itempermissions.errors.PermissionsErrorMessages.INVALID_ACTION_TYPE;
32 import static org.openecomp.sdc.itempermissions.errors.PermissionsErrorMessages.INVALID_PERMISSION_TYPE;
33
34 /**
35  * Created by ayalaben on 6/26/2017.
36  */
37 public class PermissionsRulesImpl implements PermissionsRules {
38
39
40     @Override
41     public boolean isAllowed(String permission, String action) {
42
43         if (permission == null) {
44             return false;
45         }
46         try {
47             PermissionTypes.valueOf(permission);
48         } catch (IllegalArgumentException ex) {
49             throw new CoreException(new PermissionsErrorMessagesBuilder(INVALID_PERMISSION_TYPE).build());
50         }
51
52         try {
53             switch (PermissionActionTypes.valueOf(action)) {
54                 case Create_Item:
55                     return true;
56
57                 case Edit_Item:
58                 case Commit_Item:
59                 case Delete_Item:
60                 case Submit_Item:
61                     if (permission.equals(PermissionTypes.Contributor.name()) || permission.equals
62                             (PermissionTypes.Owner.name())) {
63                         return true;
64                     }
65                     break;
66
67                 case Change_Item_Permissions:
68                     if (permission.equals(PermissionTypes.Owner.name())) {
69                         return true;
70                     }
71                     break;
72
73                 default:
74                     return false;
75             }
76         } catch (IllegalArgumentException ex) {
77             throw new CoreException(new PermissionsErrorMessagesBuilder(INVALID_ACTION_TYPE).build());
78         }
79
80         return false;
81     }
82
83     @Override
84     public void executeAction(String itemId, String userId, String action) {
85         try {
86             switch (PermissionActionTypes.valueOf(action)) {
87                 case Create_Item:
88                     caseCreateItem(userId, itemId);
89                     break;
90
91                 case Change_Item_Permissions:
92                     break;
93
94                 case Edit_Item:
95                     break;
96
97                 case Submit_Item:
98                     break;
99
100                 default:
101             }
102         } catch (IllegalArgumentException ex) {
103             throw new CoreException(new PermissionsErrorMessagesBuilder(INVALID_ACTION_TYPE).build());
104         }
105     }
106
107   @Override
108   public void updatePermission(String itemId, String currentUserId, String permission, Set<String>
109       addedUsersIds, Set<String> removedUsersIds) {
110     try {
111       PermissionTypes.valueOf(permission);
112     } catch (IllegalArgumentException ex) {
113       throw new CoreException(new PermissionsErrorMessagesBuilder(INVALID_PERMISSION_TYPE).build());
114     }
115
116     if (isOwnerAdded(permission, addedUsersIds)) {
117       handleCurrentOwner(itemId, currentUserId);
118     }
119   }
120
121   private boolean isOwnerAdded(String permission, Set<String> addedUsersIds) {
122     return permission.equals(PermissionTypes.Owner.name()) &&
123         CollectionUtils.isNotEmpty(addedUsersIds);
124   }
125
126   private void handleCurrentOwner(String itemId, String currentUserId) {
127     PermissionsServices permissionsServices =
128         PermissionsServicesFactory.getInstance().createInterface();
129     if (!permissionsServices.getUserItemPermission(itemId, currentUserId).isPresent()) {
130       return; // no current owner - first owner addition
131     }
132
133     Set<String> currentUserSet = Collections.singleton(currentUserId);
134     permissionsServices
135         .updateItemPermissions(itemId, PermissionTypes.Contributor.name(), currentUserSet,
136             new HashSet<>());
137     permissionsServices.updateItemPermissions(itemId, PermissionTypes.Owner.name(), new HashSet<>(),
138         currentUserSet);
139   }
140
141     private void caseCreateItem(String userId, String itemId) {
142         HashSet<String> ownerId = new HashSet<>();
143         ownerId.add(userId);
144         PermissionsServicesFactory.getInstance().createInterface()
145                 .updateItemPermissions(itemId, PermissionTypes.Owner.name(), ownerId,
146                         new HashSet<>());
147     }
148
149 }