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