re base code
[sdc.git] / openecomp-be / backend / openecomp-sdc-item-permissions-manager / src / main / java / org / openecomp / sdc / itempermissions / dao / impl / PermissionsManagerImpl.java
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
17 package org.openecomp.sdc.itempermissions.dao.impl;
18
19 import org.openecomp.sdc.common.errors.CoreException;
20 import org.openecomp.sdc.common.errors.ErrorCategory;
21 import org.openecomp.sdc.common.errors.ErrorCode;
22 import org.openecomp.sdc.common.errors.Messages;
23 import org.openecomp.sdc.common.session.SessionContextProviderFactory;
24 import org.openecomp.sdc.itempermissions.PermissionsManager;
25 import org.openecomp.sdc.itempermissions.PermissionsServices;
26 import org.openecomp.sdc.itempermissions.impl.types.PermissionTypes;
27 import org.openecomp.sdc.itempermissions.type.ItemPermissionsEntity;
28 import org.openecomp.sdc.logging.api.Logger;
29 import org.openecomp.sdc.logging.api.LoggerFactory;
30 import org.openecomp.sdc.notification.dtos.Event;
31 import org.openecomp.sdc.notification.services.NotificationPropagationManager;
32 import org.openecomp.sdc.notification.services.SubscriptionService;
33 import org.openecomp.sdc.versioning.AsdcItemManager;
34 import org.openecomp.sdc.versioning.types.Item;
35
36 import java.util.*;
37
38 import static org.openecomp.sdc.itempermissions.notifications.NotificationConstants.*;
39
40 /**
41  * Created by ayalaben on 6/18/2017.
42  */
43 public class PermissionsManagerImpl implements PermissionsManager {
44
45   private static final Logger LOGGER = LoggerFactory.getLogger(PermissionsManagerImpl.class);
46   private static final String CHANGE_PERMISSIONS = "Change_Item_Permissions";
47
48   private PermissionsServices permissionsServices;
49   private AsdcItemManager asdcItemManager;
50   private NotificationPropagationManager notifier;
51   private SubscriptionService subscriptionService;
52
53   public PermissionsManagerImpl(PermissionsServices permissionsServices,
54                                 AsdcItemManager asdcItemManager,
55                                 NotificationPropagationManager notificationPropagationManager,
56                                 SubscriptionService subscriptionService) {
57     this.permissionsServices = permissionsServices;
58     this.asdcItemManager = asdcItemManager;
59     this.notifier = notificationPropagationManager;
60     this.subscriptionService = subscriptionService;
61   }
62
63   @Override
64   public Collection<ItemPermissionsEntity> listItemPermissions(String itemId) {
65
66     return permissionsServices.listItemPermissions(itemId);
67   }
68
69   @Override
70   public Set<String> listUserPermittedItems(String userId, String permission) {
71     return permissionsServices.listUserPermittedItems(userId,permission);
72   }
73
74   @Override
75   public void updateItemPermissions(String itemId, String permission, Set<String> addedUsersIds,
76                                     Set<String> removedUsersIds) {
77
78     String currentUser =
79         SessionContextProviderFactory.getInstance().createInterface().get().getUser().getUserId();
80
81     if (!permissionsServices.isAllowed(itemId, currentUser, CHANGE_PERMISSIONS)) {
82       throw new CoreException(new ErrorCode.ErrorCodeBuilder()
83           .withMessage(Messages.PERMISSIONS_ERROR.getErrorMessage())
84           .withId(Messages.PERMISSIONS_ERROR.name())
85           .withCategory(ErrorCategory.SECURITY).build());
86     }
87
88     if (permission.equals(PermissionTypes.Owner.name()) ){
89       if (addedUsersIds.size() == 1){
90         asdcItemManager.updateOwner(itemId,addedUsersIds.iterator().next());
91     } else {
92         throw new CoreException(new ErrorCode.ErrorCodeBuilder()
93             .withMessage(Messages.PERMISSIONS_OWNER_ERROR.getErrorMessage())
94             .withId(Messages.PERMISSIONS_OWNER_ERROR.name())
95             .withCategory(ErrorCategory.SECURITY).build());
96       }
97     }
98
99     permissionsServices
100         .updateItemPermissions(itemId, permission, addedUsersIds, removedUsersIds);
101
102     sendNotifications(itemId, permission, addedUsersIds, removedUsersIds, currentUser);
103   }
104
105   private void sendNotifications(String itemId, String permission, Set<String> addedUsersIds,
106                                    Set<String> removedUsersIds, String userName) {
107
108     Item item = asdcItemManager.get(itemId);
109     addedUsersIds.forEach(affectedUser -> {
110       notifyUser(userName, true, item.getName(), itemId, affectedUser, permission);
111       subscriptionService.subscribe(affectedUser, itemId);
112     });
113     removedUsersIds.forEach(affectedUser -> {
114       notifyUser(userName, false, item.getName(), itemId, affectedUser, permission);
115       subscriptionService.unsubscribe(affectedUser, itemId);
116     });
117
118   }
119
120   private void notifyUser(String userName, boolean granted, String itemName, String itemId,
121                           String affectedUser, String permission) {
122     Map<String, Object> details = new HashMap<>();
123     details.put(PERMISSION_ITEM, permission);
124     details.put(ITEM_ID_PROP, itemId);
125     details.put(ITEM_NAME_PROP, itemName);
126     details.put(PERMISSION_GRANTED, granted);
127     details.put(PERMISSION_USER, userName);
128     PermissionEvent permissionEvent = new PermissionEvent(PERMISSION_CHANGED, affectedUser,
129         details, affectedUser);
130
131     try {
132       notifier.directNotification(permissionEvent, affectedUser);
133     } catch (Exception e) {
134       LOGGER.error("Failed to send notification on permission changed for user '" +
135           affectedUser + "'");
136     }
137
138   }
139
140   @Override
141   public boolean isAllowed(String itemId, String userId, String action) {
142     return permissionsServices.isAllowed(itemId, userId, action);
143   }
144
145   @Override
146   public Optional<String> getUserItemPermission(String itemId, String userId) {
147     return permissionsServices.getUserItemPermission(itemId, userId);
148   }
149
150   @Override
151   public void deleteItemPermissions(String itemId) {
152     permissionsServices.deleteItemPermissions(itemId);
153   }
154
155   private class PermissionEvent implements Event {
156
157     private String eventType;
158     private String originatorId;
159     private Map<String, Object> attributes;
160     private String entityId;
161
162     private PermissionEvent(String eventType, String originatorId,
163                             Map<String, Object> attributes, String entityId) {
164       this.eventType = eventType;
165       this.originatorId = originatorId;
166       this.attributes = attributes;
167       this.entityId = entityId;
168     }
169
170     @Override
171     public String getEventType() {
172       return eventType;
173     }
174
175     @Override
176     public String getOriginatorId() {
177       return originatorId;
178     }
179
180     @Override
181     public Map<String, Object> getAttributes() {
182       return attributes;
183     }
184
185     @Override
186     public String getEntityId() {
187       return entityId;
188     }
189   }
190 }