001/* 002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others. 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 * 016 * Contributors: 017 * Thomas Roger 018 */ 019 020package org.nuxeo.ecm.core.security; 021 022import static org.nuxeo.ecm.core.api.event.CoreEventConstants.CHANGED_ACL_NAME; 023import static org.nuxeo.ecm.core.api.event.CoreEventConstants.DOCUMENT_REFS; 024import static org.nuxeo.ecm.core.api.event.CoreEventConstants.REPOSITORY_NAME; 025import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.ACE_STATUS_UPDATED; 026 027import java.io.Serializable; 028import java.util.ArrayList; 029import java.util.Calendar; 030import java.util.Date; 031import java.util.HashMap; 032import java.util.List; 033import java.util.Map; 034 035import org.apache.commons.lang3.time.FastDateFormat; 036import org.nuxeo.ecm.core.api.DocumentRef; 037import org.nuxeo.ecm.core.api.IdRef; 038import org.nuxeo.ecm.core.api.IterableQueryResult; 039import org.nuxeo.ecm.core.api.security.ACE; 040import org.nuxeo.ecm.core.api.security.ACP; 041import org.nuxeo.ecm.core.event.EventContext; 042import org.nuxeo.ecm.core.event.EventService; 043import org.nuxeo.ecm.core.event.impl.EventContextImpl; 044import org.nuxeo.ecm.core.query.sql.NXQL; 045import org.nuxeo.ecm.core.work.AbstractWork; 046import org.nuxeo.runtime.api.Framework; 047import org.nuxeo.runtime.transaction.TransactionRuntimeException; 048 049/** 050 * Work updating ACE status. 051 * 052 * @since 7.4 053 */ 054public class UpdateACEStatusWork extends AbstractWork { 055 056 public static final int DEFAULT_BATCH_SIZE = 20; 057 058 public static final String ID = "updateACEStatus"; 059 060 public static final String CATEGORY = "updateACEStatus"; 061 062 public static final String QUERY = "SELECT ecm:uuid, ecm:acl/*1/principal, ecm:acl/*1/permission," 063 + " ecm:acl/*1/grant, ecm:acl/*1/creator, ecm:acl/*1/begin, ecm:acl/*1/end, ecm:acl/*1/name FROM Document" 064 + " WHERE (ecm:acl/*1/status = 0 AND ecm:acl/*1/begin <= TIMESTAMP '%s')" 065 + " OR (ecm:acl/*1/status = 1 AND ecm:acl/*1/end <= TIMESTAMP '%s')"; 066 067 public static final FastDateFormat FORMATTER = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss"); 068 069 protected int batchSize = DEFAULT_BATCH_SIZE; 070 071 public UpdateACEStatusWork() { 072 super(ID); 073 } 074 075 @Override 076 public void work() { 077 setStatus("Updating ACE status"); 078 openSystemSession(); 079 080 Date now = new Date(); 081 String formattedDate = FORMATTER.format(now); 082 083 IterableQueryResult result = session.queryAndFetch(String.format(QUERY, formattedDate, formattedDate), 084 NXQL.NXQL); 085 Map<String, List<ACE>> docIdsToACEs = new HashMap<>(); 086 try { 087 for (Map<String, Serializable> map : result) { 088 String docId = (String) map.get("ecm:uuid"); 089 List<ACE> aces = docIdsToACEs.get(docId); 090 if (aces == null) { 091 aces = new ArrayList<>(); 092 docIdsToACEs.put(docId, aces); 093 } 094 095 String username = (String) map.get("ecm:acl/*1/principal"); 096 String permission = (String) map.get("ecm:acl/*1/permission"); 097 Boolean grant = (Boolean) map.get("ecm:acl/*1/grant"); 098 String creator = (String) map.get("ecm:acl/*1/creator"); 099 Calendar begin = (Calendar) map.get("ecm:acl/*1/begin"); 100 Calendar end = (Calendar) map.get("ecm:acl/*1/end"); 101 String aclName = (String) map.get("ecm:acl/*1/name"); 102 Map<String, Serializable> contextData = new HashMap<>(); 103 contextData.put(CHANGED_ACL_NAME, aclName); 104 ACE ace = ACE.builder(username, permission) 105 .isGranted(grant) 106 .creator(creator) 107 .begin(begin) 108 .end(end) 109 .contextData(contextData) 110 .build(); 111 aces.add(ace); 112 } 113 } finally { 114 result.close(); 115 } 116 117 int acpUpdatedCount = 0; 118 Map<DocumentRef, List<ACE>> processedRefToACEs = new HashMap<>(); 119 for (Map.Entry<String, List<ACE>> entry : docIdsToACEs.entrySet()) { 120 try { 121 DocumentRef ref = new IdRef(entry.getKey()); 122 ACP acp = session.getACP(ref); 123 // re-set the ACP to actually write the new status 124 session.setACP(ref, acp, true); 125 acpUpdatedCount++; 126 processedRefToACEs.put(ref, entry.getValue()); 127 if (acpUpdatedCount % batchSize == 0) { 128 fireACEStatusUpdatedEvent(processedRefToACEs); 129 commitOrRollbackTransaction(); 130 startTransaction(); 131 processedRefToACEs.clear(); 132 } 133 } catch (TransactionRuntimeException e) { 134 if (e.getMessage().contains("Transaction timeout")) { 135 batchSize = 1; 136 } 137 throw e; 138 } 139 } 140 fireACEStatusUpdatedEvent(processedRefToACEs); 141 142 setStatus(null); 143 } 144 145 protected void fireACEStatusUpdatedEvent(Map<DocumentRef, List<ACE>> refToACEs) { 146 EventContext eventContext = new EventContextImpl(session, session.getPrincipal()); 147 eventContext.setProperty(DOCUMENT_REFS, (Serializable) refToACEs); 148 eventContext.setProperty(REPOSITORY_NAME, session.getRepositoryName()); 149 Framework.getService(EventService.class).fireEvent(ACE_STATUS_UPDATED, eventContext); 150 } 151 152 @Override 153 public String getCategory() { 154 return CATEGORY; 155 } 156 157 @Override 158 public String getTitle() { 159 return "Updating ACE status"; 160 } 161 162 @Override 163 public int getRetryCount() { 164 return 10; 165 } 166}