1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.security.permission;
16  
17  import com.liferay.portal.NoSuchResourceActionException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.kernel.language.LanguageUtil;
20  import com.liferay.portal.kernel.log.Log;
21  import com.liferay.portal.kernel.log.LogFactoryUtil;
22  import com.liferay.portal.kernel.util.ContentTypes;
23  import com.liferay.portal.kernel.util.GetterUtil;
24  import com.liferay.portal.kernel.util.ListUtil;
25  import com.liferay.portal.kernel.util.PropsKeys;
26  import com.liferay.portal.kernel.util.StringPool;
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.Group;
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.ResourceAction;
39  import com.liferay.portal.model.Role;
40  import com.liferay.portal.model.RoleConstants;
41  import com.liferay.portal.model.User;
42  import com.liferay.portal.model.UserGroup;
43  import com.liferay.portal.service.PortletLocalServiceUtil;
44  import com.liferay.portal.service.ResourceActionLocalServiceUtil;
45  import com.liferay.portal.service.RoleLocalServiceUtil;
46  import com.liferay.portal.util.PortalUtil;
47  import com.liferay.portal.util.PortletKeys;
48  import com.liferay.portal.util.PropsUtil;
49  import com.liferay.portal.util.PropsValues;
50  import com.liferay.portlet.PortletResourceBundles;
51  import com.liferay.portlet.expando.model.ExpandoColumn;
52  import com.liferay.portlet.social.model.SocialEquityActionMapping;
53  import com.liferay.util.UniqueList;
54  
55  import java.io.InputStream;
56  
57  import java.util.ArrayList;
58  import java.util.Collections;
59  import java.util.HashMap;
60  import java.util.HashSet;
61  import java.util.Iterator;
62  import java.util.List;
63  import java.util.Locale;
64  import java.util.Map;
65  import java.util.Set;
66  
67  import javax.servlet.jsp.PageContext;
68  
69  /**
70   * <a href="ResourceActionsUtil.java.html"><b><i>View Source</i></b></a>
71   *
72   * @author Brian Wing Shun Chan
73   */
74  public class ResourceActionsUtil {
75  
76      public static final String ACTION_NAME_PREFIX = "action.";
77  
78      public static final String MODEL_RESOURCE_NAME_PREFIX = "model.resource.";
79  
80      public static final String[] ORGANIZATION_MODEL_RESOURCES = {
81          Organization.class.getName(), PasswordPolicy.class.getName(),
82          User.class.getName()
83      };
84  
85      public static final String[] PORTAL_MODEL_RESOURCES = {
86          ExpandoColumn.class.getName(), Organization.class.getName(),
87          PasswordPolicy.class.getName(), Role.class.getName(),
88          User.class.getName(), UserGroup.class.getName()
89      };
90  
91      public static void checkAction(String name, String actionId)
92          throws NoSuchResourceActionException {
93  
94          List<String> resourceActions = getResourceActions(name);
95  
96          if (!resourceActions.contains(actionId)) {
97              throw new NoSuchResourceActionException(
98                  name.concat(StringPool.POUND).concat(actionId));
99          }
100     }
101 
102     public static String getAction(Locale locale, String action) {
103         String key = ACTION_NAME_PREFIX + action;
104 
105         String value = LanguageUtil.get(locale, key, null);
106 
107         if ((value == null) || (value.equals(key))) {
108             value = PortletResourceBundles.getString(locale, key);
109         }
110 
111         if (value == null) {
112             value = key;
113         }
114 
115         return value;
116     }
117 
118     public static String getAction(PageContext pageContext, String action) {
119         String key = ACTION_NAME_PREFIX + action;
120 
121         String value = LanguageUtil.get(pageContext, key, null);
122 
123         if ((value == null) || (value.equals(key))) {
124             value = PortletResourceBundles.getString(pageContext, key);
125         }
126 
127         if (value == null) {
128             value = key;
129         }
130 
131         return value;
132     }
133 
134     public static List<String> getActions(List<Permission> permissions) {
135         List<String> actions = new UniqueList<String>();
136 
137         for (Permission permission : permissions) {
138             actions.add(permission.getActionId());
139         }
140 
141         return actions;
142     }
143 
144     public static List<String> getActionsNames(
145         PageContext pageContext, List<String> actions) {
146 
147         List<String> uniqueList = new UniqueList<String>();
148 
149         for (String action : actions) {
150             uniqueList.add(getAction(pageContext, action));
151         }
152 
153         List<String> list = new ArrayList<String>();
154 
155         list.addAll(uniqueList);
156 
157         return list;
158     }
159 
160     public static List<String> getActionsNames(
161         PageContext pageContext, String name, long actionIds) {
162 
163         try {
164             List<ResourceAction> resourceActions =
165                 ResourceActionLocalServiceUtil.getResourceActions(name);
166 
167             List<String> actions = new ArrayList<String>();
168 
169             for (ResourceAction resourceAction : resourceActions) {
170                 long bitwiseValue = resourceAction.getBitwiseValue();
171 
172                 if ((actionIds & bitwiseValue) == bitwiseValue) {
173                     actions.add(resourceAction.getActionId());
174                 }
175             }
176 
177             return getActionsNames(pageContext, actions);
178         }
179         catch (Exception e) {
180             _log.error(e, e);
181 
182             return Collections.EMPTY_LIST;
183         }
184     }
185 
186     public static List<String> getModelNames() {
187         return _instance._getModelNames();
188     }
189 
190     public static List<String> getModelPortletResources(String name) {
191         return _instance._getModelPortletResources(name);
192     }
193 
194     public static String getModelResource(Locale locale, String name) {
195         String key = MODEL_RESOURCE_NAME_PREFIX + name;
196 
197         String value = LanguageUtil.get(locale, key, null);
198 
199         if ((value == null) || (value.equals(key))) {
200             value = PortletResourceBundles.getString(locale, key);
201         }
202 
203         if (value == null) {
204             value = key;
205         }
206 
207         return value;
208     }
209 
210     public static String getModelResource(
211         PageContext pageContext, String name) {
212 
213         String key = MODEL_RESOURCE_NAME_PREFIX + name;
214 
215         String value = LanguageUtil.get(pageContext, key, null);
216 
217         if ((value == null) || (value.equals(key))) {
218             value = PortletResourceBundles.getString(pageContext, key);
219         }
220 
221         if (value == null) {
222             value = key;
223         }
224 
225         return value;
226     }
227 
228     public static List<String> getModelResourceActions(String name) {
229         return _instance._getModelResourceActions(name);
230     }
231 
232     public static List<String> getModelResourceCommunityDefaultActions(
233         String name) {
234 
235         return _instance._getModelResourceCommunityDefaultActions(name);
236     }
237 
238     public static List<String> getModelResourceGuestDefaultActions(
239         String name) {
240 
241         return _instance._getModelResourceGuestDefaultActions(name);
242     }
243 
244     public static List<String> getModelResourceGuestUnsupportedActions(
245         String name) {
246 
247         return _instance._getModelResourceGuestUnsupportedActions(name);
248     }
249 
250     public static List<String> getModelResourceOwnerDefaultActions(
251         String name) {
252 
253         return _instance._getModelResourceOwnerDefaultActions(name);
254     }
255 
256     public static List<String> getPortletModelResources(String portletName) {
257         return _instance._getPortletModelResources(portletName);
258     }
259 
260     public static List<String> getPortletNames() {
261         return _instance._getPortletNames();
262     }
263 
264     public static List<String> getPortletResourceActions(String name) {
265         return _instance._getPortletResourceActions(name);
266     }
267 
268     public static List<String> getPortletResourceCommunityDefaultActions(
269         String name) {
270 
271         return _instance._getPortletResourceCommunityDefaultActions(name);
272     }
273 
274     public static List<String> getPortletResourceGuestDefaultActions(
275         String name) {
276 
277         return _instance._getPortletResourceGuestDefaultActions(name);
278     }
279 
280     public static List<String> getPortletResourceGuestUnsupportedActions(
281         String name) {
282 
283         return _instance._getPortletResourceGuestUnsupportedActions(name);
284     }
285 
286     public static List<String> getPortletResourceLayoutManagerActions(
287         String name) {
288 
289         return _instance._getPortletResourceLayoutManagerActions(name);
290     }
291 
292     public static List<String> getResourceActions(String name) {
293         if (name.contains(StringPool.PERIOD)) {
294             return getModelResourceActions(name);
295         }
296         else {
297             return getPortletResourceActions(name);
298         }
299     }
300 
301     public static List<String> getResourceActions(
302         String portletResource, String modelResource) {
303 
304         List<String> actions = null;
305 
306         if (Validator.isNull(modelResource)) {
307             actions = getPortletResourceActions(portletResource);
308         }
309         else {
310             actions = getModelResourceActions(modelResource);
311         }
312 
313         return actions;
314     }
315 
316     public static List<String> getResourceCommunityDefaultActions(String name) {
317         if (name.contains(StringPool.PERIOD)) {
318             return getModelResourceCommunityDefaultActions(name);
319         }
320         else {
321             return getPortletResourceCommunityDefaultActions(name);
322         }
323     }
324 
325     public static List<String> getResourceGuestUnsupportedActions(
326         String portletResource, String modelResource) {
327 
328         List<String> actions = null;
329 
330         if (Validator.isNull(modelResource)) {
331             actions =
332                 getPortletResourceGuestUnsupportedActions(portletResource);
333         }
334         else {
335             actions = getModelResourceGuestUnsupportedActions(modelResource);
336         }
337 
338         return actions;
339     }
340 
341     public static List<Role> getRoles(
342             long companyId, Group group, String modelResource)
343         throws SystemException {
344 
345         List<Role> allRoles = RoleLocalServiceUtil.getRoles(companyId);
346 
347         int[] types = new int[] {
348             RoleConstants.TYPE_REGULAR, RoleConstants.TYPE_COMMUNITY
349         };
350 
351         if (isPortalModelResource(modelResource)) {
352             if (modelResource.equals(Organization.class.getName()) ||
353                 modelResource.equals(User.class.getName())) {
354 
355                 types = new int[] {
356                     RoleConstants.TYPE_REGULAR,
357                     RoleConstants.TYPE_ORGANIZATION
358                 };
359             }
360             else {
361                 types = new int[] {RoleConstants.TYPE_REGULAR};
362             }
363         }
364         else {
365             if (group != null) {
366                 if (group.isOrganization(true)) {
367                     types = new int[] {
368                         RoleConstants.TYPE_REGULAR,
369                         RoleConstants.TYPE_ORGANIZATION
370                     };
371                 }
372                 else if (group.isUser()) {
373                     types = new int[] {RoleConstants.TYPE_REGULAR};
374                 }
375             }
376         }
377 
378         List<Role> roles = new ArrayList<Role>();
379 
380         for (int type : types) {
381             for (Role role : allRoles) {
382                 if (role.getType() == type) {
383                     roles.add(role);
384                 }
385             }
386         }
387 
388         return roles;
389     }
390 
391     public static SocialEquityActionMapping getSocialEquityActionMapping(
392         String name, String actionId) {
393 
394         return _instance._getSocialEquityActionMapping(name, actionId);
395     }
396 
397     public static List<SocialEquityActionMapping> getSocialEquityActionMappings(
398         String name) {
399 
400         return _instance._getSocialEquityActionMappings(name);
401     }
402 
403     public static String[] getSocialEquityClassNames() {
404         return _instance._getSocialEquityClassNames();
405     }
406 
407     public static boolean hasModelResourceActions(String name) {
408         return _instance._hasModelResourceActions(name);
409     }
410 
411     public static void init() {
412         _instance._init();
413     }
414 
415     public static boolean isOrganizationModelResource(String modelResource) {
416         return _instance._isOrganizationModelResource(modelResource);
417     }
418 
419     public static boolean isPortalModelResource(String modelResource) {
420         return _instance._isPortalModelResource(modelResource);
421     }
422 
423     public static void read(
424             String servletContextName, ClassLoader classLoader, String source)
425         throws Exception {
426 
427         _instance._read(servletContextName, classLoader, source);
428     }
429 
430     private ResourceActionsUtil() {
431         _organizationModelResources = new HashSet<String>();
432 
433         for (String resource : ORGANIZATION_MODEL_RESOURCES) {
434             _organizationModelResources.add(resource);
435         }
436 
437         _portalModelResources = new HashSet<String>();
438 
439         for (String resource : PORTAL_MODEL_RESOURCES) {
440             _portalModelResources.add(resource);
441         }
442 
443         _portletModelResources = new HashMap<String, Set<String>>();
444         _portletResourceActions = new HashMap<String, List<String>>();
445         _portletResourceCommunityDefaultActions =
446             new HashMap<String, List<String>>();
447         _portletResourceGuestDefaultActions =
448             new HashMap<String, List<String>>();
449         _portletResourceGuestUnsupportedActions =
450             new HashMap<String, List<String>>();
451         _portletResourceLayoutManagerActions =
452             new HashMap<String, List<String>>();
453         _modelPortletResources = new HashMap<String, Set<String>>();
454         _modelResourceActions = new HashMap<String, List<String>>();
455         _modelResourceCommunityDefaultActions =
456             new HashMap<String, List<String>>();
457         _modelResourceGuestDefaultActions =
458             new HashMap<String, List<String>>();
459         _modelResourceGuestUnsupportedActions =
460             new HashMap<String, List<String>>();
461         _modelResourceOwnerDefaultActions =
462             new HashMap<String, List<String>>();
463         _socialEquityActionMappings =
464             new HashMap<String, Map<String, SocialEquityActionMapping>>();
465 
466         try {
467             ClassLoader classLoader = getClass().getClassLoader();
468 
469             String[] configs = StringUtil.split(
470                 PropsUtil.get(PropsKeys.RESOURCE_ACTIONS_CONFIGS));
471 
472             for (String config : configs) {
473                 _read(null, classLoader, config);
474             }
475         }
476         catch (Exception e) {
477             _log.error(e, e);
478         }
479     }
480 
481     private void _checkGuestUnsupportedActions(
482         List<String> guestUnsupportedActions,
483         List<String> guestDefaultActions) {
484 
485         // Guest default actions cannot reference guest unsupported actions
486 
487         Iterator<String> itr = guestDefaultActions.iterator();
488 
489         while (itr.hasNext()) {
490             String actionId = itr.next();
491 
492             if (guestUnsupportedActions.contains(actionId)) {
493                 itr.remove();
494             }
495         }
496     }
497 
498     private void _checkPortletActions(List<String> actions) {
499         if (!actions.contains(ActionKeys.ACCESS_IN_CONTROL_PANEL) &&
500             !actions.contains(ActionKeys.ADD_TO_PAGE)) {
501 
502             actions.add(ActionKeys.ADD_TO_PAGE);
503         }
504 
505         if (!actions.contains(ActionKeys.CONFIGURATION)) {
506             actions.add(ActionKeys.CONFIGURATION);
507         }
508 
509         if (!actions.contains(ActionKeys.VIEW)) {
510             actions.add(ActionKeys.VIEW);
511         }
512     }
513 
514     private void _checkPortletCommunityDefaultActions(List<String> actions) {
515         if (actions.size() == 0) {
516             actions.add(ActionKeys.VIEW);
517         }
518     }
519 
520     private void _checkPortletGuestDefaultActions(List<String> actions) {
521         if (actions.size() == 0) {
522             actions.add(ActionKeys.VIEW);
523         }
524     }
525 
526     private void _checkPortletLayoutManagerActions(List<String> actions) {
527         if (!actions.contains(ActionKeys.CONFIGURATION)) {
528             actions.add(ActionKeys.CONFIGURATION);
529         }
530 
531         if (!actions.contains(ActionKeys.PREFERENCES)) {
532             actions.add(ActionKeys.PREFERENCES);
533         }
534 
535         if (!actions.contains(ActionKeys.VIEW)) {
536             actions.add(ActionKeys.VIEW);
537         }
538     }
539 
540     private List<String> _getActions(
541         Map<String, List<String>> actionsMap, String name) {
542 
543         List<String> actions = actionsMap.get(name);
544 
545         if (actions == null) {
546             actions = new UniqueList<String>();
547 
548             actionsMap.put(name, actions);
549         }
550 
551         return actions;
552     }
553 
554     private List<String> _getModelNames() {
555         return ListUtil.fromCollection(_modelPortletResources.keySet());
556     }
557 
558     private List<String> _getModelPortletResources(String name) {
559         Set<String> resources = _modelPortletResources.get(name);
560 
561         if (resources == null) {
562             return new UniqueList<String>();
563         }
564         else {
565             return Collections.list(Collections.enumeration(resources));
566         }
567     }
568 
569     private List<String> _getModelResourceActions(String name) {
570         return _getActions(_modelResourceActions, name);
571     }
572 
573     private List<String> _getModelResourceCommunityDefaultActions(
574         String name) {
575 
576         return _getActions(_modelResourceCommunityDefaultActions, name);
577     }
578 
579     private List<String> _getModelResourceGuestDefaultActions(String name) {
580         return _getActions(_modelResourceGuestDefaultActions, name);
581     }
582 
583     private List<String> _getModelResourceGuestUnsupportedActions(String name) {
584         return _getActions(_modelResourceGuestUnsupportedActions, name);
585     }
586 
587     private List<String> _getModelResourceOwnerDefaultActions(String name) {
588         return _getActions(_modelResourceOwnerDefaultActions, name);
589     }
590 
591     private Element _getPermissionsChildElement(
592         Element parentElement, String childElementName) {
593 
594         Element permissionsElement = parentElement.element("permissions");
595 
596         if (permissionsElement != null) {
597             return permissionsElement.element(childElementName);
598         }
599         else {
600             return parentElement.element(childElementName);
601         }
602     }
603 
604     private List<String> _getPortletMimeTypeActions(String name) {
605         List<String> actions = new UniqueList<String>();
606 
607         Portlet portlet = PortletLocalServiceUtil.getPortletById(name);
608 
609         if (portlet != null) {
610             Map<String, Set<String>> portletModes =
611                 portlet.getPortletModes();
612 
613             Set<String> mimeTypePortletModes = portletModes.get(
614                 ContentTypes.TEXT_HTML);
615 
616             if (mimeTypePortletModes != null) {
617                 for (String actionId : mimeTypePortletModes) {
618                     if (actionId.equalsIgnoreCase("edit")) {
619                         actions.add(ActionKeys.PREFERENCES);
620                     }
621                     else if (actionId.equalsIgnoreCase("edit_guest")) {
622                         actions.add(ActionKeys.GUEST_PREFERENCES);
623                     }
624                     else {
625                         actions.add(actionId.toUpperCase());
626                     }
627                 }
628             }
629         }
630         else {
631             if (_log.isDebugEnabled()) {
632                 _log.debug(
633                     "Unable to obtain resource actions for unknown portlet " +
634                         name);
635             }
636         }
637 
638         return actions;
639     }
640 
641     private List<String> _getPortletModelResources(String portletName) {
642         portletName = PortletConstants.getRootPortletId(portletName);
643 
644         Set<String> resources = _portletModelResources.get(portletName);
645 
646         if (resources == null) {
647             return new UniqueList<String>();
648         }
649         else {
650             return Collections.list(Collections.enumeration(resources));
651         }
652     }
653 
654     private List<String> _getPortletNames() {
655         return ListUtil.fromCollection(_portletModelResources.keySet());
656     }
657 
658     private List<String> _getPortletResourceActions(String name) {
659         name = PortletConstants.getRootPortletId(name);
660 
661         List<String> actions = _getActions(_portletResourceActions, name);
662 
663         if (actions.size() == 0) {
664             synchronized (this) {
665                 actions = _getPortletMimeTypeActions(name);
666 
667                 if (!name.equals(PortletKeys.PORTAL)) {
668                     _checkPortletActions(actions);
669                 }
670 
671                 List<String> communityDefaultActions =
672                     _portletResourceCommunityDefaultActions.get(name);
673 
674                 if (communityDefaultActions == null) {
675                     communityDefaultActions = new UniqueList<String>();
676 
677                     _portletResourceCommunityDefaultActions.put(
678                         name, communityDefaultActions);
679 
680                     _checkPortletCommunityDefaultActions(
681                         communityDefaultActions);
682                 }
683 
684                 List<String> guestDefaultActions =
685                     _portletResourceGuestDefaultActions.get(name);
686 
687                 if (guestDefaultActions == null) {
688                     guestDefaultActions = new UniqueList<String>();
689 
690                     _portletResourceGuestDefaultActions.put(
691                         name, guestDefaultActions);
692 
693                     _checkPortletGuestDefaultActions(guestDefaultActions);
694                 }
695 
696                 List<String> layoutManagerActions =
697                     _portletResourceLayoutManagerActions.get(name);
698 
699                 if (layoutManagerActions == null) {
700                     layoutManagerActions = new UniqueList<String>();
701 
702                     _portletResourceLayoutManagerActions.put(
703                         name, layoutManagerActions);
704 
705                     _checkPortletLayoutManagerActions(layoutManagerActions);
706                 }
707             }
708         }
709 
710         return actions;
711     }
712 
713     private List<String> _getPortletResourceCommunityDefaultActions(
714         String name) {
715 
716         // This method should always be called only after
717         // _getPortletResourceActions has been called at least once to
718         // populate the default community actions. Check to make sure this is
719         // the case. However, if it is not, that means the methods
720         // _getPortletResourceGuestDefaultActions and
721         // _getPortletResourceGuestDefaultActions may not work either.
722 
723         name = PortletConstants.getRootPortletId(name);
724 
725         return _getActions(_portletResourceCommunityDefaultActions, name);
726     }
727 
728     private List<String> _getPortletResourceGuestDefaultActions(String name) {
729         name = PortletConstants.getRootPortletId(name);
730 
731         return _getActions(_portletResourceGuestDefaultActions, name);
732     }
733 
734     private List<String> _getPortletResourceGuestUnsupportedActions(
735         String name) {
736 
737         name = PortletConstants.getRootPortletId(name);
738 
739         return _getActions(_portletResourceGuestUnsupportedActions, name);
740     }
741 
742     private List<String> _getPortletResourceLayoutManagerActions(String name) {
743         name = PortletConstants.getRootPortletId(name);
744 
745         List<String> actions = _getActions(
746             _portletResourceLayoutManagerActions, name);
747 
748         // This check can never return an empty list. If the list is empty, it
749         // means that the portlet does not have an explicit resource-actions
750         // configuration file and should therefore be handled as if it has
751         // defaults of CONFIGURATION, PREFERENCES, and VIEW.
752 
753         if (actions.size() < 1) {
754             actions.add(ActionKeys.CONFIGURATION);
755             actions.add(ActionKeys.PREFERENCES);
756             actions.add(ActionKeys.VIEW);
757         }
758 
759         return actions;
760     }
761 
762     private SocialEquityActionMapping _getSocialEquityActionMapping(
763         String name, String actionId) {
764 
765         Map<String, SocialEquityActionMapping> socialEquityActionMappings =
766             _socialEquityActionMappings.get(name);
767 
768         if (socialEquityActionMappings == null) {
769             return null;
770         }
771 
772         return socialEquityActionMappings.get(actionId);
773     }
774 
775     private List<SocialEquityActionMapping> _getSocialEquityActionMappings(
776         String name) {
777 
778         Map<String, SocialEquityActionMapping> socialEquityActionMappings =
779             _socialEquityActionMappings.get(name);
780 
781         if (socialEquityActionMappings == null) {
782             return Collections.EMPTY_LIST;
783         }
784 
785         List<SocialEquityActionMapping> socialEquityActionMappingList =
786             new ArrayList<SocialEquityActionMapping>();
787 
788         for (Map.Entry<String, SocialEquityActionMapping> entry :
789                 socialEquityActionMappings.entrySet()) {
790 
791             socialEquityActionMappingList.add(entry.getValue());
792         }
793 
794         return socialEquityActionMappingList;
795     }
796 
797     private String[] _getSocialEquityClassNames() {
798         Set<String> classNames = _socialEquityActionMappings.keySet();
799 
800         return classNames.toArray(new String[classNames.size()]);
801     }
802 
803     private boolean _hasModelResourceActions(String name) {
804         List<String> actions = _modelResourceActions.get(name);
805 
806         if ((actions != null) && !actions.isEmpty()) {
807             return true;
808         }
809         else {
810             return false;
811         }
812     }
813 
814     private void _init() {
815     }
816 
817     private boolean _isOrganizationModelResource(String modelResource) {
818         if (_organizationModelResources.contains(modelResource)) {
819             return true;
820         }
821         else {
822             return false;
823         }
824     }
825 
826     private boolean _isPortalModelResource(String modelResource) {
827         if (_portalModelResources.contains(modelResource)) {
828             return true;
829         }
830         else {
831             return false;
832         }
833     }
834 
835     private void _read(
836             String servletContextName, ClassLoader classLoader, String source)
837         throws Exception {
838 
839         InputStream inputStream = classLoader.getResourceAsStream(source);
840 
841         if (inputStream == null) {
842             if (_log.isWarnEnabled() && !source.endsWith("-ext.xml")) {
843                 _log.warn("Cannot load " + source);
844             }
845 
846             return;
847         }
848 
849         if (_log.isDebugEnabled()) {
850             _log.debug("Loading " + source);
851         }
852 
853         Document document = SAXReaderUtil.read(inputStream);
854 
855         Element rootElement = document.getRootElement();
856 
857         for (Element resourceElement : rootElement.elements("resource")) {
858             String file = resourceElement.attributeValue("file").trim();
859 
860             _read(servletContextName, classLoader, file);
861 
862             String extFile = StringUtil.replace(file, ".xml", "-ext.xml");
863 
864             _read(servletContextName, classLoader, extFile);
865         }
866 
867         if (PropsValues.RESOURCE_ACTIONS_READ_PORTLET_RESOURCES) {
868             for (Element portletResourceElement :
869                     rootElement.elements("portlet-resource")) {
870 
871                 _readPortletResource(
872                     servletContextName, portletResourceElement);
873             }
874         }
875 
876         for (Element modelResourceElement :
877                 rootElement.elements("model-resource")) {
878 
879             _readModelResource(servletContextName, modelResourceElement);
880         }
881     }
882 
883     private void _readActionKeys(Element parentElement, List<String> actions) {
884         for (Element actionKeyElement : parentElement.elements("action-key")) {
885             String actionKey = actionKeyElement.getTextTrim();
886 
887             if (Validator.isNull(actionKey)) {
888                 continue;
889             }
890 
891             actions.add(actionKey);
892         }
893     }
894 
895     private void _readCommunityDefaultActions(
896         Element parentElement, Map<String, List<String>> actionsMap,
897         String name) {
898 
899         List<String> communityDefaultActions = _getActions(actionsMap, name);
900 
901         Element communityDefaultsElement = _getPermissionsChildElement(
902             parentElement, "community-defaults");
903 
904         _readActionKeys(communityDefaultsElement, communityDefaultActions);
905     }
906 
907     private List<String> _readGuestDefaultActions(
908         Element parentElement, Map<String, List<String>> actionsMap,
909         String name) {
910 
911         List<String> guestDefaultActions = _getActions(actionsMap, name);
912 
913         Element guestDefaultsElement = _getPermissionsChildElement(
914             parentElement, "guest-defaults");
915 
916         _readActionKeys(guestDefaultsElement, guestDefaultActions);
917 
918         return guestDefaultActions;
919     }
920 
921     private void _readGuestUnsupportedActions(
922         Element parentElement, Map<String, List<String>> actionsMap,
923         String name, List<String> guestDefaultActions) {
924 
925         List<String> guestUnsupportedActions = _getActions(actionsMap, name);
926 
927         Element guestUnsupportedElement = _getPermissionsChildElement(
928             parentElement, "guest-unsupported");
929 
930         _readActionKeys(guestUnsupportedElement, guestUnsupportedActions);
931 
932         _checkGuestUnsupportedActions(
933             guestUnsupportedActions, guestDefaultActions);
934     }
935 
936     private void _readLayoutManagerActions(
937         Element parentElement, Map<String, List<String>> actionsMap,
938         String name, List<String> supportsActions) {
939 
940         List<String> layoutManagerActions = _getActions(actionsMap, name);
941 
942         Element layoutManagerElement = _getPermissionsChildElement(
943             parentElement, "layout-manager");
944 
945         if (layoutManagerElement != null) {
946             _readActionKeys(layoutManagerElement, layoutManagerActions);
947         }
948         else {
949             layoutManagerActions.addAll(supportsActions);
950         }
951     }
952 
953     private void _readModelResource(
954         String servletContextName, Element modelResourceElement) {
955 
956         String name = modelResourceElement.elementTextTrim("model-name");
957 
958         Element portletRefElement = modelResourceElement.element("portlet-ref");
959 
960         for (Element portletNameElement :
961                 portletRefElement.elements("portlet-name")) {
962 
963             String portletName = portletNameElement.getTextTrim();
964 
965             if (servletContextName != null) {
966                 portletName =
967                     portletName.concat(PortletConstants.WAR_SEPARATOR).concat(
968                         servletContextName);
969             }
970 
971             portletName = PortalUtil.getJsSafePortletId(portletName);
972 
973             // Reference for a portlet to child models
974 
975             Set<String> modelResources = _portletModelResources.get(
976                 portletName);
977 
978             if (modelResources == null) {
979                 modelResources = new HashSet<String>();
980 
981                 _portletModelResources.put(portletName, modelResources);
982             }
983 
984             modelResources.add(name);
985 
986             // Reference for a model to parent portlets
987 
988             Set<String> portletResources = _modelPortletResources.get(name);
989 
990             if (portletResources == null) {
991                 portletResources = new HashSet<String>();
992 
993                 _modelPortletResources.put(name, portletResources);
994             }
995 
996             portletResources.add(portletName);
997         }
998 
999         _readSupportsActions(modelResourceElement, _modelResourceActions, name);
1000
1001        _readCommunityDefaultActions(
1002            modelResourceElement, _modelResourceCommunityDefaultActions,  name);
1003
1004        List<String> guestDefaultActions = _readGuestDefaultActions(
1005            modelResourceElement, _modelResourceGuestDefaultActions, name);
1006
1007        _readGuestUnsupportedActions(
1008            modelResourceElement, _modelResourceGuestUnsupportedActions, name,
1009            guestDefaultActions);
1010
1011        _readOwnerDefaultActions(
1012            modelResourceElement, _modelResourceOwnerDefaultActions, name);
1013
1014        _readSocialEquity(modelResourceElement, name);
1015    }
1016
1017    private void _readOwnerDefaultActions(
1018        Element parentElement, Map<String, List<String>> actionsMap,
1019        String name) {
1020
1021        List<String> ownerDefaultActions = _getActions(actionsMap, name);
1022
1023        Element ownerDefaultsElement = _getPermissionsChildElement(
1024            parentElement, "owner-defaults");
1025
1026        if (ownerDefaultsElement == null) {
1027            return;
1028        }
1029
1030        _readActionKeys(ownerDefaultsElement, ownerDefaultActions);
1031    }
1032
1033    private void _readPortletResource(
1034        String servletContextName, Element portletResourceElement) {
1035
1036        String name = portletResourceElement.elementTextTrim("portlet-name");
1037
1038        if (servletContextName != null) {
1039            name = name.concat(PortletConstants.WAR_SEPARATOR).concat(
1040                servletContextName);
1041        }
1042
1043        name = PortalUtil.getJsSafePortletId(name);
1044
1045        List<String> supportsActions = _readSupportsActions(
1046            portletResourceElement, _portletResourceActions, name);
1047
1048        supportsActions.addAll(_getPortletMimeTypeActions(name));
1049
1050        if (!name.equals(PortletKeys.PORTAL)) {
1051            _checkPortletActions(supportsActions);
1052        }
1053
1054        _readCommunityDefaultActions(
1055            portletResourceElement, _portletResourceCommunityDefaultActions,
1056            name);
1057
1058        List<String> guestDefaultActions = _readGuestDefaultActions(
1059            portletResourceElement, _portletResourceGuestDefaultActions, name);
1060
1061        _readGuestUnsupportedActions(
1062            portletResourceElement, _portletResourceGuestUnsupportedActions,
1063            name, guestDefaultActions);
1064
1065        _readLayoutManagerActions(
1066            portletResourceElement, _portletResourceLayoutManagerActions, name,
1067            supportsActions);
1068    }
1069
1070    private void _readSocialEquity(Element parentElement, String name) {
1071        Element socialEquityElement = parentElement.element("social-equity");
1072
1073        if (socialEquityElement == null) {
1074            return;
1075        }
1076
1077        for (Element socialEquityMappingElement :
1078                socialEquityElement.elements("social-equity-mapping")) {
1079
1080            _readSocialEquityMapping(socialEquityMappingElement, name);
1081        }
1082    }
1083
1084    private void _readSocialEquityMapping(
1085        Element socialEquityMappingElement, String name) {
1086
1087        Element actionKeyElement =
1088            socialEquityMappingElement.element("action-key");
1089
1090        if (actionKeyElement == null) {
1091            return;
1092        }
1093
1094        String actionKey = actionKeyElement.getTextTrim();
1095
1096        if (Validator.isNull(actionKey)) {
1097            return;
1098        }
1099
1100        int informationValue = GetterUtil.getInteger(
1101            socialEquityMappingElement.elementText("information-value"));
1102        int informationLifespan = GetterUtil.getInteger(
1103            socialEquityMappingElement.elementText("information-lifespan"));
1104        int participationValue = GetterUtil.getInteger(
1105            socialEquityMappingElement.elementText("participation-value"));
1106        int participationLifespan = GetterUtil.getInteger(
1107            socialEquityMappingElement.elementText("participation-lifespan"));
1108
1109        SocialEquityActionMapping socialEquityActionMapping =
1110            new SocialEquityActionMapping();
1111
1112        socialEquityActionMapping.setActionId(actionKey);
1113        socialEquityActionMapping.setClassName(name);
1114        socialEquityActionMapping.setInformationValue(informationValue);
1115        socialEquityActionMapping.setInformationLifespan(informationLifespan);
1116        socialEquityActionMapping.setParticipationValue(participationValue);
1117        socialEquityActionMapping.setParticipationLifespan(
1118            participationLifespan);
1119
1120        Map<String, SocialEquityActionMapping> socialEquityActionMappings =
1121            _socialEquityActionMappings.get(name);
1122
1123        if (socialEquityActionMappings == null) {
1124            socialEquityActionMappings =
1125                new HashMap<String, SocialEquityActionMapping>();
1126
1127            _socialEquityActionMappings.put(name, socialEquityActionMappings);
1128        }
1129
1130        socialEquityActionMappings.put(actionKey, socialEquityActionMapping);
1131    }
1132
1133    private List<String> _readSupportsActions(
1134        Element parentElement, Map<String, List<String>> actionsMap,
1135        String name) {
1136
1137        List<String> supportsActions = _getActions(actionsMap, name);
1138
1139        Element supportsElement = _getPermissionsChildElement(
1140            parentElement, "supports");
1141
1142        _readActionKeys(supportsElement, supportsActions);
1143
1144        return supportsActions;
1145    }
1146
1147    private static Log _log = LogFactoryUtil.getLog(ResourceActionsUtil.class);
1148
1149    private static ResourceActionsUtil _instance = new ResourceActionsUtil();
1150
1151    private Map<String, Set<String>> _modelPortletResources;
1152    private Map<String, List<String>> _modelResourceActions;
1153    private Map<String, List<String>> _modelResourceCommunityDefaultActions;
1154    private Map<String, List<String>> _modelResourceGuestDefaultActions;
1155    private Map<String, List<String>> _modelResourceGuestUnsupportedActions;
1156    private Map<String, List<String>> _modelResourceOwnerDefaultActions;
1157    private Set<String> _organizationModelResources;
1158    private Set<String> _portalModelResources;
1159    private Map<String, Set<String>> _portletModelResources;
1160    private Map<String, List<String>> _portletResourceActions;
1161    private Map<String, List<String>> _portletResourceCommunityDefaultActions;
1162    private Map<String, List<String>> _portletResourceGuestDefaultActions;
1163    private Map<String, List<String>> _portletResourceGuestUnsupportedActions;
1164    private Map<String, List<String>> _portletResourceLayoutManagerActions;
1165    private Map<String, Map<String, SocialEquityActionMapping>>
1166        _socialEquityActionMappings;
1167
1168}