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