1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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.log.Log;
28  import com.liferay.portal.kernel.log.LogFactoryUtil;
29  import com.liferay.portal.kernel.util.StringUtil;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.kernel.xml.Document;
32  import com.liferay.portal.kernel.xml.Element;
33  import com.liferay.portal.kernel.xml.SAXReaderUtil;
34  import com.liferay.portal.model.Group;
35  import com.liferay.portal.model.Organization;
36  import com.liferay.portal.model.PasswordPolicy;
37  import com.liferay.portal.model.Permission;
38  import com.liferay.portal.model.Portlet;
39  import com.liferay.portal.model.PortletConstants;
40  import com.liferay.portal.model.Role;
41  import com.liferay.portal.model.RoleConstants;
42  import com.liferay.portal.model.User;
43  import com.liferay.portal.model.UserGroup;
44  import com.liferay.portal.service.PortletLocalServiceUtil;
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.PropsKeys;
49  import com.liferay.portal.util.PropsUtil;
50  import com.liferay.portlet.PortletResourceBundles;
51  import com.liferay.portlet.expando.model.ExpandoColumn;
52  import com.liferay.portlet.expando.model.ExpandoValue;
53  import com.liferay.util.UniqueList;
54  
55  import java.util.ArrayList;
56  import java.util.Collections;
57  import java.util.HashMap;
58  import java.util.HashSet;
59  import java.util.Iterator;
60  import java.util.List;
61  import java.util.Locale;
62  import java.util.Map;
63  import java.util.Set;
64  
65  import javax.servlet.jsp.PageContext;
66  
67  /**
68   * <a href="ResourceActionsUtil.java.html"><b><i>View Source</i></b></a>
69   *
70   * @author Brian Wing Shun Chan
71   *
72   */
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          Organization.class.getName(), PasswordPolicy.class.getName(),
81          User.class.getName()
82      };
83  
84      public static final String[] PORTAL_MODEL_RESOURCES = {
85          ExpandoColumn.class.getName(), ExpandoValue.class.getName(),
86          Organization.class.getName(), PasswordPolicy.class.getName(),
87          Role.class.getName(), 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         return list;
148     }
149 
150     public static List<String> getModelPortletResources(String name) {
151         return _instance._getModelPortletResources(name);
152     }
153 
154     public static String getModelResource(
155         long companyId, Locale locale, String name) {
156 
157         String key = MODEL_RESOURCE_NAME_PREFIX + name;
158 
159         String value = LanguageUtil.get(companyId, locale, key, null);
160 
161         if ((value == null) || (value.equals(key))) {
162             value = PortletResourceBundles.getString(locale, key);
163         }
164 
165         if (value == null) {
166             value = key;
167         }
168 
169         return value;
170     }
171 
172     public static String getModelResource(
173         PageContext pageContext, String name) {
174 
175         String key = MODEL_RESOURCE_NAME_PREFIX + name;
176 
177         String value = LanguageUtil.get(pageContext, key, null);
178 
179         if ((value == null) || (value.equals(key))) {
180             value = PortletResourceBundles.getString(pageContext, key);
181         }
182 
183         if (value == null) {
184             value = key;
185         }
186 
187         return value;
188     }
189 
190     public static List<String> getModelResourceActions(String name) {
191         return _instance._getModelResourceActions(name);
192     }
193 
194     public static List<String> getModelResourceCommunityDefaultActions(
195         String name) {
196 
197         return _instance._getModelResourceCommunityDefaultActions(name);
198     }
199 
200     public static List<String> getModelResourceGuestDefaultActions(
201         String name) {
202 
203         return _instance._getModelResourceGuestDefaultActions(name);
204     }
205 
206     public static List<String> getModelResourceGuestUnsupportedActions(
207         String name) {
208 
209         return _instance._getModelResourceGuestUnsupportedActions(name);
210     }
211 
212     public static List<String> getPortletModelResources(String portletName) {
213         return _instance._getPortletModelResources(portletName);
214     }
215 
216     public static List<String> getPortletResourceActions(
217             long companyId, String name)
218         throws SystemException {
219 
220         return _instance._getPortletResourceActions(companyId, name);
221     }
222 
223     public static List<String> getPortletResourceCommunityDefaultActions(
224         String name) {
225 
226         return _instance._getPortletResourceCommunityDefaultActions(name);
227     }
228 
229     public static List<String> getPortletResourceGuestDefaultActions(
230         String name) {
231 
232         return _instance._getPortletResourceGuestDefaultActions(name);
233     }
234 
235     public static List<String> getPortletResourceGuestUnsupportedActions(
236         String name) {
237 
238         return _instance._getPortletResourceGuestUnsupportedActions(name);
239     }
240 
241     public static List<String> getPortletResourceLayoutManagerActions(
242         String name) {
243 
244         return _instance._getPortletResourceLayoutManagerActions(name);
245     }
246 
247     public static List<String> getResourceActions(
248             long companyId, String portletResource, String modelResource)
249         throws SystemException {
250 
251         List<String> actions = null;
252 
253         if (Validator.isNull(modelResource)) {
254             actions = getPortletResourceActions(companyId, portletResource);
255         }
256         else {
257             actions = getModelResourceActions(modelResource);
258         }
259 
260         return actions;
261     }
262 
263     public static List<String> getResourceGuestUnsupportedActions(
264         String portletResource, String modelResource) {
265 
266         List<String> actions = null;
267 
268         if (Validator.isNull(modelResource)) {
269             actions =
270                 getPortletResourceGuestUnsupportedActions(portletResource);
271         }
272         else {
273             actions = getModelResourceGuestUnsupportedActions(modelResource);
274         }
275 
276         return actions;
277     }
278 
279     public static List<Role> getRoles(Group group, String modelResource)
280         throws SystemException {
281 
282         List<Role> allRoles = RoleLocalServiceUtil.getRoles(
283             group.getCompanyId());
284 
285         int[] types = new int[] {
286             RoleConstants.TYPE_REGULAR, RoleConstants.TYPE_COMMUNITY
287         };
288 
289         if (isPortalModelResource(modelResource)) {
290             if (modelResource.equals(Organization.class.getName()) ||
291                 modelResource.equals(User.class.getName())) {
292 
293                 types = new int[] {
294                     RoleConstants.TYPE_REGULAR,
295                     RoleConstants.TYPE_ORGANIZATION
296                 };
297             }
298             else {
299                 types = new int[] {RoleConstants.TYPE_REGULAR};
300             }
301         }
302         else {
303             if (group.isOrganization()) {
304                 types = new int[] {
305                     RoleConstants.TYPE_REGULAR,
306                     RoleConstants.TYPE_ORGANIZATION
307                 };
308             }
309             else if (group.isUser()) {
310                 types = new int[] {RoleConstants.TYPE_REGULAR};
311             }
312         }
313 
314         List<Role> roles = new ArrayList<Role>();
315 
316         for (int type : types) {
317             for (Role role : allRoles) {
318                 if (role.getType() == type) {
319                     roles.add(role);
320                 }
321             }
322         }
323 
324         return roles;
325     }
326 
327     public static boolean isOrganizationModelResource(String modelResource) {
328         return _instance._isOrganizationModelResource(modelResource);
329     }
330 
331     public static boolean isPortalModelResource(String modelResource) {
332         return _instance._isPortalModelResource(modelResource);
333     }
334 
335     public static void read(
336             String servletContextName, ClassLoader classLoader, String source)
337         throws Exception {
338 
339         _instance._read(servletContextName, classLoader, source);
340     }
341 
342     private ResourceActionsUtil() {
343         _organizationModelResources = new HashSet<String>();
344 
345         for (int i = 0; i < ORGANIZATION_MODEL_RESOURCES.length; i++) {
346             _organizationModelResources.add(ORGANIZATION_MODEL_RESOURCES[i]);
347         }
348 
349         _portalModelResources = new HashSet<String>();
350 
351         for (int i = 0; i < PORTAL_MODEL_RESOURCES.length; i++) {
352             _portalModelResources.add(PORTAL_MODEL_RESOURCES[i]);
353         }
354 
355         _portletModelResources = new HashMap<String, Set<String>>();
356         _portletResourceActions = new HashMap<String, List<String>>();
357         _portletResourceCommunityDefaultActions =
358             new HashMap<String, List<String>>();
359         _portletResourceGuestDefaultActions =
360             new HashMap<String, List<String>>();
361         _portletResourceGuestUnsupportedActions =
362             new HashMap<String, List<String>>();
363         _portletResourceLayoutManagerActions =
364             new HashMap<String, List<String>>();
365         _modelPortletResources = new HashMap<String, Set<String>>();
366         _modelResourceActions = new HashMap<String, List<String>>();
367         _modelResourceCommunityDefaultActions =
368             new HashMap<String, List<String>>();
369         _modelResourceGuestDefaultActions =
370             new HashMap<String, List<String>>();
371         _modelResourceGuestUnsupportedActions =
372             new HashMap<String, List<String>>();
373 
374         try {
375             ClassLoader classLoader = getClass().getClassLoader();
376 
377             String[] configs = StringUtil.split(
378                 PropsUtil.get(PropsKeys.RESOURCE_ACTIONS_CONFIGS));
379 
380             for (int i = 0; i < configs.length; i++) {
381                 _read(null, classLoader, configs[i]);
382             }
383         }
384         catch (Exception e) {
385             _log.error(e, e);
386         }
387     }
388 
389     private void _checkGuestUnsupportedActions(
390         List<String> guestUnsupportedActions,
391         List<String> guestDefaultActions) {
392 
393         // Guest default actions cannot reference guest unsupported actions
394 
395         Iterator<String> itr = guestDefaultActions.iterator();
396 
397         while (itr.hasNext()) {
398             String actionId = itr.next();
399 
400             if (guestUnsupportedActions.contains(actionId)) {
401                 itr.remove();
402             }
403         }
404     }
405 
406     private void _checkPortletActions(List<String> actions) {
407         if (!actions.contains("CONFIGURATION")) {
408             actions.add("CONFIGURATION");
409         }
410 
411         if (!actions.contains("VIEW")) {
412             actions.add("VIEW");
413         }
414     }
415 
416     private void _checkPortletCommunityDefaultActions(List<String> actions) {
417         if (actions.size() == 0) {
418             actions.add("VIEW");
419         }
420     }
421 
422     private void _checkPortletGuestDefaultActions(List<String> actions) {
423         if (actions.size() == 0) {
424             actions.add("VIEW");
425         }
426     }
427 
428     private void _checkPortletLayoutManagerActions(List<String> actions) {
429         if (!actions.contains("CONFIGURATION")) {
430             actions.add("CONFIGURATION");
431         }
432 
433         if (!actions.contains("PREFERENCES")) {
434             actions.add("PREFERENCES");
435         }
436 
437         if (!actions.contains("VIEW")) {
438             actions.add("VIEW");
439         }
440     }
441 
442     private List<String> _getActions(
443         Map<String, List<String>> map, String name) {
444 
445         List<String> actions = map.get(name);
446 
447         if (actions == null) {
448             actions = new UniqueList<String>();
449 
450             map.put(name, actions);
451         }
452 
453         return actions;
454     }
455 
456     private List<String> _getModelPortletResources(String name) {
457         Set<String> resources = _modelPortletResources.get(name);
458 
459         if (resources == null) {
460             return new UniqueList<String>();
461         }
462         else {
463             return Collections.list(Collections.enumeration(resources));
464         }
465     }
466 
467     private List<String> _getModelResourceActions(String name) {
468         return _getActions(_modelResourceActions, name);
469     }
470 
471     private List<String> _getModelResourceCommunityDefaultActions(
472         String name) {
473 
474         return _getActions(_modelResourceCommunityDefaultActions, name);
475     }
476 
477     private List<String> _getModelResourceGuestDefaultActions(String name) {
478         return _getActions(_modelResourceGuestDefaultActions, name);
479     }
480 
481     private List<String> _getModelResourceGuestUnsupportedActions(String name) {
482         return _getActions(_modelResourceGuestUnsupportedActions, name);
483     }
484 
485     private List<String> _getPortletModelResources(String portletName) {
486         portletName = PortletConstants.getRootPortletId(portletName);
487 
488         Set<String> resources = _portletModelResources.get(portletName);
489 
490         if (resources == null) {
491             return new UniqueList<String>();
492         }
493         else {
494             return Collections.list(Collections.enumeration(resources));
495         }
496     }
497 
498     private List<String> _getPortletResourceActions(long companyId, String name)
499         throws SystemException {
500 
501         name = PortletConstants.getRootPortletId(name);
502 
503         List<String> actions = _getActions(_portletResourceActions, name);
504 
505         if (actions.size() == 0) {
506             synchronized (this) {
507                 Portlet portlet = PortletLocalServiceUtil.getPortletById(
508                     companyId, name);
509 
510                 Map<String, Set<String>> portletModes =
511                     portlet.getPortletModes();
512 
513                 Set<String> mimeTypeModes = portletModes.get("text/html");
514 
515                 if (mimeTypeModes != null) {
516                     for (String actionId : mimeTypeModes) {
517                         if (actionId.equalsIgnoreCase("edit")) {
518                             actions.add(ActionKeys.PREFERENCES);
519                         }
520                         else if (actionId.equalsIgnoreCase("edit_guest")) {
521                             actions.add(ActionKeys.GUEST_PREFERENCES);
522                         }
523                         else {
524                             actions.add(actionId.toUpperCase());
525                         }
526                     }
527                 }
528 
529                 _checkPortletActions(actions);
530 
531                 List<String> communityDefaultActions =
532                     _portletResourceCommunityDefaultActions.get(name);
533 
534                 if (communityDefaultActions == null) {
535                     communityDefaultActions = new UniqueList<String>();
536 
537                     _portletResourceCommunityDefaultActions.put(
538                         name, communityDefaultActions);
539 
540                     _checkPortletCommunityDefaultActions(
541                         communityDefaultActions);
542                 }
543 
544                 List<String> guestDefaultActions =
545                     _portletResourceGuestDefaultActions.get(name);
546 
547                 if (guestDefaultActions == null) {
548                     guestDefaultActions = new UniqueList<String>();
549 
550                     _portletResourceGuestDefaultActions.put(
551                         name, guestDefaultActions);
552 
553                     _checkPortletGuestDefaultActions(guestDefaultActions);
554                 }
555 
556                 List<String> layoutManagerActions =
557                     _portletResourceLayoutManagerActions.get(name);
558 
559                 if (layoutManagerActions == null) {
560                     layoutManagerActions = new UniqueList<String>();
561 
562                     _portletResourceLayoutManagerActions.put(
563                         name, layoutManagerActions);
564 
565                     _checkPortletLayoutManagerActions(layoutManagerActions);
566                 }
567             }
568         }
569 
570         return actions;
571     }
572 
573     private List<String> _getPortletResourceCommunityDefaultActions(
574         String name) {
575 
576         // This method should always be called only after
577         // _getPortletResourceActions has been called at least once to
578         // populate the default community actions. Check to make sure this is
579         // the case. However, if it is not, that means the methods
580         // _getPortletResourceGuestDefaultActions and
581         // _getPortletResourceGuestDefaultActions may not work either.
582 
583         name = PortletConstants.getRootPortletId(name);
584 
585         return _getActions(_portletResourceCommunityDefaultActions, name);
586     }
587 
588     private List<String> _getPortletResourceGuestDefaultActions(String name) {
589         name = PortletConstants.getRootPortletId(name);
590 
591         return _getActions(_portletResourceGuestDefaultActions, name);
592     }
593 
594     private List<String> _getPortletResourceGuestUnsupportedActions(
595         String name) {
596 
597         name = PortletConstants.getRootPortletId(name);
598 
599         return _getActions(_portletResourceGuestUnsupportedActions, name);
600     }
601 
602     private List<String> _getPortletResourceLayoutManagerActions(String name) {
603         name = PortletConstants.getRootPortletId(name);
604 
605         List<String> actions = _getActions(
606             _portletResourceLayoutManagerActions, name);
607 
608         // This check can never return an empty list. If the list is empty, it
609         // means that the portlet does not have an explicit resource-actions
610         // configuration file and should therefore be handled as if it has
611         // defaults of CONFIGURATION, PREFERENCES, and VIEW.
612 
613         if (actions.size() < 1) {
614             actions.add("CONFIGURATION");
615             actions.add("PREFERENCES");
616             actions.add("VIEW");
617         }
618 
619         return actions;
620     }
621 
622     private boolean _isOrganizationModelResource(String modelResource) {
623         if (_organizationModelResources.contains(modelResource)) {
624             return true;
625         }
626         else {
627             return false;
628         }
629     }
630 
631     private boolean _isPortalModelResource(String modelResource) {
632         if (_portalModelResources.contains(modelResource)) {
633             return true;
634         }
635         else {
636             return false;
637         }
638     }
639 
640     private void _read(
641             String servletContextName, ClassLoader classLoader, String source)
642         throws Exception {
643 
644         String xml = null;
645 
646         try {
647             xml = StringUtil.read(classLoader, source);
648         }
649         catch (Exception e) {
650             _log.warn("Cannot load " + source);
651         }
652 
653         if (xml == null) {
654             return;
655         }
656 
657         if (_log.isDebugEnabled()) {
658             _log.debug("Loading " + source);
659         }
660 
661         Document doc = SAXReaderUtil.read(xml);
662 
663         Element root = doc.getRootElement();
664 
665         Iterator<Element> itr1 = root.elements("resource").iterator();
666 
667         while (itr1.hasNext()) {
668             Element resource = itr1.next();
669 
670             String file = resource.attributeValue("file");
671 
672             _read(servletContextName, classLoader, file);
673         }
674 
675         itr1 = root.elements("portlet-resource").iterator();
676 
677         while (itr1.hasNext()) {
678             Element resource = itr1.next();
679 
680             String name = resource.elementText("portlet-name");
681 
682             if (servletContextName != null) {
683                 name =
684                     name + PortletConstants.WAR_SEPARATOR + servletContextName;
685             }
686 
687             name = PortalUtil.getJsSafePortletId(name);
688 
689             // Actions
690 
691             List<String> actions = _getActions(_portletResourceActions, name);
692 
693             Element supports = resource.element("supports");
694 
695             Iterator<Element> itr2 = supports.elements("action-key").iterator();
696 
697             while (itr2.hasNext()) {
698                 Element actionKey = itr2.next();
699 
700                 String actionKeyText = actionKey.getText();
701 
702                 if (Validator.isNotNull(actionKeyText)) {
703                     actions.add(actionKeyText);
704                 }
705             }
706 
707             if (!name.equals(PortletKeys.PORTAL)) {
708                 _checkPortletActions(actions);
709             }
710 
711             // Community default actions
712 
713             List<String> communityDefaultActions =
714                 _getActions(_portletResourceCommunityDefaultActions, name);
715 
716             Element communityDefaults = resource.element("community-defaults");
717 
718             itr2 = communityDefaults.elements("action-key").iterator();
719 
720             while (itr2.hasNext()) {
721                 Element actionKey = itr2.next();
722 
723                 String actionKeyText = actionKey.getText();
724 
725                 if (Validator.isNotNull(actionKeyText)) {
726                     communityDefaultActions.add(actionKeyText);
727                 }
728             }
729 
730             // Guest default actions
731 
732             List<String> guestDefaultActions =
733                 _getActions(_portletResourceGuestDefaultActions, name);
734 
735             Element guestDefaults = resource.element("guest-defaults");
736 
737             itr2 = guestDefaults.elements("action-key").iterator();
738 
739             while (itr2.hasNext()) {
740                 Element actionKey = itr2.next();
741 
742                 String actionKeyText = actionKey.getText();
743 
744                 if (Validator.isNotNull(actionKeyText)) {
745                     guestDefaultActions.add(actionKeyText);
746                 }
747             }
748 
749             // Guest unsupported actions
750 
751             List<String> guestUnsupportedActions =
752                 _getActions(_portletResourceGuestUnsupportedActions, name);
753 
754             Element guestUnsupported = resource.element("guest-unsupported");
755 
756             itr2 = guestUnsupported.elements("action-key").iterator();
757 
758             while (itr2.hasNext()) {
759                 Element actionKey = itr2.next();
760 
761                 String actionKeyText = actionKey.getText();
762 
763                 if (Validator.isNotNull(actionKeyText)) {
764                     guestUnsupportedActions.add(actionKeyText);
765                 }
766             }
767 
768             _checkGuestUnsupportedActions(
769                 guestUnsupportedActions, guestDefaultActions);
770 
771             // Layout manager actions
772 
773             List<String> layoutManagerActions = _getActions(
774                 _portletResourceLayoutManagerActions, name);
775 
776             Element layoutManager = resource.element("layout-manager");
777 
778             if (layoutManager != null) {
779                 itr2 = layoutManager.elements("action-key").iterator();
780 
781                 while (itr2.hasNext()) {
782                     Element actionKey = itr2.next();
783 
784                     String actionKeyText = actionKey.getText();
785 
786                     if (Validator.isNotNull(actionKeyText)) {
787                         layoutManagerActions.add(actionKeyText);
788                     }
789                 }
790             }
791             else {
792 
793                 // Set the layout manager actions to contain all the portlet
794                 // resource actions if the element is not specified
795 
796                 layoutManagerActions.addAll(actions);
797             }
798         }
799 
800         itr1 = root.elements("model-resource").iterator();
801 
802         while (itr1.hasNext()) {
803             Element resource = itr1.next();
804 
805             String name = resource.elementText("model-name");
806 
807             Element portletRef = resource.element("portlet-ref");
808 
809             Iterator<Element> itr2 = portletRef.elements(
810                 "portlet-name").iterator();
811 
812             while (itr2.hasNext()) {
813                 Element portletName = itr2.next();
814 
815                 String portletNameString = portletName.getText();
816 
817                 if (servletContextName != null) {
818                     portletNameString =
819                         portletNameString + PortletConstants.WAR_SEPARATOR +
820                             servletContextName;
821                 }
822 
823                 portletNameString = PortalUtil.getJsSafePortletId(
824                     portletNameString);
825 
826                 // Reference for a portlet to child models
827 
828                 Set<String> modelResources = _portletModelResources.get(
829                     portletNameString);
830 
831                 if (modelResources == null) {
832                     modelResources = new HashSet<String>();
833 
834                     _portletModelResources.put(
835                         portletNameString, modelResources);
836                 }
837 
838                 modelResources.add(name);
839 
840                 // Reference for a model to parent portlets
841 
842                 Set<String> portletResources = _modelPortletResources.get(name);
843 
844                 if (portletResources == null) {
845                     portletResources = new HashSet<String>();
846 
847                     _modelPortletResources.put(name, portletResources);
848                 }
849 
850                 portletResources.add(portletNameString);
851             }
852 
853             // Actions
854 
855             List<String> actions = _getActions(_modelResourceActions, name);
856 
857             Element supports = resource.element("supports");
858 
859             itr2 = supports.elements("action-key").iterator();
860 
861             while (itr2.hasNext()) {
862                 Element actionKey = itr2.next();
863 
864                 String actionKeyText = actionKey.getText();
865 
866                 if (Validator.isNotNull(actionKeyText)) {
867                     actions.add(actionKeyText);
868                 }
869             }
870 
871             // Community default actions
872 
873             List<String> communityDefaultActions =
874                 _getActions(_modelResourceCommunityDefaultActions, name);
875 
876             Element communityDefaults = resource.element("community-defaults");
877 
878             itr2 = communityDefaults.elements("action-key").iterator();
879 
880             while (itr2.hasNext()) {
881                 Element actionKey = itr2.next();
882 
883                 String actionKeyText = actionKey.getText();
884 
885                 if (Validator.isNotNull(actionKeyText)) {
886                     communityDefaultActions.add(actionKeyText);
887                 }
888             }
889 
890             // Guest default actions
891 
892             List<String> guestDefaultActions =
893                 _getActions(_modelResourceGuestDefaultActions, name);
894 
895             Element guestDefaults = resource.element("guest-defaults");
896 
897             itr2 = guestDefaults.elements("action-key").iterator();
898 
899             while (itr2.hasNext()) {
900                 Element actionKey = itr2.next();
901 
902                 String actionKeyText = actionKey.getText();
903 
904                 if (Validator.isNotNull(actionKeyText)) {
905                     guestDefaultActions.add(actionKeyText);
906                 }
907             }
908 
909             // Guest unsupported actions
910 
911             List<String> guestUnsupportedActions =
912                 _getActions(_modelResourceGuestUnsupportedActions, name);
913 
914             Element guestUnsupported = resource.element("guest-unsupported");
915 
916             itr2 = guestUnsupported.elements("action-key").iterator();
917 
918             while (itr2.hasNext()) {
919                 Element actionKey = itr2.next();
920 
921                 String actionKeyText = actionKey.getText();
922 
923                 if (Validator.isNotNull(actionKeyText)) {
924                     guestUnsupportedActions.add(actionKeyText);
925                 }
926             }
927 
928             _checkGuestUnsupportedActions(
929                 guestUnsupportedActions, guestDefaultActions);
930         }
931     }
932 
933     private static Log _log = LogFactoryUtil.getLog(ResourceActionsUtil.class);
934 
935     private static ResourceActionsUtil _instance = new ResourceActionsUtil();
936 
937     private Set<String> _organizationModelResources;
938     private Set<String> _portalModelResources;
939     private Map<String, Set<String>> _portletModelResources;
940     private Map<String, List<String>> _portletResourceActions;
941     private Map<String, List<String>> _portletResourceCommunityDefaultActions;
942     private Map<String, List<String>> _portletResourceGuestDefaultActions;
943     private Map<String, List<String>> _portletResourceGuestUnsupportedActions;
944     private Map<String, List<String>> _portletResourceLayoutManagerActions;
945     private Map<String, Set<String>> _modelPortletResources;
946     private Map<String, List<String>> _modelResourceActions;
947     private Map<String, List<String>> _modelResourceCommunityDefaultActions;
948     private Map<String, List<String>> _modelResourceGuestDefaultActions;
949     private Map<String, List<String>> _modelResourceGuestUnsupportedActions;
950 
951 }