001/*
002 * (C) Copyright 2013-2014 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 *     Martin Pernollet
018 */
019
020package org.nuxeo.ecm.platform.groups.audit.seam;
021
022import java.io.File;
023import java.io.IOException;
024import java.io.Serializable;
025import java.util.HashSet;
026import java.util.List;
027import java.util.Map;
028import java.util.Set;
029
030import org.apache.commons.lang.StringUtils;
031import org.apache.commons.logging.Log;
032import org.apache.commons.logging.LogFactory;
033import org.jboss.seam.ScopeType;
034import org.jboss.seam.annotations.In;
035import org.jboss.seam.annotations.Name;
036import org.jboss.seam.annotations.Scope;
037import org.jboss.seam.faces.FacesMessages;
038import org.jboss.seam.international.StatusMessage;
039
040import org.nuxeo.ecm.core.api.CoreSession;
041import org.nuxeo.ecm.core.api.DocumentModel;
042import org.nuxeo.ecm.core.api.NuxeoPrincipal;
043import org.nuxeo.ecm.core.work.api.Work;
044import org.nuxeo.ecm.core.work.api.WorkManager;
045import org.nuxeo.ecm.platform.groups.audit.service.acl.excel.AclNameShortner;
046import org.nuxeo.ecm.platform.groups.audit.service.acl.job.AclAuditWork;
047import org.nuxeo.ecm.platform.groups.audit.service.acl.job.publish.IResultPublisher;
048import org.nuxeo.ecm.platform.groups.audit.service.acl.job.publish.PublishByMail;
049import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
050import org.nuxeo.ecm.webapp.documentsLists.DocumentsListsManager;
051import org.nuxeo.runtime.api.Framework;
052
053@Name("excelExportRightsAction")
054@Scope(ScopeType.EVENT)
055public class ExcelExportRightsActionBean implements Serializable {
056    private static final long serialVersionUID = 1L;
057
058    private static final Log log = LogFactory.getLog(ExcelExportRightsActionBean.class);
059
060    @In(create = true, required = false)
061    protected transient CoreSession documentManager;
062
063    @In(create = true)
064    protected NavigationContext navigationContext;
065
066    @In(create = true)
067    protected DocumentsListsManager documentsListsManager;
068
069    @In(create = true, required = false)
070    protected transient FacesMessages facesMessages;
071
072    @In(create = true)
073    protected Map<String, String> messages;
074
075    @In(create = true, required = false)
076    protected NuxeoPrincipal currentNuxeoPrincipal;
077
078    private static final String WORK_NAME = "Permission Audit for ";
079
080    public String doGet() {
081        try {
082            buildAndSendByMail();
083        } catch (IOException e) {
084            log.error(e, e);
085            facesMessages.add(StatusMessage.Severity.ERROR, "doGet error: " + e.getMessage());
086        }
087        return null;
088    }
089
090    public boolean accept() {
091        return true;
092    }
093
094    protected List<DocumentModel> getCurrentlySelectedDocuments() {
095        if (navigationContext.getCurrentDocument().isFolder()) {
096            return documentsListsManager.getWorkingList(DocumentsListsManager.CURRENT_DOCUMENT_SELECTION);
097        } else {
098            return null;
099        }
100    }
101
102    /* XLS REPORT */
103
104    public void buildAndSendByMail() throws IOException {
105        File tmpFile = Framework.createTempFile("rights", ".xls");
106        Framework.trackFile(tmpFile, tmpFile);
107        buildAndSendByMail(tmpFile);
108    }
109
110    /**
111     * Execute ACL audit asynchronously and send the result to current user.
112     */
113    protected void buildAndSendByMail(final File tmpFile) {
114        final DocumentModel auditRoot = navigationContext.getCurrentDocument();
115        final String repositoryName = documentManager.getRepositoryName();
116        final String to = currentNuxeoPrincipal.getEmail();
117        final String defaultFrom = "noreply@nuxeo.com";
118        final String workName = WORK_NAME + auditRoot.getPathAsString();
119        WorkManager wm = Framework.getLocalService(WorkManager.class);
120
121        if (StringUtils.isBlank(to)) {
122            facesMessages.add(StatusMessage.Severity.ERROR, "Your email is missing from your profile.");
123            return;
124        }
125
126        // Work to do and publishing
127        IResultPublisher publisher = new PublishByMail(to, defaultFrom, repositoryName);
128        Work work = new AclAuditWork(workName, repositoryName, auditRoot.getId(), tmpFile, publisher);
129        wm.schedule(work, true);
130
131        // Shows information about work, and output
132        String message = messages.get("message.acl.audit.started");
133        facesMessages.add(StatusMessage.Severity.INFO, message);
134    }
135
136    /* */
137
138    protected Set<String> existingPermissions = new HashSet<>();
139
140    {
141        AclNameShortner names = new AclNameShortner();
142        existingPermissions.addAll(names.getFullNames());
143    }
144
145    public Set<String> getExistingPermissions() {
146        return existingPermissions;
147    }
148
149}