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 long defaultUserId = UserLocalServiceUtil.getDefaultUserId(
123 companyId);
124
125 if ((userId > 0) && (userId != defaultUserId)) {
126 UserUtil.addPermissions(userId, permissions);
127 }
128
129
131 if ((groupId > 0) && (communityPermissions != null)) {
132 addModelPermissions(
133 groupId, resource.getResourceId(), communityPermissions);
134 }
135
136
138 if (guestPermissions != null) {
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 long defaultUserId = UserLocalServiceUtil.getDefaultUserId(
246 companyId);
247
248 if ((userId > 0) && (userId != defaultUserId)) {
249 UserUtil.addPermissions(userId, permissions);
250 }
251
252 logAddResources(name, primKey, stopWatch, 6);
253
254
256 if ((groupId > 0) && addCommunityPermissions) {
257 addCommunityPermissions(
258 groupId, name, resource.getResourceId(), portletActions);
259 }
260
261 logAddResources(name, primKey, stopWatch, 7);
262
263
265 if (addGuestPermissions) {
266
267
271 addGuestPermissions(
272 companyId, name, resource.getResourceId(), portletActions);
273 }
274
275 logAddResources(name, primKey, stopWatch, 9);
276 }
277 }
278
279 public void deleteResource(long resourceId)
280 throws PortalException, SystemException {
281
282 try {
283 Resource resource = ResourceUtil.findByPrimaryKey(resourceId);
284
285 deleteResource(resource);
286 }
287 catch (NoSuchResourceException nsre) {
288 _log.warn(nsre);
289 }
290 }
291
292 public void deleteResource(Resource resource)
293 throws PortalException, SystemException {
294
295
297 Iterator itr = PermissionUtil.findByResourceId(
298 resource.getResourceId()).iterator();
299
300 while (itr.hasNext()) {
301 Permission permission = (Permission)itr.next();
302
303 OrgGroupPermissionUtil.removeByPermissionId(
304 permission.getPermissionId());
305 }
306
307 PermissionUtil.removeByResourceId(resource.getResourceId());
308
309
311 ResourceUtil.remove(resource.getResourceId());
312 }
313
314 public void deleteResource(
315 long companyId, String name, int scope, long primKey)
316 throws PortalException, SystemException {
317
318 deleteResource(companyId, name, scope, String.valueOf(primKey));
319 }
320
321 public void deleteResource(
322 long companyId, String name, int scope, String primKey)
323 throws PortalException, SystemException {
324
325 try {
326 Resource resource = getResource(companyId, name, scope, primKey);
327
328 deleteResource(resource.getResourceId());
329 }
330 catch (NoSuchResourceException nsre) {
331 _log.warn(nsre);
332 }
333 }
334
335 public void deleteResources(String name)
336 throws PortalException, SystemException {
337
338 Iterator itr = ResourceFinder.findByName(name).iterator();
339
340 while (itr.hasNext()) {
341 Resource resource = (Resource)itr.next();
342
343 deleteResource(resource);
344 }
345 }
346
347 public long getLatestResourceId()
348 throws PortalException, SystemException {
349
350 List list = ResourceUtil.findAll(0, 1, new ResourceComparator());
351
352 if (list.size() == 0) {
353 return 0;
354 }
355 else {
356 Resource resource = (Resource)list.get(0);
357
358 return resource.getResourceId();
359 }
360 }
361
362 public Resource getResource(long resourceId)
363 throws PortalException, SystemException {
364
365 return ResourceUtil.findByPrimaryKey(resourceId);
366 }
367
368 public List getResources() throws SystemException {
369 return ResourceUtil.findAll();
370 }
371
372 public Resource getResource(
373 long companyId, String name, int scope, String primKey)
374 throws PortalException, SystemException {
375
376 ResourceCode resourceCode =
377 ResourceCodeLocalServiceUtil.getResourceCode(
378 companyId, name, scope);
379
380 return ResourceUtil.findByC_P(resourceCode.getCodeId(), primKey);
381 }
382
383 protected void addCommunityPermissions(
384 long groupId, String name, long resourceId, boolean portletActions)
385 throws PortalException, SystemException {
386
387 StopWatch stopWatch = null;
388
389 if (_log.isDebugEnabled()) {
390 stopWatch = new StopWatch();
391
392 stopWatch.start();
393 }
394
395 Group group = GroupUtil.findByPrimaryKey(groupId);
396
397 logAddCommunityPermissions(groupId, name, resourceId, stopWatch, 1);
398
399 List actions = null;
400
401 if (portletActions) {
402 actions =
403 ResourceActionsUtil.getPortletResourceCommunityDefaultActions(
404 name);
405 }
406 else {
407 actions =
408 ResourceActionsUtil.getModelResourceCommunityDefaultActions(
409 name);
410 }
411
412 logAddCommunityPermissions(groupId, name, resourceId, stopWatch, 2);
413
414 String[] actionIds = (String[])actions.toArray(new String[0]);
415
416 List permissions = PermissionLocalServiceUtil.getPermissions(
417 group.getCompanyId(), actionIds, resourceId);
418
419 logAddCommunityPermissions(groupId, name, resourceId, stopWatch, 3);
420
421 GroupUtil.addPermissions(groupId, permissions);
422
423 logAddCommunityPermissions(groupId, name, resourceId, stopWatch, 4);
424 }
425
426 protected void addGuestPermissions(
427 long companyId, String name, long resourceId,
428 boolean portletActions)
429 throws PortalException, SystemException {
430
431 long defaultUserId = UserLocalServiceUtil.getDefaultUserId(companyId);
432
433 List actions = null;
434
435 if (portletActions) {
436 actions =
437 ResourceActionsUtil.getPortletResourceGuestDefaultActions(name);
438 }
439 else {
440 actions =
441 ResourceActionsUtil.getModelResourceGuestDefaultActions(name);
442 }
443
444 String[] actionIds = (String[])actions.toArray(new String[0]);
445
446 List permissions = PermissionLocalServiceUtil.getPermissions(
447 companyId, actionIds, resourceId);
448
449 UserUtil.addPermissions(defaultUserId, permissions);
450 }
451
452 protected void addModelPermissions(
453 long groupId, long resourceId, String[] actionIds)
454 throws PortalException, SystemException {
455
456 Group group = GroupUtil.findByPrimaryKey(groupId);
457
458 List permissions = PermissionLocalServiceUtil.getPermissions(
459 group.getCompanyId(), actionIds, resourceId);
460
461 GroupUtil.addPermissions(groupId, permissions);
462 }
463
464 protected void logAddCommunityPermissions(
465 long groupId, String name, long resourceId, StopWatch stopWatch,
466 int block) {
467
468 if (!_log.isDebugEnabled()) {
469 return;
470 }
471
472 _log.debug(
473 "Adding community permissions block " + block + " for " + groupId +
474 " " + name + " " + resourceId + " takes " +
475 stopWatch.getTime() + " ms");
476 }
477
478 protected void logAddResources(
479 String name, String primKey, StopWatch stopWatch, int block) {
480
481 if (!_log.isDebugEnabled()) {
482 return;
483 }
484
485 _log.debug(
486 "Adding resources block " + block + " for " + name + " " + primKey +
487 " takes " + stopWatch.getTime() + " ms");
488 }
489
490 protected void validate(
491 long companyId, String name, boolean portletActions)
492 throws PortalException, SystemException {
493
494 List actions = null;
495
496 if (portletActions) {
497 actions =
498 ResourceActionsUtil.getPortletResourceActions(companyId, name);
499 }
500 else {
501 actions = ResourceActionsUtil.getModelResourceActions(name);
502 }
503
504 if (actions.size() == 0) {
505 throw new ResourceActionsException(
506 "There are no actions associated with the resource " + name);
507 }
508 }
509
510 private static Log _log = LogFactory.getLog(ResourceLocalServiceImpl.class);
511
512 }