1
14
15 package com.liferay.portal.service.impl;
16
17 import com.liferay.portal.DuplicateRoleException;
18 import com.liferay.portal.NoSuchRoleException;
19 import com.liferay.portal.PortalException;
20 import com.liferay.portal.RequiredRoleException;
21 import com.liferay.portal.RoleNameException;
22 import com.liferay.portal.SystemException;
23 import com.liferay.portal.kernel.annotation.Propagation;
24 import com.liferay.portal.kernel.annotation.Transactional;
25 import com.liferay.portal.kernel.language.LanguageUtil;
26 import com.liferay.portal.kernel.util.CharPool;
27 import com.liferay.portal.kernel.util.GetterUtil;
28 import com.liferay.portal.kernel.util.OrderByComparator;
29 import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
30 import com.liferay.portal.kernel.util.StringUtil;
31 import com.liferay.portal.kernel.util.Validator;
32 import com.liferay.portal.model.Group;
33 import com.liferay.portal.model.ResourceConstants;
34 import com.liferay.portal.model.Role;
35 import com.liferay.portal.model.RoleConstants;
36 import com.liferay.portal.security.permission.PermissionCacheUtil;
37 import com.liferay.portal.service.base.RoleLocalServiceBaseImpl;
38 import com.liferay.portal.util.PortalUtil;
39 import com.liferay.portal.util.PropsUtil;
40 import com.liferay.portal.util.PropsValues;
41 import com.liferay.portlet.enterpriseadmin.util.EnterpriseAdminUtil;
42
43 import java.util.ArrayList;
44 import java.util.Collections;
45 import java.util.HashMap;
46 import java.util.LinkedHashMap;
47 import java.util.List;
48 import java.util.Locale;
49 import java.util.Map;
50
51
56 public class RoleLocalServiceImpl extends RoleLocalServiceBaseImpl {
57
58 public Role addRole(
59 long userId, long companyId, String name, String description,
60 int type)
61 throws PortalException, SystemException {
62
63 return addRole(userId, companyId, name, description, type, null, 0);
64 }
65
66 public Role addRole(
67 long userId, long companyId, String name, String description,
68 int type, String className, long classPK)
69 throws PortalException, SystemException {
70
71
73 className = GetterUtil.getString(className);
74 long classNameId = PortalUtil.getClassNameId(className);
75
76 validate(0, companyId, name);
77
78 long roleId = counterLocalService.increment();
79
80 if ((classNameId <= 0) || className.equals(Role.class.getName())) {
81 classNameId = PortalUtil.getClassNameId(Role.class);
82 classPK = roleId;
83 }
84
85 Role role = rolePersistence.create(roleId);
86
87 role.setCompanyId(companyId);
88 role.setClassNameId(classNameId);
89 role.setClassPK(classPK);
90 role.setName(name);
91 role.setDescription(description);
92 role.setType(type);
93
94 rolePersistence.update(role, false);
95
96
98 if (userId > 0) {
99 resourceLocalService.addResources(
100 companyId, 0, userId, Role.class.getName(), role.getRoleId(),
101 false, false, false);
102
103 userLocalService.reIndex(userId);
104 }
105
106 return role;
107 }
108
109 public void addUserRoles(long userId, long[] roleIds)
110 throws SystemException {
111
112 userPersistence.addRoles(userId, roleIds);
113
114 userLocalService.reIndex(userId);
115
116 PermissionCacheUtil.clearCache();
117 }
118
119 @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
120 public void checkSystemRoles(long companyId)
121 throws PortalException, SystemException {
122
123 for (Role role : roleFinder.findBySystem(companyId)) {
124 _systemRolesMap.put(companyId + role.getName(), role);
125 }
126
127
129 String[] systemRoles = PortalUtil.getSystemRoles();
130
131 for (String name : systemRoles) {
132 String description = PropsUtil.get(
133 "system.role." + StringUtil.replace(name, " ", ".") +
134 ".description");
135 int type = RoleConstants.TYPE_REGULAR;
136
137 checkSystemRole(companyId, name, description, type);
138 }
139
140
142 String[] systemCommunityRoles = PortalUtil.getSystemCommunityRoles();
143
144 for (String name : systemCommunityRoles) {
145 String description = PropsUtil.get(
146 "system.community.role." +
147 StringUtil.replace(name, " ", ".") + ".description");
148 int type = RoleConstants.TYPE_COMMUNITY;
149
150 checkSystemRole(companyId, name, description, type);
151 }
152
153
155 String[] systemOrganizationRoles =
156 PortalUtil.getSystemOrganizationRoles();
157
158 for (String name : systemOrganizationRoles) {
159 String description = PropsUtil.get(
160 "system.organization.role." +
161 StringUtil.replace(name, " ", ".") + ".description");
162 int type = RoleConstants.TYPE_ORGANIZATION;
163
164 checkSystemRole(companyId, name, description, type);
165 }
166 }
167
168 public void deleteRole(long roleId)
169 throws PortalException, SystemException {
170
171 Role role = rolePersistence.findByPrimaryKey(roleId);
172
173 if (PortalUtil.isSystemRole(role.getName())) {
174 throw new RequiredRoleException();
175 }
176
177
179 String className = role.getClassName();
180 long classNameId = role.getClassNameId();
181
182 if ((classNameId <= 0) || className.equals(Role.class.getName())) {
183 resourceLocalService.deleteResource(
184 role.getCompanyId(), Role.class.getName(),
185 ResourceConstants.SCOPE_INDIVIDUAL, role.getRoleId());
186 }
187
188 if ((role.getType() == RoleConstants.TYPE_COMMUNITY) ||
189 (role.getType() == RoleConstants.TYPE_ORGANIZATION)) {
190
191 userGroupRoleLocalService.deleteUserGroupRolesByRoleId(
192 role.getRoleId());
193
194 userGroupGroupRoleLocalService.deleteUserGroupGroupRolesByRoleId(
195 role.getRoleId());
196 }
197
198
200 rolePersistence.remove(role);
201
202
204 PermissionCacheUtil.clearCache();
205 }
206
207 public Role getGroupRole(long companyId, long groupId)
208 throws PortalException, SystemException {
209
210 long classNameId = PortalUtil.getClassNameId(Group.class);
211
212 return rolePersistence.findByC_C_C(companyId, classNameId, groupId);
213 }
214
215 public List<Role> getGroupRoles(long groupId) throws SystemException {
216 return groupPersistence.getRoles(groupId);
217 }
218
219 public Map<String, List<String>> getResourceRoles(
220 long companyId, String name, int scope, String primKey)
221 throws SystemException {
222
223 return roleFinder.findByC_N_S_P(companyId, name, scope, primKey);
224 }
225
226 public Role getRole(long roleId) throws PortalException, SystemException {
227 return rolePersistence.findByPrimaryKey(roleId);
228 }
229
230 public Role getRole(long companyId, String name)
231 throws PortalException, SystemException {
232
233 Role role = _systemRolesMap.get(companyId + name);
234
235 if (role != null) {
236 return role;
237 }
238
239 return rolePersistence.findByC_N(companyId, name);
240 }
241
242 public List<Role> getRoles(long companyId) throws SystemException {
243 return rolePersistence.findByCompanyId(companyId);
244 }
245
246 public List<Role> getRoles(long[] roleIds)
247 throws PortalException, SystemException {
248
249 List<Role> roles = new ArrayList<Role>(roleIds.length);
250
251 for (long roleId : roleIds) {
252 Role role = getRole(roleId);
253
254 roles.add(role);
255 }
256
257 return roles;
258 }
259
260 public List<Role> getRoles(int type, String subtype)
261 throws SystemException {
262
263 return rolePersistence.findByT_S(type, subtype);
264 }
265
266 public List<Role> getSubtypeRoles(String subtype) throws SystemException {
267 return rolePersistence.findBySubtype(subtype);
268 }
269
270 public int getSubtypeRolesCount(String subtype) throws SystemException {
271 return rolePersistence.countBySubtype(subtype);
272 }
273
274 public List<Role> getUserGroupGroupRoles(long userId, long groupId)
275 throws SystemException {
276
277 return roleFinder.findByUserGroupGroupRole(userId, groupId);
278 }
279
280 public List<Role> getUserGroupRoles(long userId, long groupId)
281 throws SystemException {
282
283 return roleFinder.findByUserGroupRole(userId, groupId);
284 }
285
286 public List<Role> getUserRelatedRoles(long userId, long groupId)
287 throws SystemException {
288
289 return roleFinder.findByU_G(userId, groupId);
290 }
291
292 public List<Role> getUserRelatedRoles(long userId, long[] groupIds)
293 throws SystemException {
294
295 return roleFinder.findByU_G(userId, groupIds);
296 }
297
298 public List<Role> getUserRelatedRoles(long userId, List<Group> groups)
299 throws SystemException {
300
301 if ((groups == null) || groups.isEmpty()) {
302 return Collections.EMPTY_LIST;
303 }
304
305 return roleFinder.findByU_G(userId, groups);
306 }
307
308 public List<Role> getUserRoles(long userId) throws SystemException {
309 return userPersistence.getRoles(userId);
310 }
311
312 public boolean hasUserRole(long userId, long roleId)
313 throws SystemException {
314
315 return userPersistence.containsRole(userId, roleId);
316 }
317
318
323 public boolean hasUserRole(
324 long userId, long companyId, String name, boolean inherited)
325 throws PortalException, SystemException {
326
327 Role role = rolePersistence.findByC_N(companyId, name);
328
329 if (role.getType() != RoleConstants.TYPE_REGULAR) {
330 throw new IllegalArgumentException(name + " is not a regular role");
331 }
332
333 if (inherited) {
334 if (roleFinder.countByR_U(role.getRoleId(), userId) > 0) {
335 return true;
336 }
337 else {
338 return false;
339 }
340 }
341 else {
342 return userPersistence.containsRole(userId, role.getRoleId());
343 }
344 }
345
346
351 public boolean hasUserRoles(
352 long userId, long companyId, String[] names, boolean inherited)
353 throws PortalException, SystemException {
354
355 for (int i = 0; i < names.length; i++) {
356 if (hasUserRole(userId, companyId, names[i], inherited)) {
357 return true;
358 }
359 }
360
361 return false;
362 }
363
364 public List<Role> search(
365 long companyId, String name, String description, Integer type,
366 int start, int end, OrderByComparator obc)
367 throws SystemException {
368
369 return search(
370 companyId, name, description, type,
371 new LinkedHashMap<String, Object>(), start, end, obc);
372 }
373
374 public List<Role> search(
375 long companyId, String name, String description, Integer type,
376 LinkedHashMap<String, Object> params, int start, int end,
377 OrderByComparator obc)
378 throws SystemException {
379
380 return roleFinder.findByC_N_D_T(
381 companyId, name, description, type, params, start, end, obc);
382 }
383
384 public int searchCount(
385 long companyId, String name, String description, Integer type)
386 throws SystemException {
387
388 return searchCount(
389 companyId, name, description, type,
390 new LinkedHashMap<String, Object>());
391 }
392
393 public int searchCount(
394 long companyId, String name, String description, Integer type,
395 LinkedHashMap<String, Object> params)
396 throws SystemException {
397
398 return roleFinder.countByC_N_D_T(
399 companyId, name, description, type, params);
400 }
401
402 public void setUserRoles(long userId, long[] roleIds)
403 throws PortalException, SystemException {
404
405 roleIds = EnterpriseAdminUtil.addRequiredRoles(userId, roleIds);
406
407 userPersistence.setRoles(userId, roleIds);
408
409 userLocalService.reIndex(userId);
410
411 PermissionCacheUtil.clearCache();
412 }
413
414 public void unsetUserRoles(long userId, long[] roleIds)
415 throws PortalException, SystemException {
416
417 roleIds = EnterpriseAdminUtil.removeRequiredRoles(userId, roleIds);
418
419 userPersistence.removeRoles(userId, roleIds);
420
421 userLocalService.reIndex(userId);
422
423 PermissionCacheUtil.clearCache();
424 }
425
426 public Role updateRole(
427 long roleId, String name, Map<Locale, String> localeTitlesMap,
428 String description, String subtype)
429 throws PortalException, SystemException {
430
431 Role role = rolePersistence.findByPrimaryKey(roleId);
432
433 validate(roleId, role.getCompanyId(), name);
434
435 if (PortalUtil.isSystemRole(role.getName())) {
436 name = role.getName();
437 subtype = null;
438 }
439
440 role.setName(name);
441 role.setDescription(description);
442 role.setSubtype(subtype);
443
444 setLocalizedAttributes(role, localeTitlesMap);
445
446 rolePersistence.update(role, false);
447
448 return role;
449 }
450
451 protected void checkSystemRole(
452 long companyId, String name, String description, int type)
453 throws PortalException, SystemException {
454
455 Role role = _systemRolesMap.get(companyId + name);
456
457 try {
458 if (role == null) {
459 role = rolePersistence.findByC_N(companyId, name);
460 }
461
462 if (!role.getDescription().equals(description)) {
463 role.setDescription(description);
464
465 roleLocalService.updateRole(role, false);
466 }
467 }
468 catch (NoSuchRoleException nsre) {
469 role = roleLocalService.addRole(
470 0, companyId, name, description, type);
471 }
472
473 _systemRolesMap.put(companyId + name, role);
474 }
475
476 protected void setLocalizedAttributes(
477 Role role, Map<Locale, String> localeTitlesMap) {
478
479 if (localeTitlesMap == null) {
480 return;
481 }
482
483 ClassLoader portalClassLoader = PortalClassLoaderUtil.getClassLoader();
484
485 Thread currentThread = Thread.currentThread();
486
487 ClassLoader contextClassLoader = currentThread.getContextClassLoader();
488
489 try {
490 if (contextClassLoader != portalClassLoader) {
491 currentThread.setContextClassLoader(portalClassLoader);
492 }
493
494 Locale[] locales = LanguageUtil.getAvailableLocales();
495
496 for (Locale locale : locales) {
497 String title = localeTitlesMap.get(locale);
498
499 role.setTitle(title, locale);
500 }
501 }
502 finally {
503 if (contextClassLoader != portalClassLoader) {
504 currentThread.setContextClassLoader(contextClassLoader);
505 }
506 }
507 }
508
509 protected void validate(long roleId, long companyId, String name)
510 throws PortalException, SystemException {
511
512 if (Validator.isNull(name) || (name.indexOf(CharPool.COMMA) != -1) ||
513 (name.indexOf(CharPool.STAR) != -1)) {
514
515 throw new RoleNameException();
516 }
517
518 if (Validator.isNumber(name) && !PropsValues.ROLES_NAME_ALLOW_NUMERIC) {
519 throw new RoleNameException();
520 }
521
522 try {
523 Role role = roleFinder.findByC_N(companyId, name);
524
525 if (role.getRoleId() != roleId) {
526 throw new DuplicateRoleException();
527 }
528 }
529 catch (NoSuchRoleException nsge) {
530 }
531 }
532
533 private Map<String, Role> _systemRolesMap = new HashMap<String, Role>();
534
535 }