001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.rolesadmin.action;
016    
017    import com.liferay.portal.kernel.util.ParamUtil;
018    import com.liferay.portal.model.Group;
019    import com.liferay.portal.model.Organization;
020    import com.liferay.portal.model.OrganizationConstants;
021    import com.liferay.portal.model.Role;
022    import com.liferay.portal.model.RoleConstants;
023    import com.liferay.portal.security.permission.ActionKeys;
024    import com.liferay.portal.security.permission.PermissionChecker;
025    import com.liferay.portal.service.OrganizationLocalServiceUtil;
026    import com.liferay.portal.service.RoleLocalServiceUtil;
027    import com.liferay.portal.service.RoleServiceUtil;
028    import com.liferay.portal.service.UserGroupRoleLocalServiceUtil;
029    import com.liferay.portal.service.permission.GroupPermissionUtil;
030    import com.liferay.portal.theme.ThemeDisplay;
031    import com.liferay.portal.util.PortalUtil;
032    import com.liferay.portal.util.WebKeys;
033    
034    import javax.portlet.PortletRequest;
035    
036    import javax.servlet.http.HttpServletRequest;
037    
038    /**
039     * @author Brian Wing Shun Chan
040     */
041    public class ActionUtil {
042    
043            public static void getRole(HttpServletRequest request)
044                    throws Exception {
045    
046                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
047                            WebKeys.THEME_DISPLAY);
048    
049                    PermissionChecker permissionChecker =
050                            themeDisplay.getPermissionChecker();
051    
052                    long roleId = ParamUtil.getLong(request, "roleId");
053    
054                    Role role = null;
055    
056                    Group group = (Group)request.getAttribute(WebKeys.GROUP);
057    
058                    if ((group != null) && group.isOrganization()) {
059                            long organizationId = group.getOrganizationId();
060    
061                            while (organizationId !=
062                                                    OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
063    
064                                    Organization organization =
065                                            OrganizationLocalServiceUtil.getOrganization(
066                                                    organizationId);
067    
068                                    Group organizationGroup = organization.getGroup();
069    
070                                    long organizationGroupId = organizationGroup.getGroupId();
071    
072                                    if (GroupPermissionUtil.contains(
073                                                    permissionChecker, organizationGroupId,
074                                                    ActionKeys.ASSIGN_USER_ROLES) ||
075                                            UserGroupRoleLocalServiceUtil.hasUserGroupRole(
076                                                    themeDisplay.getUserId(), organizationGroupId,
077                                                    RoleConstants.ORGANIZATION_ADMINISTRATOR, true) ||
078                                            UserGroupRoleLocalServiceUtil.hasUserGroupRole(
079                                                    themeDisplay.getUserId(), organizationGroupId,
080                                                    RoleConstants.ORGANIZATION_OWNER, true)) {
081    
082                                            if (roleId > 0) {
083                                                    role = RoleLocalServiceUtil.getRole(roleId);
084                                            }
085    
086                                            break;
087                                    }
088    
089                                    organizationId = organization.getParentOrganizationId();
090                            }
091    
092                            if (roleId > 0 && (role == null)) {
093                                    role = RoleServiceUtil.getRole(roleId);
094                            }
095                    }
096                    else if ((group != null) && group.isRegularSite()) {
097                            if (GroupPermissionUtil.contains(
098                                            permissionChecker, group.getGroupId(),
099                                            ActionKeys.ASSIGN_USER_ROLES) ||
100                                    UserGroupRoleLocalServiceUtil.hasUserGroupRole(
101                                            themeDisplay.getUserId(), group.getGroupId(),
102                                            RoleConstants.SITE_ADMINISTRATOR, true) ||
103                                    UserGroupRoleLocalServiceUtil.hasUserGroupRole(
104                                            themeDisplay.getUserId(), group.getGroupId(),
105                                            RoleConstants.SITE_OWNER, true)) {
106    
107                                    if (roleId > 0) {
108                                            role = RoleLocalServiceUtil.getRole(roleId);
109                                    }
110                            }
111                            else {
112                                    if (roleId > 0) {
113                                            role = RoleServiceUtil.getRole(roleId);
114                                    }
115                            }
116                    }
117                    else {
118                            if (roleId > 0) {
119                                    role = RoleServiceUtil.getRole(roleId);
120                            }
121                    }
122    
123                    request.setAttribute(WebKeys.ROLE, role);
124            }
125    
126            public static void getRole(PortletRequest portletRequest) throws Exception {
127                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
128                            portletRequest);
129    
130                    getRole(request);
131            }
132    
133    }