1
22
23 package com.liferay.portal.security.permission;
24
25 import com.liferay.portal.SystemException;
26 import com.liferay.portal.kernel.language.LanguageUtil;
27 import com.liferay.portal.kernel.util.StringUtil;
28 import com.liferay.portal.kernel.util.Validator;
29 import com.liferay.portal.model.Location;
30 import com.liferay.portal.model.Organization;
31 import com.liferay.portal.model.PasswordPolicy;
32 import com.liferay.portal.model.Permission;
33 import com.liferay.portal.model.Portlet;
34 import com.liferay.portal.model.Role;
35 import com.liferay.portal.model.User;
36 import com.liferay.portal.model.UserGroup;
37 import com.liferay.portal.model.impl.PortletImpl;
38 import com.liferay.portal.service.PortletLocalServiceUtil;
39 import com.liferay.portal.util.PortalUtil;
40 import com.liferay.portal.util.PortletKeys;
41 import com.liferay.portal.util.PropsUtil;
42 import com.liferay.portlet.PortletResourceBundles;
43 import com.liferay.util.UniqueList;
44
45 import java.io.StringReader;
46
47 import java.util.ArrayList;
48 import java.util.Collections;
49 import java.util.HashMap;
50 import java.util.HashSet;
51 import java.util.Iterator;
52 import java.util.List;
53 import java.util.Locale;
54 import java.util.Map;
55 import java.util.Set;
56
57 import javax.servlet.jsp.PageContext;
58
59 import org.apache.commons.logging.Log;
60 import org.apache.commons.logging.LogFactory;
61
62 import org.dom4j.Document;
63 import org.dom4j.Element;
64 import org.dom4j.io.SAXReader;
65
66
72 public class ResourceActionsUtil {
73
74 public static final String ACTION_NAME_PREFIX = "action.";
75
76 public static final String MODEL_RESOURCE_NAME_PREFIX = "model.resource.";
77
78 public static final String[] ORGANIZATION_MODEL_RESOURCES = {
79 Location.class.getName(), Organization.class.getName(),
80 PasswordPolicy.class.getName(), User.class.getName()
81 };
82
83 public static final String[] PORTAL_MODEL_RESOURCES = {
84 Location.class.getName(), Organization.class.getName(),
85 PasswordPolicy.class.getName(), Role.class.getName(),
86 User.class.getName(), UserGroup.class.getName()
87 };
88
89 public static String getAction(
90 long companyId, Locale locale, String action) {
91
92 String key = ACTION_NAME_PREFIX + action;
93
94 String value = LanguageUtil.get(companyId, locale, key, null);
95
96 if ((value == null) || (value.equals(key))) {
97 value = PortletResourceBundles.getString(locale, key);
98 }
99
100 if (value == null) {
101 value = key;
102 }
103
104 return value;
105 }
106
107 public static String getAction(PageContext pageContext, String action) {
108 String key = ACTION_NAME_PREFIX + action;
109
110 String value = LanguageUtil.get(pageContext, key, null);
111
112 if ((value == null) || (value.equals(key))) {
113 value = PortletResourceBundles.getString(pageContext, key);
114 }
115
116 if (value == null) {
117 value = key;
118 }
119
120 return value;
121 }
122
123 public static List<String> getActions(List<Permission> permissions) {
124 List<String> actions = new UniqueList<String>();
125
126 for (Permission permission : permissions) {
127 actions.add(permission.getActionId());
128 }
129
130 return actions;
131 }
132
133 public static List<String> getActionsNames(
134 PageContext pageContext, List<String> actions) {
135
136 List<String> uniqueList = new UniqueList<String>();
137
138 for (String action : actions) {
139 uniqueList.add(getAction(pageContext, action));
140 }
141
142 List<String> list = new ArrayList<String>();
143
144 list.addAll(uniqueList);
145
146 Collections.sort(list);
147
148 return list;
149 }
150
151 public static List<String> getModelPortletResources(String name) {
152 return _instance._getModelPortletResources(name);
153 }
154
155 public static String getModelResource(
156 long companyId, Locale locale, String name) {
157
158 String key = MODEL_RESOURCE_NAME_PREFIX + name;
159
160 String value = LanguageUtil.get(companyId, locale, key, null);
161
162 if ((value == null) || (value.equals(key))) {
163 value = PortletResourceBundles.getString(locale, key);
164 }
165
166 if (value == null) {
167 value = key;
168 }
169
170 return value;
171 }
172
173 public static String getModelResource(
174 PageContext pageContext, String name) {
175
176 String key = MODEL_RESOURCE_NAME_PREFIX + name;
177
178 String value = LanguageUtil.get(pageContext, key, null);
179
180 if ((value == null) || (value.equals(key))) {
181 value = PortletResourceBundles.getString(pageContext, key);
182 }
183
184 if (value == null) {
185 value = key;
186 }
187
188 return value;
189 }
190
191 public static List<String> getModelResourceActions(String name) {
192 return _instance._getModelResourceActions(name);
193 }
194
195 public static List<String> getModelResourceCommunityDefaultActions(
196 String name) {
197
198 return _instance._getModelResourceCommunityDefaultActions(name);
199 }
200
201 public static List<String> getModelResourceGuestDefaultActions(
202 String name) {
203
204 return _instance._getModelResourceGuestDefaultActions(name);
205 }
206
207 public static List<String> getModelResourceGuestUnsupportedActions(
208 String name) {
209
210 return _instance._getModelResourceGuestUnsupportedActions(name);
211 }
212
213 public static List<String> getPortletModelResources(String portletName) {
214 return _instance._getPortletModelResources(portletName);
215 }
216
217 public static List<String> getPortletResourceActions(
218 long companyId, String name)
219 throws SystemException {
220
221 return _instance._getPortletResourceActions(companyId, name);
222 }
223
224 public static List<String> getPortletResourceCommunityDefaultActions(
225 String name)
226 throws SystemException {
227
228 return _instance._getPortletResourceCommunityDefaultActions(name);
229 }
230
231 public static List<String> getPortletResourceGuestDefaultActions(
232 String name)
233 throws SystemException {
234
235 return _instance._getPortletResourceGuestDefaultActions(name);
236 }
237
238 public static List<String> getPortletResourceGuestUnsupportedActions(
239 String name)
240 throws SystemException {
241
242 return _instance._getPortletResourceGuestUnsupportedActions(name);
243 }
244
245 public static List<String> getPortletResourceLayoutManagerActions(
246 String name)
247 throws SystemException {
248
249 return _instance._getPortletResourceLayoutManagerActions(name);
250 }
251
252 public static List<String> getResourceActions(
253 long companyId, String portletResource, String modelResource)
254 throws SystemException {
255
256 List<String> actions = null;
257
258 if (Validator.isNull(modelResource)) {
259 actions = getPortletResourceActions(companyId, portletResource);
260 }
261 else {
262 actions = getModelResourceActions(modelResource);
263 }
264
265 return actions;
266 }
267
268 public static List<String> getResourceGuestUnsupportedActions(
269 String portletResource, String modelResource)
270 throws SystemException {
271
272 List<String> actions = null;
273
274 if (Validator.isNull(modelResource)) {
275 actions =
276 getPortletResourceGuestUnsupportedActions(portletResource);
277 }
278 else {
279 actions = getModelResourceGuestUnsupportedActions(modelResource);
280 }
281
282 return actions;
283 }
284
285 public static boolean isOrganizationModelResource(String modelResource) {
286 return _instance._isOrganizationModelResource(modelResource);
287 }
288
289 public static boolean isPortalModelResource(String modelResource) {
290 return _instance._isPortalModelResource(modelResource);
291 }
292
293 public static void read(
294 String servletContextName, ClassLoader classLoader, String source)
295 throws Exception {
296
297 _instance._read(servletContextName, classLoader, source);
298 }
299
300 private ResourceActionsUtil() {
301 _organizationModelResources = new HashSet<String>();
302
303 for (int i = 0; i < ORGANIZATION_MODEL_RESOURCES.length; i++) {
304 _organizationModelResources.add(ORGANIZATION_MODEL_RESOURCES[i]);
305 }
306
307 _portalModelResources = new HashSet<String>();
308
309 for (int i = 0; i < PORTAL_MODEL_RESOURCES.length; i++) {
310 _portalModelResources.add(PORTAL_MODEL_RESOURCES[i]);
311 }
312
313 _portletModelResources = new HashMap<String, Set<String>>();
314 _portletResourceActions = new HashMap<String, List<String>>();
315 _portletResourceCommunityDefaultActions =
316 new HashMap<String, List<String>>();
317 _portletResourceGuestDefaultActions =
318 new HashMap<String, List<String>>();
319 _portletResourceGuestUnsupportedActions =
320 new HashMap<String, List<String>>();
321 _portletResourceLayoutManagerActions =
322 new HashMap<String, List<String>>();
323 _modelPortletResources = new HashMap<String, Set<String>>();
324 _modelResourceActions = new HashMap<String, List<String>>();
325 _modelResourceCommunityDefaultActions =
326 new HashMap<String, List<String>>();
327 _modelResourceGuestDefaultActions =
328 new HashMap<String, List<String>>();
329 _modelResourceGuestUnsupportedActions =
330 new HashMap<String, List<String>>();
331
332 try {
333 ClassLoader classLoader = getClass().getClassLoader();
334
335 String[] configs = StringUtil.split(
336 PropsUtil.get(PropsUtil.RESOURCE_ACTIONS_CONFIGS));
337
338 for (int i = 0; i < configs.length; i++) {
339 _read(null, classLoader, configs[i]);
340 }
341 }
342 catch (Exception e) {
343 _log.error(e, e);
344 }
345 }
346
347 private void _checkGuestUnsupportedActions(
348 List<String> guestUnsupportedActions,
349 List<String> guestDefaultActions) {
350
351
353 Iterator<String> itr = guestDefaultActions.iterator();
354
355 while (itr.hasNext()) {
356 String actionId = itr.next();
357
358 if (guestUnsupportedActions.contains(actionId)) {
359 itr.remove();
360 }
361 }
362 }
363
364 private void _checkPortletActions(List<String> actions) {
365 if (!actions.contains("CONFIGURATION")) {
366 actions.add("CONFIGURATION");
367 }
368
369 if (!actions.contains("VIEW")) {
370 actions.add("VIEW");
371 }
372 }
373
374 private void _checkPortletCommunityDefaultActions(List<String> actions) {
375 if (actions.size() == 0) {
376 actions.add("VIEW");
377 }
378 }
379
380 private void _checkPortletGuestDefaultActions(List<String> actions) {
381 if (actions.size() == 0) {
382 actions.add("VIEW");
383 }
384 }
385
386 private void _checkPortletLayoutManagerActions(List<String> actions) {
387 if (!actions.contains("CONFIGURATION")) {
388 actions.add("CONFIGURATION");
389 }
390
391 if (!actions.contains("VIEW")) {
392 actions.add("VIEW");
393 }
394 }
395
396 private List<String> _getActions(
397 Map<String, List<String>> map, String name) {
398
399 List<String> actions = map.get(name);
400
401 if (actions == null) {
402 actions = new UniqueList<String>();
403
404 map.put(name, actions);
405 }
406
407 return actions;
408 }
409
410 private List<String> _getModelPortletResources(String name) {
411 Set<String> resources = _modelPortletResources.get(name);
412
413 if (resources == null) {
414 return new UniqueList<String>();
415 }
416 else {
417 return Collections.list(Collections.enumeration(resources));
418 }
419 }
420
421 private List<String> _getModelResourceActions(String name) {
422 return _getActions(_modelResourceActions, name);
423 }
424
425 private List<String> _getModelResourceCommunityDefaultActions(
426 String name) {
427
428 return _getActions(_modelResourceCommunityDefaultActions, name);
429 }
430
431 private List<String> _getModelResourceGuestDefaultActions(String name) {
432 return _getActions(_modelResourceGuestDefaultActions, name);
433 }
434
435 private List<String> _getModelResourceGuestUnsupportedActions(String name) {
436 return _getActions(_modelResourceGuestUnsupportedActions, name);
437 }
438
439 private List<String> _getPortletModelResources(String portletName) {
440 portletName = PortletImpl.getRootPortletId(portletName);
441
442 Set<String> resources = _portletModelResources.get(portletName);
443
444 if (resources == null) {
445 return new UniqueList<String>();
446 }
447 else {
448 return Collections.list(Collections.enumeration(resources));
449 }
450 }
451
452 private List<String> _getPortletResourceActions(long companyId, String name)
453 throws SystemException {
454
455 name = PortletImpl.getRootPortletId(name);
456
457 List<String> actions = _getActions(_portletResourceActions, name);
458
459 if (actions.size() == 0) {
460 synchronized (this) {
461 Portlet portlet = PortletLocalServiceUtil.getPortletById(
462 companyId, name);
463
464 Map<String, Set<String>> portletModes =
465 portlet.getPortletModes();
466
467 Set<String> mimeTypeModes = portletModes.get("text/html");
468
469 if (mimeTypeModes != null) {
470 for (String actionId : mimeTypeModes) {
471 if (actionId.equalsIgnoreCase("edit")) {
472 actions.add(ActionKeys.PREFERENCES);
473 }
474 else if (actionId.equalsIgnoreCase("edit_guest")) {
475 actions.add(ActionKeys.GUEST_PREFERENCES);
476 }
477 else {
478 actions.add(actionId.toUpperCase());
479 }
480 }
481 }
482
483 _checkPortletActions(actions);
484
485 List<String> communityDefaultActions =
486 _portletResourceCommunityDefaultActions.get(name);
487
488 if (communityDefaultActions == null) {
489 communityDefaultActions = new UniqueList<String>();
490
491 _portletResourceCommunityDefaultActions.put(
492 name, communityDefaultActions);
493
494 _checkPortletCommunityDefaultActions(
495 communityDefaultActions);
496 }
497
498 List<String> guestDefaultActions =
499 _portletResourceGuestDefaultActions.get(name);
500
501 if (guestDefaultActions == null) {
502 guestDefaultActions = new UniqueList<String>();
503
504 _portletResourceGuestDefaultActions.put(
505 name, guestDefaultActions);
506
507 _checkPortletGuestDefaultActions(guestDefaultActions);
508 }
509
510 List<String> layoutManagerActions =
511 _portletResourceLayoutManagerActions.get(name);
512
513 if (layoutManagerActions == null) {
514 layoutManagerActions = new UniqueList<String>();
515
516 _portletResourceLayoutManagerActions.put(
517 name, layoutManagerActions);
518
519 _checkPortletLayoutManagerActions(layoutManagerActions);
520 }
521 }
522 }
523
524 return actions;
525 }
526
527 private List<String> _getPortletResourceCommunityDefaultActions(String name)
528 throws SystemException {
529
530
537 name = PortletImpl.getRootPortletId(name);
538
539 return _getActions(_portletResourceCommunityDefaultActions, name);
540 }
541
542 private List<String> _getPortletResourceGuestDefaultActions(String name)
543 throws SystemException {
544
545 name = PortletImpl.getRootPortletId(name);
546
547 return _getActions(_portletResourceGuestDefaultActions, name);
548 }
549
550 private List<String> _getPortletResourceGuestUnsupportedActions(String name)
551 throws SystemException {
552
553 name = PortletImpl.getRootPortletId(name);
554
555 return _getActions(_portletResourceGuestUnsupportedActions, name);
556 }
557
558 private List<String> _getPortletResourceLayoutManagerActions(String name)
559 throws SystemException {
560
561 name = PortletImpl.getRootPortletId(name);
562
563 List<String> actions = _getActions(
564 _portletResourceLayoutManagerActions, name);
565
566
571 if (actions.size() < 1) {
572 actions.add("CONFIGURATION");
573 actions.add("VIEW");
574 }
575
576 return actions;
577 }
578
579 private boolean _isOrganizationModelResource(String modelResource) {
580 if (_organizationModelResources.contains(modelResource)) {
581 return true;
582 }
583 else {
584 return false;
585 }
586 }
587
588 private boolean _isPortalModelResource(String modelResource) {
589 if (_portalModelResources.contains(modelResource)) {
590 return true;
591 }
592 else {
593 return false;
594 }
595 }
596
597 private void _read(
598 String servletContextName, ClassLoader classLoader, String source)
599 throws Exception {
600
601 String xml = null;
602
603 try {
604 xml = StringUtil.read(classLoader, source);
605 }
606 catch (Exception e) {
607 _log.warn("Cannot load " + source);
608 }
609
610 if (xml == null) {
611 return;
612 }
613
614 if (_log.isDebugEnabled()) {
615 _log.debug("Loading " + source);
616 }
617
618 SAXReader reader = new SAXReader();
619
620 Document doc = reader.read(new StringReader(xml));
621
622 Element root = doc.getRootElement();
623
624 Iterator<Element> itr1 = root.elements("resource").iterator();
625
626 while (itr1.hasNext()) {
627 Element resource = itr1.next();
628
629 String file = resource.attributeValue("file");
630
631 _read(servletContextName, classLoader, file);
632 }
633
634 itr1 = root.elements("portlet-resource").iterator();
635
636 while (itr1.hasNext()) {
637 Element resource = itr1.next();
638
639 String name = resource.elementText("portlet-name");
640
641 if (servletContextName != null) {
642 name = name + PortletImpl.WAR_SEPARATOR + servletContextName;
643 }
644
645 name = PortalUtil.getJsSafePortletId(name);
646
647
649 List<String> actions = _getActions(_portletResourceActions, name);
650
651 Element supports = resource.element("supports");
652
653 Iterator<Element> itr2 = supports.elements("action-key").iterator();
654
655 while (itr2.hasNext()) {
656 Element actionKey = itr2.next();
657
658 String actionKeyText = actionKey.getText();
659
660 if (Validator.isNotNull(actionKeyText)) {
661 actions.add(actionKeyText);
662 }
663 }
664
665 if (!name.equals(PortletKeys.PORTAL)) {
666 _checkPortletActions(actions);
667 }
668
669
671 List<String> communityDefaultActions =
672 _getActions(_portletResourceCommunityDefaultActions, name);
673
674 Element communityDefaults = resource.element("community-defaults");
675
676 itr2 = communityDefaults.elements("action-key").iterator();
677
678 while (itr2.hasNext()) {
679 Element actionKey = itr2.next();
680
681 String actionKeyText = actionKey.getText();
682
683 if (Validator.isNotNull(actionKeyText)) {
684 communityDefaultActions.add(actionKeyText);
685 }
686 }
687
688
690 List<String> guestDefaultActions =
691 _getActions(_portletResourceGuestDefaultActions, name);
692
693 Element guestDefaults = resource.element("guest-defaults");
694
695 itr2 = guestDefaults.elements("action-key").iterator();
696
697 while (itr2.hasNext()) {
698 Element actionKey = itr2.next();
699
700 String actionKeyText = actionKey.getText();
701
702 if (Validator.isNotNull(actionKeyText)) {
703 guestDefaultActions.add(actionKeyText);
704 }
705 }
706
707
709 List<String> guestUnsupportedActions =
710 _getActions(_portletResourceGuestUnsupportedActions, name);
711
712 Element guestUnsupported = resource.element("guest-unsupported");
713
714 itr2 = guestUnsupported.elements("action-key").iterator();
715
716 while (itr2.hasNext()) {
717 Element actionKey = itr2.next();
718
719 String actionKeyText = actionKey.getText();
720
721 if (Validator.isNotNull(actionKeyText)) {
722 guestUnsupportedActions.add(actionKeyText);
723 }
724 }
725
726 _checkGuestUnsupportedActions(
727 guestUnsupportedActions, guestDefaultActions);
728
729
731 List<String> layoutManagerActions = _getActions(
732 _portletResourceLayoutManagerActions, name);
733
734 Element layoutManager = resource.element("layout-manager");
735
736 if (layoutManager != null) {
737 itr2 = layoutManager.elements("action-key").iterator();
738
739 while (itr2.hasNext()) {
740 Element actionKey = itr2.next();
741
742 String actionKeyText = actionKey.getText();
743
744 if (Validator.isNotNull(actionKeyText)) {
745 layoutManagerActions.add(actionKeyText);
746 }
747 }
748 }
749 else {
750
751
754 layoutManagerActions.addAll(actions);
755 }
756 }
757
758 itr1 = root.elements("model-resource").iterator();
759
760 while (itr1.hasNext()) {
761 Element resource = itr1.next();
762
763 String name = resource.elementText("model-name");
764
765 Element portletRef = resource.element("portlet-ref");
766
767 Iterator<Element> itr2 = portletRef.elements(
768 "portlet-name").iterator();
769
770 while (itr2.hasNext()) {
771 Element portletName = itr2.next();
772
773 String portletNameString = portletName.getText();
774
775 if (servletContextName != null) {
776 portletNameString =
777 portletNameString + PortletImpl.WAR_SEPARATOR +
778 servletContextName;
779 }
780
781 portletNameString = PortalUtil.getJsSafePortletId(
782 portletNameString);
783
784
786 Set<String> modelResources = _portletModelResources.get(
787 portletNameString);
788
789 if (modelResources == null) {
790 modelResources = new HashSet<String>();
791
792 _portletModelResources.put(
793 portletNameString, modelResources);
794 }
795
796 modelResources.add(name);
797
798
800 Set<String> portletResources = _modelPortletResources.get(name);
801
802 if (portletResources == null) {
803 portletResources = new HashSet<String>();
804
805 _modelPortletResources.put(name, portletResources);
806 }
807
808 portletResources.add(portletNameString);
809 }
810
811
813 List<String> actions = _getActions(_modelResourceActions, name);
814
815 Element supports = resource.element("supports");
816
817 itr2 = supports.elements("action-key").iterator();
818
819 while (itr2.hasNext()) {
820 Element actionKey = itr2.next();
821
822 String actionKeyText = actionKey.getText();
823
824 if (Validator.isNotNull(actionKeyText)) {
825 actions.add(actionKeyText);
826 }
827 }
828
829
831 List<String> communityDefaultActions =
832 _getActions(_modelResourceCommunityDefaultActions, name);
833
834 Element communityDefaults = resource.element("community-defaults");
835
836 itr2 = communityDefaults.elements("action-key").iterator();
837
838 while (itr2.hasNext()) {
839 Element actionKey = itr2.next();
840
841 String actionKeyText = actionKey.getText();
842
843 if (Validator.isNotNull(actionKeyText)) {
844 communityDefaultActions.add(actionKeyText);
845 }
846 }
847
848
850 List<String> guestDefaultActions =
851 _getActions(_modelResourceGuestDefaultActions, name);
852
853 Element guestDefaults = resource.element("guest-defaults");
854
855 itr2 = guestDefaults.elements("action-key").iterator();
856
857 while (itr2.hasNext()) {
858 Element actionKey = itr2.next();
859
860 String actionKeyText = actionKey.getText();
861
862 if (Validator.isNotNull(actionKeyText)) {
863 guestDefaultActions.add(actionKeyText);
864 }
865 }
866
867
869 List<String> guestUnsupportedActions =
870 _getActions(_modelResourceGuestUnsupportedActions, name);
871
872 Element guestUnsupported = resource.element("guest-unsupported");
873
874 itr2 = guestUnsupported.elements("action-key").iterator();
875
876 while (itr2.hasNext()) {
877 Element actionKey = itr2.next();
878
879 String actionKeyText = actionKey.getText();
880
881 if (Validator.isNotNull(actionKeyText)) {
882 guestUnsupportedActions.add(actionKeyText);
883 }
884 }
885
886 _checkGuestUnsupportedActions(
887 guestUnsupportedActions, guestDefaultActions);
888 }
889 }
890
891 private static Log _log = LogFactory.getLog(ResourceActionsUtil.class);
892
893 private static ResourceActionsUtil _instance = new ResourceActionsUtil();
894
895 private Set<String> _organizationModelResources;
896 private Set<String> _portalModelResources;
897 private Map<String, Set<String>> _portletModelResources;
898 private Map<String, List<String>> _portletResourceActions;
899 private Map<String, List<String>> _portletResourceCommunityDefaultActions;
900 private Map<String, List<String>> _portletResourceGuestDefaultActions;
901 private Map<String, List<String>> _portletResourceGuestUnsupportedActions;
902 private Map<String, List<String>> _portletResourceLayoutManagerActions;
903 private Map<String, Set<String>> _modelPortletResources;
904 private Map<String, List<String>> _modelResourceActions;
905 private Map<String, List<String>> _modelResourceCommunityDefaultActions;
906 private Map<String, List<String>> _modelResourceGuestDefaultActions;
907 private Map<String, List<String>> _modelResourceGuestUnsupportedActions;
908
909 }