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.model.Group;
30  import com.liferay.portal.model.Permission;
31  import com.liferay.portal.model.Resource;
32  import com.liferay.portal.model.ResourceCode;
33  import com.liferay.portal.model.ResourceConstants;
34  import com.liferay.portal.model.impl.GroupImpl;
35  import com.liferay.portal.security.permission.PermissionsListFilter;
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.List;
42  
43  import org.apache.commons.lang.time.StopWatch;
44  import org.apache.commons.logging.Log;
45  import org.apache.commons.logging.LogFactory;
46  
47  /**
48   * <a href="ResourceLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   * @author Wilson S. Man
52   *
53   */
54  public class ResourceLocalServiceImpl extends ResourceLocalServiceBaseImpl {
55  
56      public void addModelResources(
57              long companyId, long groupId, long userId, String name,
58              long primKey, String[] communityPermissions,
59              String[] guestPermissions)
60          throws PortalException, SystemException {
61  
62          addModelResources(
63              companyId, groupId, userId, name, String.valueOf(primKey),
64              communityPermissions, guestPermissions);
65      }
66  
67      public void addModelResources(
68              long companyId, long groupId, long userId, String name,
69              String primKey, String[] communityPermissions,
70              String[] guestPermissions)
71          throws PortalException, SystemException {
72  
73          validate(companyId, name, false);
74  
75          // Company
76  
77          addResource(
78              companyId, name, ResourceConstants.SCOPE_COMPANY,
79              String.valueOf(companyId));
80  
81          // Guest
82  
83          Group guestGroup = groupLocalService.getGroup(
84              companyId, GroupImpl.GUEST);
85  
86          addResource(
87              companyId, name, ResourceConstants.SCOPE_GROUP,
88              String.valueOf(guestGroup.getGroupId()));
89  
90          // Group
91  
92          if ((groupId > 0) && (guestGroup.getGroupId() != groupId)) {
93              addResource(
94                  companyId, name, ResourceConstants.SCOPE_GROUP,
95                  String.valueOf(groupId));
96          }
97  
98          if (primKey != null) {
99  
100             // Individual
101 
102             Resource resource = addResource(
103                 companyId, name, ResourceConstants.SCOPE_INDIVIDUAL, primKey);
104 
105             // Permissions
106 
107             List<Permission> permissionsList =
108                 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<Permission> 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<Permission> 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<Permission> 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, false);
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, ResourceConstants.SCOPE_COMPANY,
234             String.valueOf(companyId));
235 
236         logAddResources(name, primKey, stopWatch, 2);
237 
238         if (groupId > 0) {
239             addResource(
240                 companyId, name, ResourceConstants.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, ResourceConstants.SCOPE_INDIVIDUAL, primKey);
252 
253             logAddResources(name, primKey, stopWatch, 4);
254 
255             // Permissions
256 
257             List<Permission> permissionsList =
258                 permissionLocalService.addPermissions(
259                     companyId, name, resource.getResourceId(), portletActions);
260 
261             logAddResources(name, primKey, stopWatch, 5);
262 
263             // User permissions
264 
265             PermissionsListFilter permissionsListFilter =
266                 PermissionsListFilterFactory.getInstance();
267 
268             long defaultUserId = userLocalService.getDefaultUserId(companyId);
269 
270             if ((userId > 0) && (userId != defaultUserId)) {
271                 List<Permission> userPermissionsList =
272                     permissionsListFilter.filterUserPermissions(
273                         companyId, groupId, userId, name, primKey,
274                         portletActions, permissionsList);
275 
276                 userPersistence.addPermissions(userId, userPermissionsList);
277             }
278 
279             logAddResources(name, primKey, stopWatch, 6);
280 
281             // Community permissions
282 
283             if ((groupId > 0) && addCommunityPermissions) {
284                 addCommunityPermissions(
285                     companyId, groupId, userId, name, resource, portletActions);
286             }
287 
288             logAddResources(name, primKey, stopWatch, 7);
289 
290             // Guest permissions
291 
292             if (addGuestPermissions) {
293 
294                 // Don't add guest permissions when you've already added
295                 // community permissions and the given community is the guest
296                 // community.
297 
298                 addGuestPermissions(
299                     companyId, groupId, userId, name, resource, portletActions);
300             }
301 
302             logAddResources(name, primKey, stopWatch, 9);
303         }
304     }
305 
306     public void deleteResource(long resourceId)
307         throws PortalException, SystemException {
308 
309         try {
310             Resource resource = resourcePersistence.findByPrimaryKey(
311                 resourceId);
312 
313             deleteResource(resource);
314         }
315         catch (NoSuchResourceException nsre) {
316             _log.warn(nsre);
317         }
318     }
319 
320     public void deleteResource(Resource resource)
321         throws PortalException, SystemException {
322 
323         // Permissions
324 
325         List<Permission> permissions = permissionPersistence.findByResourceId(
326             resource.getResourceId());
327 
328         for (Permission permission : permissions) {
329             orgGroupPermissionPersistence.removeByPermissionId(
330                 permission.getPermissionId());
331         }
332 
333         permissionPersistence.removeByResourceId(resource.getResourceId());
334 
335         // Resource
336 
337         resourcePersistence.remove(resource.getResourceId());
338     }
339 
340     public void deleteResource(
341             long companyId, String name, int scope, long primKey)
342         throws PortalException, SystemException {
343 
344         deleteResource(companyId, name, scope, String.valueOf(primKey));
345     }
346 
347     public void deleteResource(
348             long companyId, String name, int scope, String primKey)
349         throws PortalException, SystemException {
350 
351         try {
352             Resource resource = getResource(companyId, name, scope, primKey);
353 
354             deleteResource(resource.getResourceId());
355         }
356         catch (NoSuchResourceException nsre) {
357             _log.warn(nsre);
358         }
359     }
360 
361     public void deleteResources(String name)
362         throws PortalException, SystemException {
363 
364         List<Resource> resources = resourceFinder.findByName(name);
365 
366         for (Resource resource : resources) {
367             deleteResource(resource);
368         }
369     }
370 
371     public long getLatestResourceId()
372         throws PortalException, SystemException {
373 
374         List<Resource> resources = resourcePersistence.findAll(
375             0, 1, new ResourceComparator());
376 
377         if (resources.size() == 0) {
378             return 0;
379         }
380         else {
381             Resource resource = resources.get(0);
382 
383             return resource.getResourceId();
384         }
385     }
386 
387     public Resource getResource(long resourceId)
388         throws PortalException, SystemException {
389 
390         return resourcePersistence.findByPrimaryKey(resourceId);
391     }
392 
393     public List<Resource> getResources() throws SystemException {
394         return resourcePersistence.findAll();
395     }
396 
397     public Resource getResource(
398             long companyId, String name, int scope, String primKey)
399         throws PortalException, SystemException {
400 
401         ResourceCode resourceCode = resourceCodeLocalService.getResourceCode(
402             companyId, name, scope);
403 
404         return resourcePersistence.findByC_P(resourceCode.getCodeId(), primKey);
405     }
406 
407     protected void addCommunityPermissions(
408             long companyId, long groupId, long userId, String name,
409             Resource resource, boolean portletActions)
410         throws PortalException, SystemException {
411 
412         StopWatch stopWatch = null;
413 
414         if (_log.isDebugEnabled()) {
415             stopWatch = new StopWatch();
416 
417             stopWatch.start();
418         }
419 
420         Group group = groupPersistence.findByPrimaryKey(groupId);
421 
422         long resourceId = resource.getResourceId();
423         String primKey = resource.getPrimKey();
424 
425         logAddCommunityPermissions(groupId, name, resourceId, stopWatch, 1);
426 
427         List<String> actions = null;
428 
429         if (portletActions) {
430             actions =
431                 ResourceActionsUtil.getPortletResourceCommunityDefaultActions(
432                     name);
433         }
434         else {
435             actions =
436                 ResourceActionsUtil.getModelResourceCommunityDefaultActions(
437                     name);
438         }
439 
440         logAddCommunityPermissions(groupId, name, resourceId, stopWatch, 2);
441 
442         String[] actionIds = actions.toArray(new String[actions.size()]);
443 
444         List<Permission> communityPermissionsList =
445             permissionLocalService.getPermissions(
446                 group.getCompanyId(), actionIds, resourceId);
447 
448         logAddCommunityPermissions(groupId, name, resourceId, stopWatch, 3);
449 
450         PermissionsListFilter permissionsListFilter =
451             PermissionsListFilterFactory.getInstance();
452 
453         communityPermissionsList =
454             permissionsListFilter.filterCommunityPermissions(
455                 companyId, groupId, userId, name, primKey, portletActions,
456                 communityPermissionsList);
457 
458         logAddCommunityPermissions(groupId, name, resourceId, stopWatch, 4);
459 
460         groupPersistence.addPermissions(groupId, communityPermissionsList);
461 
462         logAddCommunityPermissions(groupId, name, resourceId, stopWatch, 5);
463     }
464 
465     protected void addGuestPermissions(
466             long companyId, long groupId, long userId, String name,
467             Resource resource, boolean portletActions)
468         throws PortalException, SystemException {
469 
470         long defaultUserId = userLocalService.getDefaultUserId(companyId);
471 
472         List<String> actions = null;
473 
474         if (portletActions) {
475             actions =
476                 ResourceActionsUtil.getPortletResourceGuestDefaultActions(name);
477         }
478         else {
479             actions =
480                 ResourceActionsUtil.getModelResourceGuestDefaultActions(name);
481         }
482 
483         String[] actionIds = actions.toArray(new String[actions.size()]);
484 
485         List<Permission> guestPermissionsList =
486             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<String> 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 }