1
22
23 package com.liferay.portal.service.impl;
24
25 import com.liferay.counter.service.CounterLocalServiceUtil;
26 import com.liferay.portal.DuplicateRoleException;
27 import com.liferay.portal.NoSuchRoleException;
28 import com.liferay.portal.PortalException;
29 import com.liferay.portal.RequiredRoleException;
30 import com.liferay.portal.RoleNameException;
31 import com.liferay.portal.SystemException;
32 import com.liferay.portal.kernel.security.permission.ActionKeys;
33 import com.liferay.portal.kernel.util.StringPool;
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.GroupImpl;
38 import com.liferay.portal.model.impl.ResourceImpl;
39 import com.liferay.portal.model.impl.RoleImpl;
40 import com.liferay.portal.security.permission.PermissionCacheUtil;
41 import com.liferay.portal.security.permission.ResourceActionsUtil;
42 import com.liferay.portal.service.PermissionLocalServiceUtil;
43 import com.liferay.portal.service.ResourceLocalServiceUtil;
44 import com.liferay.portal.service.UserGroupRoleLocalServiceUtil;
45 import com.liferay.portal.service.base.RoleLocalServiceBaseImpl;
46 import com.liferay.portal.service.persistence.GroupUtil;
47 import com.liferay.portal.service.persistence.RoleFinder;
48 import com.liferay.portal.service.persistence.RoleUtil;
49 import com.liferay.portal.service.persistence.UserUtil;
50 import com.liferay.portal.util.PortalUtil;
51
52 import java.util.List;
53 import java.util.Map;
54
55
61 public class RoleLocalServiceImpl extends RoleLocalServiceBaseImpl {
62
63 public Role addRole(long userId, long companyId, String name, int type)
64 throws PortalException, SystemException {
65
66 return addRole(userId, companyId, name, type, null, 0);
67 }
68
69 public Role addRole(
70 long userId, long companyId, String name, int type,
71 String className, long classPK)
72 throws PortalException, SystemException {
73
74
76 long classNameId = PortalUtil.getClassNameId(className);
77
78 validate(0, companyId, name);
79
80 long roleId = CounterLocalServiceUtil.increment();
81
82 Role role = RoleUtil.create(roleId);
83
84 role.setCompanyId(companyId);
85 role.setClassNameId(classNameId);
86 role.setClassPK(classPK);
87 role.setName(name);
88 role.setType(type);
89
90 RoleUtil.update(role);
91
92
94 if (userId > 0) {
95 ResourceLocalServiceUtil.addResources(
96 companyId, 0, userId, Role.class.getName(), role.getRoleId(),
97 false, false, false);
98 }
99
100 return role;
101 }
102
103 public void checkSystemRoles(long companyId)
104 throws PortalException, SystemException {
105
106
108 String[] systemRoles = PortalUtil.getSystemRoles();
109
110 for (int i = 0; i < systemRoles.length; i++) {
111 try {
112 RoleFinder.findByC_N(companyId, systemRoles[i]);
113 }
114 catch (NoSuchRoleException nsre) {
115 addRole(0, companyId, systemRoles[i], RoleImpl.TYPE_REGULAR);
116 }
117 }
118
119
121 String[] systemCommunityRoles = PortalUtil.getSystemCommunityRoles();
122
123 for (int i = 0; i < systemCommunityRoles.length; i++) {
124 try {
125 RoleFinder.findByC_N(companyId, systemCommunityRoles[i]);
126 }
127 catch (NoSuchRoleException nsre) {
128 Role role = addRole(
129 0, companyId, systemCommunityRoles[i],
130 RoleImpl.TYPE_COMMUNITY);
131
132 if (systemCommunityRoles[i].equals(RoleImpl.COMMUNITY_OWNER)) {
133 List actions = ResourceActionsUtil.getModelResourceActions(
134 Group.class.getName());
135
136 PermissionLocalServiceUtil.setRolePermissions(
137 role.getRoleId(), role.getCompanyId(),
138 Group.class.getName(),
139 ResourceImpl.SCOPE_GROUP_TEMPLATE,
140 String.valueOf(GroupImpl.DEFAULT_PARENT_GROUP_ID),
141 (String[])actions.toArray(new String[0]));
142 }
143 else if (systemCommunityRoles[i].equals(
144 RoleImpl.COMMUNITY_ADMINISTRATOR)) {
145
146 String[] actionIds = new String[] {
147 ActionKeys.ASSIGN_USERS, ActionKeys.MANAGE_LAYOUTS,
148 ActionKeys.UPDATE
149 };
150
151 PermissionLocalServiceUtil.setRolePermissions(
152 role.getRoleId(), role.getCompanyId(),
153 Group.class.getName(),
154 ResourceImpl.SCOPE_GROUP_TEMPLATE,
155 String.valueOf(GroupImpl.DEFAULT_PARENT_GROUP_ID),
156 actionIds);
157 }
158 }
159 }
160 }
161
162 public void deleteRole(long roleId)
163 throws PortalException, SystemException {
164
165 Role role = RoleUtil.findByPrimaryKey(roleId);
166
167 if (PortalUtil.isSystemRole(role.getName())) {
168 throw new RequiredRoleException();
169 }
170
171
173 if ((role.getClassNameId() <= 0) && (role.getClassPK() <= 0)) {
174 ResourceLocalServiceUtil.deleteResource(
175 role.getCompanyId(), Role.class.getName(),
176 ResourceImpl.SCOPE_INDIVIDUAL, role.getRoleId());
177 }
178
179 if (role.getType() == RoleImpl.TYPE_COMMUNITY) {
180 UserGroupRoleLocalServiceUtil.deleteUserGroupRolesByRoleId(
181 role.getRoleId());
182 }
183
184
186 RoleUtil.remove(roleId);
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 RoleUtil.findByC_C_C(companyId, classNameId, groupId);
199 }
200
201 public List getGroupRoles(long groupId)
202 throws PortalException, SystemException {
203
204 return GroupUtil.getRoles(groupId);
205 }
206
207 public Map getResourceRoles(
208 long companyId, String name, int scope, String primKey)
209 throws SystemException {
210
211 return RoleFinder.findByC_N_S_P(companyId, name, scope, primKey);
212 }
213
214 public Role getRole(long roleId) throws PortalException, SystemException {
215 return RoleUtil.findByPrimaryKey(roleId);
216 }
217
218 public Role getRole(long companyId, String name)
219 throws PortalException, SystemException {
220
221 return RoleFinder.findByC_N(companyId, name);
222 }
223
224 public List getUserGroupRoles(long userId, long groupId)
225 throws SystemException {
226
227 return RoleFinder.findByUserGroupRole(userId, groupId);
228 }
229
230 public List getUserRelatedRoles(long userId, long groupId)
231 throws SystemException {
232
233 return RoleFinder.findByU_G(userId, groupId);
234 }
235
236 public List getUserRelatedRoles(long userId, long[] groupIds)
237 throws SystemException {
238
239 return RoleFinder.findByU_G(userId, groupIds);
240 }
241
242 public List getUserRelatedRoles(long userId, List groups)
243 throws SystemException {
244
245 return RoleFinder.findByU_G(userId, groups);
246 }
247
248 public List getUserRoles(long userId)
249 throws PortalException, SystemException {
250
251 return UserUtil.getRoles(userId);
252 }
253
254
264 public boolean hasUserRole(
265 long userId, long companyId, String name, boolean inherited)
266 throws PortalException, SystemException {
267
268 Role role = RoleFinder.findByC_N(companyId, name);
269
270 if (inherited) {
271 if (RoleFinder.countByR_U(role.getRoleId(), userId) > 0) {
272 return true;
273 }
274 else {
275 return false;
276 }
277 }
278 else {
279 return UserUtil.containsRole(userId, role.getRoleId());
280 }
281 }
282
283
293 public boolean hasUserRoles(
294 long userId, long companyId, String[] names, boolean inherited)
295 throws PortalException, SystemException {
296
297 for (int i = 0; i < names.length; i++) {
298 if (hasUserRole(userId, companyId, names[i], inherited)) {
299 return true;
300 }
301 }
302
303 return false;
304 }
305
306 public List search(
307 long companyId, String name, String description, Integer type,
308 int begin, int end)
309 throws SystemException {
310
311 return RoleFinder.findByC_N_D_T(
312 companyId, name, description, type, begin, end);
313 }
314
315 public int searchCount(
316 long companyId, String name, String description, Integer type)
317 throws SystemException {
318
319 return RoleFinder.countByC_N_D_T(companyId, name, description, type);
320 }
321
322 public void setUserRoles(long userId, long[] roleIds)
323 throws PortalException, SystemException {
324
325 UserUtil.setRoles(userId, roleIds);
326
327 PermissionCacheUtil.clearCache();
328 }
329
330 public Role updateRole(long roleId, String name)
331 throws PortalException, SystemException {
332
333 Role role = RoleUtil.findByPrimaryKey(roleId);
334
335 validate(roleId, role.getCompanyId(), name);
336
337 if (PortalUtil.isSystemRole(role.getName())) {
338 throw new RequiredRoleException();
339 }
340
341 role.setName(name);
342
343 RoleUtil.update(role);
344
345 return role;
346 }
347
348 protected void validate(long roleId, long companyId, String name)
349 throws PortalException, SystemException {
350
351 if ((Validator.isNull(name)) || (Validator.isNumber(name)) ||
352 (name.indexOf(StringPool.COMMA) != -1) ||
353 (name.indexOf(StringPool.STAR) != -1)) {
354
355 throw new RoleNameException();
356 }
357
358 try {
359 Role role = RoleFinder.findByC_N(companyId, name);
360
361 if (role.getRoleId() != roleId) {
362 throw new DuplicateRoleException();
363 }
364 }
365 catch (NoSuchRoleException nsge) {
366 }
367 }
368
369 }