View Javadoc

1   /* 
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.security.spi.impl;
18  
19  import java.security.Principal;
20  import java.util.Iterator;
21  import java.util.LinkedList;
22  import java.util.List;
23  
24  import org.apache.jetspeed.security.SecurityException;
25  import org.apache.jetspeed.security.UserPrincipal;
26  import org.apache.jetspeed.security.impl.UserPrincipalImpl;
27  import org.apache.jetspeed.security.om.InternalUserPrincipal;
28  import org.apache.jetspeed.security.om.impl.InternalUserPrincipalImpl;
29  import org.apache.jetspeed.security.spi.SecurityAccess;
30  import org.apache.jetspeed.security.spi.UserSecurityHandler;
31  
32  /***
33   * @see org.apache.jetspeed.security.spi.UserSecurityHandler
34   * @author <a href="mailto:dlestrat@apache.org">David Le Strat</a>
35   */
36  public class DefaultUserSecurityHandler implements UserSecurityHandler
37  {
38      /*** SecurityAccess. */
39      private SecurityAccess securityAccess = null;
40      
41      /***
42       * <p>Constructor providing access to the SecurityAccess implementation.</p>
43       */
44      public DefaultUserSecurityHandler(SecurityAccess securityAccess)
45      {
46          this.securityAccess = securityAccess;
47      }
48      
49      /***
50       * @see org.apache.jetspeed.security.spi.UserSecurityHandler#isUserPrincipal(java.lang.String)
51       */
52      public boolean isUserPrincipal(String userName)
53      {
54          return securityAccess.isKnownUser(userName);
55      }
56      
57      /***
58       * @see org.apache.jetspeed.security.spi.UserSecurityHandler#getUserPrincipal(java.lang.String)
59       */
60      public Principal getUserPrincipal(String username)
61      {
62          UserPrincipal userPrincipal = null;
63          InternalUserPrincipal internalUser = securityAccess.getInternalUserPrincipal(username, false);
64          if (null != internalUser)
65          {
66              userPrincipal = new UserPrincipalImpl(UserPrincipalImpl.getPrincipalNameFromFullPath(internalUser.getFullPath()), true, internalUser.isMappingOnly());
67              userPrincipal.setEnabled(internalUser.isEnabled());
68          }
69          return userPrincipal;
70      }
71      
72      /***
73       * @see org.apache.jetspeed.security.spi.UserSecurityHandler#getUserPrincipals(java.lang.String)
74       */
75      public List getUserPrincipals(String filter)
76      {
77          List userPrincipals = new LinkedList();
78          Iterator result = securityAccess.getInternalUserPrincipals(filter);
79          while (result.hasNext())
80          {
81              InternalUserPrincipal internalUser = (InternalUserPrincipal) result.next();
82              String path = internalUser.getFullPath();
83              if (path == null)
84              {
85                  continue;
86              }
87              UserPrincipal userPrincipal = new UserPrincipalImpl(UserPrincipalImpl.getPrincipalNameFromFullPath(internalUser.getFullPath()));
88              userPrincipal.setEnabled(internalUser.isEnabled());
89              userPrincipals.add(userPrincipal);
90          }
91          return userPrincipals;
92      }
93  
94      /***
95       * @see org.apache.jetspeed.security.spi.UserSecurityHandler#addUserPrincipal(org.apache.jetspeed.security.UserPrincipal)
96       */
97      public void addUserPrincipal(UserPrincipal userPrincipal) throws SecurityException
98      {
99          if ( null == securityAccess.getInternalUserPrincipal(userPrincipal.getName(), false) )
100         {
101             securityAccess.setInternalUserPrincipal(new InternalUserPrincipalImpl(userPrincipal.getFullPath()), false);        
102         }
103         else
104         {
105             throw new SecurityException(SecurityException.USER_ALREADY_EXISTS.create(userPrincipal.getName()));
106         }
107     }
108     
109     /***
110      * @see org.apache.jetspeed.security.spi.UserSecurityHandler#updateUserPrincipal(org.apache.jetspeed.security.UserPrincipal)
111      */
112     public void updateUserPrincipal(UserPrincipal userPrincipal) throws SecurityException
113     {
114         InternalUserPrincipal internalUser = securityAccess.getInternalUserPrincipal(userPrincipal.getName(), false);
115         if ( null != internalUser )
116         {
117             if ( internalUser.isEnabled() != userPrincipal.isEnabled())
118             {
119                 internalUser.setEnabled(userPrincipal.isEnabled());
120                 securityAccess.setInternalUserPrincipal(internalUser, false);        
121             }
122         }
123         else
124         {
125             throw new SecurityException(SecurityException.USER_DOES_NOT_EXIST.create(userPrincipal.getName()));
126         }
127     }
128     
129     /***
130      * @see org.apache.jetspeed.security.spi.UserSecurityHandler#removeUserPrincipal(org.apache.jetspeed.security.UserPrincipal)
131      */
132     public void removeUserPrincipal(UserPrincipal userPrincipal) throws SecurityException
133     {
134         InternalUserPrincipal internalUser = securityAccess.getInternalUserPrincipal(userPrincipal.getName(), false);
135         if (null != internalUser)
136         {
137             securityAccess.removeInternalUserPrincipal(internalUser);
138         }
139         else
140         {
141             internalUser = securityAccess.getInternalUserPrincipal(userPrincipal.getName(), true);
142             if (null != internalUser)
143             {
144                 securityAccess.removeInternalUserPrincipal(internalUser);
145             }
146         }
147     }
148 
149 }