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.GroupPrincipal;
024: import org.apache.jetspeed.security.SecurityException;
025: import org.apache.jetspeed.security.impl.GroupPrincipalImpl;
026: import org.apache.jetspeed.security.om.InternalGroupPrincipal;
027: import org.apache.jetspeed.security.om.impl.InternalGroupPrincipalImpl;
028: import org.apache.jetspeed.security.spi.GroupSecurityHandler;
029: import org.apache.jetspeed.security.spi.SecurityAccess;
030:
031: /**
032: * @see org.apache.jetspeed.security.spi.GroupSecurityHandler
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 DefaultGroupSecurityHandler implements
037: GroupSecurityHandler {
038:
039: /** Common queries. */
040: private SecurityAccess commonQueries = null;
041:
042: /**
043: * <p>
044: * Constructor providing access to the common queries.
045: * </p>
046: */
047: public DefaultGroupSecurityHandler(SecurityAccess commonQueries) {
048: this .commonQueries = commonQueries;
049: }
050:
051: /**
052: * @see org.apache.jetspeed.security.spi.GroupSecurityHandler#getGroupPrincipal(java.lang.String)
053: */
054: public GroupPrincipal getGroupPrincipal(String groupFullPathName) {
055: GroupPrincipal groupPrincipal = null;
056: InternalGroupPrincipal internalGroup = commonQueries
057: .getInternalGroupPrincipal(GroupPrincipalImpl
058: .getFullPathFromPrincipalName(groupFullPathName));
059: if (null != internalGroup) {
060: groupPrincipal = new GroupPrincipalImpl(GroupPrincipalImpl
061: .getPrincipalNameFromFullPath(internalGroup
062: .getFullPath()), internalGroup.isEnabled(),
063: internalGroup.isMappingOnly());
064: }
065: return groupPrincipal;
066: }
067:
068: /**
069: * @see org.apache.jetspeed.security.spi.GroupSecurityHandler#setGroupPrincipal(org.apache.jetspeed.security.GroupPrincipal)
070: */
071: public void setGroupPrincipal(GroupPrincipal groupPrincipal)
072: throws SecurityException {
073: String fullPath = groupPrincipal.getFullPath();
074: InternalGroupPrincipal internalGroup = commonQueries
075: .getInternalGroupPrincipal(fullPath);
076: if (null == internalGroup) {
077: internalGroup = new InternalGroupPrincipalImpl(fullPath);
078: internalGroup.setEnabled(groupPrincipal.isEnabled());
079: commonQueries.setInternalGroupPrincipal(internalGroup,
080: false);
081: } else if (!internalGroup.isMappingOnly()) {
082: if (internalGroup.isEnabled() != groupPrincipal.isEnabled()) {
083: internalGroup.setEnabled(groupPrincipal.isEnabled());
084: commonQueries.setInternalGroupPrincipal(internalGroup,
085: false);
086: }
087: } else {
088: // TODO: should we throw an exception here?
089: }
090: }
091:
092: /**
093: * @see org.apache.jetspeed.security.spi.GroupSecurityHandler#removeGroupPrincipal(org.apache.jetspeed.security.GroupPrincipal)
094: */
095: public void removeGroupPrincipal(GroupPrincipal groupPrincipal)
096: throws SecurityException {
097: InternalGroupPrincipal internalGroup = commonQueries
098: .getInternalGroupPrincipal(groupPrincipal.getFullPath());
099: if (null != internalGroup) {
100: commonQueries.removeInternalGroupPrincipal(internalGroup);
101: }
102: }
103:
104: /**
105: * @see org.apache.jetspeed.security.spi.GroupSecurityHandler#getGroupPrincipals(java.lang.String)
106: */
107: public List getGroupPrincipals(String filter) {
108: List groupPrincipals = new LinkedList();
109: Iterator result = commonQueries
110: .getInternalGroupPrincipals(filter);
111: while (result.hasNext()) {
112: InternalGroupPrincipal internalGroup = (InternalGroupPrincipal) result
113: .next();
114: String path = internalGroup.getFullPath();
115: if (path == null) {
116: continue;
117: }
118: groupPrincipals
119: .add(new GroupPrincipalImpl(GroupPrincipalImpl
120: .getPrincipalNameFromFullPath(internalGroup
121: .getFullPath()), internalGroup
122: .isEnabled(), internalGroup.isMappingOnly()));
123: }
124: return groupPrincipals;
125: }
126:
127: }
|