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