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