1
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.log.Log;
30 import com.liferay.portal.kernel.log.LogFactoryUtil;
31 import com.liferay.portal.model.Group;
32 import com.liferay.portal.model.GroupConstants;
33 import com.liferay.portal.model.Layout;
34 import com.liferay.portal.model.Permission;
35 import com.liferay.portal.model.Resource;
36 import com.liferay.portal.model.ResourceCode;
37 import com.liferay.portal.model.ResourceConstants;
38 import com.liferay.portal.model.Role;
39 import com.liferay.portal.model.RoleConstants;
40 import com.liferay.portal.security.permission.PermissionsListFilter;
41 import com.liferay.portal.security.permission.PermissionsListFilterFactory;
42 import com.liferay.portal.security.permission.ResourceActionsUtil;
43 import com.liferay.portal.service.base.ResourceLocalServiceBaseImpl;
44 import com.liferay.portal.util.PropsValues;
45 import com.liferay.portal.util.comparator.ResourceComparator;
46
47 import java.util.List;
48
49 import org.apache.commons.lang.time.StopWatch;
50
51
59 public class ResourceLocalServiceImpl extends ResourceLocalServiceBaseImpl {
60
61 public void addModelResources(
62 long companyId, long groupId, long userId, String name,
63 long primKey, String[] communityPermissions,
64 String[] guestPermissions)
65 throws PortalException, SystemException {
66
67 addModelResources(
68 companyId, groupId, userId, name, String.valueOf(primKey),
69 communityPermissions, guestPermissions);
70 }
71
72 public void addModelResources(
73 long companyId, long groupId, long userId, String name,
74 String primKey, String[] communityPermissions,
75 String[] guestPermissions)
76 throws PortalException, SystemException {
77
78 validate(companyId, name, false);
79
80
82 addResource(
83 companyId, name, ResourceConstants.SCOPE_COMPANY,
84 String.valueOf(companyId));
85
86
88 Group guestGroup = groupLocalService.getGroup(
89 companyId, GroupConstants.GUEST);
90
91 addResource(
92 companyId, name, ResourceConstants.SCOPE_GROUP,
93 String.valueOf(guestGroup.getGroupId()));
94
95
97 if ((groupId > 0) && (guestGroup.getGroupId() != groupId)) {
98 addResource(
99 companyId, name, ResourceConstants.SCOPE_GROUP,
100 String.valueOf(groupId));
101 }
102
103 if (primKey != null) {
104
105
107 Resource resource = addResource(
108 companyId, name, ResourceConstants.SCOPE_INDIVIDUAL, primKey);
109
110 long defaultUserId = userLocalService.getDefaultUserId(
111 companyId);
112
113 PermissionsListFilter permissionsListFilter =
114 PermissionsListFilterFactory.getInstance();
115
116
118 List<Permission> permissionsList =
119 permissionLocalService.addPermissions(
120 companyId, name, resource.getResourceId(), false);
121
122 List<Permission> userPermissionsList =
123 permissionsListFilter.filterUserPermissions(
124 companyId, groupId, userId, name, primKey, false,
125 permissionsList);
126
127 if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 5) {
128
129
131 Role ownerRole = roleLocalService.getRole(
132 companyId, RoleConstants.OWNER);
133
134 rolePersistence.addPermissions(
135 ownerRole.getRoleId(), userPermissionsList);
136 }
137 else {
138
139
141 if ((userId > 0) && (userId != defaultUserId)) {
142 userPersistence.addPermissions(userId, userPermissionsList);
143 }
144 }
145
146
148 if (groupId > 0) {
149 Group group = groupPersistence.findByPrimaryKey(groupId);
150
151 if (communityPermissions == null) {
152 communityPermissions = new String[0];
153 }
154
155 List<Permission> communityPermissionsList =
156 permissionLocalService.getPermissions(
157 companyId, communityPermissions,
158 resource.getResourceId());
159
160 communityPermissionsList =
161 permissionsListFilter.filterCommunityPermissions(
162 companyId, groupId, userId, name, primKey, false,
163 communityPermissionsList);
164
165 if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 5) {
166 Role role = null;
167
168 if (group.isLayout()) {
169 Layout layout = layoutLocalService.getLayout(
170 group.getClassPK());
171
172 group = layout.getGroup();
173 }
174
175 if (group.isCommunity()) {
176 role = roleLocalService.getRole(
177 companyId, RoleConstants.COMMUNITY_MEMBER);
178 }
179 else if (group.isOrganization()) {
180 role = roleLocalService.getRole(
181 companyId, RoleConstants.ORGANIZATION_MEMBER);
182 }
183 else if (group.isUser() || group.isUserGroup()) {
184 role = roleLocalService.getRole(
185 companyId, RoleConstants.POWER_USER);
186 }
187
188 rolePersistence.addPermissions(
189 role.getRoleId(), communityPermissionsList);
190 }
191 else {
192 groupPersistence.addPermissions(
193 groupId, communityPermissionsList);
194 }
195 }
196
197
199 if (guestPermissions == null) {
200 guestPermissions = new String[0];
201 }
202
203 List<Permission> guestPermissionsList =
204 permissionLocalService.getPermissions(
205 companyId, guestPermissions, resource.getResourceId());
206
207 guestPermissionsList = permissionsListFilter.filterGuestPermissions(
208 companyId, groupId, userId, name, primKey, false,
209 guestPermissionsList);
210
211 if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 5) {
212
213
215 Role guestRole = roleLocalService.getRole(
216 companyId, RoleConstants.GUEST);
217
218 rolePersistence.addPermissions(
219 guestRole.getRoleId(), guestPermissionsList);
220 }
221 else {
222 userPersistence.addPermissions(
223 defaultUserId, guestPermissionsList);
224 }
225 }
226 }
227
228 public Resource addResource(
229 long companyId, String name, int scope, String primKey)
230 throws SystemException {
231
232 ResourceCode resourceCode = resourceCodeLocalService.getResourceCode(
233 companyId, name, scope);
234
235 Resource resource = resourcePersistence.fetchByC_P(
236 resourceCode.getCodeId(), primKey);
237
238 if (resource == null) {
239 long resourceId = counterLocalService.increment(
240 Resource.class.getName());
241
242 resource = resourcePersistence.create(resourceId);
243
244 resource.setCodeId(resourceCode.getCodeId());
245 resource.setPrimKey(primKey);
246
247 resourcePersistence.update(resource, false);
248 }
249
250 return resource;
251 }
252
253 public void addResources(
254 long companyId, long groupId, String name, boolean portletActions)
255 throws PortalException, SystemException {
256
257 addResources(
258 companyId, groupId, 0, name, null, portletActions, false, false);
259 }
260
261 public void addResources(
262 long companyId, long groupId, long userId, String name,
263 long primKey, boolean portletActions,
264 boolean addCommunityPermissions, boolean addGuestPermissions)
265 throws PortalException, SystemException {
266
267 addResources(
268 companyId, groupId, userId, name, String.valueOf(primKey),
269 portletActions, addCommunityPermissions, addGuestPermissions);
270 }
271
272 public void addResources(
273 long companyId, long groupId, long userId, String name,
274 String primKey, boolean portletActions,
275 boolean addCommunityPermissions, boolean addGuestPermissions)
276 throws PortalException, SystemException {
277
278 StopWatch stopWatch = null;
279
280 if (_log.isDebugEnabled()) {
281 stopWatch = new StopWatch();
282
283 stopWatch.start();
284 }
285
286 validate(companyId, name, portletActions);
287
288 logAddResources(name, primKey, stopWatch, 1);
289
290
292 addResource(
293 companyId, name, ResourceConstants.SCOPE_COMPANY,
294 String.valueOf(companyId));
295
296 logAddResources(name, primKey, stopWatch, 2);
297
298 if (groupId > 0) {
299 addResource(
300 companyId, name, ResourceConstants.SCOPE_GROUP,
301 String.valueOf(groupId));
302 }
303
304 logAddResources(name, primKey, stopWatch, 3);
305
306 if (primKey != null) {
307
308
310 Resource resource = addResource(
311 companyId, name, ResourceConstants.SCOPE_INDIVIDUAL, primKey);
312
313 logAddResources(name, primKey, stopWatch, 4);
314
315
317 List<Permission> permissionsList =
318 permissionLocalService.addPermissions(
319 companyId, name, resource.getResourceId(), portletActions);
320
321 logAddResources(name, primKey, stopWatch, 5);
322
323 PermissionsListFilter permissionsListFilter =
324 PermissionsListFilterFactory.getInstance();
325
326 List<Permission> userPermissionsList =
327 permissionsListFilter.filterUserPermissions(
328 companyId, groupId, userId, name, primKey,
329 portletActions, permissionsList);
330
331 if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 5) {
332
333
335 Role ownerRole = roleLocalService.getRole(
336 companyId, RoleConstants.OWNER);
337
338 rolePersistence.addPermissions(
339 ownerRole.getRoleId(), userPermissionsList);
340 }
341 else {
342
343
345 long defaultUserId = userLocalService.getDefaultUserId(
346 companyId);
347
348 if ((userId > 0) && (userId != defaultUserId)) {
349 userPersistence.addPermissions(userId, userPermissionsList);
350 }
351 }
352
353 logAddResources(name, primKey, stopWatch, 6);
354
355
357 if ((groupId > 0) && addCommunityPermissions) {
358 addCommunityPermissions(
359 companyId, groupId, userId, name, resource, portletActions);
360 }
361
362 logAddResources(name, primKey, stopWatch, 7);
363
364
366 if (addGuestPermissions) {
367
368
372 addGuestPermissions(
373 companyId, groupId, userId, name, resource, portletActions);
374 }
375
376 logAddResources(name, primKey, stopWatch, 8);
377 }
378 }
379
380 public void deleteResource(long resourceId) throws SystemException {
381 try {
382 Resource resource = resourcePersistence.findByPrimaryKey(
383 resourceId);
384
385 deleteResource(resource);
386 }
387 catch (NoSuchResourceException nsre) {
388 if (_log.isWarnEnabled()) {
389 _log.warn(nsre);
390 }
391 }
392 }
393
394 public void deleteResource(Resource resource) throws SystemException {
395
396
398 List<Permission> permissions = permissionPersistence.findByResourceId(
399 resource.getResourceId());
400
401 for (Permission permission : permissions) {
402 orgGroupPermissionPersistence.removeByPermissionId(
403 permission.getPermissionId());
404 }
405
406 permissionPersistence.removeByResourceId(resource.getResourceId());
407
408
410 resourcePersistence.remove(resource);
411 }
412
413 public void deleteResource(
414 long companyId, String name, int scope, long primKey)
415 throws PortalException, SystemException {
416
417 deleteResource(companyId, name, scope, String.valueOf(primKey));
418 }
419
420 public void deleteResource(
421 long companyId, String name, int scope, String primKey)
422 throws PortalException, SystemException {
423
424 try {
425 Resource resource = getResource(companyId, name, scope, primKey);
426
427 deleteResource(resource.getResourceId());
428 }
429 catch (NoSuchResourceException nsre) {
430 if (_log.isWarnEnabled()) {
431 _log.warn(nsre);
432 }
433 }
434 }
435
436 public void deleteResources(String name) throws SystemException {
437 List<Resource> resources = resourceFinder.findByName(name);
438
439 for (Resource resource : resources) {
440 deleteResource(resource);
441 }
442 }
443
444 public long getLatestResourceId() throws SystemException {
445 List<Resource> resources = resourcePersistence.findAll(
446 0, 1, new ResourceComparator());
447
448 if (resources.size() == 0) {
449 return 0;
450 }
451 else {
452 Resource resource = resources.get(0);
453
454 return resource.getResourceId();
455 }
456 }
457
458 public Resource getResource(long resourceId)
459 throws PortalException, SystemException {
460
461 return resourcePersistence.findByPrimaryKey(resourceId);
462 }
463
464 public List<Resource> getResources() throws SystemException {
465 return resourcePersistence.findAll();
466 }
467
468 public Resource getResource(
469 long companyId, String name, int scope, String primKey)
470 throws PortalException, SystemException {
471
472 ResourceCode resourceCode = resourceCodeLocalService.getResourceCode(
473 companyId, name, scope);
474
475 return resourcePersistence.findByC_P(resourceCode.getCodeId(), primKey);
476 }
477
478 protected void addCommunityPermissions(
479 long companyId, long groupId, long userId, String name,
480 Resource resource, boolean portletActions)
481 throws PortalException, SystemException {
482
483 StopWatch stopWatch = null;
484
485 if (_log.isDebugEnabled()) {
486 stopWatch = new StopWatch();
487
488 stopWatch.start();
489 }
490
491 Group group = groupPersistence.findByPrimaryKey(groupId);
492
493 long resourceId = resource.getResourceId();
494 String primKey = resource.getPrimKey();
495
496 logAddCommunityPermissions(groupId, name, resourceId, stopWatch, 1);
497
498 List<String> actions = null;
499
500 if (portletActions) {
501 actions =
502 ResourceActionsUtil.getPortletResourceCommunityDefaultActions(
503 name);
504 }
505 else {
506 actions =
507 ResourceActionsUtil.getModelResourceCommunityDefaultActions(
508 name);
509 }
510
511 logAddCommunityPermissions(groupId, name, resourceId, stopWatch, 2);
512
513 String[] actionIds = actions.toArray(new String[actions.size()]);
514
515 List<Permission> communityPermissionsList =
516 permissionLocalService.getPermissions(
517 group.getCompanyId(), actionIds, resourceId);
518
519 logAddCommunityPermissions(groupId, name, resourceId, stopWatch, 3);
520
521 PermissionsListFilter permissionsListFilter =
522 PermissionsListFilterFactory.getInstance();
523
524 communityPermissionsList =
525 permissionsListFilter.filterCommunityPermissions(
526 companyId, groupId, userId, name, primKey, portletActions,
527 communityPermissionsList);
528
529 logAddCommunityPermissions(groupId, name, resourceId, stopWatch, 4);
530
531 if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 5) {
532 Role role = null;
533
534 if (group.isLayout()) {
535 Layout layout = layoutLocalService.getLayout(
536 group.getClassPK());
537
538 group = layout.getGroup();
539 }
540
541 if (group.isCommunity()) {
542 role = roleLocalService.getRole(
543 companyId, RoleConstants.COMMUNITY_MEMBER);
544 }
545 else if (group.isOrganization()) {
546 role = roleLocalService.getRole(
547 companyId, RoleConstants.ORGANIZATION_MEMBER);
548 }
549 else if (group.isUser() || group.isUserGroup()) {
550 role = roleLocalService.getRole(
551 companyId, RoleConstants.POWER_USER);
552 }
553
554 rolePersistence.addPermissions(
555 role.getRoleId(), communityPermissionsList);
556 }
557 else {
558 groupPersistence.addPermissions(groupId, communityPermissionsList);
559 }
560
561 logAddCommunityPermissions(groupId, name, resourceId, stopWatch, 5);
562 }
563
564 protected void addGuestPermissions(
565 long companyId, long groupId, long userId, String name,
566 Resource resource, boolean portletActions)
567 throws PortalException, SystemException {
568
569 List<String> actions = null;
570
571 if (portletActions) {
572 actions = ResourceActionsUtil.getPortletResourceGuestDefaultActions(
573 name);
574 }
575 else {
576 actions = ResourceActionsUtil.getModelResourceGuestDefaultActions(
577 name);
578 }
579
580 String[] actionIds = actions.toArray(new String[actions.size()]);
581
582 List<Permission> guestPermissionsList =
583 permissionLocalService.getPermissions(
584 companyId, actionIds, resource.getResourceId());
585
586 PermissionsListFilter permissionsListFilter =
587 PermissionsListFilterFactory.getInstance();
588
589 guestPermissionsList =
590 permissionsListFilter.filterGuestPermissions(
591 companyId, groupId, userId, name, resource.getPrimKey(),
592 portletActions, guestPermissionsList);
593
594 if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 5) {
595 Role guestRole = roleLocalService.getRole(
596 companyId, RoleConstants.GUEST);
597
598 rolePersistence.addPermissions(
599 guestRole.getRoleId(), guestPermissionsList);
600 }
601 else {
602 long defaultUserId = userLocalService.getDefaultUserId(companyId);
603
604 userPersistence.addPermissions(defaultUserId, guestPermissionsList);
605 }
606 }
607
608 protected void logAddCommunityPermissions(
609 long groupId, String name, long resourceId, StopWatch stopWatch,
610 int block) {
611
612 if (!_log.isDebugEnabled()) {
613 return;
614 }
615
616 _log.debug(
617 "Adding community permissions block " + block + " for " + groupId +
618 " " + name + " " + resourceId + " takes " +
619 stopWatch.getTime() + " ms");
620 }
621
622 protected void logAddResources(
623 String name, String primKey, StopWatch stopWatch, int block) {
624
625 if (!_log.isDebugEnabled()) {
626 return;
627 }
628
629 _log.debug(
630 "Adding resources block " + block + " for " + name + " " + primKey +
631 " takes " + stopWatch.getTime() + " ms");
632 }
633
634 protected void validate(
635 long companyId, String name, boolean portletActions)
636 throws PortalException, SystemException {
637
638 List<String> actions = null;
639
640 if (portletActions) {
641 actions = ResourceActionsUtil.getPortletResourceActions(
642 companyId, name);
643 }
644 else {
645 actions = ResourceActionsUtil.getModelResourceActions(name);
646 }
647
648 if (actions.size() == 0) {
649 throw new ResourceActionsException(
650 "There are no actions associated with the resource " + name);
651 }
652 }
653
654 private static Log _log =
655 LogFactoryUtil.getLog(ResourceLocalServiceImpl.class);
656
657 }