Skip to main content
summaryrefslogtreecommitdiffstats
blob: 828908485e12ce7f4c3d67fcbf9821a6327d5f27 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*******************************************************************************
 * Copyright (c) 2010 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.ui.skynet.internal;

import java.util.Collection;
import java.util.logging.Level;
import org.eclipse.osee.framework.core.data.IArtifactType;
import org.eclipse.osee.framework.core.data.IAttributeType;
import org.eclipse.osee.framework.core.data.IOseeBranch;
import org.eclipse.osee.framework.core.data.IRelationTypeSide;
import org.eclipse.osee.framework.core.enums.PermissionEnum;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.model.IBasicArtifact;
import org.eclipse.osee.framework.core.model.access.AccessDataQuery;
import org.eclipse.osee.framework.core.model.access.PermissionStatus;
import org.eclipse.osee.framework.core.services.IAccessControlService;
import org.eclipse.osee.framework.jdk.core.util.Collections;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.ui.skynet.SkynetGuiPlugin;
import org.eclipse.osee.framework.ui.skynet.artifact.IAccessPolicyHandlerService;

/**
 * @author Jeff C. Phillips
 */
public class AccessPolicyHandlerServiceImpl implements IAccessPolicyHandlerService {
   private final IBasicArtifact<?> user;
   private final IAccessControlService accessControlService;

   public AccessPolicyHandlerServiceImpl(IBasicArtifact<?> user, IAccessControlService accessControlService) {
      super();
      this.user = user;
      this.accessControlService = accessControlService;
   }

   /**
    * @param level - A level of OseeLevel.SEVERE_POPUP will cause an error dialog to be displayed to the user. All
    * others will write to the log.
    */
   @Override
   public PermissionStatus hasAttributeTypePermission(Collection<? extends IBasicArtifact<?>> artifacts, IAttributeType attributeType, PermissionEnum permission, Level level) throws OseeCoreException {
      AccessDataQuery query = accessControlService.getAccessData(user, artifacts);
      PermissionStatus permissionStatus = new PermissionStatus();

      if (artifacts != null) {
         for (IBasicArtifact<?> artifact : artifacts) {
            query.attributeTypeMatches(permission, artifact, attributeType, permissionStatus);

            if (printErrorMessage(artifacts, permissionStatus, level)) {
               break;
            }
         }
      }
      return permissionStatus;
   }

   @Override
   public PermissionStatus hasArtifactPermission(Collection<? extends IBasicArtifact<?>> artifacts, PermissionEnum permission, Level level) throws OseeCoreException {
      AccessDataQuery query = accessControlService.getAccessData(user, artifacts);
      PermissionStatus permissionStatus = new PermissionStatus();

      if (artifacts != null) {
         for (IBasicArtifact<?> artifact : artifacts) {
            query.artifactMatches(PermissionEnum.WRITE, artifact, permissionStatus);

            if (printErrorMessage(artifacts, permissionStatus, level)) {
               break;
            }
         }
      }
      return permissionStatus;
   }

   @Override
   public PermissionStatus hasRelationSidePermission(Collection<? extends IRelationTypeSide> relationTypeSides, PermissionEnum permission, Level level) throws OseeCoreException {
      AccessDataQuery query = accessControlService.getAccessData(user, relationTypeSides);
      PermissionStatus permissionStatus = new PermissionStatus();

      if (relationTypeSides != null) {
         for (IRelationTypeSide relationTypeSide : relationTypeSides) {
            query.relationTypeMatches(permission, relationTypeSide, permissionStatus);
            if (printErrorMessage(relationTypeSides, permissionStatus, level)) {
               break;
            }
         }
      }
      return permissionStatus;
   }

   private boolean printErrorMessage(Collection<?> objects, PermissionStatus permissionStatus, Level level) {
      boolean notMatched = !permissionStatus.matched();

      if (notMatched) {
         OseeLog.log(SkynetGuiPlugin.class, level, String.format(
            "Access Denied - [%s] does not have valid permission to edit item(s) : [%s]%s", user,
            Collections.toString("; ", objects),
            (Strings.isValid(permissionStatus.getReason()) ? "\n reason:[%s]" : "")));
      }
      return notMatched;
   }

   @Override
   public PermissionStatus hasArtifactTypePermission(IOseeBranch branch, Collection<? extends IArtifactType> artifactTypes, PermissionEnum permission, Level level) throws OseeCoreException {
      AccessDataQuery query = accessControlService.getAccessData(user, java.util.Collections.singleton(branch));
      PermissionStatus permissionStatus = new PermissionStatus();

      if (artifactTypes != null) {
         for (IArtifactType artifactType : artifactTypes) {
            query.branchArtifactTypeMatches(permission, branch, artifactType, permissionStatus);

            if (printErrorMessage(artifactTypes, permissionStatus, level)) {
               break;
            }
         }
      }
      return permissionStatus;
   }

   @Override
   public IAccessControlService getAccessService() {
      return accessControlService;
   }

   @Override
   public PermissionStatus hasArtifactRelatablePermission(Collection<? extends IBasicArtifact<?>> artifacts, Collection<? extends IRelationTypeSide> relationTypeSides, PermissionEnum permission, Level level) throws OseeCoreException {
      PermissionStatus status = hasRelationSidePermission(relationTypeSides, permission, level);
      if (status.matched()) {
         status = hasArtifactPermission(artifacts, permission, level);
      }
      return status;
   }
}

Back to the top