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.util.GetterUtil;
34 import com.liferay.portal.kernel.util.OrderByComparator;
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.security.permission.PermissionCacheUtil;
43 import com.liferay.portal.service.base.RoleLocalServiceBaseImpl;
44 import com.liferay.portal.util.PortalUtil;
45 import com.liferay.portal.util.PropsUtil;
46
47 import java.util.HashMap;
48 import java.util.LinkedHashMap;
49 import java.util.List;
50 import java.util.Map;
51
52
57 public class RoleLocalServiceImpl extends RoleLocalServiceBaseImpl {
58
59 public Role addRole(
60 long userId, long companyId, String name, String description,
61 int type)
62 throws PortalException, SystemException {
63
64 return addRole(userId, companyId, name, description, type, null, 0);
65 }
66
67 public Role addRole(
68 long userId, long companyId, String name, String description,
69 int type, String className, long classPK)
70 throws PortalException, SystemException {
71
72
74 className = GetterUtil.getString(className);
75 long classNameId = PortalUtil.getClassNameId(className);
76
77 validate(0, companyId, name);
78
79 long roleId = counterLocalService.increment();
80
81 if ((classNameId <= 0) || className.equals(Role.class.getName())) {
82 classNameId = PortalUtil.getClassNameId(Role.class);
83 classPK = roleId;
84 }
85
86 Role role = rolePersistence.create(roleId);
87
88 role.setCompanyId(companyId);
89 role.setClassNameId(classNameId);
90 role.setClassPK(classPK);
91 role.setName(name);
92 role.setDescription(description);
93 role.setType(type);
94
95 rolePersistence.update(role, false);
96
97
99 if (userId > 0) {
100 resourceLocalService.addResources(
101 companyId, 0, userId, Role.class.getName(), role.getRoleId(),
102 false, false, false);
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 PermissionCacheUtil.clearCache();
114 }
115
116 @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
117 public void checkSystemRoles(long companyId)
118 throws PortalException, SystemException {
119
120 for (Role role : roleFinder.findBySystem(companyId)) {
121 _systemRolesMap.put(companyId + role.getName(), role);
122 }
123
124
126 String[] systemRoles = PortalUtil.getSystemRoles();
127
128 for (String name : systemRoles) {
129 String description = PropsUtil.get(
130 "system.role." + StringUtil.replace(name, " ", ".") +
131 ".description");
132 int type = RoleConstants.TYPE_REGULAR;
133
134 checkSystemRole(companyId, name, description, type);
135 }
136
137
139 String[] systemCommunityRoles = PortalUtil.getSystemCommunityRoles();
140
141 for (String name : systemCommunityRoles) {
142 String description = PropsUtil.get(
143 "system.community.role." +
144 StringUtil.replace(name, " ", ".") + ".description");
145 int type = RoleConstants.TYPE_COMMUNITY;
146
147 checkSystemRole(companyId, name, description, type);
148 }
149
150
152 String[] systemOrganizationRoles =
153 PortalUtil.getSystemOrganizationRoles();
154
155 for (String name : systemOrganizationRoles) {
156 String description = PropsUtil.get(
157 "system.organization.role." +
158 StringUtil.replace(name, " ", ".") + ".description");
159 int type = RoleConstants.TYPE_ORGANIZATION;
160
161 checkSystemRole(companyId, name, description, type);
162 }
163 }
164
165 public void deleteRole(long roleId)
166 throws PortalException, SystemException {
167
168 Role role = rolePersistence.findByPrimaryKey(roleId);
169
170 if (PortalUtil.isSystemRole(role.getName())) {
171 throw new RequiredRoleException();
172 }
173
174
176 String className = role.getClassName();
177 long classNameId = role.getClassNameId();
178
179 if ((classNameId <= 0) || className.equals(Role.class.getName())) {
180 resourceLocalService.deleteResource(
181 role.getCompanyId(), Role.class.getName(),
182 ResourceConstants.SCOPE_INDIVIDUAL, role.getRoleId());
183 }
184
185 if ((role.getType() == RoleConstants.TYPE_COMMUNITY) ||
186 (role.getType() == RoleConstants.TYPE_ORGANIZATION)) {
187
188 userGroupRoleLocalService.deleteUserGroupRolesByRoleId(
189 role.getRoleId());
190 }
191
192
194 rolePersistence.remove(role);
195
196
198 PermissionCacheUtil.clearCache();
199 }
200
201 public Role getGroupRole(long companyId, long groupId)
202 throws PortalException, SystemException {
203
204 long classNameId = PortalUtil.getClassNameId(Group.class);
205
206 return rolePersistence.findByC_C_C(companyId, classNameId, groupId);
207 }
208
209 public List<Role> getGroupRoles(long groupId) throws SystemException {
210 return groupPersistence.getRoles(groupId);
211 }
212
213 public Map<String, List<String>> getResourceRoles(
214 long companyId, String name, int scope, String primKey)
215 throws SystemException {
216
217 return roleFinder.findByC_N_S_P(companyId, name, scope, primKey);
218 }
219
220 public Role getRole(long roleId) throws PortalException, SystemException {
221 return rolePersistence.findByPrimaryKey(roleId);
222 }
223
224 public Role getRole(long companyId, String name)
225 throws PortalException, SystemException {
226
227 Role role = _systemRolesMap.get(companyId + name);
228
229 if (role != null) {
230 return role;
231 }
232
233 return rolePersistence.findByC_N(companyId, name);
234 }
235
236 public List<Role> getRoles(long companyId) throws SystemException {
237 return rolePersistence.findByCompanyId(companyId);
238 }
239
240 public List<Role> getUserGroupRoles(long userId, long groupId)
241 throws SystemException {
242
243 return roleFinder.findByUserGroupRole(userId, groupId);
244 }
245
246 public List<Role> getUserRelatedRoles(long userId, long groupId)
247 throws SystemException {
248
249 return roleFinder.findByU_G(userId, groupId);
250 }
251
252 public List<Role> getUserRelatedRoles(long userId, long[] groupIds)
253 throws SystemException {
254
255 return roleFinder.findByU_G(userId, groupIds);
256 }
257
258 public List<Role> getUserRelatedRoles(long userId, List<Group> groups)
259 throws SystemException {
260
261 return roleFinder.findByU_G(userId, groups);
262 }
263
264 public List<Role> getUserRoles(long userId) throws SystemException {
265 return userPersistence.getRoles(userId);
266 }
267
268 public boolean hasUserRole(long userId, long roleId)
269 throws SystemException {
270
271 return userPersistence.containsRole(userId, roleId);
272 }
273
274
279 public boolean hasUserRole(
280 long userId, long companyId, String name, boolean inherited)
281 throws PortalException, SystemException {
282
283 Role role = rolePersistence.findByC_N(companyId, name);
284
285 if (inherited) {
286 if (roleFinder.countByR_U(role.getRoleId(), userId) > 0) {
287 return true;
288 }
289 else {
290 return false;
291 }
292 }
293 else {
294 return userPersistence.containsRole(userId, role.getRoleId());
295 }
296 }
297
298
303 public boolean hasUserRoles(
304 long userId, long companyId, String[] names, boolean inherited)
305 throws PortalException, SystemException {
306
307 for (int i = 0; i < names.length; i++) {
308 if (hasUserRole(userId, companyId, names[i], inherited)) {
309 return true;
310 }
311 }
312
313 return false;
314 }
315
316 public List<Role> search(
317 long companyId, String name, String description, Integer type,
318 int start, int end, OrderByComparator obc)
319 throws SystemException {
320
321 return search(
322 companyId, name, description, type,
323 new LinkedHashMap<String, Object>(), start, end, obc);
324 }
325
326 public List<Role> search(
327 long companyId, String name, String description, Integer type,
328 LinkedHashMap<String, Object> params, int start, int end,
329 OrderByComparator obc)
330 throws SystemException {
331
332 return roleFinder.findByC_N_D_T(
333 companyId, name, description, type, params, start, end, obc);
334 }
335
336 public int searchCount(
337 long companyId, String name, String description, Integer type)
338 throws SystemException {
339
340 return searchCount(
341 companyId, name, description, type,
342 new LinkedHashMap<String, Object>());
343 }
344
345 public int searchCount(
346 long companyId, String name, String description, Integer type,
347 LinkedHashMap<String, Object> params)
348 throws SystemException {
349
350 return roleFinder.countByC_N_D_T(
351 companyId, name, description, type, params);
352 }
353
354 public void setUserRoles(long userId, long[] roleIds)
355 throws SystemException {
356
357 userPersistence.setRoles(userId, roleIds);
358
359 PermissionCacheUtil.clearCache();
360 }
361
362 public void unsetUserRoles(long userId, long[] roleIds)
363 throws SystemException {
364
365 userPersistence.removeRoles(userId, roleIds);
366
367 PermissionCacheUtil.clearCache();
368 }
369
370 public Role updateRole(long roleId, String name, String description)
371 throws PortalException, SystemException {
372
373 Role role = rolePersistence.findByPrimaryKey(roleId);
374
375 validate(roleId, role.getCompanyId(), name);
376
377 if (PortalUtil.isSystemRole(role.getName())) {
378 throw new RequiredRoleException();
379 }
380
381 role.setName(name);
382 role.setDescription(description);
383
384 rolePersistence.update(role, false);
385
386 return role;
387 }
388
389 protected void checkSystemRole(
390 long companyId, String name, String description, int type)
391 throws PortalException, SystemException {
392
393 Role role = _systemRolesMap.get(companyId + name);
394
395 try {
396 if (role == null) {
397 role = rolePersistence.findByC_N(companyId, name);
398 }
399
400 if (!role.getDescription().equals(description)) {
401 role.setDescription(description);
402
403 roleLocalService.updateRole(role, false);
404 }
405 }
406 catch (NoSuchRoleException nsre) {
407 role = roleLocalService.addRole(
408 0, companyId, name, description, type);
409 }
410
411 _systemRolesMap.put(companyId + name, role);
412 }
413
414 protected void validate(long roleId, long companyId, String name)
415 throws PortalException, SystemException {
416
417 if ((Validator.isNull(name)) || (Validator.isNumber(name)) ||
418 (name.indexOf(StringPool.COMMA) != -1) ||
419 (name.indexOf(StringPool.STAR) != -1)) {
420
421 throw new RoleNameException();
422 }
423
424 try {
425 Role role = roleFinder.findByC_N(companyId, name);
426
427 if (role.getRoleId() != roleId) {
428 throw new DuplicateRoleException();
429 }
430 }
431 catch (NoSuchRoleException nsge) {
432 }
433 }
434
435 private Map<String, Role> _systemRolesMap = new HashMap<String, Role>();
436
437 }