001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.service.impl;
022:
023: import com.liferay.portal.NoSuchUserGroupRoleException;
024: import com.liferay.portal.PortalException;
025: import com.liferay.portal.SystemException;
026: import com.liferay.portal.model.Group;
027: import com.liferay.portal.model.Role;
028: import com.liferay.portal.model.User;
029: import com.liferay.portal.model.UserGroupRole;
030: import com.liferay.portal.model.impl.ResourceImpl;
031: import com.liferay.portal.security.permission.PermissionCacheUtil;
032: import com.liferay.portal.service.base.UserGroupRoleLocalServiceBaseImpl;
033: import com.liferay.portal.service.persistence.UserGroupRolePK;
034:
035: import java.util.List;
036:
037: /**
038: * <a href="UserGroupRoleLocalServiceImpl.java.html"><b><i>View Source</i></b>
039: * </a>
040: *
041: * @author Jorge Ferrer
042: *
043: */
044: public class UserGroupRoleLocalServiceImpl extends
045: UserGroupRoleLocalServiceBaseImpl {
046:
047: public void addUserGroupRoles(long userId, long groupId,
048: long[] roleIds) throws PortalException, SystemException {
049:
050: checkGroupResource(groupId);
051:
052: for (int i = 0; i < roleIds.length; i++) {
053: long roleId = roleIds[i];
054:
055: UserGroupRolePK pk = new UserGroupRolePK(userId, groupId,
056: roleId);
057:
058: UserGroupRole userGroupRole = userGroupRolePersistence
059: .fetchByPrimaryKey(pk);
060:
061: if (userGroupRole == null) {
062: userGroupRole = userGroupRolePersistence.create(pk);
063:
064: userGroupRolePersistence.update(userGroupRole);
065: }
066: }
067:
068: PermissionCacheUtil.clearCache();
069: }
070:
071: public void addUserGroupRoles(long[] userIds, long groupId,
072: long roleId) throws PortalException, SystemException {
073:
074: checkGroupResource(groupId);
075:
076: for (int i = 0; i < userIds.length; i++) {
077: long userId = userIds[i];
078:
079: UserGroupRolePK pk = new UserGroupRolePK(userId, groupId,
080: roleId);
081:
082: UserGroupRole userGroupRole = userGroupRolePersistence
083: .fetchByPrimaryKey(pk);
084:
085: if (userGroupRole == null) {
086: userGroupRole = userGroupRolePersistence.create(pk);
087:
088: userGroupRolePersistence.update(userGroupRole);
089: }
090: }
091:
092: PermissionCacheUtil.clearCache();
093: }
094:
095: public void deleteUserGroupRoles(long userId, long[] groupIds)
096: throws SystemException {
097:
098: for (int i = 0; i < groupIds.length; i++) {
099: long groupId = groupIds[i];
100:
101: userGroupRolePersistence.removeByU_G(userId, groupId);
102: }
103:
104: PermissionCacheUtil.clearCache();
105: }
106:
107: public void deleteUserGroupRoles(long[] userIds, long groupId)
108: throws SystemException {
109:
110: for (int i = 0; i < userIds.length; i++) {
111: long userId = userIds[i];
112:
113: userGroupRolePersistence.removeByU_G(userId, groupId);
114: }
115:
116: PermissionCacheUtil.clearCache();
117: }
118:
119: public void deleteUserGroupRoles(long userId, long groupId,
120: long[] roleIds) throws PortalException, SystemException {
121:
122: for (int i = 0; i < roleIds.length; i++) {
123: long roleId = roleIds[i];
124:
125: UserGroupRolePK pk = new UserGroupRolePK(userId, groupId,
126: roleId);
127:
128: try {
129: userGroupRolePersistence.remove(pk);
130: } catch (NoSuchUserGroupRoleException nsugre) {
131: }
132: }
133:
134: PermissionCacheUtil.clearCache();
135: }
136:
137: public void deleteUserGroupRoles(long[] userIds, long groupId,
138: long roleId) throws PortalException, SystemException {
139:
140: for (int i = 0; i < userIds.length; i++) {
141: long userId = userIds[i];
142:
143: UserGroupRolePK pk = new UserGroupRolePK(userId, groupId,
144: roleId);
145:
146: try {
147: userGroupRolePersistence.remove(pk);
148: } catch (NoSuchUserGroupRoleException nsugre) {
149: }
150: }
151:
152: PermissionCacheUtil.clearCache();
153: }
154:
155: public void deleteUserGroupRolesByGroupId(long groupId)
156: throws SystemException {
157:
158: userGroupRolePersistence.removeByGroupId(groupId);
159:
160: PermissionCacheUtil.clearCache();
161: }
162:
163: public void deleteUserGroupRolesByRoleId(long roleId)
164: throws SystemException {
165:
166: userGroupRolePersistence.removeByRoleId(roleId);
167:
168: PermissionCacheUtil.clearCache();
169: }
170:
171: public void deleteUserGroupRolesByUserId(long userId)
172: throws SystemException {
173:
174: userGroupRolePersistence.removeByUserId(userId);
175:
176: PermissionCacheUtil.clearCache();
177: }
178:
179: public List getUserGroupRoles(long userId, long groupId)
180: throws PortalException, SystemException {
181:
182: return userGroupRolePersistence.findByU_G(userId, groupId);
183: }
184:
185: public List getUserGroupRolesByGroupAndRole(long groupId,
186: long roleId) throws PortalException, SystemException {
187:
188: return userGroupRolePersistence.findByG_R(groupId, roleId);
189: }
190:
191: public boolean hasUserGroupRole(long userId, long groupId,
192: long roleId) throws SystemException {
193:
194: UserGroupRolePK pk = new UserGroupRolePK(userId, groupId,
195: roleId);
196:
197: UserGroupRole userGroupRole = userGroupRolePersistence
198: .fetchByPrimaryKey(pk);
199:
200: if (userGroupRole != null) {
201: return true;
202: } else {
203: return false;
204: }
205: }
206:
207: public boolean hasUserGroupRole(long userId, long groupId,
208: String roleName) throws PortalException, SystemException {
209:
210: User user = userPersistence.findByPrimaryKey(userId);
211:
212: long companyId = user.getCompanyId();
213:
214: Role role = rolePersistence.findByC_N(companyId, roleName);
215:
216: long roleId = role.getRoleId();
217:
218: return hasUserGroupRole(userId, groupId, roleId);
219: }
220:
221: protected void checkGroupResource(long groupId)
222: throws PortalException, SystemException {
223:
224: // Make sure that the individual resource for the group exists
225:
226: Group group = groupPersistence.findByPrimaryKey(groupId);
227:
228: resourceLocalService.addResource(group.getCompanyId(),
229: Group.class.getName(), ResourceImpl.SCOPE_INDIVIDUAL,
230: String.valueOf(groupId));
231: }
232:
233: }
|