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