001/* 002 * (C) Copyright 2013-2014 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl-2.1.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Martin Pernollet 016 */ 017 018package org.nuxeo.ecm.platform.groups.audit.seam; 019 020import java.io.File; 021import java.io.IOException; 022import java.io.Serializable; 023import java.util.HashSet; 024import java.util.List; 025import java.util.Map; 026import java.util.Set; 027 028import org.apache.commons.lang.StringUtils; 029import org.apache.commons.logging.Log; 030import org.apache.commons.logging.LogFactory; 031import org.jboss.seam.ScopeType; 032import org.jboss.seam.annotations.In; 033import org.jboss.seam.annotations.Name; 034import org.jboss.seam.annotations.Scope; 035import org.jboss.seam.faces.FacesMessages; 036import org.jboss.seam.international.StatusMessage; 037 038import org.nuxeo.ecm.core.api.CoreSession; 039import org.nuxeo.ecm.core.api.DocumentModel; 040import org.nuxeo.ecm.core.api.NuxeoPrincipal; 041import org.nuxeo.ecm.core.work.api.Work; 042import org.nuxeo.ecm.core.work.api.WorkManager; 043import org.nuxeo.ecm.platform.groups.audit.service.acl.excel.AclNameShortner; 044import org.nuxeo.ecm.platform.groups.audit.service.acl.job.AclAuditWork; 045import org.nuxeo.ecm.platform.groups.audit.service.acl.job.publish.IResultPublisher; 046import org.nuxeo.ecm.platform.groups.audit.service.acl.job.publish.PublishByMail; 047import org.nuxeo.ecm.platform.ui.web.api.NavigationContext; 048import org.nuxeo.ecm.webapp.documentsLists.DocumentsListsManager; 049import org.nuxeo.runtime.api.Framework; 050 051@Name("excelExportRightsAction") 052@Scope(ScopeType.EVENT) 053public class ExcelExportRightsActionBean implements Serializable { 054 private static final long serialVersionUID = 1L; 055 056 private static final Log log = LogFactory.getLog(ExcelExportRightsActionBean.class); 057 058 @In(create = true, required = false) 059 protected transient CoreSession documentManager; 060 061 @In(create = true) 062 protected NavigationContext navigationContext; 063 064 @In(create = true) 065 protected DocumentsListsManager documentsListsManager; 066 067 @In(create = true, required = false) 068 protected transient FacesMessages facesMessages; 069 070 @In(create = true) 071 protected Map<String, String> messages; 072 073 @In(create = true, required = false) 074 protected NuxeoPrincipal currentNuxeoPrincipal; 075 076 private static final String WORK_NAME = "Permission Audit for "; 077 078 public String doGet() { 079 try { 080 buildAndSendByMail(); 081 } catch (IOException e) { 082 log.error(e, e); 083 facesMessages.add(StatusMessage.Severity.ERROR, "doGet error: " + e.getMessage()); 084 } 085 return null; 086 } 087 088 public boolean accept() { 089 return true; 090 } 091 092 protected List<DocumentModel> getCurrentlySelectedDocuments() { 093 if (navigationContext.getCurrentDocument().isFolder()) { 094 return documentsListsManager.getWorkingList(DocumentsListsManager.CURRENT_DOCUMENT_SELECTION); 095 } else { 096 return null; 097 } 098 } 099 100 /* XLS REPORT */ 101 102 public void buildAndSendByMail() throws IOException { 103 File tmpFile = File.createTempFile("rights", ".xls"); 104 Framework.trackFile(tmpFile, tmpFile); 105 buildAndSendByMail(tmpFile); 106 } 107 108 /** 109 * Execute ACL audit asynchronously and send the result to current user. 110 */ 111 protected void buildAndSendByMail(final File tmpFile) { 112 final DocumentModel auditRoot = navigationContext.getCurrentDocument(); 113 final String repositoryName = documentManager.getRepositoryName(); 114 final String to = currentNuxeoPrincipal.getEmail(); 115 final String defaultFrom = "noreply@nuxeo.com"; 116 final String workName = WORK_NAME + auditRoot.getPathAsString(); 117 WorkManager wm = Framework.getLocalService(WorkManager.class); 118 119 if (StringUtils.isBlank(to)) { 120 facesMessages.add(StatusMessage.Severity.ERROR, "Your email is missing from your profile."); 121 return; 122 } 123 124 // Work to do and publishing 125 IResultPublisher publisher = new PublishByMail(to, defaultFrom, repositoryName); 126 Work work = new AclAuditWork(workName, repositoryName, auditRoot.getId(), tmpFile, publisher); 127 wm.schedule(work, true); 128 129 // Shows information about work, and output 130 String message = messages.get("message.acl.audit.started"); 131 facesMessages.add(StatusMessage.Severity.INFO, message); 132 } 133 134 /* */ 135 136 protected Set<String> existingPermissions = new HashSet<>(); 137 138 { 139 AclNameShortner names = new AclNameShortner(); 140 existingPermissions.addAll(names.getFullNames()); 141 } 142 143 public Set<String> getExistingPermissions() { 144 return existingPermissions; 145 } 146 147}