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