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.language.LanguageUtil;
32 import com.liferay.portal.kernel.util.ArrayUtil;
33 import com.liferay.portal.kernel.util.OrderByComparator;
34 import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
35 import com.liferay.portal.kernel.util.StringPool;
36 import com.liferay.portal.kernel.util.StringUtil;
37 import com.liferay.portal.kernel.util.Validator;
38 import com.liferay.portal.model.Group;
39 import com.liferay.portal.model.ResourceConstants;
40 import com.liferay.portal.model.Role;
41 import com.liferay.portal.model.RoleConstants;
42 import com.liferay.portal.model.User;
43 import com.liferay.portal.security.permission.PermissionCacheUtil;
44 import com.liferay.portal.service.base.RoleLocalServiceBaseImpl;
45 import com.liferay.portal.util.PortalUtil;
46 import com.liferay.portal.util.PropsUtil;
47
48 import java.util.ArrayList;
49 import java.util.LinkedHashMap;
50 import java.util.List;
51 import java.util.Locale;
52 import java.util.Map;
53
54
60 public class RoleLocalServiceImpl extends RoleLocalServiceBaseImpl {
61
62 public Role addRole(
63 long userId, long companyId, String name, String description,
64 int type)
65 throws PortalException, SystemException {
66
67 return addRole(userId, companyId, name, description, type, null, 0);
68 }
69
70 public Role addRole(
71 long userId, long companyId, String name, String description,
72 int type, String className, long classPK)
73 throws PortalException, SystemException {
74
75
77 long classNameId = PortalUtil.getClassNameId(className);
78
79 validate(0, companyId, name);
80
81 long roleId = counterLocalService.increment();
82
83 Role role = rolePersistence.create(roleId);
84
85 role.setCompanyId(companyId);
86 role.setClassNameId(classNameId);
87 role.setClassPK(classPK);
88 role.setName(name);
89 role.setDescription(description);
90 role.setType(type);
91
92 rolePersistence.update(role, false);
93
94
96 if (userId > 0) {
97 resourceLocalService.addResources(
98 companyId, 0, userId, Role.class.getName(), role.getRoleId(),
99 false, false, false);
100 }
101
102 return role;
103 }
104
105 public void addUserRoles(long userId, long[] roleIds)
106 throws SystemException {
107
108 userPersistence.addRoles(userId, roleIds);
109
110 PermissionCacheUtil.clearCache();
111 }
112
113 public void checkSystemRoles(long companyId)
114 throws PortalException, SystemException {
115
116
118 String[] systemRoles = PortalUtil.getSystemRoles();
119
120 for (int i = 0; i < systemRoles.length; i++) {
121 String roleName = systemRoles[i];
122 String roleDescription = PropsUtil.get(
123 "system.role." + StringUtil.replace(roleName, " ", ".") +
124 ".description");
125 int roleType = RoleConstants.TYPE_REGULAR;
126
127 try {
128 Role role = roleFinder.findByC_N(companyId, roleName);
129
130 if (!role.getDescription().equals(roleDescription)) {
131 role.setDescription(roleDescription);
132
133 rolePersistence.update(role, false);
134 }
135 }
136 catch (NoSuchRoleException nsre) {
137 addRole(0, companyId, roleName, roleDescription, roleType);
138 }
139 }
140
141
143 String[] systemCommunityRoles = PortalUtil.getSystemCommunityRoles();
144
145 for (int i = 0; i < systemCommunityRoles.length; i++) {
146 String roleName = systemCommunityRoles[i];
147 String roleDescription = PropsUtil.get(
148 "system.community.role." +
149 StringUtil.replace(roleName, " ", ".") + ".description");
150 int roleType = RoleConstants.TYPE_COMMUNITY;
151
152 try {
153 Role role = roleFinder.findByC_N(companyId, roleName);
154
155 if (!role.getDescription().equals(roleDescription)) {
156 role.setDescription(roleDescription);
157
158 rolePersistence.update(role, false);
159 }
160 }
161 catch (NoSuchRoleException nsre) {
162 addRole(0, companyId, roleName, roleDescription, roleType);
163 }
164 }
165
166
168 String[] systemOrganizationRoles =
169 PortalUtil.getSystemOrganizationRoles();
170
171 for (int i = 0; i < systemOrganizationRoles.length; i++) {
172 String roleName = systemOrganizationRoles[i];
173 String roleDescription = PropsUtil.get(
174 "system.organization.role." +
175 StringUtil.replace(roleName, " ", ".") + ".description");
176 int roleType = RoleConstants.TYPE_ORGANIZATION;
177
178 try {
179 Role role = roleFinder.findByC_N(companyId, roleName);
180
181 if (!role.getDescription().equals(roleDescription)) {
182 role.setDescription(roleDescription);
183
184 rolePersistence.update(role, false);
185 }
186 }
187 catch (NoSuchRoleException nsre) {
188 addRole(0, companyId, roleName, roleDescription, roleType);
189 }
190 }
191 }
192
193 public void deleteRole(long roleId)
194 throws PortalException, SystemException {
195
196 Role role = rolePersistence.findByPrimaryKey(roleId);
197
198 if (PortalUtil.isSystemRole(role.getName())) {
199 throw new RequiredRoleException();
200 }
201
202
204 if ((role.getClassNameId() <= 0) && (role.getClassPK() <= 0)) {
205 resourceLocalService.deleteResource(
206 role.getCompanyId(), Role.class.getName(),
207 ResourceConstants.SCOPE_INDIVIDUAL, role.getRoleId());
208 }
209
210 if ((role.getType() == RoleConstants.TYPE_COMMUNITY) ||
211 (role.getType() == RoleConstants.TYPE_ORGANIZATION)) {
212
213 userGroupRoleLocalService.deleteUserGroupRolesByRoleId(
214 role.getRoleId());
215 }
216
217
219 rolePersistence.remove(role);
220
221
223 PermissionCacheUtil.clearCache();
224 }
225
226 public Role getGroupRole(long companyId, long groupId)
227 throws PortalException, SystemException {
228
229 long classNameId = PortalUtil.getClassNameId(Group.class);
230
231 return rolePersistence.findByC_C_C(companyId, classNameId, groupId);
232 }
233
234 public List<Role> getGroupRoles(long groupId) throws SystemException {
235 return groupPersistence.getRoles(groupId);
236 }
237
238 public Map<String, List<String>> getResourceRoles(
239 long companyId, String name, int scope, String primKey)
240 throws SystemException {
241
242 return roleFinder.findByC_N_S_P(companyId, name, scope, primKey);
243 }
244
245 public Role getRole(long roleId) throws PortalException, SystemException {
246 return rolePersistence.findByPrimaryKey(roleId);
247 }
248
249 public Role getRole(long companyId, String name)
250 throws PortalException, SystemException {
251
252 return roleFinder.findByC_N(companyId, name);
253 }
254
255 public List<Role> getRoles(long companyId) throws SystemException {
256 return rolePersistence.findByCompanyId(companyId);
257 }
258
259 public List<Role> getRoles(long[] roleIds)
260 throws PortalException, SystemException {
261
262 List<Role> roles = new ArrayList<Role>(roleIds.length);
263
264 for (long roleId : roleIds) {
265 Role role = getRole(roleId);
266
267 roles.add(role);
268 }
269
270 return roles;
271 }
272
273 public List<Role> getUserGroupRoles(long userId, long groupId)
274 throws SystemException {
275
276 return roleFinder.findByUserGroupRole(userId, groupId);
277 }
278
279 public List<Role> getUserRelatedRoles(long userId, long groupId)
280 throws SystemException {
281
282 return roleFinder.findByU_G(userId, groupId);
283 }
284
285 public List<Role> getUserRelatedRoles(long userId, long[] groupIds)
286 throws SystemException {
287
288 return roleFinder.findByU_G(userId, groupIds);
289 }
290
291 public List<Role> getUserRelatedRoles(long userId, List<Group> groups)
292 throws SystemException {
293
294 return roleFinder.findByU_G(userId, groups);
295 }
296
297 public List<Role> getUserRoles(long userId) throws SystemException {
298 return userPersistence.getRoles(userId);
299 }
300
301 public boolean hasUserRole(long userId, long roleId)
302 throws SystemException {
303
304 return userPersistence.containsRole(userId, roleId);
305 }
306
307
317 public boolean hasUserRole(
318 long userId, long companyId, String name, boolean inherited)
319 throws PortalException, SystemException {
320
321 Role role = roleFinder.findByC_N(companyId, name);
322
323 if (inherited) {
324 if (roleFinder.countByR_U(role.getRoleId(), userId) > 0) {
325 return true;
326 }
327 else {
328 return false;
329 }
330 }
331 else {
332 return userPersistence.containsRole(userId, role.getRoleId());
333 }
334 }
335
336
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 userPersistence.setRoles(userId, roleIds);
401
402 User user = userLocalService.getUser(userId);
403
404 Role role = roleLocalService.getRole(
405 user.getCompanyId(), RoleConstants.USER);
406
407 if (!ArrayUtil.contains(roleIds, role.getRoleId())) {
408 userPersistence.addRole(userId, role.getRoleId());
409 }
410
411 PermissionCacheUtil.clearCache();
412 }
413
414 public void unsetUserRoles(long userId, long[] roleIds)
415 throws SystemException {
416
417 userPersistence.removeRoles(userId, roleIds);
418
419 PermissionCacheUtil.clearCache();
420 }
421
422 public Role updateRole(
423 long roleId, String name, Map<Locale, String> localeTitlesMap,
424 String description, String subtype)
425 throws PortalException, SystemException {
426
427 Role role = rolePersistence.findByPrimaryKey(roleId);
428
429 validate(roleId, role.getCompanyId(), name);
430
431 if (PortalUtil.isSystemRole(role.getName())) {
432 name = role.getName();
433 subtype = null;
434 }
435
436 role.setName(name);
437 role.setDescription(description);
438 role.setSubtype(subtype);
439
440 setLocalizedAttributes(role, localeTitlesMap);
441
442 rolePersistence.update(role, false);
443
444 return role;
445 }
446
447 protected void setLocalizedAttributes(
448 Role role, Map<Locale, String> localeTitlesMap) {
449
450 if (localeTitlesMap == null) {
451 return;
452 }
453
454 ClassLoader portalClassLoader = PortalClassLoaderUtil.getClassLoader();
455
456 Thread currentThread = Thread.currentThread();
457
458 ClassLoader contextClassLoader = currentThread.getContextClassLoader();
459
460 try {
461 if (contextClassLoader != portalClassLoader) {
462 currentThread.setContextClassLoader(portalClassLoader);
463 }
464
465 Locale[] locales = LanguageUtil.getAvailableLocales();
466
467 for (Locale locale : locales) {
468 String title = localeTitlesMap.get(locale);
469
470 role.setTitle(title, locale);
471 }
472 }
473 finally {
474 if (contextClassLoader != portalClassLoader) {
475 currentThread.setContextClassLoader(contextClassLoader);
476 }
477 }
478 }
479
480 protected void validate(long roleId, long companyId, String name)
481 throws PortalException, SystemException {
482
483 if ((Validator.isNull(name)) || (Validator.isNumber(name)) ||
484 (name.indexOf(StringPool.COMMA) != -1) ||
485 (name.indexOf(StringPool.STAR) != -1)) {
486
487 throw new RoleNameException();
488 }
489
490 try {
491 Role role = roleFinder.findByC_N(companyId, name);
492
493 if (role.getRoleId() != roleId) {
494 throw new DuplicateRoleException();
495 }
496 }
497 catch (NoSuchRoleException nsge) {
498 }
499 }
500
501 }