Hi Bertrand Maybe this module and Java package should be called "jackrabbit" rather than "jcr"?
As I understand it, this module is not really Sling-specific. So calling it "jcr" might be misleading to someone trying to use it with a non-Jackrabbit JCR implementation. WDYT? Regards Julian On Mon, Dec 21, 2015 at 10:49 AM, <[email protected]> wrote: > Author: bdelacretaz > Date: Mon Dec 21 09:49:23 2015 > New Revision: 1721113 > > URL: http://svn.apache.org/viewvc?rev=1721113&view=rev > Log: > SLING-5355 - jcr module added, work in progress > > Added: > sling/trunk/contrib/extensions/acldef/jcr/ (with props) > sling/trunk/contrib/extensions/acldef/jcr/pom.xml > sling/trunk/contrib/extensions/acldef/jcr/src/ > sling/trunk/contrib/extensions/acldef/jcr/src/main/ > sling/trunk/contrib/extensions/acldef/jcr/src/main/java/ > sling/trunk/contrib/extensions/acldef/jcr/src/main/java/org/ > sling/trunk/contrib/extensions/acldef/jcr/src/main/java/org/apache/ > sling/trunk/contrib/extensions/acldef/jcr/src/main/java/org/apache/sling/ > > sling/trunk/contrib/extensions/acldef/jcr/src/main/java/org/apache/sling/acldef/ > > sling/trunk/contrib/extensions/acldef/jcr/src/main/java/org/apache/sling/acldef/jcr/ > > sling/trunk/contrib/extensions/acldef/jcr/src/main/java/org/apache/sling/acldef/jcr/AclOperationVisitor.java > > sling/trunk/contrib/extensions/acldef/jcr/src/main/java/org/apache/sling/acldef/jcr/ServiceUserUtil.java > sling/trunk/contrib/extensions/acldef/jcr/src/test/ > sling/trunk/contrib/extensions/acldef/jcr/src/test/java/ > sling/trunk/contrib/extensions/acldef/jcr/src/test/java/org/ > sling/trunk/contrib/extensions/acldef/jcr/src/test/java/org/apache/ > sling/trunk/contrib/extensions/acldef/jcr/src/test/java/org/apache/sling/ > > sling/trunk/contrib/extensions/acldef/jcr/src/test/java/org/apache/sling/acldef/ > > sling/trunk/contrib/extensions/acldef/jcr/src/test/java/org/apache/sling/acldef/jcr/ > > sling/trunk/contrib/extensions/acldef/jcr/src/test/java/org/apache/sling/acldef/jcr/CreateServiceUsersTest.java > Modified: > > sling/trunk/contrib/extensions/acldef/parser/src/main/java/org/apache/sling/acldef/parser/operations/ServiceUserOperation.java > > Propchange: sling/trunk/contrib/extensions/acldef/jcr/ > ------------------------------------------------------------------------------ > --- svn:ignore (added) > +++ svn:ignore Mon Dec 21 09:49:23 2015 > @@ -0,0 +1,14 @@ > +target > +bin > +derby.log > +*.iml > +*.ipr > +*.iws > +.settings > +.project > +.classpath > +.externalToolBuilders > +maven-eclipse.xml > +sling > + > + > > Added: sling/trunk/contrib/extensions/acldef/jcr/pom.xml > URL: > http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/acldef/jcr/pom.xml?rev=1721113&view=auto > ============================================================================== > --- sling/trunk/contrib/extensions/acldef/jcr/pom.xml (added) > +++ sling/trunk/contrib/extensions/acldef/jcr/pom.xml Mon Dec 21 09:49:23 2015 > @@ -0,0 +1,128 @@ > +<?xml version="1.0" encoding="ISO-8859-1"?> > +<!-- > + Licensed to the Apache Software Foundation (ASF) under one > + or more contributor license agreements. See the NOTICE file > + distributed with this work for additional information > + regarding copyright ownership. The ASF licenses this file > + to you under the Apache License, Version 2.0 (the > + "License"); you may not use this file except in compliance > + with the License. You may obtain a copy of the License at > + > + http://www.apache.org/licenses/LICENSE-2.0 > + > + Unless required by applicable law or agreed to in writing, > + software distributed under the License is distributed on an > + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY > + KIND, either express or implied. See the License for the > + specific language governing permissions and limitations > + under the License. > +--> > +<project xmlns="http://maven.apache.org/POM/4.0.0" > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" > xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 > http://maven.apache.org/maven-v4_0_0.xsd"> > + <modelVersion>4.0.0</modelVersion> > + > + <parent> > + <groupId>org.apache.sling</groupId> > + <artifactId>sling</artifactId> > + <version>25</version> > + <relativePath/> > + </parent> > + > + <artifactId>org.apache.sling.acldef.jcr</artifactId> > + <packaging>bundle</packaging> > + <version>0.0.1-SNAPSHOT</version> > + <name>Apache Sling ACL Definitions JCR module</name> > + <description> > + Applies ACLs to a JCR repository based on the output > + of the ACL Definition Language parser. > + </description> > + > + <scm> > + > <connection>scm:svn:https://svn.apache.org/repos/asf/sling/trunk/contrib/extensions/acldef-jcr</connection> > + > <developerConnection>scm:svn:https://svn.apache.org/repos/asf/sling/trunk/contrib/extensions/acldef-jcr</developerConnection> > + > <url>https://svn.apache.org/repos/asf/sling/trunk/contrib/extensions/acldef-jcr</url> > + </scm> > + > + <build> > + <plugins> > + <plugin> > + <groupId>org.apache.felix</groupId> > + <artifactId>maven-scr-plugin</artifactId> > + </plugin> > + <plugin> > + <groupId>org.apache.felix</groupId> > + <artifactId>maven-bundle-plugin</artifactId> > + <extensions>true</extensions> > + </plugin> > + </plugins> > + </build> > + > + <dependencies> > + <dependency> > + <groupId>org.slf4j</groupId> > + <artifactId>slf4j-api</artifactId> > + </dependency> > + <dependency> > + <groupId>org.slf4j</groupId> > + <artifactId>slf4j-simple</artifactId> > + <scope>test</scope> > + </dependency> > + <dependency> > + <groupId>javax.jcr</groupId> > + <artifactId>jcr</artifactId> > + <version>2.0</version> > + <scope>provided</scope> > + </dependency> > + <dependency> > + <groupId>org.apache.jackrabbit</groupId> > + <artifactId>jackrabbit-api</artifactId> > + <version>2.11.3</version> > + <scope>provided</scope> > + </dependency> > + <dependency> > + <groupId>org.apache.jackrabbit</groupId> > + <artifactId>jackrabbit-jcr-commons</artifactId> > + <version>2.2.9</version> > + <scope>provided</scope> > + </dependency> > + <dependency> > + <groupId>org.apache.sling</groupId> > + <artifactId>org.apache.sling.acldef.parser</artifactId> > + <version>0.0.1-SNAPSHOT</version> > + <scope>provided</scope> > + </dependency> > + <dependency> > + <groupId>junit</groupId> > + <artifactId>junit</artifactId> > + </dependency> > + <dependency> > + <groupId>commons-io</groupId> > + <artifactId>commons-io</artifactId> > + <version>2.4</version> > + <scope>test</scope> > + </dependency> > + <dependency> > + <groupId>org.apache.sling</groupId> > + <artifactId>org.apache.sling.testing.osgi-mock</artifactId> > + <version>1.6.0</version> > + <scope>test</scope> > + </dependency> > + <dependency> > + <groupId>org.apache.sling</groupId> > + <artifactId>org.apache.sling.testing.jcr-mock</artifactId> > + <version>1.1.10</version> > + <scope>test</scope> > + </dependency> > + <dependency> > + <groupId>org.apache.sling</groupId> > + <artifactId>org.apache.sling.testing.sling-mock</artifactId> > + <version>1.6.0</version> > + <scope>test</scope> > + </dependency> > + <dependency> > + <groupId>org.apache.sling</groupId> > + <artifactId>org.apache.sling.testing.sling-mock-oak</artifactId> > + <version>1.0.1-SNAPSHOT</version> > + <scope>test</scope> > + </dependency> > + </dependencies> > +</project> > > Added: > sling/trunk/contrib/extensions/acldef/jcr/src/main/java/org/apache/sling/acldef/jcr/AclOperationVisitor.java > URL: > http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/acldef/jcr/src/main/java/org/apache/sling/acldef/jcr/AclOperationVisitor.java?rev=1721113&view=auto > ============================================================================== > --- > sling/trunk/contrib/extensions/acldef/jcr/src/main/java/org/apache/sling/acldef/jcr/AclOperationVisitor.java > (added) > +++ > sling/trunk/contrib/extensions/acldef/jcr/src/main/java/org/apache/sling/acldef/jcr/AclOperationVisitor.java > Mon Dec 21 09:49:23 2015 > @@ -0,0 +1,82 @@ > +/* > + * Licensed to the Apache Software Foundation (ASF) under one or more > + * contributor license agreements. See the NOTICE file distributed with > + * this work for additional information regarding copyright ownership. > + * The ASF licenses this file to You under the Apache License, Version 2.0 > + * (the "License"); you may not use this file except in compliance with > + * the License. You may obtain a copy of the License at > + * > + * http://www.apache.org/licenses/LICENSE-2.0 > + * > + * Unless required by applicable law or agreed to in writing, software > + * distributed under the License is distributed on an "AS IS" BASIS, > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. > + * See the License for the specific language governing permissions and > + * limitations under the License. > + */ > +package org.apache.sling.acldef.jcr; > + > +import javax.jcr.Session; > + > +import org.apache.sling.acldef.parser.operations.CreateServiceUser; > +import org.apache.sling.acldef.parser.operations.DeleteServiceUser; > +import org.apache.sling.acldef.parser.operations.OperationVisitor; > +import org.apache.sling.acldef.parser.operations.SetAclPaths; > +import org.apache.sling.acldef.parser.operations.SetAclPrincipals; > +import org.slf4j.Logger; > +import org.slf4j.LoggerFactory; > + > +/** Processes the Operations produced by the ACL > + * definitions parser to create the users and > + * set the ACLs defined by the parser input. > + */ > +public class AclOperationVisitor implements OperationVisitor { > + > + private final Logger log = LoggerFactory.getLogger(getClass()); > + > + private Session session; > + > + /** Create a visitor using the supplied JCR Session. > + * @param s must have sufficient rights to create users > + * and set ACLs. > + */ > + public AclOperationVisitor(Session s) { > + session = s; > + } > + > + private void report(Exception e, String message) { > + throw new RuntimeException(message, e); > + } > + > + @Override > + public void visitCreateServiceUser(CreateServiceUser s) { > + final String id = s.getUsername(); > + log.info("Creating service user {}", id); > + try { > + ServiceUserUtil.createServiceUser(session, id); > + } catch(Exception e) { > + report(e, "Unable to create service user [" + id + "]:" + e); > + } > + } > + > + @Override > + public void visitDeleteServiceUser(DeleteServiceUser s) { > + final String id = s.getUsername(); > + log.info("Deleting service user {}", id); > + try { > + ServiceUserUtil.deleteServiceUser(session, id); > + } catch(Exception e) { > + report(e, "Unable to delete service user [" + id + "]:" + e); > + } > + } > + > + @Override > + public void visitSetAclPrincipal(SetAclPrincipals s) { > + log.warn("TODO - set ACL for Principals"); > + } > + > + @Override > + public void visitSetAclPaths(SetAclPaths s) { > + log.warn("TODO - set ACL for Paths"); > + } > +} > > Added: > sling/trunk/contrib/extensions/acldef/jcr/src/main/java/org/apache/sling/acldef/jcr/ServiceUserUtil.java > URL: > http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/acldef/jcr/src/main/java/org/apache/sling/acldef/jcr/ServiceUserUtil.java?rev=1721113&view=auto > ============================================================================== > --- > sling/trunk/contrib/extensions/acldef/jcr/src/main/java/org/apache/sling/acldef/jcr/ServiceUserUtil.java > (added) > +++ > sling/trunk/contrib/extensions/acldef/jcr/src/main/java/org/apache/sling/acldef/jcr/ServiceUserUtil.java > Mon Dec 21 09:49:23 2015 > @@ -0,0 +1,48 @@ > +/* > + * Licensed to the Apache Software Foundation (ASF) under one or more > + * contributor license agreements. See the NOTICE file distributed with > + * this work for additional information regarding copyright ownership. > + * The ASF licenses this file to You under the Apache License, Version 2.0 > + * (the "License"); you may not use this file except in compliance with > + * the License. You may obtain a copy of the License at > + * > + * http://www.apache.org/licenses/LICENSE-2.0 > + * > + * Unless required by applicable law or agreed to in writing, software > + * distributed under the License is distributed on an "AS IS" BASIS, > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. > + * See the License for the specific language governing permissions and > + * limitations under the License. > + */ > +package org.apache.sling.acldef.jcr; > + > +import javax.jcr.RepositoryException; > +import javax.jcr.Session; > + > +import org.apache.jackrabbit.api.JackrabbitSession; > +import org.apache.jackrabbit.api.security.user.Authorizable; > +import org.apache.jackrabbit.api.security.user.UserManager; > + > +/** Utilities for Service Users management */ > +public class ServiceUserUtil { > + > + public static UserManager getUserManager(Session session) throws > RepositoryException { > + if(!(session instanceof JackrabbitSession)) { > + throw new IllegalArgumentException("Session is not a > JackrabbitSession"); > + } > + return ((JackrabbitSession)session).getUserManager(); > + } > + > + public static void createServiceUser(Session s, String username) throws > RepositoryException { > + getUserManager(s).createSystemUser(username, null); > + } > + > + public static void deleteServiceUser(Session s, String username) throws > RepositoryException { > + final Authorizable a = getUserManager(s).getAuthorizable(username); > + if(a == null) { > + throw new IllegalStateException("Authorizable not found:" + > username); > + } > + a.remove(); > + } > + > +} > > Added: > sling/trunk/contrib/extensions/acldef/jcr/src/test/java/org/apache/sling/acldef/jcr/CreateServiceUsersTest.java > URL: > http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/acldef/jcr/src/test/java/org/apache/sling/acldef/jcr/CreateServiceUsersTest.java?rev=1721113&view=auto > ============================================================================== > --- > sling/trunk/contrib/extensions/acldef/jcr/src/test/java/org/apache/sling/acldef/jcr/CreateServiceUsersTest.java > (added) > +++ > sling/trunk/contrib/extensions/acldef/jcr/src/test/java/org/apache/sling/acldef/jcr/CreateServiceUsersTest.java > Mon Dec 21 09:49:23 2015 > @@ -0,0 +1,103 @@ > +/* > + * Licensed to the Apache Software Foundation (ASF) under one or more > + * contributor license agreements. See the NOTICE file distributed with > + * this work for additional information regarding copyright ownership. > + * The ASF licenses this file to You under the Apache License, Version 2.0 > + * (the "License"); you may not use this file except in compliance with > + * the License. You may obtain a copy of the License at > + * > + * http://www.apache.org/licenses/LICENSE-2.0 > + * > + * Unless required by applicable law or agreed to in writing, software > + * distributed under the License is distributed on an "AS IS" BASIS, > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. > + * See the License for the specific language governing permissions and > + * limitations under the License. > + */ > +package org.apache.sling.acldef.jcr; > + > +import static org.junit.Assert.assertNotNull; > +import static org.junit.Assert.assertNull; > + > +import java.io.Reader; > +import java.io.StringReader; > +import java.util.List; > +import java.util.concurrent.atomic.AtomicLong; > + > +import javax.jcr.RepositoryException; > +import javax.jcr.Session; > + > +import org.apache.commons.io.IOUtils; > +import org.apache.jackrabbit.api.security.user.Authorizable; > +import org.apache.jackrabbit.api.security.user.User; > +import org.apache.jackrabbit.api.security.user.UserManager; > +import org.apache.sling.acldef.parser.ACLDefinitions; > +import org.apache.sling.acldef.parser.ParseException; > +import org.apache.sling.acldef.parser.operations.Operation; > +import org.apache.sling.testing.mock.sling.ResourceResolverType; > +import org.apache.sling.testing.mock.sling.junit.SlingContext; > +import org.junit.After; > +import org.junit.Before; > +import org.junit.Rule; > +import org.junit.Test; > + > +/** Test the creation and delete of service users */ > +public class CreateServiceUsersTest { > + > + @Rule > + public final SlingContext context = new > SlingContext(ResourceResolverType.JCR_OAK); > + > + private Session session; > + private UserManager userManager; > + private AtomicLong counter = new AtomicLong(System.currentTimeMillis()); > + private long id; > + > + private List<Operation> parse(String input) throws ParseException { > + final Reader r = new StringReader(input); > + try { > + return new ACLDefinitions(r).parse(); > + } finally { > + IOUtils.closeQuietly(r); > + } > + } > + > + @Before > + public void setup() throws RepositoryException { > + session = context.resourceResolver().adaptTo(Session.class); > + userManager = ServiceUserUtil.getUserManager(session); > + id = counter.incrementAndGet(); > + } > + > + @After > + public void cleanup() { > + session.logout(); > + } > + > + private void assertServiceUser(String info, String id, boolean > expectToExist) throws RepositoryException { > + final Authorizable a = userManager.getAuthorizable(id); > + if(!expectToExist) { > + assertNull(info + ", expecting Principal to be absent:" + id, a); > + } else { > + assertNotNull(info + ", expecting Principal to exist:" + id, a); > + final User u = (User)a; > + assertNotNull(info + ", expecting Principal to be a System > user:" + id, u.isSystemUser()); > + } > + } > + > + private void exec(String input) throws ParseException { > + final AclOperationVisitor v = new AclOperationVisitor(session); > + for(Operation o : parse(input)) { > + o.accept(v); > + } > + } > + > + @Test > + public void createDeleteTest() throws Exception { > + final String userId = "bob_" + id; > + assertServiceUser("at start of test", userId, false); > + exec("create service user " + userId); > + assertServiceUser("affter creating user", userId, true); > + exec("delete service user " + userId); > + assertServiceUser("after deleting user", userId, false); > + } > +} > > Modified: > sling/trunk/contrib/extensions/acldef/parser/src/main/java/org/apache/sling/acldef/parser/operations/ServiceUserOperation.java > URL: > http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/acldef/parser/src/main/java/org/apache/sling/acldef/parser/operations/ServiceUserOperation.java?rev=1721113&r1=1721112&r2=1721113&view=diff > ============================================================================== > --- > sling/trunk/contrib/extensions/acldef/parser/src/main/java/org/apache/sling/acldef/parser/operations/ServiceUserOperation.java > (original) > +++ > sling/trunk/contrib/extensions/acldef/parser/src/main/java/org/apache/sling/acldef/parser/operations/ServiceUserOperation.java > Mon Dec 21 09:49:23 2015 > @@ -27,5 +27,8 @@ public abstract class ServiceUserOperati > protected String getParametersDescription() { > return username; > } > - > + > + public String getUsername() { > + return username; > + } > } > >
