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.usersadmin;
016    
017    import com.liferay.portal.model.Organization;
018    import com.liferay.portal.model.Portlet;
019    import com.liferay.portal.model.Role;
020    import com.liferay.portal.model.RoleConstants;
021    import com.liferay.portal.model.UserGroupRole;
022    import com.liferay.portal.security.permission.ActionKeys;
023    import com.liferay.portal.security.permission.PermissionChecker;
024    import com.liferay.portal.service.OrganizationLocalServiceUtil;
025    import com.liferay.portal.service.UserGroupRoleLocalServiceUtil;
026    import com.liferay.portal.service.permission.OrganizationPermissionUtil;
027    import com.liferay.portlet.BaseControlPanelEntry;
028    
029    import java.util.List;
030    
031    /**
032     * @author Jorge Ferrer
033     */
034    public class UsersControlPanelEntry extends BaseControlPanelEntry {
035    
036            @Override
037            public boolean isVisible(
038                            PermissionChecker permissionChecker, Portlet portlet)
039                    throws Exception {
040    
041                    List<UserGroupRole> userGroupRoles =
042                            UserGroupRoleLocalServiceUtil.getUserGroupRoles(
043                                    permissionChecker.getUserId());
044    
045                    for (UserGroupRole userGroupRole : userGroupRoles) {
046                            Role role = userGroupRole.getRole();
047    
048                            String roleName = role.getName();
049    
050                            if (roleName.equals(RoleConstants.ORGANIZATION_ADMINISTRATOR) ||
051                                    roleName.equals(RoleConstants.ORGANIZATION_OWNER)) {
052    
053                                    return true;
054                            }
055                    }
056    
057                    List<Organization> organizations =
058                            OrganizationLocalServiceUtil.getUserOrganizations(
059                                    permissionChecker.getUserId());
060    
061                    for (Organization organization : organizations) {
062                            if (OrganizationPermissionUtil.contains(
063                                            permissionChecker, organization.getOrganizationId(),
064                                            ActionKeys.MANAGE_USERS)) {
065    
066                                    return true;
067                            }
068    
069                            if (OrganizationPermissionUtil.contains(
070                                            permissionChecker, organization.getOrganizationId(),
071                                            ActionKeys.MANAGE_SUBORGANIZATIONS)) {
072    
073                                    return true;
074                            }
075    
076                            /*if (OrganizationPermissionUtil.contains(
077                                            permissionChecker, organization.getOrganizationId(),
078                                            ActionKeys.VIEW)) {
079    
080                                    return true;
081                            }*/
082                    }
083    
084                    return false;
085            }
086    
087    }