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