Skip to main content
summaryrefslogtreecommitdiffstats
blob: 07115865bf955b042f5c610e6f6376e149e91180 (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
141
142
143
144
145
146
/*******************************************************************************
 * Copyright (c) 2004, 2007 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.skynet.core;

import static org.eclipse.osee.framework.core.enums.CoreBranches.COMMON;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.osee.framework.core.data.IArtifactToken;
import org.eclipse.osee.framework.core.data.IOseeBranch;
import org.eclipse.osee.framework.core.enums.CoreArtifactTokens;
import org.eclipse.osee.framework.core.enums.CoreArtifactTypes;
import org.eclipse.osee.framework.core.enums.CoreRelationTypes;
import org.eclipse.osee.framework.core.exception.ArtifactDoesNotExist;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.artifact.ArtifactCache;
import org.eclipse.osee.framework.skynet.core.artifact.ArtifactTypeManager;
import org.eclipse.osee.framework.skynet.core.artifact.search.ArtifactQuery;

/**
 * @author Roberto E. Escobar
 */
public class OseeGroup {

   private Artifact groupArtifact;
   private final Map<IArtifactToken, Boolean> temporaryOverride = new HashMap<>();
   private final IArtifactToken token;

   public OseeGroup(IArtifactToken token) {
      this.token = token;
      this.groupArtifact = null;
   }

   /**
    * @return Returns the group.
    */
   public Artifact getGroupArtifact() throws OseeCoreException {
      checkGroupExists();
      return groupArtifact;
   }

   /**
    * This does not persist the newly created relation that is the callers responsibility.
    */
   public void addMember(User user) throws OseeCoreException {
      getGroupArtifact().addRelation(CoreRelationTypes.Users_User, user);
   }

   /**
    * Determines whether the user is a member of this group
    *
    * @param user to check
    * @return whether the user is a member of this group
    */
   public boolean isMember(User user) throws OseeCoreException {
      return isTemporaryOverride(user) || getGroupArtifact().isRelated(CoreRelationTypes.Users_User, user);
   }

   /**
    * Determines whether the current user is a member of this group
    *
    * @return whether the current user is a member of this group
    */
   public boolean isCurrentUserMember() throws OseeCoreException {
      return isMember(UserManager.getUser());
   }

   public boolean isTemporaryOverride(User user) {
      if (temporaryOverride.get(token) != null) {
         return temporaryOverride.get(token);
      }
      return false;
   }

   /**
    * Allow user to temporarily override admin
    */
   public void setTemporaryOverride(boolean member) {
      temporaryOverride.put(token, member);
   }

   public void removeTemporaryOverride() {
      temporaryOverride.remove(token);
   }

   private void checkGroupExists() throws OseeCoreException {
      if (groupArtifact == null) {
         groupArtifact = getOrCreateGroupArtifact(token);
      }
   }

   private Artifact getOrCreateGroupArtifact(IArtifactToken token) throws OseeCoreException {
      String cacheKey = CoreArtifactTypes.UserGroup + "." + token;
      Artifact groupArtifact = ArtifactCache.getByTextId(cacheKey, COMMON);

      if (groupArtifact == null) {
         try {
            groupArtifact = ArtifactQuery.getArtifactFromToken(token, COMMON);
         } catch (ArtifactDoesNotExist ex) {
            // do nothing
         }
         if (groupArtifact == null) {
            Artifact userGroupsFolder = getOrCreateUserGroupsFolder(COMMON);
            groupArtifact =
               ArtifactTypeManager.addArtifact(CoreArtifactTypes.UserGroup, COMMON, token.getName(), token.getGuid());
            userGroupsFolder.addChild(groupArtifact);
         }
         ArtifactCache.cacheByTextId(cacheKey, groupArtifact);
      }

      return groupArtifact;
   }

   private Artifact getOrCreateUserGroupsFolder(IOseeBranch branch) throws OseeCoreException {
      String cacheKey = CoreArtifactTypes.Folder.getName() + "." + CoreArtifactTokens.UserGroups.getName();
      Artifact usersGroupFolder = ArtifactCache.getByTextId(cacheKey, branch);
      if (usersGroupFolder == null) {
         usersGroupFolder = ArtifactQuery.checkArtifactFromId(CoreArtifactTokens.UserGroups.getUuid(), branch);
         if (usersGroupFolder == null) {
            Artifact root = OseeSystemArtifacts.getDefaultHierarchyRootArtifact(branch);
            if (root.hasChild(CoreArtifactTokens.UserGroups.getName())) {
               usersGroupFolder = root.getChild(CoreArtifactTokens.UserGroups.getName());
            } else {
               usersGroupFolder = ArtifactTypeManager.addArtifact(CoreArtifactTokens.UserGroups, branch);
               root.addChild(usersGroupFolder);
            }
         }

         ArtifactCache.cacheByTextId(cacheKey, usersGroupFolder);
      }
      return usersGroupFolder;
   }

   @Override
   public String toString() {
      return "OseeGroup [groupName=" + token.getName() + "]";
   }
}

Back to the top