Ignore:
Timestamp:
2015-05-12T21:47:35+12:00 (9 years ago)
Author:
ak19
Message:

First part of commit for ensuring the user is authenticated when running the scripts used by the online metadata editor. Running metaserver, BuildAndActivate and other GS2Construct.java commands should not be possible from a web browser.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/GS2Construct.java

    r28966 r29869  
    2424import java.io.Serializable;
    2525import java.util.Collections;
     26import java.util.Iterator;
     27import java.util.Map.Entry;
    2628import java.util.HashMap;
    2729import java.util.Map;
     30import java.util.Set;
    2831
    2932import org.apache.log4j.Logger;
     
    6366    private static final String DELETE_SERVICE = "DeleteCollection";
    6467    private static final String RELOAD_SERVICE = "ReloadCollection";
     68    private static final String SET_METADATA_SERVICE = "SetMetadata";
    6569
    6670    // params used
     
    120124                param_list.appendChild(param);
    121125            }
    122             else if (service.equals(ACTIVATE_SERVICE) || service.equals(IMPORT_SERVICE) || service.equals(BUILD_SERVICE) || service.equals(RELOAD_SERVICE) || service.equals(DELETE_SERVICE))
     126            else if (service.equals(ACTIVATE_SERVICE) || service.equals(IMPORT_SERVICE) || service.equals(BUILD_SERVICE) || service.equals(RELOAD_SERVICE) || service.equals(DELETE_SERVICE) || service.equals(SET_METADATA_SERVICE))
    123127            {
    124128
     
    140144    protected Element processNewCollection(Element request)
    141145    {
    142         return runCommand(request, GS2PerlConstructor.NEW);
     146        if (!userHasCollectionEditPermissions(request)) {
     147        Document result_doc = XMLConverter.newDOM();
     148        Element result = GSXML.createBasicResponse(result_doc, "processNewCollection");
     149        GSXML.addError(result, "This user does not have the required permissions to perform this action.");
     150        return result;
     151        }
     152        return runCommand(request, GS2PerlConstructor.NEW);
    143153    }
    144154
     
    146156    protected Element processAddDocument(Element request)
    147157    {
     158        if (!userHasCollectionEditPermissions(request)) {
     159        Document result_doc = XMLConverter.newDOM();
     160        Element result = GSXML.createBasicResponse(result_doc, "processAddDocument");
     161        GSXML.addError(result, "This user does not have the required permissions to perform this action.");
     162        return result;
     163        }
     164
    148165      Document result_doc = XMLConverter.newDOM();
    149166        // decode the file name, add it to the import directory
     
    163180    protected Element processBuildAndActivateCollection(Element request)
    164181    {
    165      
     182        // check permissions
     183        if (!userHasCollectionEditPermissions(request)) {
     184            Document result_doc = XMLConverter.newDOM();
     185            Element result = GSXML.createBasicResponse(result_doc, "processBuildAndActivateCollection");
     186            GSXML.addError(result, "This user does not have the required permissions to perform this action.");
     187            return result;
     188        }
     189
    166190        waitUntilReady(request);
    167191        Element buildResponse = processBuildCollection(request);
     
    197221    protected Element processImportCollection(Element request)
    198222    {
     223        if (!userHasCollectionEditPermissions(request)) {
     224        Document result_doc = XMLConverter.newDOM();
     225        Element result = GSXML.createBasicResponse(result_doc, "processImportCollection");
     226        GSXML.addError(result, "This user does not have the required permissions to perform this action.");
     227        return result;
     228        }
     229
    199230        Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
    200231        HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
     
    260291    protected Element processBuildCollection(Element request)
    261292    {
     293        if (!userHasCollectionEditPermissions(request)) {
     294        Document result_doc = XMLConverter.newDOM();
     295        Element result = GSXML.createBasicResponse(result_doc, "processBuildCollection");
     296        GSXML.addError(result, "This user does not have the required permissions to perform this action.");
     297        return result;
     298        }
     299
    262300        return runCommand(request, GS2PerlConstructor.BUILD);
    263301    }
    264302
     303    protected Element processSetMetadata(Element request)
     304    {
     305        if (!userHasCollectionEditPermissions(request)) {
     306        Document result_doc = XMLConverter.newDOM();
     307        Element result = GSXML.createBasicResponse(result_doc, "processSetMetadata");
     308        GSXML.addError(result, "This user does not have the required permissions to perform this action.");
     309        return result;
     310        }
     311
     312        return runCommand(request, GS2PerlConstructor.SET_METADATA_SERVER);
     313    }
     314
    265315    protected Element processActivateCollection(Element request)
    266316    {
     317
     318        if (!userHasCollectionEditPermissions(request)) {
     319        Document result_doc = XMLConverter.newDOM();
     320        Element result = GSXML.createBasicResponse(result_doc, "processActivateCollection");
     321        GSXML.addError(result, "This user does not have the required permissions to perform this action.");
     322        return result;
     323        }
     324
    267325        // this activates the collection on disk. but now we need to tell
    268326        // the MR about it. but we have to wait until the process is finished.
     
    334392    protected Element processDeleteCollection(Element request)
    335393    {
     394        if (!userHasCollectionEditPermissions(request)) {
     395        Document result_doc = XMLConverter.newDOM();
     396        Element result = GSXML.createBasicResponse(result_doc, "processDeleteCollection");
     397        GSXML.addError(result, "This user does not have the required permissions to perform this action.");
     398        return result;
     399        }
     400
    336401      Document result_doc = XMLConverter.newDOM();
    337402        // the response to send back
     
    391456    protected Element processReloadCollection(Element request)
    392457    {
     458        if (!userHasCollectionEditPermissions(request)) {
     459        Document result_doc = XMLConverter.newDOM();
     460        Element result = GSXML.createBasicResponse(result_doc, "processReloadCollection");
     461        GSXML.addError(result, "This user does not have the required permissions to perform this action.");
     462        return result;
     463        }
     464
    393465      Document result_doc = XMLConverter.newDOM();
    394466        // the response to send back
     
    539611        //this.short_service_info.appendChild(e);
    540612
     613        e = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
     614        e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_PROCESS);
     615        e.setAttribute(GSXML.NAME_ATT, SET_METADATA_SERVICE);
     616        this.short_service_info.appendChild(e);
     617
    541618        return true;
    542619    }
     
    592669        }
    593670
    594         // do teh actual command
     671        // do the actual command
    595672        String coll_name = null;
    596673        if (type == GS2PerlConstructor.NEW)
     
    624701        {
    625702            constructor.setManifestFile(this.site_home + File.separator + "collect" + File.separator + params.get(COL_PARAM) + File.separator + "manifests" + File.separator + "tempManifest.xml");
     703        }
     704        else if (type == GS2PerlConstructor.SET_METADATA_SERVER) {
     705            StringBuffer querystring = new StringBuffer();
     706           
     707            // convert params into a single string again?
     708            Set<Map.Entry<String, Serializable>> entries = params.entrySet();
     709            Iterator<Map.Entry<String, Serializable>> i = entries.iterator();
     710            while(i.hasNext()) {
     711           
     712            Map.Entry<String, Serializable> entry = i.next();
     713            String paramname = entry.getKey();
     714            paramname = paramname.replace("s1.", ""); // replaces all occurrences
     715            if(paramname.equals("collection")) {
     716                paramname = "c";
     717            }
     718            String paramvalue = (String)entry.getValue();
     719
     720            querystring.append(paramname + "=" + paramvalue);
     721            if(i.hasNext()) {
     722                querystring.append("&");
     723            }
     724            }
     725            constructor.setQueryString(querystring.toString());
    626726        }
    627727
     
    805905        return false;
    806906    }
     907
     908
     909    /** Copy from DebugService.userHasEditPermissions
     910     This function checks that the user is logged in and that the user
     911     is in the right group to edit the collection */
     912    protected boolean userHasCollectionEditPermissions(Element request) {
     913    Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
     914    HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
     915    String collection = (String) params.get(COL_PARAM); // could be null on newcoll operation
     916
     917    UserContext context = new UserContext(request);
     918    if(collection == null) {
     919    return !context.getUsername().equals("");
     920    }
     921    for (String group : context.getGroups()) {
     922      // administrator always has permission
     923      if (group.equals("administrator")) {
     924    return true;
     925      }
     926      // all-collections-editor can edit any collection
     927      if (!collection.equals("")) {
     928    if (group.equals("all-collections-editor")) {
     929      return true;
     930    }
     931    if (group.equals(collection+"-collection-editor")) {
     932      return true;
     933    }
     934      }
     935    }
     936    // haven't found a group with edit permissions
     937    return false;
     938   
     939  }
    807940}
Note: See TracChangeset for help on using the changeset viewer.