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