Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 09738b6cfb5715894a3d96b502dcf4497aa5a5ff (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
 * Copyright (c) 2013, 2014 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.security.Access;
import org.eclipse.emf.cdo.security.PatternStyle;
import org.eclipse.emf.cdo.security.Realm;
import org.eclipse.emf.cdo.security.Role;
import org.eclipse.emf.cdo.security.SecurityFactory;
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.internal.security.bundle.OM;
import org.eclipse.emf.cdo.server.security.ISecurityManager.RealmOperation;
import org.eclipse.emf.cdo.server.spi.security.InternalSecurityManager.CommitHandler;
import org.eclipse.emf.cdo.spi.common.revision.InternalCDORevision;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.view.CDOView;

import org.eclipse.net4j.util.concurrent.ExecutorServiceFactory;
import org.eclipse.net4j.util.container.IManagedContainer;
import org.eclipse.net4j.util.container.IManagedContainer.ContainerAware;
import org.eclipse.net4j.util.factory.ProductCreationException;

import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;

import java.util.List;
import java.util.concurrent.ExecutorService;

/**
 * If the meaning of this type isn't clear, there really should be more of a description here...
 *
 * @author Eike Stepper
 * @since 4.3
 */
public class HomeFolderHandler implements InternalSecurityManager.CommitHandler2
{
  public static final String DEFAULT_HOME_FOLDER = "/home";

  private static final SecurityFactory SF = SecurityFactory.eINSTANCE;

  private final String homeFolder;

  private ExecutorService executorService;

  public HomeFolderHandler(String homeFolder)
  {
    this.homeFolder = homeFolder == null || homeFolder.length() == 0 ? DEFAULT_HOME_FOLDER : homeFolder;
  }

  public HomeFolderHandler()
  {
    this(null);
  }

  public String getHomeFolder()
  {
    return homeFolder;
  }

  public ExecutorService getExecutorService()
  {
    return executorService;
  }

  public void setExecutorService(ExecutorService executorService)
  {
    this.executorService = executorService;
  }

  public void init(InternalSecurityManager securityManager, boolean firstTime)
  {
    if (firstTime)
    {
      EList<User> users = securityManager.getRealm().getAllUsers();
      if (!users.isEmpty())
      {
        List<String> userIDs = new BasicEList<String>();
        for (User user : users)
        {
          userIDs.add(user.getId());
        }

        handleUsers(securityManager, userIDs, true);
      }
    }
  }

  protected void initRole(Role role)
  {
    role.getPermissions().add(SF.createFilterPermission(Access.WRITE, //
        SF.createResourceFilter(homeFolder + "/${user}", PatternStyle.TREE, false)));

    role.getPermissions().add(SF.createFilterPermission(Access.READ, //
        SF.createResourceFilter(homeFolder, PatternStyle.EXACT, true)));
  }

  public void handleCommit(final InternalSecurityManager securityManager, CommitContext commitContext, User user)
  {
    // Do nothing
  }

  public void handleCommitted(final InternalSecurityManager securityManager, CommitContext commitContext)
  {
    List<String> userIDs = null;

    InternalCDORevision[] newObjects = commitContext.getNewObjects();
    for (int i = 0; i < newObjects.length; i++)
    {
      InternalCDORevision newObject = newObjects[i];
      EClass eClass = newObject.getEClass();
      if (eClass == SecurityPackage.Literals.USER)
      {
        String userID = (String)newObject.getValue(SecurityPackage.Literals.ASSIGNEE__ID);
        if (userID != null)
        {
          if (userIDs == null)
          {
            userIDs = new BasicEList<String>();
          }

          userIDs.add(userID);
        }
      }
    }

    if (userIDs != null)
    {
      final List<String> list = userIDs;
      long commitTime = commitContext.getBranchPoint().getTimeStamp();

      CDOView view = securityManager.getRealm().cdoView();
      view.runAfterUpdate(commitTime, new Runnable()
      {
        public void run()
        {
          handleUsers(securityManager, list, false);
        }
      });
    }
  }

  protected void handleUsers(final InternalSecurityManager securityManager, final List<String> userIDs,
      final boolean init)
  {
    executorService.submit(new Runnable()
    {
      public void run()
      {
        securityManager.modify(new RealmOperation()
        {
          public void execute(Realm realm)
          {
            String roleID = "Home Folder " + homeFolder;
            Role role;

            if (init)
            {
              role = realm.addRole(roleID);
              initRole(role);
            }
            else
            {
              role = realm.getRole(roleID);
              if (role == null)
              {
                OM.LOG.warn("Role '" + roleID + "' not found in " + HomeFolderHandler.this);
                return;
              }
            }

            CDOTransaction transaction = (CDOTransaction)realm.cdoView();

            for (String userID : userIDs)
            {
              try
              {
                User user = realm.getUser(userID);
                if (user == null)
                {
                  OM.LOG.warn("User '" + userID + "' not found in " + HomeFolderHandler.this);
                }
                else
                {
                  handleUser(transaction, realm, role, user);
                }
              }
              catch (Exception ex)
              {
                OM.LOG.error(ex);
              }
            }
          }
        });
      }
    });
  }

  protected void handleUser(CDOTransaction transaction, Realm realm, Role role, User user) throws Exception
  {
    EList<Role> roles = user.getRoles();
    roles.add(role);

    String path = getHomeFolder() + "/" + user.getId();
    transaction.getOrCreateResourceFolder(path);
  }

  @Override
  public String toString()
  {
    return getClass().getSimpleName() + "[" + homeFolder + "]";
  }

  /**
   * Creates {@link CommitHandler} instances.
   *
   * @author Eike Stepper
   */
  public static class Factory extends InternalSecurityManager.CommitHandler.Factory implements ContainerAware
  {
    private IManagedContainer container;

    public Factory()
    {
      super("home");
    }

    public void setManagedContainer(IManagedContainer container)
    {
      this.container = container;
    }

    @Override
    public CommitHandler create(String homeFolder) throws ProductCreationException
    {
      ExecutorService executorService = ExecutorServiceFactory.get(container);

      HomeFolderHandler handler = new HomeFolderHandler(homeFolder);
      handler.setExecutorService(executorService);
      return handler;
    }
  }
}

Back to the top