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