001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.security.permission;
016    
017    import com.liferay.portal.NoSuchResourceActionException;
018    import com.liferay.portal.kernel.bean.BeanReference;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.language.LanguageUtil;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.portal.kernel.util.ContentTypes;
024    import com.liferay.portal.kernel.util.GetterUtil;
025    import com.liferay.portal.kernel.util.ListUtil;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.kernel.util.StringUtil;
028    import com.liferay.portal.kernel.util.UnmodifiableList;
029    import com.liferay.portal.kernel.util.Validator;
030    import com.liferay.portal.kernel.xml.Document;
031    import com.liferay.portal.kernel.xml.DocumentType;
032    import com.liferay.portal.kernel.xml.Element;
033    import com.liferay.portal.kernel.xml.SAXReaderUtil;
034    import com.liferay.portal.model.Group;
035    import com.liferay.portal.model.Organization;
036    import com.liferay.portal.model.PasswordPolicy;
037    import com.liferay.portal.model.Permission;
038    import com.liferay.portal.model.Portlet;
039    import com.liferay.portal.model.PortletConstants;
040    import com.liferay.portal.model.ResourceAction;
041    import com.liferay.portal.model.Role;
042    import com.liferay.portal.model.RoleConstants;
043    import com.liferay.portal.model.User;
044    import com.liferay.portal.model.UserGroup;
045    import com.liferay.portal.service.GroupServiceUtil;
046    import com.liferay.portal.service.PortletLocalService;
047    import com.liferay.portal.service.ResourceActionLocalService;
048    import com.liferay.portal.service.RoleLocalService;
049    import com.liferay.portal.util.Portal;
050    import com.liferay.portal.util.PortletKeys;
051    import com.liferay.portal.util.PropsValues;
052    import com.liferay.portlet.PortletResourceBundles;
053    import com.liferay.portlet.expando.model.ExpandoColumn;
054    import com.liferay.util.UniqueList;
055    
056    import java.io.InputStream;
057    
058    import java.util.ArrayList;
059    import java.util.Collections;
060    import java.util.HashMap;
061    import java.util.HashSet;
062    import java.util.Iterator;
063    import java.util.List;
064    import java.util.Locale;
065    import java.util.Map;
066    import java.util.Set;
067    
068    import javax.servlet.jsp.PageContext;
069    
070    /**
071     * @author Brian Wing Shun Chan
072     * @author Daeyoung Song
073     * @author Raymond Augé
074     */
075    public class ResourceActionsImpl implements ResourceActions {
076    
077            public void afterPropertiesSet() {
078                    _organizationModelResources = new HashSet<String>();
079    
080                    for (String resource : getOrganizationModelResources()) {
081                            _organizationModelResources.add(resource);
082                    }
083    
084                    _portalModelResources = new HashSet<String>();
085    
086                    for (String resource : getPortalModelResources()) {
087                            _portalModelResources.add(resource);
088                    }
089    
090                    _portletModelResources = new HashMap<String, Set<String>>();
091                    _portletResourceActions = new HashMap<String, List<String>>();
092                    _portletResourceGroupDefaultActions =
093                            new HashMap<String, List<String>>();
094                    _portletResourceGuestDefaultActions =
095                            new HashMap<String, List<String>>();
096                    _portletResourceGuestUnsupportedActions =
097                            new HashMap<String, List<String>>();
098                    _portletResourceLayoutManagerActions =
099                            new HashMap<String, List<String>>();
100                    _modelPortletResources = new HashMap<String, Set<String>>();
101                    _modelResourceActions = new HashMap<String, List<String>>();
102                    _modelResourceGroupDefaultActions =
103                            new HashMap<String, List<String>>();
104                    _modelResourceGuestDefaultActions =
105                            new HashMap<String, List<String>>();
106                    _modelResourceGuestUnsupportedActions =
107                            new HashMap<String, List<String>>();
108                    _modelResourceOwnerDefaultActions =
109                            new HashMap<String, List<String>>();
110    
111                    try {
112                            ClassLoader classLoader = getClass().getClassLoader();
113    
114                            for (String config : PropsValues.RESOURCE_ACTIONS_CONFIGS) {
115                                    read(null, classLoader, config);
116                            }
117                    }
118                    catch (Exception e) {
119                            _log.error(e, e);
120                    }
121            }
122    
123            public void checkAction(String name, String actionId)
124                    throws NoSuchResourceActionException {
125    
126                    List<String> resourceActions = getResourceActions(name);
127    
128                    if (!resourceActions.contains(actionId)) {
129                            throw new NoSuchResourceActionException(
130                                    name.concat(StringPool.POUND).concat(actionId));
131                    }
132            }
133    
134            public String getAction(Locale locale, String action) {
135                    String key = getActionNamePrefix() + action;
136    
137                    String value = LanguageUtil.get(locale, key, null);
138    
139                    if ((value == null) || (value.equals(key))) {
140                            value = PortletResourceBundles.getString(locale, key);
141                    }
142    
143                    if (value == null) {
144                            value = key;
145                    }
146    
147                    return value;
148            }
149    
150            public String getAction(PageContext pageContext, String action) {
151                    String key = getActionNamePrefix() + action;
152    
153                    String value = LanguageUtil.get(pageContext, key, null);
154    
155                    if ((value == null) || (value.equals(key))) {
156                            value = PortletResourceBundles.getString(pageContext, key);
157                    }
158    
159                    if (value == null) {
160                            value = key;
161                    }
162    
163                    return value;
164            }
165    
166            public String getActionNamePrefix() {
167                    return _ACTION_NAME_PREFIX;
168            }
169    
170            public List<String> getActions(List<Permission> permissions) {
171                    List<String> actions = new UniqueList<String>();
172    
173                    for (Permission permission : permissions) {
174                            actions.add(permission.getActionId());
175                    }
176    
177                    return actions;
178            }
179    
180            public List<String> getActionsNames(
181                    PageContext pageContext, List<String> actions) {
182    
183                    List<String> actionNames = new UniqueList<String>();
184    
185                    for (String action : actions) {
186                            actionNames.add(getAction(pageContext, action));
187                    }
188    
189                    return actionNames;
190            }
191    
192            public List<String> getActionsNames(
193                    PageContext pageContext, String name, long actionIds) {
194    
195                    try {
196                            List<ResourceAction> resourceActions =
197                                    resourceActionLocalService.getResourceActions(name);
198    
199                            List<String> actions = new ArrayList<String>();
200    
201                            for (ResourceAction resourceAction : resourceActions) {
202                                    long bitwiseValue = resourceAction.getBitwiseValue();
203    
204                                    if ((actionIds & bitwiseValue) == bitwiseValue) {
205                                            actions.add(resourceAction.getActionId());
206                                    }
207                            }
208    
209                            return getActionsNames(pageContext, actions);
210                    }
211                    catch (Exception e) {
212                            _log.error(e, e);
213    
214                            return Collections.emptyList();
215                    }
216            }
217    
218            public List<String> getModelNames() {
219                    return ListUtil.fromMapKeys(_modelPortletResources);
220            }
221    
222            public List<String> getModelPortletResources(String name) {
223                    Set<String> resources = _modelPortletResources.get(name);
224    
225                    if (resources == null) {
226                            return new UniqueList<String>();
227                    }
228                    else {
229                            return Collections.list(Collections.enumeration(resources));
230                    }
231            }
232    
233            public String getModelResource(Locale locale, String name) {
234                    String key = getModelResourceNamePrefix() + name;
235    
236                    String value = LanguageUtil.get(locale, key, null);
237    
238                    if ((value == null) || (value.equals(key))) {
239                            value = PortletResourceBundles.getString(locale, key);
240                    }
241    
242                    if (value == null) {
243                            value = key;
244                    }
245    
246                    return value;
247            }
248    
249            public String getModelResource(PageContext pageContext, String name) {
250                    String key = getModelResourceNamePrefix() + name;
251    
252                    String value = LanguageUtil.get(pageContext, key, null);
253    
254                    if ((value == null) || (value.equals(key))) {
255                            value = PortletResourceBundles.getString(pageContext, key);
256                    }
257    
258                    if (value == null) {
259                            value = key;
260                    }
261    
262                    return value;
263            }
264    
265            public List<String> getModelResourceActions(String name) {
266                    return getActions(_modelResourceActions, name);
267            }
268    
269            public List<String> getModelResourceGroupDefaultActions(String name) {
270                    return getActions(_modelResourceGroupDefaultActions, name);
271            }
272    
273            public List<String> getModelResourceGuestDefaultActions(String name) {
274                    return getActions(_modelResourceGuestDefaultActions, name);
275            }
276    
277            public List<String> getModelResourceGuestUnsupportedActions(String name) {
278                    return getActions(_modelResourceGuestUnsupportedActions, name);
279            }
280    
281            public String getModelResourceNamePrefix() {
282                    return _MODEL_RESOURCE_NAME_PREFIX;
283            }
284    
285            public List<String> getModelResourceOwnerDefaultActions(String name) {
286                    return getActions(_modelResourceOwnerDefaultActions, name);
287            }
288    
289            public String[] getOrganizationModelResources() {
290                    return _ORGANIZATION_MODEL_RESOURCES;
291            }
292    
293            public String[] getPortalModelResources() {
294                    return _PORTAL_MODEL_RESOURCES;
295            }
296    
297            public String getPortletBaseResource(String portletName) {
298                    List<String> modelNames = getPortletModelResources(portletName);
299    
300                    for (String modelName : modelNames) {
301                            if (!modelName.contains(".model.")) {
302                                    return modelName;
303                            }
304                    }
305    
306                    return null;
307            }
308    
309            public List<String> getPortletModelResources(String portletName) {
310                    portletName = PortletConstants.getRootPortletId(portletName);
311    
312                    Set<String> resources = _portletModelResources.get(portletName);
313    
314                    if (resources == null) {
315                            return new UniqueList<String>();
316                    }
317                    else {
318                            return Collections.list(Collections.enumeration(resources));
319                    }
320            }
321    
322            public List<String> getPortletNames() {
323                    return ListUtil.fromMapKeys(_portletModelResources);
324            }
325    
326            public List<String> getPortletResourceActions(Portlet portlet) {
327                    List<String> actions = ListUtil.copy(
328                            getPortletResourceActions(portlet.getPortletId()));
329    
330                    synchronized (this) {
331                            checkPortletActions(portlet, actions);
332    
333                            setActions(
334                                    _portletResourceActions, portlet.getPortletId(), actions);
335                    }
336    
337                    return actions;
338            }
339    
340            public List<String> getPortletResourceActions(String name) {
341                    name = PortletConstants.getRootPortletId(name);
342    
343                    List<String> actions = getActions(_portletResourceActions, name);
344    
345                    if (!actions.isEmpty()) {
346                            return actions;
347                    }
348    
349                    synchronized (this) {
350                            actions = getPortletMimeTypeActions(name);
351    
352                            if (!name.equals(PortletKeys.PORTAL)) {
353                                    checkPortletActions(name, actions);
354                            }
355    
356                            List<String> groupDefaultActions =
357                                    _portletResourceGroupDefaultActions.get(name);
358    
359                            if (groupDefaultActions == null) {
360                                    groupDefaultActions = new UniqueList<String>();
361    
362                                    checkPortletGroupDefaultActions(groupDefaultActions);
363    
364                                    _portletResourceGroupDefaultActions.put(
365                                            name, new UnmodifiableList<String>(groupDefaultActions));
366                            }
367    
368                            List<String> guestDefaultActions =
369                                    _portletResourceGuestDefaultActions.get(name);
370    
371                            if (guestDefaultActions == null) {
372                                    guestDefaultActions = new UniqueList<String>();
373    
374                                    checkPortletGuestDefaultActions(guestDefaultActions);
375    
376                                    _portletResourceGuestDefaultActions.put(
377                                            name, new UnmodifiableList<String>(guestDefaultActions));
378                            }
379    
380                            List<String> layoutManagerActions =
381                                    _portletResourceLayoutManagerActions.get(name);
382    
383                            if (layoutManagerActions == null) {
384                                    layoutManagerActions = new UniqueList<String>();
385    
386                                    checkPortletLayoutManagerActions(layoutManagerActions);
387    
388                                    _portletResourceLayoutManagerActions.put(
389                                            name, new UnmodifiableList<String>(layoutManagerActions));
390                            }
391    
392                            actions = setActions(_portletResourceActions, name, actions);
393                    }
394    
395                    return actions;
396            }
397    
398            public List<String> getPortletResourceGroupDefaultActions(String name) {
399    
400                    // This method should always be called only after
401                    // _getPortletResourceActions has been called at least once to
402                    // populate the default group actions. Check to make sure this is the
403                    // case. However, if it is not, that means the methods
404                    // getPortletResourceGuestDefaultActions and
405                    // getPortletResourceGuestDefaultActions may not work either.
406    
407                    name = PortletConstants.getRootPortletId(name);
408    
409                    return getActions(_portletResourceGroupDefaultActions, name);
410            }
411    
412            public List<String> getPortletResourceGuestDefaultActions(String name) {
413                    name = PortletConstants.getRootPortletId(name);
414    
415                    return getActions(_portletResourceGuestDefaultActions, name);
416            }
417    
418            public List<String> getPortletResourceGuestUnsupportedActions(String name) {
419                    name = PortletConstants.getRootPortletId(name);
420    
421                    return getActions(_portletResourceGuestUnsupportedActions, name);
422            }
423    
424            public List<String> getPortletResourceLayoutManagerActions(String name) {
425                    name = PortletConstants.getRootPortletId(name);
426    
427                    List<String> actions = getActions(
428                            _portletResourceLayoutManagerActions, name);
429    
430                    // This check can never return an empty list. If the list is empty, it
431                    // means that the portlet does not have an explicit resource-actions
432                    // configuration file and should therefore be handled as if it has
433                    // defaults of CONFIGURATION, PREFERENCES, and VIEW.
434    
435                    if (actions.isEmpty()) {
436                            actions = new UniqueList<String>();
437    
438                            actions.add(ActionKeys.CONFIGURATION);
439                            actions.add(ActionKeys.PREFERENCES);
440                            actions.add(ActionKeys.VIEW);
441    
442                            setActions(_portletResourceLayoutManagerActions, name, actions);
443                    }
444    
445                    return actions;
446            }
447    
448            public List<String> getResourceActions(String name) {
449                    if (name.contains(StringPool.PERIOD)) {
450                            return getModelResourceActions(name);
451                    }
452                    else {
453                            return getPortletResourceActions(name);
454                    }
455            }
456    
457            public List<String> getResourceActions(
458                    String portletResource, String modelResource) {
459    
460                    List<String> actions = null;
461    
462                    if (Validator.isNull(modelResource)) {
463                            actions = getPortletResourceActions(portletResource);
464                    }
465                    else {
466                            actions = getModelResourceActions(modelResource);
467                    }
468    
469                    return actions;
470            }
471    
472            public List<String> getResourceGroupDefaultActions(String name) {
473                    if (name.contains(StringPool.PERIOD)) {
474                            return getModelResourceGroupDefaultActions(name);
475                    }
476                    else {
477                            return getPortletResourceGroupDefaultActions(name);
478                    }
479            }
480    
481            public List<String> getResourceGuestUnsupportedActions(
482                    String portletResource, String modelResource) {
483    
484                    List<String> actions = null;
485    
486                    if (Validator.isNull(modelResource)) {
487                            actions =
488                                    getPortletResourceGuestUnsupportedActions(portletResource);
489                    }
490                    else {
491                            actions = getModelResourceGuestUnsupportedActions(modelResource);
492                    }
493    
494                    return actions;
495            }
496    
497            /**
498             * @deprecated {@link #getRoles(long, Group, String, int[])}
499             */
500            public List<Role> getRoles(
501                            long companyId, Group group, String modelResource)
502                            throws SystemException {
503    
504                    return getRoles(companyId, group, modelResource, null);
505            }
506    
507            public List<Role> getRoles(
508                            long companyId, Group group, String modelResource, int[] roleTypes)
509                    throws SystemException {
510    
511                    List<Role> allRoles = roleLocalService.getRoles(companyId);
512    
513                    if (roleTypes == null) {
514                            roleTypes = getRoleTypes(companyId, group, modelResource);
515                    }
516    
517                    List<Role> roles = new ArrayList<Role>();
518    
519                    for (int roleType : roleTypes) {
520                            for (Role role : allRoles) {
521                                    if (role.getType() == roleType) {
522                                            roles.add(role);
523                                    }
524                            }
525                    }
526    
527                    return roles;
528            }
529    
530            public boolean hasModelResourceActions(String name) {
531                    List<String> actions = _modelResourceActions.get(name);
532    
533                    if ((actions != null) && !actions.isEmpty()) {
534                            return true;
535                    }
536                    else {
537                            return false;
538                    }
539            }
540    
541            public boolean isOrganizationModelResource(String modelResource) {
542                    if (_organizationModelResources.contains(modelResource)) {
543                            return true;
544                    }
545                    else {
546                            return false;
547                    }
548            }
549    
550            public boolean isPortalModelResource(String modelResource) {
551                    if (_portalModelResources.contains(modelResource)) {
552                            return true;
553                    }
554                    else {
555                            return false;
556                    }
557            }
558    
559            public void read(
560                            String servletContextName, ClassLoader classLoader, String source)
561                    throws Exception {
562    
563                    InputStream inputStream = classLoader.getResourceAsStream(source);
564    
565                    if (inputStream == null) {
566                            if (_log.isWarnEnabled() && !source.endsWith("-ext.xml")) {
567                                    _log.warn("Cannot load " + source);
568                            }
569    
570                            return;
571                    }
572    
573                    if (_log.isDebugEnabled()) {
574                            _log.debug("Loading " + source);
575                    }
576    
577                    Document document = SAXReaderUtil.read(inputStream, true);
578    
579                    DocumentType documentType = document.getDocumentType();
580    
581                    String publicId = GetterUtil.getString(documentType.getPublicId());
582    
583                    if (publicId.equals(
584                                    "-//Liferay//DTD Resource Action Mapping 6.0.0//EN")) {
585    
586                            if (_log.isWarnEnabled()) {
587                                    _log.warn(
588                                            "Please update " + source + " to use the 6.1.0 format");
589                            }
590                    }
591    
592                    Element rootElement = document.getRootElement();
593    
594                    for (Element resourceElement : rootElement.elements("resource")) {
595                            String file = resourceElement.attributeValue("file").trim();
596    
597                            read(servletContextName, classLoader, file);
598    
599                            String extFile = StringUtil.replace(file, ".xml", "-ext.xml");
600    
601                            read(servletContextName, classLoader, extFile);
602                    }
603    
604                    read(servletContextName, document);
605            }
606    
607            public void read(String servletContextName, InputStream inputStream)
608                    throws Exception {
609    
610                    Document document = SAXReaderUtil.read(inputStream, true);
611    
612                    read(servletContextName, document);
613            }
614    
615            public void setPortal(Portal portal) {
616                    this.portal = portal;
617            }
618    
619            protected void checkGuestUnsupportedActions(
620                    List<String> guestUnsupportedActions,
621                    List<String> guestDefaultActions) {
622    
623                    // Guest default actions cannot reference guest unsupported actions
624    
625                    Iterator<String> itr = guestDefaultActions.iterator();
626    
627                    while (itr.hasNext()) {
628                            String actionId = itr.next();
629    
630                            if (guestUnsupportedActions.contains(actionId)) {
631                                    itr.remove();
632                            }
633                    }
634            }
635    
636            protected void checkModelActions(List<String> actions) {
637                    if (!actions.contains(ActionKeys.PERMISSIONS)) {
638                            actions.add(ActionKeys.PERMISSIONS);
639                    }
640            }
641    
642            protected void checkPortletActions(Portlet portlet, List<String> actions) {
643                    if (!actions.contains(ActionKeys.ACCESS_IN_CONTROL_PANEL) &&
644                            !actions.contains(ActionKeys.ADD_TO_PAGE)) {
645    
646                            actions.add(ActionKeys.ADD_TO_PAGE);
647                    }
648    
649                    if ((portlet != null) &&
650                            (portlet.getControlPanelEntryCategory() != null) &&
651                            !actions.contains(ActionKeys.ACCESS_IN_CONTROL_PANEL)) {
652    
653                            actions.add(ActionKeys.ACCESS_IN_CONTROL_PANEL);
654                    }
655    
656                    if (!actions.contains(ActionKeys.CONFIGURATION)) {
657                            actions.add(ActionKeys.CONFIGURATION);
658                    }
659    
660                    if (!actions.contains(ActionKeys.PERMISSIONS)) {
661                            actions.add(ActionKeys.PERMISSIONS);
662                    }
663    
664                    if (!actions.contains(ActionKeys.VIEW)) {
665                            actions.add(ActionKeys.VIEW);
666                    }
667            }
668    
669            protected void checkPortletActions(String name, List<String> actions) {
670                    Portlet portlet = portletLocalService.getPortletById(name);
671    
672                    checkPortletActions(portlet, actions);
673            }
674    
675            protected void checkPortletGroupDefaultActions(List<String> actions) {
676                    if (actions.isEmpty()) {
677                            actions.add(ActionKeys.VIEW);
678                    }
679            }
680    
681            protected void checkPortletGuestDefaultActions(List<String> actions) {
682                    if (actions.isEmpty()) {
683                            actions.add(ActionKeys.VIEW);
684                    }
685            }
686    
687            protected void checkPortletLayoutManagerActions(List<String> actions) {
688                    if (!actions.contains(ActionKeys.CONFIGURATION)) {
689                            actions.add(ActionKeys.CONFIGURATION);
690                    }
691    
692                    if (!actions.contains(ActionKeys.PERMISSIONS)) {
693                            actions.add(ActionKeys.PERMISSIONS);
694                    }
695    
696                    if (!actions.contains(ActionKeys.PREFERENCES)) {
697                            actions.add(ActionKeys.PREFERENCES);
698                    }
699    
700                    if (!actions.contains(ActionKeys.VIEW)) {
701                            actions.add(ActionKeys.VIEW);
702                    }
703            }
704    
705            protected List<String> getActions(
706                    Map<String, List<String>> actionsMap, String name) {
707    
708                    List<String> actions = actionsMap.get(name);
709    
710                    if (actions == null) {
711                            actions = new UniqueList<String>();
712    
713                            actionsMap.put(name, actions);
714                    }
715    
716                    return actions;
717            }
718    
719            protected Element getPermissionsChildElement(
720                    Element parentElement, String childElementName) {
721    
722                    Element permissionsElement = parentElement.element("permissions");
723    
724                    if (permissionsElement != null) {
725                            return permissionsElement.element(childElementName);
726                    }
727                    else {
728                            return parentElement.element(childElementName);
729                    }
730            }
731    
732            protected List<String> getPortletMimeTypeActions(String name) {
733                    List<String> actions = new UniqueList<String>();
734    
735                    Portlet portlet = portletLocalService.getPortletById(name);
736    
737                    if (portlet != null) {
738                            Map<String, Set<String>> portletModes = portlet.getPortletModes();
739    
740                            Set<String> mimeTypePortletModes = portletModes.get(
741                                    ContentTypes.TEXT_HTML);
742    
743                            if (mimeTypePortletModes != null) {
744                                    for (String actionId : mimeTypePortletModes) {
745                                            if (actionId.equalsIgnoreCase("edit")) {
746                                                    actions.add(ActionKeys.PREFERENCES);
747                                            }
748                                            else if (actionId.equalsIgnoreCase("edit_guest")) {
749                                                    actions.add(ActionKeys.GUEST_PREFERENCES);
750                                            }
751                                            else {
752                                                    actions.add(actionId.toUpperCase());
753                                            }
754                                    }
755                            }
756                    }
757                    else {
758                            if (_log.isDebugEnabled()) {
759                                    _log.debug(
760                                            "Unable to obtain resource actions for unknown portlet " +
761                                                    name);
762                            }
763                    }
764    
765                    return actions;
766            }
767    
768            protected int[] getRoleTypes(
769                    long companyId, Group group, String modelResource) {
770    
771                    int[] types = {
772                            RoleConstants.TYPE_REGULAR, RoleConstants.TYPE_SITE
773                    };
774    
775                    if (isPortalModelResource(modelResource)) {
776                            if (modelResource.equals(Organization.class.getName()) ||
777                                    modelResource.equals(User.class.getName())) {
778    
779                                    types = new int[] {
780                                            RoleConstants.TYPE_REGULAR, RoleConstants.TYPE_ORGANIZATION
781                                    };
782                            }
783                            else {
784                                    types = new int[] {RoleConstants.TYPE_REGULAR};
785                            }
786                    }
787                    else {
788                            if (group != null) {
789                                    if (group.isLayout()) {
790                                            try {
791                                                    group = GroupServiceUtil.getGroup(
792                                                            group.getParentGroupId());
793                                            }
794                                            catch (Exception e) {
795                                            }
796                                    }
797    
798                                    if (group.isOrganization()) {
799                                            types = new int[] {
800                                                    RoleConstants.TYPE_REGULAR,
801                                                    RoleConstants.TYPE_ORGANIZATION, RoleConstants.TYPE_SITE
802                                            };
803                                    }
804                                    else if (group.isUser()) {
805                                            types = new int[] {RoleConstants.TYPE_REGULAR};
806                                    }
807                            }
808                    }
809    
810                    return types;
811            }
812    
813            protected void read(String servletContextName, Document document)
814                    throws Exception {
815    
816                    Element rootElement = document.getRootElement();
817    
818                    if (PropsValues.RESOURCE_ACTIONS_READ_PORTLET_RESOURCES) {
819                            for (Element portletResourceElement :
820                                            rootElement.elements("portlet-resource")) {
821    
822                                    readPortletResource(
823                                            servletContextName, portletResourceElement);
824                            }
825                    }
826    
827                    for (Element modelResourceElement :
828                                    rootElement.elements("model-resource")) {
829    
830                            readModelResource(servletContextName, modelResourceElement);
831                    }
832            }
833    
834            protected List<String> readActionKeys(Element parentElement) {
835                    List<String> actions = new ArrayList<String>();
836    
837                    for (Element actionKeyElement : parentElement.elements("action-key")) {
838                            String actionKey = actionKeyElement.getTextTrim();
839    
840                            if (Validator.isNull(actionKey)) {
841                                    continue;
842                            }
843    
844                            actions.add(actionKey);
845                    }
846    
847                    return actions;
848            }
849    
850            protected void readGroupDefaultActions(
851                    Element parentElement, Map<String, List<String>> actionsMap,
852                    String name) {
853    
854                    List<String> groupDefaultActions = new UniqueList<String>(
855                            getActions(actionsMap, name));
856    
857                    Element groupDefaultsElement = getPermissionsChildElement(
858                            parentElement, "site-member-defaults");
859    
860                    if (groupDefaultsElement == null) {
861                            groupDefaultsElement = getPermissionsChildElement(
862                                    parentElement, "community-defaults");
863    
864                            if (_log.isWarnEnabled() && (groupDefaultsElement != null)) {
865                                    _log.warn(
866                                            "The community-defaults element is deprecated. Use the " +
867                                                    "site-defaults element instead.");
868                            }
869                    }
870    
871                    groupDefaultActions.addAll(readActionKeys(groupDefaultsElement));
872    
873                    setActions(actionsMap, name, groupDefaultActions);
874            }
875    
876            protected List<String> readGuestDefaultActions(
877                    Element parentElement, Map<String, List<String>> actionsMap,
878                    String name) {
879    
880                    List<String> guestDefaultActions = new UniqueList<String>(
881                            getActions(actionsMap, name));
882    
883                    Element guestDefaultsElement = getPermissionsChildElement(
884                            parentElement, "guest-defaults");
885    
886                    guestDefaultActions.addAll(readActionKeys(guestDefaultsElement));
887    
888                    return guestDefaultActions;
889            }
890    
891            protected void readGuestUnsupportedActions(
892                    Element parentElement, Map<String, List<String>> actionsMap,
893                    String name, List<String> guestDefaultActions) {
894    
895                    List<String> guestUnsupportedActions = new UniqueList<String>(
896                            getActions(actionsMap, name));
897    
898                    Element guestUnsupportedElement = getPermissionsChildElement(
899                            parentElement, "guest-unsupported");
900    
901                    guestUnsupportedActions.addAll(readActionKeys(guestUnsupportedElement));
902    
903                    checkGuestUnsupportedActions(
904                            guestUnsupportedActions, guestDefaultActions);
905    
906                    setActions(actionsMap, name, guestUnsupportedActions);
907            }
908    
909            protected void readLayoutManagerActions(
910                    Element parentElement, Map<String, List<String>> actionsMap,
911                    String name, List<String> supportsActions) {
912    
913                    List<String> layoutManagerActions = new UniqueList<String>(
914                            getActions(actionsMap, name));
915    
916                    Element layoutManagerElement = getPermissionsChildElement(
917                            parentElement, "layout-manager");
918    
919                    if (layoutManagerElement != null) {
920                            layoutManagerActions.addAll(readActionKeys(layoutManagerElement));
921                    }
922                    else {
923                            layoutManagerActions.addAll(supportsActions);
924                    }
925    
926                    setActions(actionsMap, name, layoutManagerActions);
927            }
928    
929            protected void readModelResource(
930                    String servletContextName, Element modelResourceElement) {
931    
932                    String name = modelResourceElement.elementTextTrim("model-name");
933    
934                    Element portletRefElement = modelResourceElement.element("portlet-ref");
935    
936                    for (Element portletNameElement :
937                                    portletRefElement.elements("portlet-name")) {
938    
939                            String portletName = portletNameElement.getTextTrim();
940    
941                            if (servletContextName != null) {
942                                    portletName =
943                                            portletName.concat(PortletConstants.WAR_SEPARATOR).concat(
944                                                    servletContextName);
945                            }
946    
947                            portletName = portal.getJsSafePortletId(portletName);
948    
949                            // Reference for a portlet to child models
950    
951                            Set<String> modelResources = _portletModelResources.get(
952                                    portletName);
953    
954                            if (modelResources == null) {
955                                    modelResources = new HashSet<String>();
956    
957                                    _portletModelResources.put(portletName, modelResources);
958                            }
959    
960                            modelResources.add(name);
961    
962                            // Reference for a model to parent portlets
963    
964                            Set<String> portletResources = _modelPortletResources.get(name);
965    
966                            if (portletResources == null) {
967                                    portletResources = new HashSet<String>();
968    
969                                    _modelPortletResources.put(name, portletResources);
970                            }
971    
972                            portletResources.add(portletName);
973                    }
974    
975                    List<String> supportsActions = readSupportsActions(
976                            modelResourceElement, _modelResourceActions, name);
977    
978                    checkModelActions(supportsActions);
979    
980                    setActions(_modelResourceActions, name, supportsActions);
981    
982                    readGroupDefaultActions(
983                            modelResourceElement, _modelResourceGroupDefaultActions, name);
984    
985                    List<String> guestDefaultActions = readGuestDefaultActions(
986                            modelResourceElement, _modelResourceGuestDefaultActions, name);
987    
988                    readGuestUnsupportedActions(
989                            modelResourceElement, _modelResourceGuestUnsupportedActions, name,
990                            guestDefaultActions);
991    
992                    setActions(
993                            _modelResourceGuestDefaultActions, name, guestDefaultActions);
994    
995                    readOwnerDefaultActions(
996                            modelResourceElement, _modelResourceOwnerDefaultActions, name);
997            }
998    
999            protected void readOwnerDefaultActions(
1000                    Element parentElement, Map<String, List<String>> actionsMap,
1001                    String name) {
1002    
1003                    List<String> ownerDefaultActions = new UniqueList<String>(
1004                            getActions(actionsMap, name));
1005    
1006                    Element ownerDefaultsElement = getPermissionsChildElement(
1007                            parentElement, "owner-defaults");
1008    
1009                    if (ownerDefaultsElement == null) {
1010                            return;
1011                    }
1012    
1013                    ownerDefaultActions.addAll(readActionKeys(ownerDefaultsElement));
1014    
1015                    setActions(actionsMap, name, ownerDefaultActions);
1016            }
1017    
1018            protected void readPortletResource(
1019                    String servletContextName, Element portletResourceElement) {
1020    
1021                    String name = portletResourceElement.elementTextTrim("portlet-name");
1022    
1023                    if (servletContextName != null) {
1024                            name = name.concat(PortletConstants.WAR_SEPARATOR).concat(
1025                                    servletContextName);
1026                    }
1027    
1028                    name = portal.getJsSafePortletId(name);
1029    
1030                    List<String> supportsActions = readSupportsActions(
1031                            portletResourceElement, _portletResourceActions, name);
1032    
1033                    supportsActions.addAll(getPortletMimeTypeActions(name));
1034    
1035                    if (!name.equals(PortletKeys.PORTAL)) {
1036                            checkPortletActions(name, supportsActions);
1037                    }
1038    
1039                    supportsActions = setActions(
1040                            _portletResourceActions, name, supportsActions);
1041    
1042                    readGroupDefaultActions(
1043                            portletResourceElement, _portletResourceGroupDefaultActions, name);
1044    
1045                    List<String> guestDefaultActions = readGuestDefaultActions(
1046                            portletResourceElement, _portletResourceGuestDefaultActions, name);
1047    
1048                    readGuestUnsupportedActions(
1049                            portletResourceElement, _portletResourceGuestUnsupportedActions,
1050                            name, guestDefaultActions);
1051    
1052                    setActions(
1053                            _portletResourceGuestDefaultActions, name, guestDefaultActions);
1054    
1055                    readLayoutManagerActions(
1056                            portletResourceElement, _portletResourceLayoutManagerActions, name,
1057                            supportsActions);
1058            }
1059    
1060            protected List<String> readSupportsActions(
1061                    Element parentElement, Map<String, List<String>> actionsMap,
1062                    String name) {
1063    
1064                    List<String> supportsActions = new UniqueList<String>(
1065                            getActions(actionsMap, name));
1066    
1067                    Element supportsElement = getPermissionsChildElement(
1068                            parentElement, "supports");
1069    
1070                    supportsActions.addAll(readActionKeys(supportsElement));
1071    
1072                    return supportsActions;
1073            }
1074    
1075            protected List<String> setActions(
1076                    Map<String, List<String>> actionsMap, String name,
1077                    List<String> actions) {
1078    
1079                    actions = new UnmodifiableList<String>(actions);
1080    
1081                    actionsMap.put(name, actions);
1082    
1083                    return actions;
1084            }
1085    
1086            protected Portal portal;
1087            @BeanReference(type = PortletLocalService.class)
1088            protected PortletLocalService portletLocalService;
1089            @BeanReference(type = ResourceActionLocalService.class)
1090            protected ResourceActionLocalService resourceActionLocalService;
1091            @BeanReference(type = RoleLocalService.class)
1092            protected RoleLocalService roleLocalService;
1093    
1094            private static final String _ACTION_NAME_PREFIX = "action.";
1095    
1096            private static final String _MODEL_RESOURCE_NAME_PREFIX = "model.resource.";
1097    
1098            private static final String[] _ORGANIZATION_MODEL_RESOURCES = {
1099                    Organization.class.getName(), PasswordPolicy.class.getName(),
1100                    User.class.getName()
1101            };
1102    
1103            private static final String[] _PORTAL_MODEL_RESOURCES = {
1104                    ExpandoColumn.class.getName(), Organization.class.getName(),
1105                    PasswordPolicy.class.getName(), Role.class.getName(),
1106                    User.class.getName(), UserGroup.class.getName()
1107            };
1108    
1109            private static Log _log = LogFactoryUtil.getLog(ResourceActionsImpl.class);
1110    
1111            private Map<String, Set<String>> _modelPortletResources;
1112            private Map<String, List<String>> _modelResourceActions;
1113            private Map<String, List<String>> _modelResourceGroupDefaultActions;
1114            private Map<String, List<String>> _modelResourceGuestDefaultActions;
1115            private Map<String, List<String>> _modelResourceGuestUnsupportedActions;
1116            private Map<String, List<String>> _modelResourceOwnerDefaultActions;
1117            private Set<String> _organizationModelResources;
1118            private Set<String> _portalModelResources;
1119            private Map<String, Set<String>> _portletModelResources;
1120            private Map<String, List<String>> _portletResourceActions;
1121            private Map<String, List<String>> _portletResourceGroupDefaultActions;
1122            private Map<String, List<String>> _portletResourceGuestDefaultActions;
1123            private Map<String, List<String>> _portletResourceGuestUnsupportedActions;
1124            private Map<String, List<String>> _portletResourceLayoutManagerActions;
1125    
1126    }