001    /**
002     * Copyright (c) 2000-2010 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.portal.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.model.Group;
020    import com.liferay.portal.model.Role;
021    import com.liferay.portal.model.User;
022    import com.liferay.portal.security.permission.ActionKeys;
023    import com.liferay.portal.service.base.RoleServiceBaseImpl;
024    import com.liferay.portal.service.permission.PortalPermissionUtil;
025    import com.liferay.portal.service.permission.RolePermissionUtil;
026    
027    import java.util.List;
028    import java.util.Locale;
029    import java.util.Map;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    public class RoleServiceImpl extends RoleServiceBaseImpl {
035    
036            public Role addRole(
037                            String name, Map<Locale, String> titleMap, String description,
038                            int type)
039                    throws PortalException, SystemException {
040    
041                    User user = getUser();
042    
043                    PortalPermissionUtil.check(getPermissionChecker(), ActionKeys.ADD_ROLE);
044    
045                    return roleLocalService.addRole(
046                            user.getUserId(), user.getCompanyId(), name, titleMap, description,
047                            type);
048            }
049    
050            public void addUserRoles(long userId, long[] roleIds)
051                    throws PortalException, SystemException {
052    
053                    checkUserRolesPermission(userId, roleIds);
054    
055                    roleLocalService.addUserRoles(userId, roleIds);
056            }
057    
058            public void deleteRole(long roleId)
059                    throws PortalException, SystemException {
060    
061                    RolePermissionUtil.check(
062                            getPermissionChecker(), roleId, ActionKeys.DELETE);
063    
064                    roleLocalService.deleteRole(roleId);
065            }
066    
067            public List<Role> getGroupRoles(long groupId) throws SystemException {
068                    return roleLocalService.getGroupRoles(groupId);
069            }
070    
071            public Role getRole(long roleId)
072                    throws PortalException, SystemException {
073    
074                    RolePermissionUtil.check(
075                            getPermissionChecker(), roleId, ActionKeys.VIEW);
076    
077                    return roleLocalService.getRole(roleId);
078            }
079    
080            public Role getRole(long companyId, String name)
081                    throws PortalException, SystemException {
082    
083                    Role role = roleLocalService.getRole(companyId, name);
084    
085                    RolePermissionUtil.check(
086                            getPermissionChecker(), role.getRoleId(), ActionKeys.VIEW);
087    
088                    return role;
089            }
090    
091            public List<Role> getUserGroupGroupRoles(long userId, long groupId)
092                    throws SystemException {
093    
094                    return roleLocalService.getUserGroupGroupRoles(userId, groupId);
095            }
096    
097            public List<Role> getUserGroupRoles(long userId, long groupId)
098                    throws SystemException {
099    
100                    return roleLocalService.getUserGroupRoles(userId, groupId);
101            }
102    
103            public List<Role> getUserRelatedRoles(long userId, List<Group> groups)
104                    throws SystemException {
105    
106                    return roleLocalService.getUserRelatedRoles(userId, groups);
107            }
108    
109            public List<Role> getUserRoles(long userId) throws SystemException {
110                    return roleLocalService.getUserRoles(userId);
111            }
112    
113            public boolean hasUserRole(
114                            long userId, long companyId, String name, boolean inherited)
115                    throws PortalException, SystemException {
116    
117                    return roleLocalService.hasUserRole(userId, companyId, name, inherited);
118            }
119    
120            public boolean hasUserRoles(
121                            long userId, long companyId, String[] names, boolean inherited)
122                    throws PortalException, SystemException {
123    
124                    return roleLocalService.hasUserRoles(
125                            userId, companyId, names, inherited);
126            }
127    
128            public void unsetUserRoles(long userId, long[] roleIds)
129                    throws PortalException, SystemException {
130    
131                    checkUserRolesPermission(userId, roleIds);
132    
133                    roleLocalService.unsetUserRoles(userId, roleIds);
134            }
135    
136            public Role updateRole(
137                            long roleId, String name, Map<Locale, String> titleMap,
138                            String description, String subtype)
139                    throws PortalException, SystemException {
140    
141                    RolePermissionUtil.check(
142                            getPermissionChecker(), roleId, ActionKeys.UPDATE);
143    
144                    return roleLocalService.updateRole(
145                            roleId, name, titleMap, description, subtype);
146            }
147    
148            protected void checkUserRolesPermission(long userId, long[] roleIds)
149                    throws PortalException {
150    
151                    for (int i = 0; i < roleIds.length; i++) {
152                            RolePermissionUtil.check(
153                                    getPermissionChecker(), roleIds[i], ActionKeys.ASSIGN_MEMBERS);
154                    }
155            }
156    
157    }