001/* 002 * Copyright 2010 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.cms.lock; 017 018import java.io.IOException; 019import java.time.Duration; 020import java.time.LocalDateTime; 021import java.util.Map; 022 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025import org.apache.cocoon.ProcessingException; 026import org.apache.cocoon.xml.XMLUtils; 027import org.xml.sax.SAXException; 028import org.xml.sax.helpers.AttributesImpl; 029 030import org.ametys.cms.content.ContentHelper; 031import org.ametys.cms.repository.Content; 032import org.ametys.core.right.RightManager; 033import org.ametys.core.right.RightManager.RightResult; 034import org.ametys.core.user.User; 035import org.ametys.core.user.UserIdentity; 036import org.ametys.core.user.UserManager; 037import org.ametys.core.util.cocoon.AbstractCurrentUserProviderServiceableGenerator; 038import org.ametys.plugins.repository.lock.LockHelper; 039import org.ametys.plugins.repository.lock.LockableAmetysObject; 040import org.ametys.plugins.repository.lock.UnlockHelper; 041 042/** 043 * List contents that can be unlocked. 044 */ 045public class UnlockGenerator extends AbstractCurrentUserProviderServiceableGenerator 046{ 047 /** The unlock helper */ 048 protected UnlockHelper _unlockHelper; 049 /** The rights manager */ 050 protected RightManager _rightManager; 051 /** The users manager */ 052 protected UserManager _userManager; 053 /** The content helper */ 054 protected ContentHelper _contentHelper; 055 056 @Override 057 public void service(ServiceManager sManager) throws ServiceException 058 { 059 super.service(sManager); 060 061 _unlockHelper = (UnlockHelper) manager.lookup(UnlockHelper.ROLE); 062 _rightManager = (RightManager) manager.lookup(RightManager.ROLE); 063 _userManager = (UserManager) manager.lookup(UserManager.ROLE); 064 _contentHelper = (ContentHelper) manager.lookup(ContentHelper.ROLE); 065 } 066 067 public void generate() throws IOException, SAXException, ProcessingException 068 { 069 contentHandler.startDocument(); 070 XMLUtils.startElement(contentHandler, "List"); 071 072 Map<LockableAmetysObject, LocalDateTime> lockedObjects = _getLockedObjects(); 073 074 _saxLockedContents(lockedObjects); 075 076 XMLUtils.endElement(contentHandler, "List"); 077 contentHandler.endDocument(); 078 } 079 080 /** 081 * Get the locked objects. 082 * @return the locked objects. 083 */ 084 protected Map<LockableAmetysObject, LocalDateTime> _getLockedObjects() 085 { 086 return _unlockHelper.getLockedObjects(); 087 } 088 089 /** 090 * SAX the locked objects. 091 * @param lockedObjects the locked objects. 092 * @throws SAXException if an error occurs generating the XML data. 093 */ 094 protected void _saxLockedContents(Map<LockableAmetysObject, LocalDateTime> lockedObjects) throws SAXException 095 { 096 boolean unlockingActivated = _unlockHelper.isUnlockingActivated(); 097 098 // Get the current user 099 UserIdentity userIdentity = _getCurrentUser(); 100 101 boolean canSeeLocked = _rightManager.hasRight(userIdentity, "CMS_Rights_SeeLocked", "/cms") == RightResult.RIGHT_ALLOW; 102 boolean canUnlockAll = _rightManager.hasRight(userIdentity, "CMS_Rights_UnlockAll", "/cms") == RightResult.RIGHT_ALLOW; 103 long period = _unlockHelper.getTimeBeforeUnlock(); 104 105 for (LockableAmetysObject ametysObject : lockedObjects.keySet()) 106 { 107 // Filter contents 108 if (ametysObject instanceof Content) 109 { 110 UserIdentity lockOwner = ametysObject.getLockOwner(); 111 boolean isLockOwner = LockHelper.isLockOwner(ametysObject, userIdentity); 112 113 if (isLockOwner || canSeeLocked) 114 { 115 LocalDateTime lockTime = lockedObjects.get(ametysObject); 116 117 AttributesImpl attrs = new AttributesImpl(); 118 attrs.addAttribute("", "id", "id", "CDATA", ametysObject.getId()); 119 attrs.addAttribute("", "title", "title", "CDATA", _contentHelper.getTitle((Content) ametysObject)); 120 121 if (unlockingActivated) 122 { 123 long unlockDuration = Duration.between(LocalDateTime.now(), lockTime.plusHours(period)).toMillis(); 124 attrs.addAttribute("", "unlockTime", "unlockTime", "CDATA", Long.toString(unlockDuration)); 125 } 126 Duration sinceDuration = Duration.between(lockTime, LocalDateTime.now()); 127 attrs.addAttribute("", "since", "since", "CDATA", Long.toString(sinceDuration.toMillis())); 128 129 User user = null; 130 if (lockOwner != null) 131 { 132 user = _userManager.getUser(lockOwner.getPopulationId(), lockOwner.getLogin()); 133 } 134 String lockBy = user != null ? user.getFullName() + " (" + user.getIdentity().getLogin() + ")" : (lockOwner != null ? lockOwner.getLogin() : "Anonymous"); 135 attrs.addAttribute("", "lockBy", "lockBy", "CDATA", lockBy); 136 137 if (canUnlockAll || isLockOwner) 138 { 139 attrs.addAttribute("", "canUnlock", "canUnlock", "CDATA", "true"); 140 } 141 142 if (isLockOwner) 143 { 144 attrs.addAttribute("", "isLockOwner", "isLockOwner", "CDATA", "true"); 145 } 146 147 XMLUtils.createElement(contentHandler, "LockedContent", attrs); 148 } 149 } 150 } 151 } 152 153}