001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.security.spi.impl;
018:
019: import java.util.Iterator;
020: import java.util.LinkedList;
021: import java.util.List;
022:
023: import org.apache.jetspeed.security.RolePrincipal;
024: import org.apache.jetspeed.security.SecurityException;
025: import org.apache.jetspeed.security.impl.RolePrincipalImpl;
026: import org.apache.jetspeed.security.om.InternalRolePrincipal;
027: import org.apache.jetspeed.security.om.impl.InternalRolePrincipalImpl;
028: import org.apache.jetspeed.security.spi.RoleSecurityHandler;
029: import org.apache.jetspeed.security.spi.SecurityAccess;
030:
031: /**
032: * @see org.apache.jetspeed.security.spi.RoleSecurityHandler
033: * @author <a href="mailto:dlestrat@apache.org">David Le Strat </a>
034: * @author <a href="mailto:taylor@apache.org">David Sean Taylor </a>
035: */
036: public class DefaultRoleSecurityHandler implements RoleSecurityHandler {
037:
038: /** Common queries. */
039: private SecurityAccess commonQueries = null;
040:
041: /**
042: * <p>
043: * Constructor providing access to the common queries.
044: * </p>
045: */
046: public DefaultRoleSecurityHandler(SecurityAccess commonQueries) {
047: this .commonQueries = commonQueries;
048: }
049:
050: /**
051: * @see org.apache.jetspeed.security.spi.RoleSecurityHandler#getRolePrincipal(java.lang.String)
052: */
053: public RolePrincipal getRolePrincipal(String roleFullPathName) {
054: RolePrincipal rolePrincipal = null;
055: InternalRolePrincipal internalRole = commonQueries
056: .getInternalRolePrincipal(RolePrincipalImpl
057: .getFullPathFromPrincipalName(roleFullPathName));
058: if (null != internalRole) {
059: rolePrincipal = new RolePrincipalImpl(RolePrincipalImpl
060: .getPrincipalNameFromFullPath(internalRole
061: .getFullPath()), internalRole.isEnabled(),
062: internalRole.isMappingOnly());
063: }
064: return rolePrincipal;
065: }
066:
067: /**
068: * @see org.apache.jetspeed.security.spi.RoleSecurityHandler#setRolePrincipal(org.apache.jetspeed.security.RolePrincipal)
069: */
070: public void setRolePrincipal(RolePrincipal rolePrincipal)
071: throws SecurityException {
072: String fullPath = rolePrincipal.getFullPath();
073: InternalRolePrincipal internalRole = commonQueries
074: .getInternalRolePrincipal(fullPath);
075: if (null == internalRole) {
076: internalRole = new InternalRolePrincipalImpl(fullPath);
077: internalRole.setEnabled(rolePrincipal.isEnabled());
078: commonQueries.setInternalRolePrincipal(internalRole, false);
079: } else if (!internalRole.isMappingOnly()) {
080: if (internalRole.isEnabled() != rolePrincipal.isEnabled()) {
081: internalRole.setEnabled(rolePrincipal.isEnabled());
082: commonQueries.setInternalRolePrincipal(internalRole,
083: false);
084: }
085: } else {
086: // TODO: should we throw an exception here?
087: }
088: }
089:
090: /**
091: * @see org.apache.jetspeed.security.spi.RoleSecurityHandler#removeRolePrincipal(org.apache.jetspeed.security.RolePrincipal)
092: */
093: public void removeRolePrincipal(RolePrincipal rolePrincipal)
094: throws SecurityException {
095: InternalRolePrincipal internalRole = commonQueries
096: .getInternalRolePrincipal(rolePrincipal.getFullPath());
097: if (null != internalRole) {
098: commonQueries.removeInternalRolePrincipal(internalRole);
099: }
100: }
101:
102: /**
103: * @see org.apache.jetspeed.security.spi.RoleSecurityHandler#getRolePrincipals(java.lang.String)
104: */
105: public List getRolePrincipals(String filter) {
106: List rolePrincipals = new LinkedList();
107: Iterator result = commonQueries
108: .getInternalRolePrincipals(filter);
109: while (result.hasNext()) {
110: InternalRolePrincipal internalRole = (InternalRolePrincipal) result
111: .next();
112: String path = internalRole.getFullPath();
113: if (path == null) {
114: continue;
115: }
116: rolePrincipals.add(new RolePrincipalImpl(RolePrincipalImpl
117: .getPrincipalNameFromFullPath(internalRole
118: .getFullPath())));
119: }
120: return rolePrincipals;
121: }
122:
123: }
|