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.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, organizationId,
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    }