1
22
23 package com.liferay.portal.service.impl;
24
25 import com.liferay.counter.service.CounterLocalServiceUtil;
26 import com.liferay.portal.NoSuchResourceException;
27 import com.liferay.portal.PortalException;
28 import com.liferay.portal.ResourceActionsException;
29 import com.liferay.portal.SystemException;
30 import com.liferay.portal.model.Group;
31 import com.liferay.portal.model.Permission;
32 import com.liferay.portal.model.Resource;
33 import com.liferay.portal.model.ResourceCode;
34 import com.liferay.portal.model.impl.GroupImpl;
35 import com.liferay.portal.model.impl.ResourceImpl;
36 import com.liferay.portal.security.permission.ResourceActionsUtil;
37 import com.liferay.portal.service.GroupLocalServiceUtil;
38 import com.liferay.portal.service.PermissionLocalServiceUtil;
39 import com.liferay.portal.service.ResourceCodeLocalServiceUtil;
40 import com.liferay.portal.service.UserLocalServiceUtil;
41 import com.liferay.portal.service.base.ResourceLocalServiceBaseImpl;
42 import com.liferay.portal.service.persistence.GroupUtil;
43 import com.liferay.portal.service.persistence.OrgGroupPermissionUtil;
44 import com.liferay.portal.service.persistence.PermissionUtil;
45 import com.liferay.portal.service.persistence.ResourceFinder;
46 import com.liferay.portal.service.persistence.ResourceUtil;
47 import com.liferay.portal.service.persistence.UserUtil;
48 import com.liferay.portal.util.comparator.ResourceComparator;
49
50 import java.util.Iterator;
51 import java.util.List;
52
53 import org.apache.commons.lang.time.StopWatch;
54 import org.apache.commons.logging.Log;
55 import org.apache.commons.logging.LogFactory;
56
57
64 public class ResourceLocalServiceImpl extends ResourceLocalServiceBaseImpl {
65
66 public void addModelResources(
67 long companyId, long groupId, long userId, String name,
68 long primKey, String[] communityPermissions,
69 String[] guestPermissions)
70 throws PortalException, SystemException {
71
72 addModelResources(
73 companyId, groupId, userId, name, String.valueOf(primKey),
74 communityPermissions, guestPermissions);
75 }
76
77 public void addModelResources(
78 long companyId, long groupId, long userId, String name,
79 String primKey, String[] communityPermissions,
80 String[] guestPermissions)
81 throws PortalException, SystemException {
82
83 validate(companyId, name, false);
84
85
87 addResource(
88 companyId, name, ResourceImpl.SCOPE_COMPANY,
89 String.valueOf(companyId));
90
91
93 Group guestGroup = GroupLocalServiceUtil.getGroup(
94 companyId, GroupImpl.GUEST);
95
96 addResource(
97 companyId, name, ResourceImpl.SCOPE_GROUP,
98 String.valueOf(guestGroup.getGroupId()));
99
100
102 if ((groupId > 0) && (guestGroup.getGroupId() != groupId)) {
103 addResource(
104 companyId, name, ResourceImpl.SCOPE_GROUP,
105 String.valueOf(groupId));
106 }
107
108 if (primKey != null) {
109
110
112 Resource resource = addResource(
113 companyId, name, ResourceImpl.SCOPE_INDIVIDUAL, primKey);
114
115
117 List permissions = PermissionLocalServiceUtil.addPermissions(
118 companyId, name, resource.getResourceId(), false);
119
120
122 if (userId > 0) {
123 UserUtil.addPermissions(userId, permissions);
124 }
125
126
128 if ((groupId > 0) && (communityPermissions != null)) {
129 addModelPermissions(
130 groupId, resource.getResourceId(), communityPermissions);
131 }
132
133
135 if (guestPermissions != null) {
136 long defaultUserId = UserLocalServiceUtil.getDefaultUserId(
137 companyId);
138
139 List guestPermissionsList =
140 PermissionLocalServiceUtil.getPermissions(
141 companyId, guestPermissions, resource.getResourceId());
142
143 UserUtil.addPermissions(defaultUserId, guestPermissionsList);
144 }
145 }
146 }
147
148 public Resource addResource(
149 long companyId, String name, int scope, String primKey)
150 throws PortalException, SystemException {
151
152 ResourceCode resourceCode =
153 ResourceCodeLocalServiceUtil.getResourceCode(
154 companyId, name, scope);
155
156 Resource resource = ResourceUtil.fetchByC_P(
157 resourceCode.getCodeId(), primKey);
158
159 if (resource == null) {
160 long resourceId = CounterLocalServiceUtil.increment(
161 Resource.class.getName());
162
163 resource = ResourceUtil.create(resourceId);
164
165 resource.setCodeId(resourceCode.getCodeId());
166 resource.setPrimKey(primKey);
167
168 ResourceUtil.update(resource);
169 }
170
171 return resource;
172 }
173
174 public void addResources(
175 long companyId, long groupId, String name, boolean portletActions)
176 throws PortalException, SystemException {
177
178 addResources(
179 companyId, groupId, 0, name, null, portletActions, false, false);
180 }
181
182 public void addResources(
183 long companyId, long groupId, long userId, String name,
184 long primKey, boolean portletActions,
185 boolean addCommunityPermissions, boolean addGuestPermissions)
186 throws PortalException, SystemException {
187
188 addResources(
189 companyId, groupId, userId, name, String.valueOf(primKey),
190 portletActions, addCommunityPermissions, addGuestPermissions);
191 }
192
193 public void addResources(
194 long companyId, long groupId, long userId, String name,
195 String primKey, boolean portletActions,
196 boolean addCommunityPermissions, boolean addGuestPermissions)
197 throws PortalException, SystemException {
198
199 StopWatch stopWatch = null;
200
201 if (_log.isDebugEnabled()) {
202 stopWatch = new StopWatch();
203
204 stopWatch.start();
205 }
206
207 validate(companyId, name, portletActions);
208
209 logAddResources(name, primKey, stopWatch, 1);
210
211
213 addResource(
214 companyId, name, ResourceImpl.SCOPE_COMPANY,
215 String.valueOf(companyId));
216
217 logAddResources(name, primKey, stopWatch, 2);
218
219 if (groupId > 0) {
220 addResource(
221 companyId, name, ResourceImpl.SCOPE_GROUP,
222 String.valueOf(groupId));
223 }
224
225 logAddResources(name, primKey, stopWatch, 3);
226
227 if (primKey != null) {
228
229
231 Resource resource = addResource(
232 companyId, name, ResourceImpl.SCOPE_INDIVIDUAL, primKey);
233
234 logAddResources(name, primKey, stopWatch, 4);
235
236
238 List permissions = PermissionLocalServiceUtil.addPermissions(
239 companyId, name, resource.getResourceId(), portletActions);
240
241 logAddResources(name, primKey, stopWatch, 5);
242
243
245 if (userId > 0) {
246 UserUtil.addPermissions(userId, permissions);
247 }
248
249 logAddResources(name, primKey, stopWatch, 6);
250
251
253 if ((groupId > 0) && addCommunityPermissions) {
254 addCommunityPermissions(
255 groupId, name, resource.getResourceId(), portletActions);
256 }
257
258 logAddResources(name, primKey, stopWatch, 7);
259
260
262 if (addGuestPermissions) {
263
264
268 addGuestPermissions(
269 companyId, name, resource.getResourceId(), portletActions);
270 }
271
272 logAddResources(name, primKey, stopWatch, 9);
273 }
274 }
275
276 public void deleteResource(long resourceId)
277 throws PortalException, SystemException {
278
279 try {
280 Resource resource = ResourceUtil.findByPrimaryKey(resourceId);
281
282 deleteResource(resource);
283 }
284 catch (NoSuchResourceException nsre) {
285 _log.warn(nsre);
286 }
287 }
288
289 public void deleteResource(Resource resource)
290 throws PortalException, SystemException {
291
292
294 Iterator itr = PermissionUtil.findByResourceId(
295 resource.getResourceId()).iterator();
296
297 while (itr.hasNext()) {
298 Permission permission = (Permission)itr.next();
299
300 OrgGroupPermissionUtil.removeByPermissionId(
301 permission.getPermissionId());
302 }
303
304 PermissionUtil.removeByResourceId(resource.getResourceId());
305
306
308 ResourceUtil.remove(resource.getResourceId());
309 }
310
311 public void deleteResource(
312 long companyId, String name, int scope, long primKey)
313 throws PortalException, SystemException {
314
315 deleteResource(companyId, name, scope, String.valueOf(primKey));
316 }
317
318 public void deleteResource(
319 long companyId, String name, int scope, String primKey)
320 throws PortalException, SystemException {
321
322 try {
323 Resource resource = getResource(companyId, name, scope, primKey);
324
325 deleteResource(resource.getResourceId());
326 }
327 catch (NoSuchResourceException nsre) {
328 _log.warn(nsre);
329 }
330 }
331
332 public void deleteResources(String name)
333 throws PortalException, SystemException {
334
335 Iterator itr = ResourceFinder.findByName(name).iterator();
336
337 while (itr.hasNext()) {
338 Resource resource = (Resource)itr.next();
339
340 deleteResource(resource);
341 }
342 }
343
344 public long getLatestResourceId()
345 throws PortalException, SystemException {
346
347 List list = ResourceUtil.findAll(0, 1, new ResourceComparator());
348
349 if (list.size() == 0) {
350 return 0;
351 }
352 else {
353 Resource resource = (Resource)list.get(0);
354
355 return resource.getResourceId();
356 }
357 }
358
359 public Resource getResource(long resourceId)
360 throws PortalException, SystemException {
361
362 return ResourceUtil.findByPrimaryKey(resourceId);
363 }
364
365 public List getResources() throws SystemException {
366 return ResourceUtil.findAll();
367 }
368
369 public Resource getResource(
370 long companyId, String name, int scope, String primKey)
371 throws PortalException, SystemException {
372
373 ResourceCode resourceCode =
374 ResourceCodeLocalServiceUtil.getResourceCode(
375 companyId, name, scope);
376
377 return ResourceUtil.findByC_P(resourceCode.getCodeId(), primKey);
378 }
379
380 protected void addCommunityPermissions(
381 long groupId, String name, long resourceId, boolean portletActions)
382 throws PortalException, SystemException {
383
384 StopWatch stopWatch = null;
385
386 if (_log.isDebugEnabled()) {
387 stopWatch = new StopWatch();
388
389 stopWatch.start();
390 }
391
392 Group group = GroupUtil.findByPrimaryKey(groupId);
393
394 logAddCommunityPermissions(groupId, name, resourceId, stopWatch, 1);
395
396 List actions = null;
397
398 if (portletActions) {
399 actions =
400 ResourceActionsUtil.getPortletResourceCommunityDefaultActions(
401 name);
402 }
403 else {
404 actions =
405 ResourceActionsUtil.getModelResourceCommunityDefaultActions(
406 name);
407 }
408
409 logAddCommunityPermissions(groupId, name, resourceId, stopWatch, 2);
410
411 String[] actionIds = (String[])actions.toArray(new String[0]);
412
413 List permissions = PermissionLocalServiceUtil.getPermissions(
414 group.getCompanyId(), actionIds, resourceId);
415
416 logAddCommunityPermissions(groupId, name, resourceId, stopWatch, 3);
417
418 GroupUtil.addPermissions(groupId, permissions);
419
420 logAddCommunityPermissions(groupId, name, resourceId, stopWatch, 4);
421 }
422
423 protected void addGuestPermissions(
424 long companyId, String name, long resourceId,
425 boolean portletActions)
426 throws PortalException, SystemException {
427
428 long defaultUserId = UserLocalServiceUtil.getDefaultUserId(companyId);
429
430 List actions = null;
431
432 if (portletActions) {
433 actions =
434 ResourceActionsUtil.getPortletResourceGuestDefaultActions(name);
435 }
436 else {
437 actions =
438 ResourceActionsUtil.getModelResourceGuestDefaultActions(name);
439 }
440
441 String[] actionIds = (String[])actions.toArray(new String[0]);
442
443 List permissions = PermissionLocalServiceUtil.getPermissions(
444 companyId, actionIds, resourceId);
445
446 UserUtil.addPermissions(defaultUserId, permissions);
447 }
448
449 protected void addModelPermissions(
450 long groupId, long resourceId, String[] actionIds)
451 throws PortalException, SystemException {
452
453 Group group = GroupUtil.findByPrimaryKey(groupId);
454
455 List permissions = PermissionLocalServiceUtil.getPermissions(
456 group.getCompanyId(), actionIds, resourceId);
457
458 GroupUtil.addPermissions(groupId, permissions);
459 }
460
461 protected void logAddCommunityPermissions(
462 long groupId, String name, long resourceId, StopWatch stopWatch,
463 int block) {
464
465 if (!_log.isDebugEnabled()) {
466 return;
467 }
468
469 _log.debug(
470 "Adding community permissions block " + block + " for " + groupId +
471 " " + name + " " + resourceId + " takes " +
472 stopWatch.getTime() + " ms");
473 }
474
475 protected void logAddResources(
476 String name, String primKey, StopWatch stopWatch, int block) {
477
478 if (!_log.isDebugEnabled()) {
479 return;
480 }
481
482 _log.debug(
483 "Adding resources block " + block + " for " + name + " " + primKey +
484 " takes " + stopWatch.getTime() + " ms");
485 }
486
487 protected void validate(
488 long companyId, String name, boolean portletActions)
489 throws PortalException, SystemException {
490
491 List actions = null;
492
493 if (portletActions) {
494 actions =
495 ResourceActionsUtil.getPortletResourceActions(companyId, name);
496 }
497 else {
498 actions = ResourceActionsUtil.getModelResourceActions(name);
499 }
500
501 if (actions.size() == 0) {
502 throw new ResourceActionsException(
503 "There are no actions associated with the resource " + name);
504 }
505 }
506
507 private static Log _log = LogFactory.getLog(ResourceLocalServiceImpl.class);
508
509 }