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.util.Iterator;
20  import java.util.LinkedList;
21  import java.util.List;
22  
23  import org.apache.jetspeed.security.GroupPrincipal;
24  import org.apache.jetspeed.security.SecurityException;
25  import org.apache.jetspeed.security.impl.GroupPrincipalImpl;
26  import org.apache.jetspeed.security.om.InternalGroupPrincipal;
27  import org.apache.jetspeed.security.om.impl.InternalGroupPrincipalImpl;
28  import org.apache.jetspeed.security.spi.GroupSecurityHandler;
29  import org.apache.jetspeed.security.spi.SecurityAccess;
30  
31  /***
32   * @see org.apache.jetspeed.security.spi.GroupSecurityHandler
33   * @author <a href="mailto:dlestrat@apache.org">David Le Strat </a>
34   * @author <a href="mailto:taylor@apache.org">David Sean Taylor </a>
35   */
36  public class DefaultGroupSecurityHandler implements GroupSecurityHandler
37  {
38  
39      /*** Common queries. */
40      private SecurityAccess commonQueries = null;
41  
42      /***
43       * <p>
44       * Constructor providing access to the common queries.
45       * </p>
46       */
47      public DefaultGroupSecurityHandler(SecurityAccess commonQueries)
48      {
49          this.commonQueries = commonQueries;
50      }
51  
52      /***
53       * @see org.apache.jetspeed.security.spi.GroupSecurityHandler#getGroupPrincipal(java.lang.String)
54       */
55      public GroupPrincipal getGroupPrincipal(String groupFullPathName)
56      {
57          GroupPrincipal groupPrincipal = null;
58          InternalGroupPrincipal internalGroup = commonQueries
59                  .getInternalGroupPrincipal(GroupPrincipalImpl
60                          .getFullPathFromPrincipalName(groupFullPathName));
61          if (null != internalGroup)
62          {
63              groupPrincipal = new GroupPrincipalImpl(GroupPrincipalImpl
64                      .getPrincipalNameFromFullPath(internalGroup.getFullPath()), internalGroup.isEnabled(), internalGroup.isMappingOnly());
65          }
66          return groupPrincipal;
67      }
68  
69      /***
70       * @see org.apache.jetspeed.security.spi.GroupSecurityHandler#setGroupPrincipal(org.apache.jetspeed.security.GroupPrincipal)
71       */
72      public void setGroupPrincipal(GroupPrincipal groupPrincipal)
73              throws SecurityException
74      {
75          String fullPath = groupPrincipal.getFullPath();
76          InternalGroupPrincipal internalGroup = commonQueries.getInternalGroupPrincipal(fullPath);
77          if ( null == internalGroup )
78          {
79              internalGroup = new InternalGroupPrincipalImpl(fullPath);
80              internalGroup.setEnabled(groupPrincipal.isEnabled());
81              commonQueries.setInternalGroupPrincipal(internalGroup, false);
82          }
83          else if ( !internalGroup.isMappingOnly() )
84          {
85              if ( internalGroup.isEnabled() != groupPrincipal.isEnabled() )
86              {
87                  internalGroup.setEnabled(groupPrincipal.isEnabled());
88                  commonQueries.setInternalGroupPrincipal(internalGroup, false);
89              }
90          }
91          else
92          {
93              // TODO: should we throw an exception here?
94          }
95      }
96  
97      /***
98       * @see org.apache.jetspeed.security.spi.GroupSecurityHandler#removeGroupPrincipal(org.apache.jetspeed.security.GroupPrincipal)
99       */
100     public void removeGroupPrincipal(GroupPrincipal groupPrincipal)
101             throws SecurityException
102     {
103         InternalGroupPrincipal internalGroup = commonQueries
104                 .getInternalGroupPrincipal(groupPrincipal.getFullPath());
105         if (null != internalGroup)
106         {
107             commonQueries.removeInternalGroupPrincipal(internalGroup);
108         }
109     }
110 
111     /***
112      * @see org.apache.jetspeed.security.spi.GroupSecurityHandler#getGroupPrincipals(java.lang.String)
113      */
114     public List getGroupPrincipals(String filter)
115     {
116         List groupPrincipals = new LinkedList();
117         Iterator result = commonQueries.getInternalGroupPrincipals(filter);
118         while (result.hasNext())
119         {
120             InternalGroupPrincipal internalGroup = (InternalGroupPrincipal) result
121                     .next();
122             String path = internalGroup.getFullPath();
123             if (path == null)
124             {
125                 continue;
126             }
127             groupPrincipals
128                     .add(new GroupPrincipalImpl(GroupPrincipalImpl.getPrincipalNameFromFullPath(internalGroup.getFullPath()),
129                                 internalGroup.isEnabled(), internalGroup.isMappingOnly()) 
130                             );
131         }
132         return groupPrincipals;
133     }
134 
135 }