View Javadoc
1   package org.apache.fulcrum.security.model.turbine;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  import java.util.Set;
22  
23  import org.apache.avalon.framework.service.ServiceException;
24  import org.apache.fulcrum.security.GroupManager;
25  import org.apache.fulcrum.security.ModelManager;
26  import org.apache.fulcrum.security.RoleManager;
27  import org.apache.fulcrum.security.acl.AccessControlList;
28  import org.apache.fulcrum.security.entity.Group;
29  import org.apache.fulcrum.security.entity.User;
30  import org.apache.fulcrum.security.model.ACLFactory;
31  import org.apache.fulcrum.security.model.turbine.entity.TurbineUser;
32  import org.apache.fulcrum.security.model.turbine.entity.TurbineUserGroupRole;
33  import org.apache.fulcrum.security.spi.AbstractManager;
34  import org.apache.fulcrum.security.util.DataBackendException;
35  import org.apache.fulcrum.security.util.EntityExistsException;
36  import org.apache.fulcrum.security.util.FulcrumSecurityException;
37  import org.apache.fulcrum.security.util.UnknownEntityException;
38  
39  /**
40   *
41   * This factory creates instance of the TurbineAccessControlList
42   *
43   * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
44   * @version $Id: DynamicACLFactory.java 1374616 2012-08-18 17:26:07Z tv $
45   */
46  public class TurbineACLFactory extends AbstractManager implements ACLFactory
47  {
48  	
49      private TurbineModelManager modelManager;
50      
51      /**
52       * @return A model manager
53       * @throws DataBackendException generic exception
54       */
55      protected TurbineModelManager getTurbineModelManager() throws DataBackendException
56      {
57          if (modelManager == null)
58          {
59              try
60              {
61              	modelManager = (TurbineModelManager) manager.lookup(ModelManager.ROLE);
62  
63              }
64              catch (ServiceException ce)
65              {
66                  throw new DataBackendException(ce.getMessage(), ce);
67              }
68          }
69          return modelManager;
70      }
71  	
72      /* (non-Javadoc)
73       * @see org.apache.fulcrum.security.model.ACLFactory#getAccessControlList(org.apache.fulcrum.security.entity.User)
74       */
75      @Override
76      public <T extends AccessControlList> T getAccessControlList(User user)
77      {
78          try
79          {
80              TurbineUser./../../../../../org/apache/fulcrum/security/model/turbine/entity/TurbineUser.html#TurbineUser">TurbineUser tu = (TurbineUser)user;
81              Set<TurbineUserGroupRole> tugr = tu.getUserGroupRoleSet();
82              
83              @SuppressWarnings("unchecked")
84  			T aclInstance = (T) getAclInstance(tugr);
85  			return aclInstance;
86          }
87          catch (UnknownEntityException uue)
88          {
89              throw new RuntimeException(uue.getMessage(), uue);
90          }
91          catch ( DataBackendException e )
92          {
93              throw new RuntimeException(e.getMessage(), e);
94          }
95      }
96  
97      /**
98       * Construct a new ACL object.
99       *
100      * This constructs a new ACL object from the configured class and
101      * initializes it with the supplied roles and permissions.
102      *
103      * @param turbineUserGroupRoleSet
104      *            The set of user/group/role relations that this acl is built from
105      *
106      * @return an object implementing ACL interface.
107      * @throws UnknownEntityException
108      *             if the object could not be instantiated.
109      */
110     private TurbineAccessControlList getAclInstance(Set<? extends TurbineUserGroupRole> turbineUserGroupRoleSet) throws UnknownEntityException
111     {
112     	GroupManager groupManager = null;
113         RoleManager roleManager = null;
114         TurbineModelManager modelManager = null;
115 
116     	try
117     	{
118     	    roleManager = getRoleManager();
119 			groupManager = getGroupManager();
120 			modelManager = getTurbineModelManager();
121 
122 	        // make sure the global group exists
123 	        if (groupManager != null)
124 	        {
125 	            Group g = null;
126 	            try
127 	            {
128 	                g = groupManager.getGroupByName(modelManager.getGlobalGroupName());
129 	            }
130 	            catch (UnknownEntityException uee)
131 	            {
132 	                g = groupManager.getGroupInstance(modelManager.getGlobalGroupName());
133 	                try
134 	                {
135 	                    groupManager.addGroup(g);
136 	                }
137 	                catch (EntityExistsException eee)
138 	                {
139 	                    throw new DataBackendException(eee.getMessage(), eee);
140 	                }
141 	            }
142 	        }
143     	}
144     	catch (DataBackendException e)
145     	{
146     		// ignore
147 		}
148 
149     	TurbineAccessControlList accessControlList;
150         try
151         {
152             accessControlList =
153                 new TurbineAccessControlListImpl(turbineUserGroupRoleSet,
154                         groupManager, roleManager, modelManager, getLogger());
155         }
156         catch (FulcrumSecurityException e)
157         {
158             throw new UnknownEntityException("Failed to instantiate an ACL implementation object", e);
159         }
160         return accessControlList;
161     }
162     
163 }