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.security.Principal;
020: import java.util.Iterator;
021: import java.util.LinkedList;
022: import java.util.List;
023:
024: import org.apache.jetspeed.security.SecurityException;
025: import org.apache.jetspeed.security.UserPrincipal;
026: import org.apache.jetspeed.security.impl.UserPrincipalImpl;
027: import org.apache.jetspeed.security.om.InternalUserPrincipal;
028: import org.apache.jetspeed.security.om.impl.InternalUserPrincipalImpl;
029: import org.apache.jetspeed.security.spi.SecurityAccess;
030: import org.apache.jetspeed.security.spi.UserSecurityHandler;
031:
032: /**
033: * @see org.apache.jetspeed.security.spi.UserSecurityHandler
034: * @author <a href="mailto:dlestrat@apache.org">David Le Strat</a>
035: */
036: public class DefaultUserSecurityHandler implements UserSecurityHandler {
037: /** SecurityAccess. */
038: private SecurityAccess securityAccess = null;
039:
040: /**
041: * <p>Constructor providing access to the SecurityAccess implementation.</p>
042: */
043: public DefaultUserSecurityHandler(SecurityAccess securityAccess) {
044: this .securityAccess = securityAccess;
045: }
046:
047: /**
048: * @see org.apache.jetspeed.security.spi.UserSecurityHandler#isUserPrincipal(java.lang.String)
049: */
050: public boolean isUserPrincipal(String userName) {
051: return securityAccess.isKnownUser(userName);
052: }
053:
054: /**
055: * @see org.apache.jetspeed.security.spi.UserSecurityHandler#getUserPrincipal(java.lang.String)
056: */
057: public Principal getUserPrincipal(String username) {
058: UserPrincipal userPrincipal = null;
059: InternalUserPrincipal internalUser = securityAccess
060: .getInternalUserPrincipal(username, false);
061: if (null != internalUser) {
062: userPrincipal = new UserPrincipalImpl(UserPrincipalImpl
063: .getPrincipalNameFromFullPath(internalUser
064: .getFullPath()), true, internalUser
065: .isMappingOnly());
066: userPrincipal.setEnabled(internalUser.isEnabled());
067: }
068: return userPrincipal;
069: }
070:
071: /**
072: * @see org.apache.jetspeed.security.spi.UserSecurityHandler#getUserPrincipals(java.lang.String)
073: */
074: public List getUserPrincipals(String filter) {
075: List userPrincipals = new LinkedList();
076: Iterator result = securityAccess
077: .getInternalUserPrincipals(filter);
078: while (result.hasNext()) {
079: InternalUserPrincipal internalUser = (InternalUserPrincipal) result
080: .next();
081: String path = internalUser.getFullPath();
082: if (path == null) {
083: continue;
084: }
085: UserPrincipal userPrincipal = new UserPrincipalImpl(
086: UserPrincipalImpl
087: .getPrincipalNameFromFullPath(internalUser
088: .getFullPath()));
089: userPrincipal.setEnabled(internalUser.isEnabled());
090: userPrincipals.add(userPrincipal);
091: }
092: return userPrincipals;
093: }
094:
095: /**
096: * @see org.apache.jetspeed.security.spi.UserSecurityHandler#addUserPrincipal(org.apache.jetspeed.security.UserPrincipal)
097: */
098: public void addUserPrincipal(UserPrincipal userPrincipal)
099: throws SecurityException {
100: if (null == securityAccess.getInternalUserPrincipal(
101: userPrincipal.getName(), false)) {
102: securityAccess.setInternalUserPrincipal(
103: new InternalUserPrincipalImpl(userPrincipal
104: .getFullPath()), false);
105: } else {
106: throw new SecurityException(
107: SecurityException.USER_ALREADY_EXISTS
108: .create(userPrincipal.getName()));
109: }
110: }
111:
112: /**
113: * @see org.apache.jetspeed.security.spi.UserSecurityHandler#updateUserPrincipal(org.apache.jetspeed.security.UserPrincipal)
114: */
115: public void updateUserPrincipal(UserPrincipal userPrincipal)
116: throws SecurityException {
117: InternalUserPrincipal internalUser = securityAccess
118: .getInternalUserPrincipal(userPrincipal.getName(),
119: false);
120: if (null != internalUser) {
121: if (internalUser.isEnabled() != userPrincipal.isEnabled()) {
122: internalUser.setEnabled(userPrincipal.isEnabled());
123: securityAccess.setInternalUserPrincipal(internalUser,
124: false);
125: }
126: } else {
127: throw new SecurityException(
128: SecurityException.USER_DOES_NOT_EXIST
129: .create(userPrincipal.getName()));
130: }
131: }
132:
133: /**
134: * @see org.apache.jetspeed.security.spi.UserSecurityHandler#removeUserPrincipal(org.apache.jetspeed.security.UserPrincipal)
135: */
136: public void removeUserPrincipal(UserPrincipal userPrincipal)
137: throws SecurityException {
138: InternalUserPrincipal internalUser = securityAccess
139: .getInternalUserPrincipal(userPrincipal.getName(),
140: false);
141: if (null != internalUser) {
142: securityAccess.removeInternalUserPrincipal(internalUser);
143: } else {
144: internalUser = securityAccess.getInternalUserPrincipal(
145: userPrincipal.getName(), true);
146: if (null != internalUser) {
147: securityAccess
148: .removeInternalUserPrincipal(internalUser);
149: }
150: }
151: }
152:
153: }
|