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