]> git.argeo.org Git - lgpl/argeo-commons.git/blob - JackrabbitAuthorizations.java
e880b6700fcbfb85c405be766c5d9561c22f5236
[lgpl/argeo-commons.git] / JackrabbitAuthorizations.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.jackrabbit;
17
18 import java.security.Principal;
19 import java.util.Arrays;
20 import java.util.List;
21
22 import javax.jcr.RepositoryException;
23 import javax.jcr.Session;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.jackrabbit.api.JackrabbitSession;
28 import org.apache.jackrabbit.api.security.user.Authorizable;
29 import org.apache.jackrabbit.api.security.user.UserManager;
30 import org.argeo.ArgeoException;
31 import org.argeo.jcr.security.JcrAuthorizations;
32
33 /** Apply authorizations to a Jackrabbit repository. */
34 public class JackrabbitAuthorizations extends JcrAuthorizations {
35 private final static Log log = LogFactory
36 .getLog(JackrabbitAuthorizations.class);
37
38 private List<String> groupPrefixes = Arrays
39 .asList(new String[] { "ROLE_" });// new ArrayList<String>();
40
41 @Override
42 protected Principal getOrCreatePrincipal(Session session,
43 String principalName) throws RepositoryException {
44 UserManager um = ((JackrabbitSession) session).getUserManager();
45 synchronized (um) {
46 Authorizable authorizable = um.getAuthorizable(principalName);
47 if (authorizable == null) {
48 groupPrefixes: for (String groupPrefix : groupPrefixes) {
49 if (principalName.startsWith(groupPrefix)) {
50 authorizable = um.createGroup(principalName);
51 log.info("Created group " + principalName);
52 break groupPrefixes;
53 }
54 }
55 if (authorizable == null)
56 throw new ArgeoException("Authorizable " + principalName
57 + " not found");
58 }
59 return authorizable.getPrincipal();
60 }
61 }
62
63 public void setGroupPrefixes(List<String> groupsToCreate) {
64 this.groupPrefixes = groupsToCreate;
65 }
66 }