1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.service.impl;
16  
17  import com.liferay.portal.NoSuchResourceActionException;
18  import com.liferay.portal.PortalException;
19  import com.liferay.portal.SystemException;
20  import com.liferay.portal.kernel.annotation.Propagation;
21  import com.liferay.portal.kernel.annotation.Transactional;
22  import com.liferay.portal.kernel.util.ListUtil;
23  import com.liferay.portal.kernel.util.MathUtil;
24  import com.liferay.portal.kernel.util.StringPool;
25  import com.liferay.portal.model.ResourceAction;
26  import com.liferay.portal.security.permission.ActionKeys;
27  import com.liferay.portal.service.base.ResourceActionLocalServiceBaseImpl;
28  import com.liferay.portal.util.PropsValues;
29  
30  import java.util.HashMap;
31  import java.util.List;
32  import java.util.Map;
33  
34  /**
35   * <a href="ResourceActionLocalServiceImpl.java.html"><b><i>View Source</i></b>
36   * </a>
37   *
38   * @author Brian Wing Shun Chan
39   */
40  public class ResourceActionLocalServiceImpl
41      extends ResourceActionLocalServiceBaseImpl {
42  
43      @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
44      public void checkResourceActions() throws SystemException {
45          if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM != 6) {
46              return;
47          }
48  
49          List<ResourceAction> resourceActions =
50              resourceActionPersistence.findAll();
51  
52          for (ResourceAction resourceAction : resourceActions) {
53              String key = encodeKey(
54                  resourceAction.getName(), resourceAction.getActionId());
55  
56              _resourceActions.put(key, resourceAction);
57          }
58      }
59  
60      public void checkResourceActions(String name, List<String> actionIds)
61          throws SystemException {
62  
63          if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM != 6) {
64              return;
65          }
66  
67          List<ResourceAction> resourceActions =
68              resourceActionPersistence.findByName(name);
69  
70          resourceActions = ListUtil.copy(resourceActions);
71  
72          checkResourceActions(name, actionIds, resourceActions);
73      }
74  
75      public ResourceAction getResourceAction(String name, String actionId)
76          throws PortalException {
77  
78          String key = encodeKey(name, actionId);
79  
80          ResourceAction resourceAction = _resourceActions.get(key);
81  
82          if (resourceAction == null) {
83              throw new NoSuchResourceActionException(key);
84          }
85  
86          return resourceAction;
87      }
88  
89      public List<ResourceAction> getResourceActions(String name)
90          throws SystemException {
91  
92          return resourceActionPersistence.findByName(name);
93      }
94  
95      protected void checkResourceActions(
96              String name, List<String> actionIds,
97              List<ResourceAction> resourceActions)
98          throws SystemException {
99  
100         long lastBitwiseValue = 1;
101 
102         if (!resourceActions.isEmpty()) {
103             ResourceAction resourceAction = resourceActions.get(
104                 resourceActions.size() - 1);
105 
106             lastBitwiseValue = resourceAction.getBitwiseValue();
107         }
108 
109         int lastBitwiseLogValue = MathUtil.base2Log(lastBitwiseValue);
110 
111         for (String actionId : actionIds) {
112             String key = encodeKey(name, actionId);
113 
114             ResourceAction resourceAction = _resourceActions.get(key);
115 
116             if (resourceAction == null) {
117                 resourceAction = resourceActionPersistence.fetchByN_A(
118                     name, actionId);
119             }
120 
121             if (resourceAction == null) {
122                 long bitwiseValue = 1;
123 
124                 if (!actionId.equals(ActionKeys.VIEW)) {
125                     bitwiseValue = MathUtil.base2Pow(++lastBitwiseLogValue);
126                 }
127 
128                 long resourceActionId = counterLocalService.increment(
129                     ResourceAction.class.getName());
130 
131                 resourceAction = resourceActionPersistence.create(
132                     resourceActionId);
133 
134                 resourceAction.setName(name);
135                 resourceAction.setActionId(actionId);
136                 resourceAction.setBitwiseValue(bitwiseValue);
137 
138                 resourceActionPersistence.update(resourceAction, false);
139 
140                 _resourceActions.put(key, resourceAction);
141             }
142         }
143     }
144 
145     protected String encodeKey(String name, String actionId) {
146         return name.concat(StringPool.POUND).concat(actionId);
147     }
148 
149     private static Map<String, ResourceAction> _resourceActions =
150         new HashMap<String, ResourceAction>();
151 
152 }