1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.service.impl;
24  
25  import com.liferay.portal.NoSuchResourceActionException;
26  import com.liferay.portal.PortalException;
27  import com.liferay.portal.SystemException;
28  import com.liferay.portal.kernel.annotation.Propagation;
29  import com.liferay.portal.kernel.annotation.Transactional;
30  import com.liferay.portal.kernel.util.ListUtil;
31  import com.liferay.portal.kernel.util.MathUtil;
32  import com.liferay.portal.kernel.util.StringPool;
33  import com.liferay.portal.model.ResourceAction;
34  import com.liferay.portal.security.permission.ActionKeys;
35  import com.liferay.portal.service.base.ResourceActionLocalServiceBaseImpl;
36  import com.liferay.portal.util.PropsValues;
37  
38  import java.util.HashMap;
39  import java.util.List;
40  import java.util.Map;
41  
42  /**
43   * <a href="ResourceActionLocalServiceImpl.java.html"><b><i>View Source</i></b>
44   * </a>
45   *
46   * @author Brian Wing Shun Chan
47   */
48  public class ResourceActionLocalServiceImpl
49      extends ResourceActionLocalServiceBaseImpl {
50  
51      @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
52      public void checkResourceActions() throws SystemException {
53          if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM != 6) {
54              return;
55          }
56  
57          List<ResourceAction> resourceActions =
58              resourceActionPersistence.findAll();
59  
60          for (ResourceAction resourceAction : resourceActions) {
61              String key = encodeKey(
62                  resourceAction.getName(), resourceAction.getActionId());
63  
64              _resourceActions.put(key, resourceAction);
65          }
66      }
67  
68      public void checkResourceActions(String name, List<String> actionIds)
69          throws SystemException {
70  
71          if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM != 6) {
72              return;
73          }
74  
75          List<ResourceAction> resourceActions =
76              resourceActionPersistence.findByName(name);
77  
78          resourceActions = ListUtil.copy(resourceActions);
79  
80          checkResourceActions(name, actionIds, resourceActions);
81      }
82  
83      public ResourceAction getResourceAction(String name, String actionId)
84          throws PortalException {
85  
86          String key = encodeKey(name, actionId);
87  
88          ResourceAction resourceAction = _resourceActions.get(key);
89  
90          if (resourceAction == null) {
91              throw new NoSuchResourceActionException(key);
92          }
93  
94          return resourceAction;
95      }
96  
97      public List<ResourceAction> getResourceActions(String name)
98          throws SystemException {
99  
100         return resourceActionPersistence.findByName(name);
101     }
102 
103     protected void checkResourceActions(
104             String name, List<String> actionIds,
105             List<ResourceAction> resourceActions)
106         throws SystemException {
107 
108         long lastBitwiseValue = 1;
109 
110         if (!resourceActions.isEmpty()) {
111             ResourceAction resourceAction = resourceActions.get(
112                 resourceActions.size() - 1);
113 
114             lastBitwiseValue = resourceAction.getBitwiseValue();
115         }
116 
117         int lastBitwiseLogValue = MathUtil.base2Log(lastBitwiseValue);
118 
119         for (String actionId : actionIds) {
120             String key = encodeKey(name, actionId);
121 
122             ResourceAction resourceAction = _resourceActions.get(key);
123 
124             if (resourceAction == null) {
125                 resourceAction = resourceActionPersistence.fetchByN_A(
126                     name, actionId);
127             }
128 
129             if (resourceAction == null) {
130                 long bitwiseValue = 1;
131 
132                 if (!actionId.equals(ActionKeys.VIEW)) {
133                     bitwiseValue = MathUtil.base2Pow(++lastBitwiseLogValue);
134                 }
135 
136                 long resourceActionId = counterLocalService.increment(
137                     ResourceAction.class.getName());
138 
139                 resourceAction = resourceActionPersistence.create(
140                     resourceActionId);
141 
142                 resourceAction.setName(name);
143                 resourceAction.setActionId(actionId);
144                 resourceAction.setBitwiseValue(bitwiseValue);
145 
146                 resourceActionPersistence.update(resourceAction, false);
147 
148                 _resourceActions.put(key, resourceAction);
149             }
150         }
151     }
152 
153     protected String encodeKey(String name, String actionId) {
154         StringBuilder sb = new StringBuilder();
155 
156         sb.append(name);
157         sb.append(StringPool.POUND);
158         sb.append(actionId);
159 
160         return sb.toString();
161     }
162 
163     private static Map<String, ResourceAction> _resourceActions =
164         new HashMap<String, ResourceAction>();
165 
166 }