001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.service.impl;
016    
017    import com.liferay.portal.NoSuchResourceActionException;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.transaction.Propagation;
021    import com.liferay.portal.kernel.transaction.Transactional;
022    import com.liferay.portal.kernel.util.ListUtil;
023    import com.liferay.portal.kernel.util.MathUtil;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.model.ResourceAction;
026    import com.liferay.portal.model.ResourceConstants;
027    import com.liferay.portal.model.RoleConstants;
028    import com.liferay.portal.security.permission.ActionKeys;
029    import com.liferay.portal.security.permission.ResourceActionsUtil;
030    import com.liferay.portal.service.base.ResourceActionLocalServiceBaseImpl;
031    import com.liferay.portal.util.PropsValues;
032    
033    import java.util.ArrayList;
034    import java.util.HashMap;
035    import java.util.List;
036    import java.util.Map;
037    
038    /**
039     * @author Brian Wing Shun Chan
040     */
041    public class ResourceActionLocalServiceImpl
042            extends ResourceActionLocalServiceBaseImpl {
043    
044            @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
045            public void checkResourceActions() throws SystemException {
046                    if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM != 6) {
047                            return;
048                    }
049    
050                    List<ResourceAction> resourceActions =
051                            resourceActionPersistence.findAll();
052    
053                    for (ResourceAction resourceAction : resourceActions) {
054                            String key = encodeKey(
055                                    resourceAction.getName(), resourceAction.getActionId());
056    
057                            _resourceActions.put(key, resourceAction);
058                    }
059            }
060    
061            public void checkResourceActions(String name, List<String> actionIds)
062                    throws SystemException {
063    
064                    checkResourceActions(name, actionIds, false);
065            }
066    
067            public void checkResourceActions(
068                            String name, List<String> actionIds, boolean addDefaultActions)
069                    throws SystemException {
070    
071                    if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM != 6) {
072                            return;
073                    }
074    
075                    List<ResourceAction> resourceActions =
076                            resourceActionPersistence.findByName(name);
077    
078                    resourceActions = ListUtil.copy(resourceActions);
079    
080                    checkResourceActions(
081                            name, actionIds, resourceActions, addDefaultActions);
082            }
083    
084            public ResourceAction fetchResourceAction(String name, String actionId) {
085                    String key = encodeKey(name, actionId);
086    
087                    return _resourceActions.get(key);
088            }
089    
090            public ResourceAction getResourceAction(String name, String actionId)
091                    throws PortalException {
092    
093                    String key = encodeKey(name, actionId);
094    
095                    ResourceAction resourceAction = _resourceActions.get(key);
096    
097                    if (resourceAction == null) {
098                            throw new NoSuchResourceActionException(key);
099                    }
100    
101                    return resourceAction;
102            }
103    
104            public List<ResourceAction> getResourceActions(String name)
105                    throws SystemException {
106    
107                    return resourceActionPersistence.findByName(name);
108            }
109    
110            protected void checkResourceActions(
111                            String name, List<String> actionIds,
112                            List<ResourceAction> resourceActions, boolean addDefaultActions)
113                    throws SystemException {
114    
115                    long lastBitwiseValue = 1;
116    
117                    if (!resourceActions.isEmpty()) {
118                            ResourceAction resourceAction = resourceActions.get(
119                                    resourceActions.size() - 1);
120    
121                            lastBitwiseValue = resourceAction.getBitwiseValue();
122                    }
123    
124                    List<ResourceAction> newResourceActions =
125                            new ArrayList<ResourceAction>();
126    
127                    int lastBitwiseLogValue = MathUtil.base2Log(lastBitwiseValue);
128    
129                    for (String actionId : actionIds) {
130                            String key = encodeKey(name, actionId);
131    
132                            ResourceAction resourceAction = _resourceActions.get(key);
133    
134                            if (resourceAction != null) {
135                                    continue;
136                            }
137    
138                            resourceAction = resourceActionPersistence.fetchByN_A(
139                                    name, actionId);
140    
141                            if (resourceAction != null) {
142                                    _resourceActions.put(key, resourceAction);
143    
144                                    continue;
145                            }
146    
147                            long bitwiseValue = 1;
148    
149                            if (!actionId.equals(ActionKeys.VIEW)) {
150                                    bitwiseValue = MathUtil.base2Pow(++lastBitwiseLogValue);
151                            }
152    
153                            long resourceActionId = counterLocalService.increment(
154                                    ResourceAction.class.getName());
155    
156                            resourceAction = resourceActionPersistence.create(resourceActionId);
157    
158                            resourceAction.setName(name);
159                            resourceAction.setActionId(actionId);
160                            resourceAction.setBitwiseValue(bitwiseValue);
161    
162                            resourceActionPersistence.update(resourceAction, false);
163    
164                            _resourceActions.put(key, resourceAction);
165    
166                            newResourceActions.add(resourceAction);
167                    }
168    
169                    if (addDefaultActions) {
170                            List<String> groupDefaultActions =
171                                    ResourceActionsUtil.getModelResourceGroupDefaultActions(name);
172    
173                            List<String> guestDefaultActions =
174                                    ResourceActionsUtil.getModelResourceGuestDefaultActions(name);
175    
176                            for (ResourceAction resourceAction : newResourceActions) {
177                                    String actionId = resourceAction.getActionId();
178    
179                                    if (groupDefaultActions.contains(actionId)) {
180                                            resourcePermissionLocalService.addResourcePermissions(
181                                                    name, RoleConstants.SITE_MEMBER,
182                                                    ResourceConstants.SCOPE_INDIVIDUAL,
183                                                    resourceAction.getBitwiseValue());
184                                    }
185    
186                                    if (guestDefaultActions.contains(actionId)) {
187                                            resourcePermissionLocalService.addResourcePermissions(
188                                                    name, RoleConstants.GUEST,
189                                                    ResourceConstants.SCOPE_INDIVIDUAL,
190                                                    resourceAction.getBitwiseValue());
191                                    }
192    
193                                    resourcePermissionLocalService.addResourcePermissions(
194                                            name, RoleConstants.OWNER,
195                                            ResourceConstants.SCOPE_INDIVIDUAL,
196                                            resourceAction.getBitwiseValue());
197                            }
198                    }
199            }
200    
201            protected String encodeKey(String name, String actionId) {
202                    String key = name.concat(StringPool.POUND).concat(actionId);
203    
204                    return key.toLowerCase();
205            }
206    
207            private static Map<String, ResourceAction> _resourceActions =
208                    new HashMap<String, ResourceAction>();
209    
210    }