001    /*
002     * 
003     * $Revision: 15202 $ $Date: 2009-05-15 17:00:44 +0200 (Fri, 15 May 2009) $
004     *
005     * This file is part of ***  M y C o R e  ***
006     * See http://www.mycore.de/ for details.
007     *
008     * This program is free software; you can use it, redistribute it
009     * and / or modify it under the terms of the GNU General Public License
010     * (GPL) as published by the Free Software Foundation; either version 2
011     * of the License or (at your option) any later version.
012     *
013     * This program is distributed in the hope that it will be useful, but
014     * WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016     * GNU General Public License for more details.
017     *
018     * You should have received a copy of the GNU General Public License
019     * along with this program, in a file called gpl.txt or license.txt.
020     * If not, write to the Free Software Foundation Inc.,
021     * 59 Temple Place - Suite 330, Boston, MA  02111-1307 USA
022     */
023    
024    package org.mycore.frontend.servlets;
025    
026    import java.util.List;
027    
028    import org.apache.log4j.Logger;
029    import org.mycore.access.MCRAccessManager;
030    import org.mycore.common.MCRConfiguration;
031    import org.mycore.common.MCRMailer;
032    import org.mycore.datamodel.common.MCRActiveLinkException;
033    import org.mycore.datamodel.metadata.MCRDerivate;
034    import org.mycore.datamodel.metadata.MCRObjectID;
035    import org.mycore.datamodel.metadata.MCRObjectService;
036    
037    /**
038     * The servlet store the MCREditorServlet output XML in a file of a MCR type
039     * dependencies directory, check it dependence of the MCR type and store the XML
040     * in a file in this directory or if an error was occured start the editor again
041     * with <b>todo </b> <em>repair</em>.
042     * 
043     * @author Jens Kupferschmidt
044     * @version $Revision: 15202 $ $Date: 2009-05-15 17:00:44 +0200 (Fri, 15 May 2009) $
045     */
046    public class MCRCheckCommitACLServlet extends MCRCheckACLBase {
047    
048        private static final long serialVersionUID = 1L;
049        private static Logger LOGGER = Logger.getLogger(MCRCheckCommitACLServlet.class);
050    
051        private static String storedrules = MCRConfiguration.instance().getString("MCR.Access.StorePermissions", "read,write,delete");
052    
053        /**
054         * The method return an URL with the next working step. If okay flag is
055         * true, the object will present else it shows the error page.
056         * 
057         * @param ID
058         *            the MCRObjectID of the MCRObject
059         * @param okay
060         *            the return value of the store operation
061         * @return the next URL as String
062         */
063        protected String getNextURL(MCRObjectID ID, boolean okay) throws MCRActiveLinkException {
064            StringBuffer sb = new StringBuffer();
065            if (okay) {
066                if (ID.getTypeId().equals("class")) {
067                    sb.append("browse?mode=edit");
068                    return sb.toString();
069                }
070                if (ID.getTypeId().equals("derivate")) {
071                    MCRDerivate der = new MCRDerivate();
072                    der.receiveFromDatastore(ID);
073                    String parent = der.getDerivate().getMetaLink().getXLinkHref();
074                    sb.append("receive/").append(parent);
075                    return sb.toString();
076                }
077                sb.append("receive/").append(ID.getId());
078            } else {
079                sb.append(MCRConfiguration.instance().getString("MCR.SWF.PageDir", "")).append(MCRConfiguration.instance().getString("MCR.SWF.PageErrorStore", "editor_error_store.xml"));
080            }
081            return sb.toString();
082        }
083    
084        /**
085         * The method send a message to the mail address for the MCRObjectType.
086         * 
087         * @param ID
088         *            the MCRObjectID of the MCRObject
089         */
090        public final void sendMail(MCRObjectID ID) {
091            List<String> addr = WFM.getMailAddress(ID.getTypeId(), "seditacl");
092    
093            if (addr.size() == 0) {
094                return;
095            }
096    
097            String sender = WFM.getMailSender();
098            String appl = MCRConfiguration.instance().getString("MCR.SWF.Mail.ApplicationID", "DocPortal");
099            String subject = "Automatically generated message from " + appl;
100            StringBuffer text = new StringBuffer();
101            text.append("The ACL data of the MyCoRe object of type ").append(ID.getTypeId()).append(" with the ID ").append(ID.getId()).append(" was changed in the server.");
102    
103            try {
104                MCRMailer.send(sender, addr, subject, text.toString(), false);
105                LOGGER.info("Send a mail about change ACLs to " + addr);
106            } catch (Exception ex) {
107                LOGGER.error("Can't send a mail to " + addr);
108            }
109        }
110    
111        /**
112         * The method store the incoming service data from the ACL editor to the
113         * workflow.
114         * 
115         * @param outelm
116         *            the service subelement of an MCRObject
117         * @param job
118         *            the MCRServletJob instance
119         * @param ID
120         *            the MCRObjectID
121         */
122        public final boolean storeService(org.jdom.Element outelm, MCRServletJob job, MCRObjectID ID) {
123    
124            MCRObjectService service = new MCRObjectService();
125            service.setFromDOM(outelm);
126            int rulesize = service.getRulesSize();
127            if (rulesize == 0) {
128                LOGGER.warn("The ACL conditions for this object was empty, no update!");
129                return false;
130            }
131            while (0 < rulesize) {
132                org.jdom.Element conditions = service.getRule(0).getCondition();
133                String permission = service.getRule(0).getPermission();
134                if (storedrules.indexOf(permission) != -1) {
135                    MCRAccessManager.updateRule(ID, permission, conditions, "");
136                }
137                service.removeRule(0);
138                rulesize--;
139            }
140    
141            LOGGER.info("Update ACLs for ID " + ID.getId() + "in server.");
142            return true;
143        }
144        
145        /**
146         * check the access permission
147         * @param ID the mycore ID
148         * @return true if the access is set
149         */
150        protected boolean checkAccess(MCRObjectID ID) {
151            if (MCRAccessManager.checkPermission(ID, "writedb")) {
152                return true;
153            }
154            return false;
155        }
156    
157    }