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