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