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 = RoleConstants.TYPES_REGULAR_AND_SITE;
811    
812                    if (isPortalModelResource(modelResource)) {
813                            if (modelResource.equals(Organization.class.getName()) ||
814                                    modelResource.equals(User.class.getName())) {
815    
816                                    types = RoleConstants.TYPES_ORGANIZATION_AND_REGULAR;
817                            }
818                            else {
819                                    types = RoleConstants.TYPES_REGULAR;
820                            }
821                    }
822                    else {
823                            if (group != null) {
824                                    if (group.isLayout()) {
825                                            try {
826                                                    group = GroupServiceUtil.getGroup(
827                                                            group.getParentGroupId());
828                                            }
829                                            catch (Exception e) {
830                                            }
831                                    }
832    
833                                    if (group.isOrganization()) {
834                                            types =
835                                                    RoleConstants.TYPES_ORGANIZATION_AND_REGULAR_AND_SITE;
836                                    }
837                                    else if (group.isUser()) {
838                                            types = RoleConstants.TYPES_REGULAR;
839                                    }
840                            }
841                    }
842    
843                    return types;
844            }
845    
846            protected void read(String servletContextName, Document document)
847                    throws Exception {
848    
849                    Element rootElement = document.getRootElement();
850    
851                    if (PropsValues.RESOURCE_ACTIONS_READ_PORTLET_RESOURCES) {
852                            for (Element portletResourceElement :
853                                            rootElement.elements("portlet-resource")) {
854    
855                                    readPortletResource(servletContextName, portletResourceElement);
856                            }
857                    }
858    
859                    for (Element modelResourceElement :
860                                    rootElement.elements("model-resource")) {
861    
862                            readModelResource(servletContextName, modelResourceElement);
863                    }
864            }
865    
866            protected List<String> readActionKeys(Element parentElement) {
867                    List<String> actions = new ArrayList<String>();
868    
869                    for (Element actionKeyElement : parentElement.elements("action-key")) {
870                            String actionKey = actionKeyElement.getTextTrim();
871    
872                            if (Validator.isNull(actionKey)) {
873                                    continue;
874                            }
875    
876                            actions.add(actionKey);
877                    }
878    
879                    return actions;
880            }
881    
882            protected void readGroupDefaultActions(
883                    Element parentElement, Map<String, List<String>> actionsMap,
884                    String name) {
885    
886                    List<String> groupDefaultActions = new UniqueList<String>(
887                            getActions(actionsMap, name));
888    
889                    Element groupDefaultsElement = getPermissionsChildElement(
890                            parentElement, "site-member-defaults");
891    
892                    if (groupDefaultsElement == null) {
893                            groupDefaultsElement = getPermissionsChildElement(
894                                    parentElement, "community-defaults");
895    
896                            if (_log.isWarnEnabled() && (groupDefaultsElement != null)) {
897                                    _log.warn(
898                                            "The community-defaults element is deprecated. Use the " +
899                                                    "site-member-defaults element instead.");
900                            }
901                    }
902    
903                    groupDefaultActions.addAll(readActionKeys(groupDefaultsElement));
904    
905                    setActions(actionsMap, name, groupDefaultActions);
906            }
907    
908            protected List<String> readGuestDefaultActions(
909                    Element parentElement, Map<String, List<String>> actionsMap,
910                    String name) {
911    
912                    List<String> guestDefaultActions = new UniqueList<String>(
913                            getActions(actionsMap, name));
914    
915                    Element guestDefaultsElement = getPermissionsChildElement(
916                            parentElement, "guest-defaults");
917    
918                    guestDefaultActions.addAll(readActionKeys(guestDefaultsElement));
919    
920                    return guestDefaultActions;
921            }
922    
923            protected void readGuestUnsupportedActions(
924                    Element parentElement, Map<String, List<String>> actionsMap,
925                    String name, List<String> guestDefaultActions) {
926    
927                    List<String> guestUnsupportedActions = new UniqueList<String>(
928                            getActions(actionsMap, name));
929    
930                    Element guestUnsupportedElement = getPermissionsChildElement(
931                            parentElement, "guest-unsupported");
932    
933                    guestUnsupportedActions.addAll(readActionKeys(guestUnsupportedElement));
934    
935                    checkGuestUnsupportedActions(
936                            guestUnsupportedActions, guestDefaultActions);
937    
938                    setActions(actionsMap, name, guestUnsupportedActions);
939            }
940    
941            protected void readLayoutManagerActions(
942                    Element parentElement, Map<String, List<String>> actionsMap,
943                    String name, List<String> supportsActions) {
944    
945                    List<String> layoutManagerActions = new UniqueList<String>(
946                            getActions(actionsMap, name));
947    
948                    Element layoutManagerElement = getPermissionsChildElement(
949                            parentElement, "layout-manager");
950    
951                    if (layoutManagerElement != null) {
952                            layoutManagerActions.addAll(readActionKeys(layoutManagerElement));
953                    }
954                    else {
955                            layoutManagerActions.addAll(supportsActions);
956                    }
957    
958                    setActions(actionsMap, name, layoutManagerActions);
959            }
960    
961            protected void readModelResource(
962                    String servletContextName, Element modelResourceElement) {
963    
964                    String name = modelResourceElement.elementTextTrim("model-name");
965    
966                    Element portletRefElement = modelResourceElement.element("portlet-ref");
967    
968                    for (Element portletNameElement :
969                                    portletRefElement.elements("portlet-name")) {
970    
971                            String portletName = portletNameElement.getTextTrim();
972    
973                            if (servletContextName != null) {
974                                    portletName = portletName.concat(
975                                            PortletConstants.WAR_SEPARATOR).concat(servletContextName);
976                            }
977    
978                            portletName = JS.getSafeName(portletName);
979    
980                            // Reference for a portlet to child models
981    
982                            Set<String> modelResources = _portletModelResources.get(
983                                    portletName);
984    
985                            if (modelResources == null) {
986                                    modelResources = new HashSet<String>();
987    
988                                    _portletModelResources.put(portletName, modelResources);
989                            }
990    
991                            modelResources.add(name);
992    
993                            // Reference for a model to parent portlets
994    
995                            Set<String> portletResources = _modelPortletResources.get(name);
996    
997                            if (portletResources == null) {
998                                    portletResources = new HashSet<String>();
999    
1000                                    _modelPortletResources.put(name, portletResources);
1001                            }
1002    
1003                            portletResources.add(portletName);
1004                    }
1005    
1006                    double weight = GetterUtil.getDouble(
1007                            modelResourceElement.elementTextTrim("weight"), 100);
1008    
1009                    _modelResourceWeights.put(name, weight);
1010    
1011                    List<String> supportsActions = readSupportsActions(
1012                            modelResourceElement, _modelResourceActions, name);
1013    
1014                    checkModelActions(supportsActions);
1015    
1016                    setActions(_modelResourceActions, name, supportsActions);
1017    
1018                    readGroupDefaultActions(
1019                            modelResourceElement, _modelResourceGroupDefaultActions, name);
1020    
1021                    List<String> guestDefaultActions = readGuestDefaultActions(
1022                            modelResourceElement, _modelResourceGuestDefaultActions, name);
1023    
1024                    readGuestUnsupportedActions(
1025                            modelResourceElement, _modelResourceGuestUnsupportedActions, name,
1026                            guestDefaultActions);
1027    
1028                    setActions(
1029                            _modelResourceGuestDefaultActions, name, guestDefaultActions);
1030    
1031                    readOwnerDefaultActions(
1032                            modelResourceElement, _modelResourceOwnerDefaultActions, name);
1033            }
1034    
1035            protected void readOwnerDefaultActions(
1036                    Element parentElement, Map<String, List<String>> actionsMap,
1037                    String name) {
1038    
1039                    List<String> ownerDefaultActions = new UniqueList<String>(
1040                            getActions(actionsMap, name));
1041    
1042                    Element ownerDefaultsElement = getPermissionsChildElement(
1043                            parentElement, "owner-defaults");
1044    
1045                    if (ownerDefaultsElement == null) {
1046                            return;
1047                    }
1048    
1049                    ownerDefaultActions.addAll(readActionKeys(ownerDefaultsElement));
1050    
1051                    setActions(actionsMap, name, ownerDefaultActions);
1052            }
1053    
1054            protected void readPortletResource(
1055                    String servletContextName, Element portletResourceElement) {
1056    
1057                    String name = portletResourceElement.elementTextTrim("portlet-name");
1058    
1059                    if (servletContextName != null) {
1060                            name = name.concat(PortletConstants.WAR_SEPARATOR).concat(
1061                                    servletContextName);
1062                    }
1063    
1064                    name = JS.getSafeName(name);
1065    
1066                    List<String> supportsActions = readSupportsActions(
1067                            portletResourceElement, _portletResourceActions, name);
1068    
1069                    supportsActions.addAll(getPortletMimeTypeActions(name));
1070    
1071                    if (!name.equals(PortletKeys.PORTAL)) {
1072                            checkPortletActions(name, supportsActions);
1073                    }
1074    
1075                    supportsActions = setActions(
1076                            _portletResourceActions, name, supportsActions);
1077    
1078                    readGroupDefaultActions(
1079                            portletResourceElement, _portletResourceGroupDefaultActions, name);
1080    
1081                    List<String> guestDefaultActions = readGuestDefaultActions(
1082                            portletResourceElement, _portletResourceGuestDefaultActions, name);
1083    
1084                    readGuestUnsupportedActions(
1085                            portletResourceElement, _portletResourceGuestUnsupportedActions,
1086                            name, guestDefaultActions);
1087    
1088                    setActions(
1089                            _portletResourceGuestDefaultActions, name, guestDefaultActions);
1090    
1091                    readLayoutManagerActions(
1092                            portletResourceElement, _portletResourceLayoutManagerActions, name,
1093                            supportsActions);
1094            }
1095    
1096            protected List<String> readSupportsActions(
1097                    Element parentElement, Map<String, List<String>> actionsMap,
1098                    String name) {
1099    
1100                    List<String> supportsActions = new UniqueList<String>(
1101                            getActions(actionsMap, name));
1102    
1103                    Element supportsElement = getPermissionsChildElement(
1104                            parentElement, "supports");
1105    
1106                    supportsActions.addAll(readActionKeys(supportsElement));
1107    
1108                    return supportsActions;
1109            }
1110    
1111            protected List<String> setActions(
1112                    Map<String, List<String>> actionsMap, String name,
1113                    List<String> actions) {
1114    
1115                    actions = new UnmodifiableList<String>(actions);
1116    
1117                    actionsMap.put(name, actions);
1118    
1119                    return actions;
1120            }
1121    
1122            @BeanReference(type = PortletLocalService.class)
1123            protected PortletLocalService portletLocalService;
1124    
1125            @BeanReference(type = ResourceActionLocalService.class)
1126            protected ResourceActionLocalService resourceActionLocalService;
1127    
1128            @BeanReference(type = RoleLocalService.class)
1129            protected RoleLocalService roleLocalService;
1130    
1131            private static final String _ACTION_NAME_PREFIX = "action.";
1132    
1133            private static final String _MODEL_RESOURCE_NAME_PREFIX = "model.resource.";
1134    
1135            private static final String[] _ORGANIZATION_MODEL_RESOURCES = {
1136                    Organization.class.getName(), PasswordPolicy.class.getName(),
1137                    User.class.getName()
1138            };
1139    
1140            private static final String[] _PORTAL_MODEL_RESOURCES = {
1141                    ExpandoColumn.class.getName(), LayoutPrototype.class.getName(),
1142                    LayoutSetPrototype.class.getName(), MDRRuleGroup.class.getName(),
1143                    Organization.class.getName(), PasswordPolicy.class.getName(),
1144                    Role.class.getName(), User.class.getName(), UserGroup.class.getName()
1145            };
1146    
1147            private static Log _log = LogFactoryUtil.getLog(ResourceActionsImpl.class);
1148    
1149            private Map<String, Set<String>> _modelPortletResources;
1150            private Map<String, List<String>> _modelResourceActions;
1151            private Map<String, List<String>> _modelResourceGroupDefaultActions;
1152            private Map<String, List<String>> _modelResourceGuestDefaultActions;
1153            private Map<String, List<String>> _modelResourceGuestUnsupportedActions;
1154            private Map<String, List<String>> _modelResourceOwnerDefaultActions;
1155            private Map<String, Double> _modelResourceWeights;
1156            private Set<String> _organizationModelResources;
1157            private Set<String> _portalModelResources;
1158            private Map<String, Set<String>> _portletModelResources;
1159            private Map<String, List<String>> _portletResourceActions;
1160            private Map<String, List<String>> _portletResourceGroupDefaultActions;
1161            private Map<String, List<String>> _portletResourceGuestDefaultActions;
1162            private Map<String, List<String>> _portletResourceGuestUnsupportedActions;
1163            private Map<String, List<String>> _portletResourceLayoutManagerActions;
1164    
1165    }