001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.service.impl;
022:
023: import com.liferay.portal.NoSuchResourceException;
024: import com.liferay.portal.PortalException;
025: import com.liferay.portal.ResourceActionsException;
026: import com.liferay.portal.SystemException;
027: import com.liferay.portal.kernel.security.permission.PermissionsListFilter;
028: import com.liferay.portal.model.Group;
029: import com.liferay.portal.model.Permission;
030: import com.liferay.portal.model.Resource;
031: import com.liferay.portal.model.ResourceCode;
032: import com.liferay.portal.model.impl.GroupImpl;
033: import com.liferay.portal.model.impl.ResourceImpl;
034: import com.liferay.portal.security.permission.PermissionsListFilterFactory;
035: import com.liferay.portal.security.permission.ResourceActionsUtil;
036: import com.liferay.portal.service.base.ResourceLocalServiceBaseImpl;
037: import com.liferay.portal.util.comparator.ResourceComparator;
038:
039: import java.util.Iterator;
040: import java.util.List;
041:
042: import org.apache.commons.lang.time.StopWatch;
043: import org.apache.commons.logging.Log;
044: import org.apache.commons.logging.LogFactory;
045:
046: /**
047: * <a href="ResourceLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
048: *
049: * @author Brian Wing Shun Chan
050: * @author Wilson S. Man
051: *
052: */
053: public class ResourceLocalServiceImpl extends
054: ResourceLocalServiceBaseImpl {
055:
056: public void addModelResources(long companyId, long groupId,
057: long userId, String name, long primKey,
058: String[] communityPermissions, String[] guestPermissions)
059: throws PortalException, SystemException {
060:
061: addModelResources(companyId, groupId, userId, name, String
062: .valueOf(primKey), communityPermissions,
063: guestPermissions);
064: }
065:
066: public void addModelResources(long companyId, long groupId,
067: long userId, String name, String primKey,
068: String[] communityPermissions, String[] guestPermissions)
069: throws PortalException, SystemException {
070:
071: validate(companyId, name, false);
072:
073: // Company
074:
075: addResource(companyId, name, ResourceImpl.SCOPE_COMPANY, String
076: .valueOf(companyId));
077:
078: // Guest
079:
080: Group guestGroup = groupLocalService.getGroup(companyId,
081: GroupImpl.GUEST);
082:
083: addResource(companyId, name, ResourceImpl.SCOPE_GROUP, String
084: .valueOf(guestGroup.getGroupId()));
085:
086: // Group
087:
088: if ((groupId > 0) && (guestGroup.getGroupId() != groupId)) {
089: addResource(companyId, name, ResourceImpl.SCOPE_GROUP,
090: String.valueOf(groupId));
091: }
092:
093: if (primKey != null) {
094:
095: // Individual
096:
097: Resource resource = addResource(companyId, name,
098: ResourceImpl.SCOPE_INDIVIDUAL, primKey);
099:
100: // Permissions
101:
102: List permissionsList = permissionLocalService
103: .addPermissions(companyId, name, resource
104: .getResourceId(), false);
105:
106: // User permissions
107:
108: PermissionsListFilter permissionsListFilter = PermissionsListFilterFactory
109: .getInstance();
110:
111: long defaultUserId = userLocalService
112: .getDefaultUserId(companyId);
113:
114: if ((userId > 0) && (userId != defaultUserId)) {
115: List userPermissionsList = permissionsListFilter
116: .filterUserPermissions(companyId, groupId,
117: userId, name, primKey, false,
118: permissionsList);
119:
120: userPersistence.addPermissions(userId,
121: userPermissionsList);
122: }
123:
124: // Community permissions
125:
126: if (groupId > 0) {
127: groupPersistence.findByPrimaryKey(groupId);
128:
129: if (communityPermissions == null) {
130: communityPermissions = new String[0];
131: }
132:
133: List communityPermissionsList = permissionLocalService
134: .getPermissions(companyId,
135: communityPermissions, resource
136: .getResourceId());
137:
138: communityPermissionsList = permissionsListFilter
139: .filterCommunityPermissions(companyId, groupId,
140: userId, name, primKey, false,
141: communityPermissionsList);
142:
143: groupPersistence.addPermissions(groupId,
144: communityPermissionsList);
145: }
146:
147: // Guest permissions
148:
149: if (guestPermissions == null) {
150: guestPermissions = new String[0];
151: }
152:
153: List guestPermissionsList = permissionLocalService
154: .getPermissions(companyId, guestPermissions,
155: resource.getResourceId());
156:
157: guestPermissionsList = permissionsListFilter
158: .filterGuestPermissions(companyId, groupId, userId,
159: name, primKey, false, guestPermissionsList);
160:
161: userPersistence.addPermissions(defaultUserId,
162: guestPermissionsList);
163: }
164: }
165:
166: public Resource addResource(long companyId, String name, int scope,
167: String primKey) throws PortalException, SystemException {
168:
169: ResourceCode resourceCode = resourceCodeLocalService
170: .getResourceCode(companyId, name, scope);
171:
172: Resource resource = resourcePersistence.fetchByC_P(resourceCode
173: .getCodeId(), primKey);
174:
175: if (resource == null) {
176: long resourceId = counterLocalService
177: .increment(Resource.class.getName());
178:
179: resource = resourcePersistence.create(resourceId);
180:
181: resource.setCodeId(resourceCode.getCodeId());
182: resource.setPrimKey(primKey);
183:
184: resourcePersistence.update(resource);
185: }
186:
187: return resource;
188: }
189:
190: public void addResources(long companyId, long groupId, String name,
191: boolean portletActions) throws PortalException,
192: SystemException {
193:
194: addResources(companyId, groupId, 0, name, null, portletActions,
195: false, false);
196: }
197:
198: public void addResources(long companyId, long groupId, long userId,
199: String name, long primKey, boolean portletActions,
200: boolean addCommunityPermissions, boolean addGuestPermissions)
201: throws PortalException, SystemException {
202:
203: addResources(companyId, groupId, userId, name, String
204: .valueOf(primKey), portletActions,
205: addCommunityPermissions, addGuestPermissions);
206: }
207:
208: public void addResources(long companyId, long groupId, long userId,
209: String name, String primKey, boolean portletActions,
210: boolean addCommunityPermissions, boolean addGuestPermissions)
211: throws PortalException, SystemException {
212:
213: StopWatch stopWatch = null;
214:
215: if (_log.isDebugEnabled()) {
216: stopWatch = new StopWatch();
217:
218: stopWatch.start();
219: }
220:
221: validate(companyId, name, portletActions);
222:
223: logAddResources(name, primKey, stopWatch, 1);
224:
225: // Company
226:
227: addResource(companyId, name, ResourceImpl.SCOPE_COMPANY, String
228: .valueOf(companyId));
229:
230: logAddResources(name, primKey, stopWatch, 2);
231:
232: if (groupId > 0) {
233: addResource(companyId, name, ResourceImpl.SCOPE_GROUP,
234: String.valueOf(groupId));
235: }
236:
237: logAddResources(name, primKey, stopWatch, 3);
238:
239: if (primKey != null) {
240:
241: // Individual
242:
243: Resource resource = addResource(companyId, name,
244: ResourceImpl.SCOPE_INDIVIDUAL, primKey);
245:
246: logAddResources(name, primKey, stopWatch, 4);
247:
248: // Permissions
249:
250: List permissionsList = permissionLocalService
251: .addPermissions(companyId, name, resource
252: .getResourceId(), portletActions);
253:
254: logAddResources(name, primKey, stopWatch, 5);
255:
256: // User permissions
257:
258: PermissionsListFilter permissionsListFilter = PermissionsListFilterFactory
259: .getInstance();
260:
261: long defaultUserId = userLocalService
262: .getDefaultUserId(companyId);
263:
264: if ((userId > 0) && (userId != defaultUserId)) {
265: List userPermissionsList = permissionsListFilter
266: .filterUserPermissions(companyId, groupId,
267: userId, name, primKey, portletActions,
268: permissionsList);
269:
270: userPersistence.addPermissions(userId,
271: userPermissionsList);
272: }
273:
274: logAddResources(name, primKey, stopWatch, 6);
275:
276: // Community permissions
277:
278: if ((groupId > 0) && addCommunityPermissions) {
279: addCommunityPermissions(companyId, groupId, userId,
280: name, resource, portletActions);
281: }
282:
283: logAddResources(name, primKey, stopWatch, 7);
284:
285: // Guest permissions
286:
287: if (addGuestPermissions) {
288:
289: // Don't add guest permissions when you've already added
290: // community permissions and the given community is the guest
291: // community.
292:
293: addGuestPermissions(companyId, groupId, userId, name,
294: resource, portletActions);
295: }
296:
297: logAddResources(name, primKey, stopWatch, 9);
298: }
299: }
300:
301: public void deleteResource(long resourceId) throws PortalException,
302: SystemException {
303:
304: try {
305: Resource resource = resourcePersistence
306: .findByPrimaryKey(resourceId);
307:
308: deleteResource(resource);
309: } catch (NoSuchResourceException nsre) {
310: _log.warn(nsre);
311: }
312: }
313:
314: public void deleteResource(Resource resource)
315: throws PortalException, SystemException {
316:
317: // Permissions
318:
319: Iterator itr = permissionPersistence.findByResourceId(
320: resource.getResourceId()).iterator();
321:
322: while (itr.hasNext()) {
323: Permission permission = (Permission) itr.next();
324:
325: orgGroupPermissionPersistence
326: .removeByPermissionId(permission.getPermissionId());
327: }
328:
329: permissionPersistence.removeByResourceId(resource
330: .getResourceId());
331:
332: // Resource
333:
334: resourcePersistence.remove(resource.getResourceId());
335: }
336:
337: public void deleteResource(long companyId, String name, int scope,
338: long primKey) throws PortalException, SystemException {
339:
340: deleteResource(companyId, name, scope, String.valueOf(primKey));
341: }
342:
343: public void deleteResource(long companyId, String name, int scope,
344: String primKey) throws PortalException, SystemException {
345:
346: try {
347: Resource resource = getResource(companyId, name, scope,
348: primKey);
349:
350: deleteResource(resource.getResourceId());
351: } catch (NoSuchResourceException nsre) {
352: _log.warn(nsre);
353: }
354: }
355:
356: public void deleteResources(String name) throws PortalException,
357: SystemException {
358:
359: Iterator itr = resourceFinder.findByName(name).iterator();
360:
361: while (itr.hasNext()) {
362: Resource resource = (Resource) itr.next();
363:
364: deleteResource(resource);
365: }
366: }
367:
368: public long getLatestResourceId() throws PortalException,
369: SystemException {
370:
371: List list = resourcePersistence.findAll(0, 1,
372: new ResourceComparator());
373:
374: if (list.size() == 0) {
375: return 0;
376: } else {
377: Resource resource = (Resource) list.get(0);
378:
379: return resource.getResourceId();
380: }
381: }
382:
383: public Resource getResource(long resourceId)
384: throws PortalException, SystemException {
385:
386: return resourcePersistence.findByPrimaryKey(resourceId);
387: }
388:
389: public List getResources() throws SystemException {
390: return resourcePersistence.findAll();
391: }
392:
393: public Resource getResource(long companyId, String name, int scope,
394: String primKey) throws PortalException, SystemException {
395:
396: ResourceCode resourceCode = resourceCodeLocalService
397: .getResourceCode(companyId, name, scope);
398:
399: return resourcePersistence.findByC_P(resourceCode.getCodeId(),
400: primKey);
401: }
402:
403: protected void addCommunityPermissions(long companyId,
404: long groupId, long userId, String name, Resource resource,
405: boolean portletActions) throws PortalException,
406: SystemException {
407:
408: StopWatch stopWatch = null;
409:
410: if (_log.isDebugEnabled()) {
411: stopWatch = new StopWatch();
412:
413: stopWatch.start();
414: }
415:
416: Group group = groupPersistence.findByPrimaryKey(groupId);
417:
418: long resourceId = resource.getResourceId();
419: String primKey = resource.getPrimKey();
420:
421: logAddCommunityPermissions(groupId, name, resourceId,
422: stopWatch, 1);
423:
424: List actions = null;
425:
426: if (portletActions) {
427: actions = ResourceActionsUtil
428: .getPortletResourceCommunityDefaultActions(name);
429: } else {
430: actions = ResourceActionsUtil
431: .getModelResourceCommunityDefaultActions(name);
432: }
433:
434: logAddCommunityPermissions(groupId, name, resourceId,
435: stopWatch, 2);
436:
437: String[] actionIds = (String[]) actions.toArray(new String[0]);
438:
439: List communityPermissionsList = permissionLocalService
440: .getPermissions(group.getCompanyId(), actionIds,
441: resourceId);
442:
443: logAddCommunityPermissions(groupId, name, resourceId,
444: stopWatch, 3);
445:
446: PermissionsListFilter permissionsListFilter = PermissionsListFilterFactory
447: .getInstance();
448:
449: communityPermissionsList = permissionsListFilter
450: .filterCommunityPermissions(companyId, groupId, userId,
451: name, primKey, portletActions,
452: communityPermissionsList);
453:
454: logAddCommunityPermissions(groupId, name, resourceId,
455: stopWatch, 4);
456:
457: groupPersistence.addPermissions(groupId,
458: communityPermissionsList);
459:
460: logAddCommunityPermissions(groupId, name, resourceId,
461: stopWatch, 5);
462: }
463:
464: protected void addGuestPermissions(long companyId, long groupId,
465: long userId, String name, Resource resource,
466: boolean portletActions) throws PortalException,
467: SystemException {
468:
469: long defaultUserId = userLocalService
470: .getDefaultUserId(companyId);
471:
472: List actions = null;
473:
474: if (portletActions) {
475: actions = ResourceActionsUtil
476: .getPortletResourceGuestDefaultActions(name);
477: } else {
478: actions = ResourceActionsUtil
479: .getModelResourceGuestDefaultActions(name);
480: }
481:
482: String[] actionIds = (String[]) actions.toArray(new String[0]);
483:
484: List guestPermissionsList = permissionLocalService
485: .getPermissions(companyId, actionIds, resource
486: .getResourceId());
487:
488: PermissionsListFilter permissionsListFilter = PermissionsListFilterFactory
489: .getInstance();
490:
491: guestPermissionsList = permissionsListFilter
492: .filterGuestPermissions(companyId, groupId, userId,
493: name, resource.getPrimKey(), portletActions,
494: guestPermissionsList);
495:
496: userPersistence.addPermissions(defaultUserId,
497: guestPermissionsList);
498: }
499:
500: protected void logAddCommunityPermissions(long groupId,
501: String name, long resourceId, StopWatch stopWatch, int block) {
502:
503: if (!_log.isDebugEnabled()) {
504: return;
505: }
506:
507: _log.debug("Adding community permissions block " + block
508: + " for " + groupId + " " + name + " " + resourceId
509: + " takes " + stopWatch.getTime() + " ms");
510: }
511:
512: protected void logAddResources(String name, String primKey,
513: StopWatch stopWatch, int block) {
514:
515: if (!_log.isDebugEnabled()) {
516: return;
517: }
518:
519: _log.debug("Adding resources block " + block + " for " + name
520: + " " + primKey + " takes " + stopWatch.getTime()
521: + " ms");
522: }
523:
524: protected void validate(long companyId, String name,
525: boolean portletActions) throws PortalException,
526: SystemException {
527:
528: List actions = null;
529:
530: if (portletActions) {
531: actions = ResourceActionsUtil.getPortletResourceActions(
532: companyId, name);
533: } else {
534: actions = ResourceActionsUtil.getModelResourceActions(name);
535: }
536:
537: if (actions.size() == 0) {
538: throw new ResourceActionsException(
539: "There are no actions associated with the resource "
540: + name);
541: }
542: }
543:
544: private static Log _log = LogFactory
545: .getLog(ResourceLocalServiceImpl.class);
546:
547: }
|