1   /**
2    * Copyright (c) 2000-2008 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.NoSuchResourceException;
26  import com.liferay.portal.PortalException;
27  import com.liferay.portal.ResourceActionsException;
28  import com.liferay.portal.SystemException;
29  import com.liferay.portal.kernel.security.permission.PermissionsListFilter;
30  import com.liferay.portal.model.Group;
31  import com.liferay.portal.model.Permission;
32  import com.liferay.portal.model.Resource;
33  import com.liferay.portal.model.ResourceCode;
34  import com.liferay.portal.model.impl.GroupImpl;
35  import com.liferay.portal.model.impl.ResourceImpl;
36  import com.liferay.portal.security.permission.PermissionsListFilterFactory;
37  import com.liferay.portal.security.permission.ResourceActionsUtil;
38  import com.liferay.portal.service.base.ResourceLocalServiceBaseImpl;
39  import com.liferay.portal.util.comparator.ResourceComparator;
40  
41  import java.util.Iterator;
42  import java.util.List;
43  
44  import org.apache.commons.lang.time.StopWatch;
45  import org.apache.commons.logging.Log;
46  import org.apache.commons.logging.LogFactory;
47  
48  /**
49   * <a href="ResourceLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Brian Wing Shun Chan
52   * @author Wilson S. Man
53   *
54   */
55  public class ResourceLocalServiceImpl extends ResourceLocalServiceBaseImpl {
56  
57      public void addModelResources(
58              long companyId, long groupId, long userId, String name,
59              long primKey, String[] communityPermissions,
60              String[] guestPermissions)
61          throws PortalException, SystemException {
62  
63          addModelResources(
64              companyId, groupId, userId, name, String.valueOf(primKey),
65              communityPermissions, guestPermissions);
66      }
67  
68      public void addModelResources(
69              long companyId, long groupId, long userId, String name,
70              String primKey, String[] communityPermissions,
71              String[] guestPermissions)
72          throws PortalException, SystemException {
73  
74          validate(companyId, name, false);
75  
76          // Company
77  
78          addResource(
79              companyId, name, ResourceImpl.SCOPE_COMPANY,
80              String.valueOf(companyId));
81  
82          // Guest
83  
84          Group guestGroup = groupLocalService.getGroup(
85              companyId, GroupImpl.GUEST);
86  
87          addResource(
88              companyId, name, ResourceImpl.SCOPE_GROUP,
89              String.valueOf(guestGroup.getGroupId()));
90  
91          // Group
92  
93          if ((groupId > 0) && (guestGroup.getGroupId() != groupId)) {
94              addResource(
95                  companyId, name, ResourceImpl.SCOPE_GROUP,
96                  String.valueOf(groupId));
97          }
98  
99          if (primKey != null) {
100 
101             // Individual
102 
103             Resource resource = addResource(
104                 companyId, name, ResourceImpl.SCOPE_INDIVIDUAL, primKey);
105 
106             // Permissions
107 
108             List permissionsList = permissionLocalService.addPermissions(
109                 companyId, name, resource.getResourceId(), false);
110 
111             // User permissions
112 
113             PermissionsListFilter permissionsListFilter =
114                 PermissionsListFilterFactory.getInstance();
115 
116             long defaultUserId = userLocalService.getDefaultUserId(companyId);
117 
118             if ((userId > 0) && (userId != defaultUserId)) {
119                 List userPermissionsList =
120                     permissionsListFilter.filterUserPermissions(
121                         companyId, groupId, userId, name, primKey, false,
122                         permissionsList);
123 
124                 userPersistence.addPermissions(userId, userPermissionsList);
125             }
126 
127             // Community permissions
128 
129             if (groupId > 0) {
130                 groupPersistence.findByPrimaryKey(groupId);
131 
132                 if (communityPermissions == null) {
133                     communityPermissions = new String[0];
134                 }
135 
136                 List communityPermissionsList =
137                     permissionLocalService.getPermissions(
138                         companyId, communityPermissions,
139                         resource.getResourceId());
140 
141                 communityPermissionsList =
142                     permissionsListFilter.filterCommunityPermissions(
143                         companyId, groupId, userId, name, primKey, false,
144                         communityPermissionsList);
145 
146                 groupPersistence.addPermissions(
147                     groupId, communityPermissionsList);
148             }
149 
150             // Guest permissions
151 
152             if (guestPermissions == null) {
153                 guestPermissions = new String[0];
154             }
155 
156             List guestPermissionsList =
157                 permissionLocalService.getPermissions(
158                     companyId, guestPermissions, resource.getResourceId());
159 
160             guestPermissionsList = permissionsListFilter.filterGuestPermissions(
161                 companyId, groupId, userId, name, primKey, false,
162                 guestPermissionsList);
163 
164             userPersistence.addPermissions(defaultUserId, guestPermissionsList);
165         }
166     }
167 
168     public Resource addResource(
169             long companyId, String name, int scope, String primKey)
170         throws PortalException, SystemException {
171 
172         ResourceCode resourceCode = resourceCodeLocalService.getResourceCode(
173             companyId, name, scope);
174 
175         Resource resource = resourcePersistence.fetchByC_P(
176             resourceCode.getCodeId(), primKey);
177 
178         if (resource == null) {
179             long resourceId = counterLocalService.increment(
180                 Resource.class.getName());
181 
182             resource = resourcePersistence.create(resourceId);
183 
184             resource.setCodeId(resourceCode.getCodeId());
185             resource.setPrimKey(primKey);
186 
187             resourcePersistence.update(resource);
188         }
189 
190         return resource;
191     }
192 
193     public void addResources(
194             long companyId, long groupId, String name, boolean portletActions)
195         throws PortalException, SystemException {
196 
197         addResources(
198             companyId, groupId, 0, name, null, portletActions, false, false);
199     }
200 
201     public void addResources(
202             long companyId, long groupId, long userId, String name,
203             long primKey, boolean portletActions,
204             boolean addCommunityPermissions, boolean addGuestPermissions)
205         throws PortalException, SystemException {
206 
207         addResources(
208             companyId, groupId, userId, name, String.valueOf(primKey),
209             portletActions, addCommunityPermissions, addGuestPermissions);
210     }
211 
212     public void addResources(
213             long companyId, long groupId, long userId, String name,
214             String primKey, boolean portletActions,
215             boolean addCommunityPermissions, boolean addGuestPermissions)
216         throws PortalException, SystemException {
217 
218         StopWatch stopWatch = null;
219 
220         if (_log.isDebugEnabled()) {
221             stopWatch = new StopWatch();
222 
223             stopWatch.start();
224         }
225 
226         validate(companyId, name, portletActions);
227 
228         logAddResources(name, primKey, stopWatch, 1);
229 
230         // Company
231 
232         addResource(
233             companyId, name, ResourceImpl.SCOPE_COMPANY,
234             String.valueOf(companyId));
235 
236         logAddResources(name, primKey, stopWatch, 2);
237 
238         if (groupId > 0) {
239             addResource(
240                 companyId, name, ResourceImpl.SCOPE_GROUP,
241                 String.valueOf(groupId));
242         }
243 
244         logAddResources(name, primKey, stopWatch, 3);
245 
246         if (primKey != null) {
247 
248             // Individual
249 
250             Resource resource = addResource(
251                 companyId, name, ResourceImpl.SCOPE_INDIVIDUAL, primKey);
252 
253             logAddResources(name, primKey, stopWatch, 4);
254 
255             // Permissions
256 
257             List permissionsList = permissionLocalService.addPermissions(
258                 companyId, name, resource.getResourceId(), portletActions);
259 
260             logAddResources(name, primKey, stopWatch, 5);
261 
262             // User permissions
263 
264             PermissionsListFilter permissionsListFilter =
265                 PermissionsListFilterFactory.getInstance();
266 
267             long defaultUserId = userLocalService.getDefaultUserId(companyId);
268 
269             if ((userId > 0) && (userId != defaultUserId)) {
270                 List userPermissionsList =
271                     permissionsListFilter.filterUserPermissions(
272                         companyId, groupId, userId, name, primKey,
273                         portletActions, permissionsList);
274 
275                 userPersistence.addPermissions(userId, userPermissionsList);
276             }
277 
278             logAddResources(name, primKey, stopWatch, 6);
279 
280             // Community permissions
281 
282             if ((groupId > 0) && addCommunityPermissions) {
283                 addCommunityPermissions(
284                     companyId, groupId, userId, name, resource, portletActions);
285             }
286 
287             logAddResources(name, primKey, stopWatch, 7);
288 
289             // Guest permissions
290 
291             if (addGuestPermissions) {
292 
293                 // Don't add guest permissions when you've already added
294                 // community permissions and the given community is the guest
295                 // community.
296 
297                 addGuestPermissions(
298                     companyId, groupId, userId, name, resource, portletActions);
299             }
300 
301             logAddResources(name, primKey, stopWatch, 9);
302         }
303     }
304 
305     public void deleteResource(long resourceId)
306         throws PortalException, SystemException {
307 
308         try {
309             Resource resource = resourcePersistence.findByPrimaryKey(
310                 resourceId);
311 
312             deleteResource(resource);
313         }
314         catch (NoSuchResourceException nsre) {
315             _log.warn(nsre);
316         }
317     }
318 
319     public void deleteResource(Resource resource)
320         throws PortalException, SystemException {
321 
322         // Permissions
323 
324         Iterator itr = permissionPersistence.findByResourceId(
325             resource.getResourceId()).iterator();
326 
327         while (itr.hasNext()) {
328             Permission permission = (Permission)itr.next();
329 
330             orgGroupPermissionPersistence.removeByPermissionId(
331                 permission.getPermissionId());
332         }
333 
334         permissionPersistence.removeByResourceId(resource.getResourceId());
335 
336         // Resource
337 
338         resourcePersistence.remove(resource.getResourceId());
339     }
340 
341     public void deleteResource(
342             long companyId, String name, int scope, long primKey)
343         throws PortalException, SystemException {
344 
345         deleteResource(companyId, name, scope, String.valueOf(primKey));
346     }
347 
348     public void deleteResource(
349             long companyId, String name, int scope, String primKey)
350         throws PortalException, SystemException {
351 
352         try {
353             Resource resource = getResource(companyId, name, scope, primKey);
354 
355             deleteResource(resource.getResourceId());
356         }
357         catch (NoSuchResourceException nsre) {
358             _log.warn(nsre);
359         }
360     }
361 
362     public void deleteResources(String name)
363         throws PortalException, SystemException {
364 
365         Iterator itr = resourceFinder.findByName(name).iterator();
366 
367         while (itr.hasNext()) {
368             Resource resource = (Resource)itr.next();
369 
370             deleteResource(resource);
371         }
372     }
373 
374     public long getLatestResourceId()
375         throws PortalException, SystemException {
376 
377         List list = resourcePersistence.findAll(0, 1, new ResourceComparator());
378 
379         if (list.size() == 0) {
380             return 0;
381         }
382         else {
383             Resource resource = (Resource)list.get(0);
384 
385             return resource.getResourceId();
386         }
387     }
388 
389     public Resource getResource(long resourceId)
390         throws PortalException, SystemException {
391 
392         return resourcePersistence.findByPrimaryKey(resourceId);
393     }
394 
395     public List getResources() throws SystemException {
396         return resourcePersistence.findAll();
397     }
398 
399     public Resource getResource(
400             long companyId, String name, int scope, String primKey)
401         throws PortalException, SystemException {
402 
403         ResourceCode resourceCode = resourceCodeLocalService.getResourceCode(
404             companyId, name, scope);
405 
406         return resourcePersistence.findByC_P(resourceCode.getCodeId(), primKey);
407     }
408 
409     protected void addCommunityPermissions(
410             long companyId, long groupId, long userId, String name,
411             Resource resource, boolean portletActions)
412         throws PortalException, SystemException {
413 
414         StopWatch stopWatch = null;
415 
416         if (_log.isDebugEnabled()) {
417             stopWatch = new StopWatch();
418 
419             stopWatch.start();
420         }
421 
422         Group group = groupPersistence.findByPrimaryKey(groupId);
423 
424         long resourceId = resource.getResourceId();
425         String primKey = resource.getPrimKey();
426 
427         logAddCommunityPermissions(groupId, name, resourceId, stopWatch, 1);
428 
429         List actions = null;
430 
431         if (portletActions) {
432             actions =
433                 ResourceActionsUtil.getPortletResourceCommunityDefaultActions(
434                     name);
435         }
436         else {
437             actions =
438                 ResourceActionsUtil.getModelResourceCommunityDefaultActions(
439                     name);
440         }
441 
442         logAddCommunityPermissions(groupId, name, resourceId, stopWatch, 2);
443 
444         String[] actionIds = (String[])actions.toArray(new String[0]);
445 
446         List communityPermissionsList = permissionLocalService.getPermissions(
447             group.getCompanyId(), actionIds, resourceId);
448 
449         logAddCommunityPermissions(groupId, name, resourceId, stopWatch, 3);
450 
451         PermissionsListFilter permissionsListFilter =
452             PermissionsListFilterFactory.getInstance();
453 
454         communityPermissionsList =
455             permissionsListFilter.filterCommunityPermissions(
456                 companyId, groupId, userId, name, primKey, portletActions,
457                 communityPermissionsList);
458 
459         logAddCommunityPermissions(groupId, name, resourceId, stopWatch, 4);
460 
461         groupPersistence.addPermissions(groupId, communityPermissionsList);
462 
463         logAddCommunityPermissions(groupId, name, resourceId, stopWatch, 5);
464     }
465 
466     protected void addGuestPermissions(
467             long companyId, long groupId, long userId, String name,
468             Resource resource, boolean portletActions)
469         throws PortalException, SystemException {
470 
471         long defaultUserId = userLocalService.getDefaultUserId(companyId);
472 
473         List actions = null;
474 
475         if (portletActions) {
476             actions =
477                 ResourceActionsUtil.getPortletResourceGuestDefaultActions(name);
478         }
479         else {
480             actions =
481                 ResourceActionsUtil.getModelResourceGuestDefaultActions(name);
482         }
483 
484         String[] actionIds = (String[])actions.toArray(new String[0]);
485 
486         List guestPermissionsList = permissionLocalService.getPermissions(
487             companyId, actionIds, resource.getResourceId());
488 
489         PermissionsListFilter permissionsListFilter =
490             PermissionsListFilterFactory.getInstance();
491 
492         guestPermissionsList =
493             permissionsListFilter.filterGuestPermissions(
494                 companyId, groupId, userId, name, resource.getPrimKey(),
495                 portletActions, guestPermissionsList);
496 
497         userPersistence.addPermissions(defaultUserId, guestPermissionsList);
498     }
499 
500     protected void logAddCommunityPermissions(
501         long groupId, String name, long resourceId, StopWatch stopWatch,
502         int block) {
503 
504         if (!_log.isDebugEnabled()) {
505             return;
506         }
507 
508         _log.debug(
509             "Adding community permissions block " + block + " for " + groupId +
510                 " " + name + " " + resourceId + " takes " +
511                     stopWatch.getTime() + " ms");
512     }
513 
514     protected void logAddResources(
515         String name, String primKey, StopWatch stopWatch, int block) {
516 
517         if (!_log.isDebugEnabled()) {
518             return;
519         }
520 
521         _log.debug(
522             "Adding resources block " + block + " for " + name + " " + primKey +
523                 " takes " + stopWatch.getTime() + " ms");
524     }
525 
526     protected void validate(
527             long companyId, String name, boolean portletActions)
528         throws PortalException, SystemException {
529 
530         List actions = null;
531 
532         if (portletActions) {
533             actions =
534                 ResourceActionsUtil.getPortletResourceActions(companyId, name);
535         }
536         else {
537             actions = ResourceActionsUtil.getModelResourceActions(name);
538         }
539 
540         if (actions.size() == 0) {
541             throw new ResourceActionsException(
542                 "There are no actions associated with the resource " + name);
543         }
544     }
545 
546     private static Log _log = LogFactory.getLog(ResourceLocalServiceImpl.class);
547 
548 }