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