Skip to main content
summaryrefslogtreecommitdiffstats
blob: a6d7d0e86f5d3b5325ff1d8f096fa64b0bdd386d (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 * Copyright (c) 2012, 2013 Eike Stepper (Berlin, Germany) and others.
 * 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:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.emf.cdo.server.spi.security;

import org.eclipse.emf.cdo.common.model.CDOPackageInfo;
import org.eclipse.emf.cdo.common.model.CDOPackageRegistry;
import org.eclipse.emf.cdo.common.model.CDOPackageUnit;
import org.eclipse.emf.cdo.security.Access;
import org.eclipse.emf.cdo.security.FilterPermission;
import org.eclipse.emf.cdo.security.PermissionFilter;
import org.eclipse.emf.cdo.security.Realm;
import org.eclipse.emf.cdo.security.RealmUtil;
import org.eclipse.emf.cdo.security.Role;
import org.eclipse.emf.cdo.security.SecurityFactory;
import org.eclipse.emf.cdo.security.SecurityItem;
import org.eclipse.emf.cdo.security.SecurityPackage;
import org.eclipse.emf.cdo.security.User;
import org.eclipse.emf.cdo.server.IStoreAccessor.CommitContext;
import org.eclipse.emf.cdo.server.security.ISecurityManager.RealmOperation;
import org.eclipse.emf.cdo.server.spi.security.InternalSecurityManager.CommitHandler;

import org.eclipse.net4j.util.factory.ProductCreationException;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EModelElement;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.util.EcoreUtil;

import java.util.StringTokenizer;

/**
 * If the meaning of this type isn't clear, there really should be more of a description here...
 *
 * @author Eike Stepper
 */
public class AnnotationHandler implements InternalSecurityManager.CommitHandler
{
  public static final String SOURCE_URI = "http://www.eclipse.org/CDO/Security";

  public static final String READ_KEY = "read";

  public static final String WRITE_KEY = "write";

  public static final String DELIMITERS = " ,;|";

  public AnnotationHandler()
  {
  }

  public void init(InternalSecurityManager securityManager, boolean firstTime)
  {
    if (firstTime)
    {
      CDOPackageRegistry packageRegistry = securityManager.getRepository().getPackageRegistry();
      handlePackageUnits(securityManager, packageRegistry.getPackageUnits());
    }
  }

  public void handleCommit(InternalSecurityManager securityManager, CommitContext commitContext, User user)
  {
    handlePackageUnits(securityManager, commitContext.getNewPackageUnits());
  }

  protected void handlePackageUnits(InternalSecurityManager securityManager, final CDOPackageUnit[] packageUnits)
  {
    securityManager.modify(new RealmOperation()
    {
      public void execute(Realm realm)
      {
        if (packageUnits != null && packageUnits.length != 0)
        {
          for (CDOPackageUnit packageUnit : packageUnits)
          {
            for (CDOPackageInfo packageInfo : packageUnit.getPackageInfos())
            {
              EPackage ePackage = packageInfo.getEPackage();
              handlePackage(realm, ePackage);
            }
          }
        }
      }
    });
  }

  protected void handlePackage(Realm realm, EPackage ePackage)
  {
    handlePackagePermission(realm, ePackage, READ_KEY, Access.READ);
    handlePackagePermission(realm, ePackage, WRITE_KEY, Access.WRITE);

    for (EClassifier eClassifier : ePackage.getEClassifiers())
    {
      if (eClassifier instanceof EClass)
      {
        EClass eClass = (EClass)eClassifier;
        handleClassPermission(realm, eClass, READ_KEY, Access.READ);
        handleClassPermission(realm, eClass, WRITE_KEY, Access.WRITE);
      }
    }
  }

  protected void handlePackagePermission(Realm realm, EPackage ePackage, String key, Access access)
  {
    EClass filterClass = SecurityPackage.Literals.PACKAGE_FILTER;
    EReference filterFeature = SecurityPackage.Literals.PACKAGE_FILTER__APPLICABLE_PACKAGE;
    handlePermission(realm, ePackage, key, access, filterClass, filterFeature);
  }

  protected void handleClassPermission(Realm realm, EClass eClass, String key, Access access)
  {
    EClass filterClass = SecurityPackage.Literals.CLASS_FILTER;
    EReference filterFeature = SecurityPackage.Literals.CLASS_FILTER__APPLICABLE_CLASS;
    handlePermission(realm, eClass, key, access, filterClass, filterFeature);
  }

  protected void handlePermission(Realm realm, EModelElement modelElement, String key, Access access,
      EClass filterClass, EReference filterFeature)
  {
    String annotation = EcoreUtil.getAnnotation(modelElement, SOURCE_URI, key);
    if (annotation == null || annotation.length() == 0)
    {
      return;
    }

    EList<SecurityItem> items = realm.getItems();

    StringTokenizer tokenizer = new StringTokenizer(annotation, DELIMITERS);
    while (tokenizer.hasMoreTokens())
    {
      String token = tokenizer.nextToken();
      if (token != null && token.length() != 0)
      {
        PermissionFilter filter = (PermissionFilter)EcoreUtil.create(filterClass);
        filter.eSet(filterFeature, modelElement);

        FilterPermission permission = SecurityFactory.eINSTANCE.createFilterPermission(access, filter);

        Role role = RealmUtil.findRole(items, token);
        if (role == null)
        {
          role = SecurityFactory.eINSTANCE.createRole();
          role.setId(token);
          items.add(role);
        }

        role.getPermissions().add(permission);
      }
    }
  }

  /**
   * @author Eike Stepper
   * @since 4.3
   */
  public static class Factory extends InternalSecurityManager.CommitHandler.Factory
  {
    public Factory()
    {
      super("annotation");
    }

    @Override
    public CommitHandler create(String description) throws ProductCreationException
    {
      return new AnnotationHandler();
    }
  }
}

Back to the top