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   */
49  public class ResourceActionLocalServiceImpl
50      extends ResourceActionLocalServiceBaseImpl {
51  
52      @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
53      public void checkResourceActions() throws SystemException {
54          if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM != 6) {
55              return;
56          }
57  
58          List<ResourceAction> resourceActions =
59              resourceActionPersistence.findAll();
60  
61          for (ResourceAction resourceAction : resourceActions) {
62              String key = encodeKey(
63                  resourceAction.getName(), resourceAction.getActionId());
64  
65              _resourceActions.put(key, resourceAction);
66          }
67      }
68  
69      public void checkResourceActions(String name, List<String> actionIds)
70          throws SystemException {
71  
72          if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM != 6) {
73              return;
74          }
75  
76          List<ResourceAction> resourceActions =
77              resourceActionPersistence.findByName(name);
78  
79          resourceActions = ListUtil.copy(resourceActions);
80  
81          checkResourceActions(name, actionIds, resourceActions);
82      }
83  
84      public ResourceAction getResourceAction(String name, String actionId)
85          throws PortalException {
86  
87          String key = encodeKey(name, actionId);
88  
89          ResourceAction resourceAction = _resourceActions.get(key);
90  
91          if (resourceAction == null) {
92              throw new NoSuchResourceActionException(key);
93          }
94  
95          return resourceAction;
96      }
97  
98      public List<ResourceAction> getResourceActions(String name)
99          throws SystemException {
100 
101         return resourceActionPersistence.findByName(name);
102     }
103 
104     protected void checkResourceActions(
105             String name, List<String> actionIds,
106             List<ResourceAction> resourceActions)
107         throws SystemException {
108 
109         long lastBitwiseValue = 1;
110 
111         if (!resourceActions.isEmpty()) {
112             ResourceAction resourceAction = resourceActions.get(
113                 resourceActions.size() - 1);
114 
115             lastBitwiseValue = resourceAction.getBitwiseValue();
116         }
117 
118         int lastBitwiseLogValue = MathUtil.base2Log(lastBitwiseValue);
119 
120         for (String actionId : actionIds) {
121             String key = encodeKey(name, actionId);
122 
123             ResourceAction resourceAction = _resourceActions.get(key);
124 
125             if (resourceAction == null) {
126                 resourceAction = resourceActionPersistence.fetchByN_A(
127                     name, actionId);
128             }
129 
130             if (resourceAction == null) {
131                 long bitwiseValue = 1;
132 
133                 if (!actionId.equals(ActionKeys.VIEW)) {
134                     bitwiseValue = MathUtil.base2Pow(++lastBitwiseLogValue);
135                 }
136 
137                 long resourceActionId = counterLocalService.increment(
138                     ResourceAction.class.getName());
139 
140                 resourceAction = resourceActionPersistence.create(
141                     resourceActionId);
142 
143                 resourceAction.setName(name);
144                 resourceAction.setActionId(actionId);
145                 resourceAction.setBitwiseValue(bitwiseValue);
146 
147                 resourceActionPersistence.update(resourceAction, false);
148 
149                 _resourceActions.put(key, resourceAction);
150             }
151         }
152     }
153 
154     protected String encodeKey(String name, String actionId) {
155         StringBuilder sb = new StringBuilder();
156 
157         sb.append(name);
158         sb.append(StringPool.POUND);
159         sb.append(actionId);
160 
161         return sb.toString();
162     }
163 
164     private static Map<String, ResourceAction> _resourceActions =
165         new HashMap<String, ResourceAction>();
166 
167 }