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                                    Group organizationGroup = organization.getGroup();
068    
069                                    long organizationGroupId = organizationGroup.getGroupId();
070    
071                                    if (GroupPermissionUtil.contains(
072                                                    permissionChecker, organizationGroupId,
073                                                    ActionKeys.ASSIGN_USER_ROLES) ||
074                                            OrganizationPermissionUtil.contains(
075                                                    permissionChecker, organizationId,
076                                                    ActionKeys.ASSIGN_USER_ROLES) ||
077                                            UserGroupRoleLocalServiceUtil.hasUserGroupRole(
078                                                    themeDisplay.getUserId(), organizationGroupId,
079                                                    RoleConstants.ORGANIZATION_ADMINISTRATOR, true) ||
080                                            UserGroupRoleLocalServiceUtil.hasUserGroupRole(
081                                                    themeDisplay.getUserId(), organizationGroupId,
082                                                    RoleConstants.ORGANIZATION_OWNER, true)) {
083    
084                                            if (roleId > 0) {
085                                                    role = RoleLocalServiceUtil.getRole(roleId);
086                                            }
087    
088                                            break;
089                                    }
090    
091                                    organizationId = organization.getParentOrganizationId();
092                            }
093    
094                            if ((roleId > 0) && (role == null)) {
095                                    role = RoleServiceUtil.getRole(roleId);
096                            }
097                    }
098                    else if ((group != null) && group.isRegularSite()) {
099                            if (GroupPermissionUtil.contains(
100                                            permissionChecker, group.getGroupId(),
101                                            ActionKeys.ASSIGN_USER_ROLES) ||
102                                    UserGroupRoleLocalServiceUtil.hasUserGroupRole(
103                                            themeDisplay.getUserId(), group.getGroupId(),
104                                            RoleConstants.SITE_ADMINISTRATOR, true) ||
105                                    UserGroupRoleLocalServiceUtil.hasUserGroupRole(
106                                            themeDisplay.getUserId(), group.getGroupId(),
107                                            RoleConstants.SITE_OWNER, true)) {
108    
109                                    if (roleId > 0) {
110                                            role = RoleLocalServiceUtil.getRole(roleId);
111                                    }
112                            }
113                            else {
114                                    if (roleId > 0) {
115                                            role = RoleServiceUtil.getRole(roleId);
116                                    }
117                            }
118                    }
119                    else {
120                            if (roleId > 0) {
121                                    role = RoleServiceUtil.getRole(roleId);
122                            }
123                    }
124    
125                    request.setAttribute(WebKeys.ROLE, role);
126            }
127    
128            public static void getRole(PortletRequest portletRequest) throws Exception {
129                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
130                            portletRequest);
131    
132                    getRole(request);
133            }
134    
135    }