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.DuplicateRoleException;
024: import com.liferay.portal.NoSuchRoleException;
025: import com.liferay.portal.PortalException;
026: import com.liferay.portal.RequiredRoleException;
027: import com.liferay.portal.RoleNameException;
028: import com.liferay.portal.SystemException;
029: import com.liferay.portal.kernel.util.OrderByComparator;
030: import com.liferay.portal.kernel.util.StringPool;
031: import com.liferay.portal.kernel.util.StringUtil;
032: import com.liferay.portal.kernel.util.Validator;
033: import com.liferay.portal.model.Group;
034: import com.liferay.portal.model.Role;
035: import com.liferay.portal.model.impl.ResourceImpl;
036: import com.liferay.portal.model.impl.RoleImpl;
037: import com.liferay.portal.security.permission.PermissionCacheUtil;
038: import com.liferay.portal.service.base.RoleLocalServiceBaseImpl;
039: import com.liferay.portal.util.PortalUtil;
040: import com.liferay.portal.util.PropsUtil;
041:
042: import java.util.LinkedHashMap;
043: import java.util.List;
044: import java.util.Map;
045:
046: /**
047: * <a href="RoleLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
048: *
049: * @author Brian Wing Shun Chan
050: *
051: */
052: public class RoleLocalServiceImpl extends RoleLocalServiceBaseImpl {
053:
054: public Role addRole(long userId, long companyId, String name,
055: String description, int type) throws PortalException,
056: SystemException {
057:
058: return addRole(userId, companyId, name, description, type,
059: null, 0);
060: }
061:
062: public Role addRole(long userId, long companyId, String name,
063: String description, int type, String className, long classPK)
064: throws PortalException, SystemException {
065:
066: // Role
067:
068: long classNameId = PortalUtil.getClassNameId(className);
069:
070: validate(0, companyId, name);
071:
072: long roleId = counterLocalService.increment();
073:
074: Role role = rolePersistence.create(roleId);
075:
076: role.setCompanyId(companyId);
077: role.setClassNameId(classNameId);
078: role.setClassPK(classPK);
079: role.setName(name);
080: role.setDescription(description);
081: role.setType(type);
082:
083: rolePersistence.update(role);
084:
085: // Resources
086:
087: if (userId > 0) {
088: resourceLocalService.addResources(companyId, 0, userId,
089: Role.class.getName(), role.getRoleId(), false,
090: false, false);
091: }
092:
093: return role;
094: }
095:
096: public void addUserRoles(long userId, long[] roleIds)
097: throws PortalException, SystemException {
098:
099: userPersistence.addRoles(userId, roleIds);
100:
101: PermissionCacheUtil.clearCache();
102: }
103:
104: public void checkSystemRoles(long companyId)
105: throws PortalException, SystemException {
106:
107: // Regular roles
108:
109: String[] systemRoles = PortalUtil.getSystemRoles();
110:
111: for (int i = 0; i < systemRoles.length; i++) {
112: String roleName = systemRoles[i];
113: String roleDescription = PropsUtil.get("system.role."
114: + StringUtil.replace(roleName, " ", ".")
115: + ".description");
116: int roleType = RoleImpl.TYPE_REGULAR;
117:
118: try {
119: Role role = roleFinder.findByC_N(companyId, roleName);
120:
121: if (!role.getDescription().equals(roleDescription)) {
122: role.setDescription(roleDescription);
123:
124: rolePersistence.update(role);
125: }
126: } catch (NoSuchRoleException nsre) {
127: addRole(0, companyId, roleName, roleDescription,
128: roleType);
129: }
130: }
131:
132: // Community roles
133:
134: String[] systemCommunityRoles = PortalUtil
135: .getSystemCommunityRoles();
136:
137: for (int i = 0; i < systemCommunityRoles.length; i++) {
138: String roleName = systemCommunityRoles[i];
139: String roleDescription = PropsUtil
140: .get("system.community.role."
141: + StringUtil.replace(roleName, " ", ".")
142: + ".description");
143: int roleType = RoleImpl.TYPE_COMMUNITY;
144:
145: try {
146: Role role = roleFinder.findByC_N(companyId, roleName);
147:
148: if (!role.getDescription().equals(roleDescription)) {
149: role.setDescription(roleDescription);
150:
151: rolePersistence.update(role);
152: }
153: } catch (NoSuchRoleException nsre) {
154: addRole(0, companyId, roleName, roleDescription,
155: roleType);
156: }
157: }
158:
159: // Organization roles
160:
161: String[] systemOrganizationRoles = PortalUtil
162: .getSystemOrganizationRoles();
163:
164: for (int i = 0; i < systemOrganizationRoles.length; i++) {
165: String roleName = systemOrganizationRoles[i];
166: String roleDescription = PropsUtil
167: .get("system.organization.role."
168: + StringUtil.replace(roleName, " ", ".")
169: + ".description");
170: int roleType = RoleImpl.TYPE_ORGANIZATION;
171:
172: try {
173: Role role = roleFinder.findByC_N(companyId, roleName);
174:
175: if (!role.getDescription().equals(roleDescription)) {
176: role.setDescription(roleDescription);
177:
178: rolePersistence.update(role);
179: }
180: } catch (NoSuchRoleException nsre) {
181: addRole(0, companyId, roleName, roleDescription,
182: roleType);
183: }
184: }
185: }
186:
187: public void deleteRole(long roleId) throws PortalException,
188: SystemException {
189:
190: Role role = rolePersistence.findByPrimaryKey(roleId);
191:
192: if (PortalUtil.isSystemRole(role.getName())) {
193: throw new RequiredRoleException();
194: }
195:
196: // Resources
197:
198: if ((role.getClassNameId() <= 0) && (role.getClassPK() <= 0)) {
199: resourceLocalService.deleteResource(role.getCompanyId(),
200: Role.class.getName(),
201: ResourceImpl.SCOPE_INDIVIDUAL, role.getRoleId());
202: }
203:
204: if ((role.getType() == RoleImpl.TYPE_COMMUNITY)
205: || (role.getType() == RoleImpl.TYPE_ORGANIZATION)) {
206:
207: userGroupRoleLocalService.deleteUserGroupRolesByRoleId(role
208: .getRoleId());
209: }
210:
211: // Role
212:
213: rolePersistence.remove(roleId);
214:
215: // Permission cache
216:
217: PermissionCacheUtil.clearCache();
218: }
219:
220: public Role getGroupRole(long companyId, long groupId)
221: throws PortalException, SystemException {
222:
223: long classNameId = PortalUtil.getClassNameId(Group.class);
224:
225: return rolePersistence.findByC_C_C(companyId, classNameId,
226: groupId);
227: }
228:
229: public List getGroupRoles(long groupId) throws PortalException,
230: SystemException {
231:
232: return groupPersistence.getRoles(groupId);
233: }
234:
235: public Map getResourceRoles(long companyId, String name, int scope,
236: String primKey) throws SystemException {
237:
238: return roleFinder
239: .findByC_N_S_P(companyId, name, scope, primKey);
240: }
241:
242: public Role getRole(long roleId) throws PortalException,
243: SystemException {
244: return rolePersistence.findByPrimaryKey(roleId);
245: }
246:
247: public Role getRole(long companyId, String name)
248: throws PortalException, SystemException {
249:
250: return roleFinder.findByC_N(companyId, name);
251: }
252:
253: public List getUserGroupRoles(long userId, long groupId)
254: throws SystemException {
255:
256: return roleFinder.findByUserGroupRole(userId, groupId);
257: }
258:
259: public List getUserRelatedRoles(long userId, long groupId)
260: throws SystemException {
261:
262: return roleFinder.findByU_G(userId, groupId);
263: }
264:
265: public List getUserRelatedRoles(long userId, long[] groupIds)
266: throws SystemException {
267:
268: return roleFinder.findByU_G(userId, groupIds);
269: }
270:
271: public List getUserRelatedRoles(long userId, List groups)
272: throws SystemException {
273:
274: return roleFinder.findByU_G(userId, groups);
275: }
276:
277: public List getUserRoles(long userId) throws PortalException,
278: SystemException {
279:
280: return userPersistence.getRoles(userId);
281: }
282:
283: public boolean hasUserRole(long userId, long roleId)
284: throws PortalException, SystemException {
285:
286: return userPersistence.containsRole(userId, roleId);
287: }
288:
289: /**
290: * Returns true if the user has the role.
291: *
292: * @param userId the user id of the user
293: * @param companyId the company id of the company
294: * @param name the name of the role
295: * @param inherited boolean value for whether to check roles inherited
296: * from the community, organization, location, or user group
297: * @return true if the user has the role
298: */
299: public boolean hasUserRole(long userId, long companyId,
300: String name, boolean inherited) throws PortalException,
301: SystemException {
302:
303: Role role = roleFinder.findByC_N(companyId, name);
304:
305: if (inherited) {
306: if (roleFinder.countByR_U(role.getRoleId(), userId) > 0) {
307: return true;
308: } else {
309: return false;
310: }
311: } else {
312: return userPersistence.containsRole(userId, role
313: .getRoleId());
314: }
315: }
316:
317: /**
318: * Returns true if the user has any one of the specified roles.
319: *
320: * @param userId the user id of the user
321: * @param companyId the company id of the company
322: * @param names an array of role names
323: * @param inherited boolean value for whether to check roles inherited
324: * from the community, organization, location, or user group
325: * @return true if the user has the role
326: */
327: public boolean hasUserRoles(long userId, long companyId,
328: String[] names, boolean inherited) throws PortalException,
329: SystemException {
330:
331: for (int i = 0; i < names.length; i++) {
332: if (hasUserRole(userId, companyId, names[i], inherited)) {
333: return true;
334: }
335: }
336:
337: return false;
338: }
339:
340: public List search(long companyId, String name, String description,
341: Integer type, int begin, int end, OrderByComparator obc)
342: throws SystemException {
343:
344: return search(companyId, name, description, type,
345: new LinkedHashMap(), begin, end, obc);
346: }
347:
348: public List search(long companyId, String name, String description,
349: Integer type, LinkedHashMap params, int begin, int end,
350: OrderByComparator obc) throws SystemException {
351:
352: return roleFinder.findByC_N_D_T(companyId, name, description,
353: type, params, begin, end, obc);
354: }
355:
356: public int searchCount(long companyId, String name,
357: String description, Integer type) throws SystemException {
358:
359: return searchCount(companyId, name, description, type,
360: new LinkedHashMap());
361: }
362:
363: public int searchCount(long companyId, String name,
364: String description, Integer type, LinkedHashMap params)
365: throws SystemException {
366:
367: return roleFinder.countByC_N_D_T(companyId, name, description,
368: type, params);
369: }
370:
371: public void setUserRoles(long userId, long[] roleIds)
372: throws PortalException, SystemException {
373:
374: userPersistence.setRoles(userId, roleIds);
375:
376: PermissionCacheUtil.clearCache();
377: }
378:
379: public void unsetUserRoles(long userId, long[] roleIds)
380: throws PortalException, SystemException {
381:
382: userPersistence.removeRoles(userId, roleIds);
383:
384: PermissionCacheUtil.clearCache();
385: }
386:
387: public Role updateRole(long roleId, String name, String description)
388: throws PortalException, SystemException {
389:
390: Role role = rolePersistence.findByPrimaryKey(roleId);
391:
392: validate(roleId, role.getCompanyId(), name);
393:
394: if (PortalUtil.isSystemRole(role.getName())) {
395: throw new RequiredRoleException();
396: }
397:
398: role.setName(name);
399: role.setDescription(description);
400:
401: rolePersistence.update(role);
402:
403: return role;
404: }
405:
406: protected void validate(long roleId, long companyId, String name)
407: throws PortalException, SystemException {
408:
409: if ((Validator.isNull(name)) || (Validator.isNumber(name))
410: || (name.indexOf(StringPool.COMMA) != -1)
411: || (name.indexOf(StringPool.STAR) != -1)) {
412:
413: throw new RoleNameException();
414: }
415:
416: try {
417: Role role = roleFinder.findByC_N(companyId, name);
418:
419: if (role.getRoleId() != roleId) {
420: throw new DuplicateRoleException();
421: }
422: } catch (NoSuchRoleException nsge) {
423: }
424: }
425:
426: }
|