Skip to main content
summaryrefslogtreecommitdiffstats
blob: 341496768aad075c3c28912d39123838808af2a5 (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
/*
 * Copyright (c) 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.tests.bugzilla;

import org.eclipse.emf.cdo.common.CDOCommonSession.Options.PassiveUpdateMode;
import org.eclipse.emf.cdo.common.security.CDOPermission;
import org.eclipse.emf.cdo.eresource.CDOResource;
import org.eclipse.emf.cdo.eresource.CDOResourceFolder;
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.User;
import org.eclipse.emf.cdo.server.security.ISecurityManager;
import org.eclipse.emf.cdo.server.security.SecurityManagerUtil;
import org.eclipse.emf.cdo.session.CDOSession;
import org.eclipse.emf.cdo.tests.AbstractCDOTest;
import org.eclipse.emf.cdo.tests.config.impl.ConfigTest.CleanRepositoriesAfter;
import org.eclipse.emf.cdo.tests.config.impl.ConfigTest.CleanRepositoriesBefore;
import org.eclipse.emf.cdo.tests.config.impl.RepositoryConfig;
import org.eclipse.emf.cdo.tests.model1.Company;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.util.CDOUtil;

import org.eclipse.net4j.util.security.IPasswordCredentials;
import org.eclipse.net4j.util.security.PasswordCredentials;

/**
 * Bug 418267 - [Security] Cached permissions are not always properly updated after commits.
 * 
 * @author Eike Stepper
 */
@CleanRepositoriesBefore(reason = "TEST_SECURITY_MANAGER")
@CleanRepositoriesAfter(reason = "TEST_SECURITY_MANAGER")
public class Bugzilla_418267_Test extends AbstractCDOTest
{
  private static final SecurityFactory SF = SecurityFactory.eINSTANCE;

  private static final IPasswordCredentials CREDENTIALS = new PasswordCredentials("user", "password");

  private static final IPasswordCredentials CREDENTIALS_WRITER = new PasswordCredentials("writer", "password");

  public void testMoveFromNoneToNone() throws Exception
  {
    move(CDOPermission.NONE, CDOPermission.NONE);
  }

  public void testMoveFromNoneToRead() throws Exception
  {
    move(CDOPermission.NONE, CDOPermission.READ);
  }

  public void testMoveFromNoneToWrite() throws Exception
  {
    move(CDOPermission.NONE, CDOPermission.WRITE);
  }

  public void testMoveFromReadToNone() throws Exception
  {
    move(CDOPermission.READ, CDOPermission.NONE);
  }

  public void testMoveFromReadToRead() throws Exception
  {
    move(CDOPermission.READ, CDOPermission.READ);
  }

  public void testMoveFromReadToWrite() throws Exception
  {
    move(CDOPermission.READ, CDOPermission.WRITE);
  }

  public void testMoveFromWriteToNone() throws Exception
  {
    move(CDOPermission.WRITE, CDOPermission.NONE);
  }

  public void testMoveFromWriteToRead() throws Exception
  {
    move(CDOPermission.WRITE, CDOPermission.READ);
  }

  public void testMoveFromWriteToWrite() throws Exception
  {
    move(CDOPermission.WRITE, CDOPermission.WRITE);
  }

  private void move(final CDOPermission from, final CDOPermission to) throws Exception
  {
    final String pathFolder2 = getResourcePath("folder2");
    final String pathFolder1 = getResourcePath("folder1");
    final String pathResource1 = pathFolder1 + "/res";

    startSecureRepository(new ISecurityManager.RealmOperation()
    {
      public void execute(Realm realm)
      {
        CDOTransaction transaction = (CDOTransaction)realm.cdoView();
        transaction.createResourceFolder(pathFolder2);

        CDOResource resource = transaction.createResource(pathResource1);
        resource.getContents().add(getModel1Factory().createCompany());

        Role role = realm.addRole("Test Role");

        Access accessFrom = getAccess(from);
        if (accessFrom != null)
        {
          role.getPermissions().add(SF.createFilterPermission(accessFrom, //
              SF.createResourceFilter(pathFolder1, PatternStyle.TREE, false)));
          role.getPermissions().add(SF.createFilterPermission(Access.READ, //
              SF.createResourceFilter(pathFolder1, PatternStyle.EXACT, true).setModelObjects(false)));
        }

        Access accessTo = getAccess(to);
        if (accessTo != null)
        {
          role.getPermissions().add(SF.createFilterPermission(accessTo, //
              SF.createResourceFilter(pathFolder2, PatternStyle.TREE, false)));
          role.getPermissions().add(SF.createFilterPermission(Access.READ, //
              SF.createResourceFilter(pathFolder2, PatternStyle.EXACT, true).setModelObjects(false)));
        }

        User user = realm.addUser(CREDENTIALS);
        user.getRoles().add(role);

        User writer = realm.addUser(CREDENTIALS_WRITER);
        writer.getRoles().add(realm.getRole(Role.ALL_OBJECTS_WRITER));
      }

      private Access getAccess(CDOPermission permission)
      {
        switch (permission)
        {
        case READ:
          return Access.READ;

        case WRITE:
          return Access.WRITE;

        default:
          return null;
        }
      }
    });

    CDOSession sessionWriter = openSession(CREDENTIALS_WRITER);
    CDOTransaction transactionWriter = sessionWriter.openTransaction();
    CDOResource resourceWriter = transactionWriter.getResource(pathResource1);
    CDOResourceFolder targetFolderWriter = transactionWriter.getResourceFolder(pathFolder2);

    CDOSession session = openSession(CREDENTIALS);
    session.options().setPassiveUpdateMode(PassiveUpdateMode.ADDITIONS);

    CDOTransaction transaction = session.openTransaction();
    assertPermission(from, resourceWriter, transaction); // Pre check

    targetFolderWriter.getNodes().add(resourceWriter); // Move
    commitAndSync(transactionWriter, transaction); // Commit + invalidate
    assertPermission(to, resourceWriter, transaction); // Post check
  }

  private static void assertPermission(CDOPermission expected, CDOResource resourceWriter, CDOTransaction transaction)
  {
    if (expected == CDOPermission.NONE)
    {
      try
      {
        transaction.getObject(resourceWriter);
        fail("Exception expected");
      }
      catch (Exception ex)
      {
        // SUCCESS
      }
    }
    else
    {
      CDOResource resource = transaction.getObject(resourceWriter);
      assertEquals(expected, resource.cdoPermission());

      Company company = (Company)resource.getContents().get(0);
      assertEquals(expected, CDOUtil.getCDOObject(company).cdoPermission());
    }
  }

  private ISecurityManager startSecureRepository(ISecurityManager.RealmOperation operation)
  {
    ISecurityManager securityManager = SecurityManagerUtil.createSecurityManager("/security", getServerContainer());

    // Start repository
    getTestProperties().put(RepositoryConfig.PROP_TEST_SECURITY_MANAGER, securityManager);
    getRepository();

    securityManager.modify(operation);
    return securityManager;
  }
}

Back to the top