001    /**
002     * Copyright (c) 2000-present 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.service.permission.OrganizationPermissionUtil;
031    import com.liferay.portal.theme.ThemeDisplay;
032    import com.liferay.portal.util.PortalUtil;
033    import com.liferay.portal.util.WebKeys;
034    
035    import javax.portlet.PortletRequest;
036    
037    import javax.servlet.http.HttpServletRequest;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     */
042    public class ActionUtil {
043    
044            public static void getRole(HttpServletRequest request) throws Exception {
045                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
046                            WebKeys.THEME_DISPLAY);
047    
048                    PermissionChecker permissionChecker =
049                            themeDisplay.getPermissionChecker();
050    
051                    long roleId = ParamUtil.getLong(request, "roleId");
052    
053                    Role role = null;
054    
055                    Group group = (Group)request.getAttribute(WebKeys.GROUP);
056    
057                    if ((group != null) && group.isOrganization()) {
058                            long organizationId = group.getOrganizationId();
059    
060                            while (organizationId !=
061                                                    OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
062    
063                                    Organization organization =
064                                            OrganizationLocalServiceUtil.getOrganization(
065                                                    organizationId);
066    
067                                    long organizationGroupId = organization.getGroupId();
068    
069                                    if (GroupPermissionUtil.contains(
070                                                    permissionChecker, organizationGroupId,
071                                                    ActionKeys.ASSIGN_USER_ROLES) ||
072                                            OrganizationPermissionUtil.contains(
073                                                    permissionChecker, organization,
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, ActionKeys.ASSIGN_USER_ROLES) ||
099                                    UserGroupRoleLocalServiceUtil.hasUserGroupRole(
100                                            themeDisplay.getUserId(), group.getGroupId(),
101                                            RoleConstants.SITE_ADMINISTRATOR, true) ||
102                                    UserGroupRoleLocalServiceUtil.hasUserGroupRole(
103                                            themeDisplay.getUserId(), group.getGroupId(),
104                                            RoleConstants.SITE_OWNER, true)) {
105    
106                                    if (roleId > 0) {
107                                            role = RoleLocalServiceUtil.getRole(roleId);
108                                    }
109                            }
110                            else {
111                                    if (roleId > 0) {
112                                            role = RoleServiceUtil.getRole(roleId);
113                                    }
114                            }
115                    }
116                    else {
117                            if (roleId > 0) {
118                                    role = RoleServiceUtil.getRole(roleId);
119                            }
120                    }
121    
122                    request.setAttribute(WebKeys.ROLE, role);
123            }
124    
125            public static void getRole(PortletRequest portletRequest) throws Exception {
126                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
127                            portletRequest);
128    
129                    getRole(request);
130            }
131    
132    }