Skip to content

Commit

Permalink
Improvements for UserManagement implementation:
Browse files Browse the repository at this point in the history
- move configuration code to UserManagerConfig
- rename public method getGroupMembershipSplitSize to getMemberSplitSize
- add method hasMemberSplitSize and replace usage of getGroupMembershipSplitSize() > 0
- remove old class name from javadoc in NodeCreationTest

git-svn-id: https://svn.apache.org/repos/asf/jackrabbit/trunk@1370326 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
anchela committed Aug 7, 2012
1 parent dd7144e commit f4e387b
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 133 deletions.
Expand Up @@ -462,12 +462,6 @@ protected MembershipCache getMembershipCache(SessionImpl session) throws Reposit
*/
protected UserManagerImpl createUserManager(SessionImpl session) throws RepositoryException {
UserManagerConfig umc = getConfig().getUserManagerConfig();
Properties params = (umc == null) ? null : umc.getParameters();

// since users are stored in and retrieved from a dedicated workspace
// only the system session assigned with that workspace will get the
// system user manager (special implementation that asserts the existence
// of the admin user).
UserManagerImpl um;
if (umc != null) {
Class<?>[] paramTypes = new Class[] {
Expand All @@ -476,12 +470,9 @@ protected UserManagerImpl createUserManager(SessionImpl session) throws Reposito
Properties.class,
MembershipCache.class};
um = (UserManagerImpl) umc.getUserManager(UserManagerImpl.class,
paramTypes, session, adminId, params, getMembershipCache(session));
// TODO: should we make sure the implementation doesn't allow
// TODO: to change the autosave behavior? since the user manager
// TODO: writes to a separate workspace this would cause troubles.
paramTypes, session, adminId, umc.getParameters(), getMembershipCache(session));
} else {
um = new UserManagerImpl(session, adminId, params, getMembershipCache(session));
um = new UserManagerImpl(session, adminId, null, getMembershipCache(session));
}

if (umc != null && !(session instanceof SystemSession)) {
Expand Down
Expand Up @@ -219,8 +219,6 @@ protected UserManager getSystemUserManager(String workspaceName) throws Reposito
@Override
protected UserManagerImpl createUserManager(SessionImpl session) throws RepositoryException {
UserManagerConfig umc = getConfig().getUserManagerConfig();
Properties params = (umc == null) ? null : umc.getParameters();

UserManagerImpl umgr;
// in contrast to the DefaultSecurityManager users are not retrieved
// from a dedicated workspace: the system session of each workspace must
Expand All @@ -232,9 +230,9 @@ protected UserManagerImpl createUserManager(SessionImpl session) throws Reposito
Properties.class,
MembershipCache.class};
umgr = (UserPerWorkspaceUserManager) umc.getUserManager(UserPerWorkspaceUserManager.class,
paramTypes, session, adminId, params, getMembershipCache(session));
paramTypes, session, adminId, umc.getParameters(), getMembershipCache(session));
} else {
umgr = new UserPerWorkspaceUserManager(session, adminId, params, getMembershipCache(session));
umgr = new UserPerWorkspaceUserManager(session, adminId, null, getMembershipCache(session));
}

if (umc != null && !(session instanceof SystemSession)) {
Expand Down
Expand Up @@ -202,7 +202,7 @@ public boolean removeMember(Authorizable authorizable) throws RepositoryExceptio
*/
private MembershipProvider getMembershipProvider(NodeImpl node) throws RepositoryException {
MembershipProvider msp;
if (userManager.getGroupMembershipSplitSize() > 0) {
if (userManager.hasMemberSplitSize()) {
if (node.hasNode(N_MEMBERS) || !node.hasProperty(P_MEMBERS)) {
msp = new NodeBasedMembershipProvider(node);
} else {
Expand All @@ -214,7 +214,7 @@ private MembershipProvider getMembershipProvider(NodeImpl node) throws Repositor

if (node.hasProperty(P_MEMBERS) && node.hasNode(N_MEMBERS)) {
log.warn("Found members node and members property on node {}. Ignoring {} members", node,
userManager.getGroupMembershipSplitSize() > 0 ? "property" : "node");
userManager.hasMemberSplitSize() ? "property" : "node");
}

return msp;
Expand Down Expand Up @@ -267,7 +267,7 @@ private String safeGetID() {

static PropertySequence getPropertySequence(Node nMembers, UserManagerImpl userManager) throws RepositoryException {
Comparator<String> order = Rank.comparableComparator();
int maxChildren = userManager.getGroupMembershipSplitSize();
int maxChildren = userManager.getMemberSplitSize();
int minChildren = maxChildren / 2;

TreeManager treeManager = new BTreeManager(nMembers, minChildren, maxChildren, order,
Expand Down
Expand Up @@ -182,7 +182,7 @@ public void init(Session systemSession, Map configuration) throws RepositoryExce
usersPath = (uMgr instanceof UserManagerImpl) ? ((UserManagerImpl) uMgr).getUsersPath() : UserConstants.USERS_PATH;
groupsPath = (uMgr instanceof UserManagerImpl) ? ((UserManagerImpl) uMgr).getGroupsPath() : UserConstants.GROUPS_PATH;

membersInProperty = (!(uMgr instanceof UserManagerImpl)) || ((UserManagerImpl) uMgr).getGroupMembershipSplitSize() <= 0;
membersInProperty = !(uMgr instanceof UserManagerImpl) || !((UserManagerImpl) uMgr).hasMemberSplitSize();

if (configuration.containsKey(PARAM_ANONYMOUS_ID)) {
anonymousId = (String) configuration.get(PARAM_ANONYMOUS_ID);
Expand Down
Expand Up @@ -440,7 +440,7 @@ public void processReferences() throws RepositoryException {
log.info("ImportBehavior.BESTEFFORT: Found " + nonExisting.size() + " entries of rep:members pointing to non-existing authorizables. Adding to rep:members.");
final NodeImpl groupNode = ((AuthorizableImpl) gr).getNode();

if (userManager.getGroupMembershipSplitSize() > 0) {
if (userManager.hasMemberSplitSize()) {
userManager.performProtectedOperation((SessionImpl) session, new SessionWriteOperation<Object>() {
public Boolean perform(SessionContext context) throws RepositoryException {
NodeImpl nMembers = (groupNode.hasNode(UserConstants.N_MEMBERS)
Expand Down
@@ -0,0 +1,102 @@
/*
* 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.jackrabbit.core.security.user;

import org.apache.jackrabbit.core.security.user.action.AuthorizableAction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Properties;

/**
* Utility to retrieve configuration parameters for UserManagerImpl
*/
class UserManagerConfig {

private static final Logger log = LoggerFactory.getLogger(UserManagerImpl.class);

private final Properties config;
private final String adminId;
/**
* Authorizable actions that will all be executed upon creation and removal
* of authorizables in the order they are contained in the array.<p/>
* Note, that if {@link #isAutoSave() autosave} is turned on, the configured
* actions are executed before persisting the creation or removal.
*/
private AuthorizableAction[] actions;

UserManagerConfig(Properties config, String adminId, AuthorizableAction[] actions) {
this.config = config;
this.adminId = adminId;
this.actions = (actions == null) ? new AuthorizableAction[0] : actions;
}

public <T> T getConfigValue(String key, T defaultValue) {
if (config != null && config.containsKey(key)) {
return convert(config.get(key), defaultValue);
} else {
return defaultValue;
}
}

public String getAdminId() {
return adminId;
}

public AuthorizableAction[] getAuthorizableActions() {
return actions;
}

public void setAuthorizableActions(AuthorizableAction[] actions) {
if (actions != null) {
this.actions = actions;
}
}

//--------------------------------------------------------< private >---
private <T> T convert(Object v, T defaultValue) {
if (v == null) {
return null;
}

T value;
String str = v.toString();
Class targetClass = (defaultValue == null) ? String.class : defaultValue.getClass();
try {
if (targetClass == String.class) {
value = (T) str;
} else if (targetClass == Integer.class) {
value = (T) Integer.valueOf(str);
} else if (targetClass == Long.class) {
value = (T) Long.valueOf(str);
} else if (targetClass == Double.class) {
value = (T) Double.valueOf(str);
} else if (targetClass == Boolean.class) {
value = (T) Boolean.valueOf(str);
} else {
// unsupported target type
log.warn("Unsupported target type {} for value {}", targetClass.getName(), v);
throw new IllegalArgumentException("Cannot convert config entry " + v + " to " + targetClass.getName());
}
} catch (NumberFormatException e) {
log.warn("Invalid value {}; cannot be parsed into {}", v, targetClass.getName());
value = defaultValue;
}

return value;
}
}

0 comments on commit f4e387b

Please sign in to comment.