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