Ignore:
Timestamp:
2023-09-25T23:16:31+13:00 (8 months ago)
Author:
anupama
Message:

Runtime server side changes to recognise remove-metadata-array method. It is not like set-metadata-array, which for UserComments means it's in add user comments mode and thus can be done by any logged in user. If remove-metadata-array is called, presumably the user should have collection editing permissions, or if usercomments are involved, then they certainly have to be (as specified by Dr Bainbridge) in the administrator group. Note that I still have issues with actually using remove_metadata_array to delete user comments as admin: things fail when attempting to remove_archives_metadata(_array), which I will be investigating next. But I wanted the code committed, so it won't get lost when we move to a new room this week, which could take place at any time possibly.

File:
1 edited

Legend:

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

    r37585 r38219  
    320320    {
    321321
    322         // There are two types of operations whereby metadata gets modified:
     322        // There are now 3 types of operations whereby metadata gets modified:
    323323        // - document including document-meta editing: user needs document editing powers
    324324        // - adding user comments: user just needs an account and needs to be logged in
    325         // We handle both cases in this service.
     325        // - removing user comments: user needs to be in administrator group
     326        // We handle all 3 cases in this service.
    326327
    327328        Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
     
    333334
    334335        String[] docids = null;
     336
     337        // For user comments (setting or removing), these are the allowed metadata fields
     338        Pattern allowedMetaFieldsPattern = Pattern.compile("^(username|usertimestamp|usercomment)$");
     339        String lang = request.getAttribute(GSXML.LANG_ATT);
    335340       
    336 
    337         if (userHasCollectionEditPermissions(request, params)) { // means user can modify ANY metadata
     341        boolean isAdminRemovingUserComments = false;
     342       
     343        // Have to be admin to do remove-metadata-array for user comments meta fields
     344        if (metaserver_command.equals("remove-metadata-array")) {
     345        // check if only removing user comments metadata fields
     346        docids = getDocIdsWithOptFilter(json_str, allowedMetaFieldsPattern);
     347        if(docids != null) {
     348            isAdminRemovingUserComments = true;
     349        }
     350
     351        if(!userIsAdministrator(request, params)) {
     352            isAdminRemovingUserComments = false;
     353            return errorResponse("processModifyMetadata", NO_PERMISSIONS_ERROR, lang);
     354        }       
     355        }
     356
     357        if(isAdminRemovingUserComments) {
     358        // everything is set up already now for admin to remove user comments
     359        }       
     360        else if (userHasCollectionEditPermissions(request, params)) { // means user can modify ANY metadata
    338361
    339362        // if dealing with an array of meta, then parse out the docids from the json
    340363        if(supportsSettingMultipleMeta) {
    341364            docids = getDocIdsWithOptFilter(json_str, null);
     365        } else if (metaserver_command.equals("remove-metadata-array")) {
     366            // removing multiple metadata that are Not user comments
     367            // can be done by any user with collection edit permissions
     368            docids = getDocIdsWithOptFilter(json_str, null);
    342369        } // else set-meta operation on single metadata field of single doc,
    343370          // and docid will be obtained in runCommand() where it's needed
     
    348375
    349376        UserContext context = new UserContext(request);
    350         String lang = request.getAttribute(GSXML.LANG_ATT);
    351377        if (context.getUsername().equals("")) {
    352378           
     
    358384           
    359385            boolean isAddingUserComments = false;
    360             Pattern allowedMetaFieldsPattern = Pattern.compile("^(username|usertimestamp|usercomment)$");
     386           
    361387            if(supportsSettingMultipleMeta) {
    362388           
     
    367393            } else {
    368394            String metaname = (String) params.get("metaname");
    369             if(isAllowedToSetMeta(metaname, allowedMetaFieldsPattern)) {
     395            if(isAllowedToModifyMeta(metaname, allowedMetaFieldsPattern)) {
    370396                isAddingUserComments = true;
    371397            }
     
    743769
    744770    protected Element runCommand(Element request, int type) {
    745         return runCommand(request, type, null);
     771        return runCommand(request, type, null);
    746772    }
    747773
    748774    /** returns a response element */
    749775    protected Element runCommand(Element request, int type, String[] docids)
    750     {
     776    {       
    751777        Document result_doc = XMLConverter.newDOM();
    752778        // the response to send back
     
    869895                }
    870896            }
     897           
     898            // Mark files for reindexing (e.g. if set-meta or remove-meta was called)
     899            // Note that remove-meta doesn't mean the document should be marked for
     900            // Deletion: only meta was removed.
    871901           
    872902            if (oid != null) { // if we have only one oid
     
    11021132    }
    11031133
    1104     protected boolean isAllowedToSetMeta(String metaname, Pattern allowedMetaFieldsPattern)
     1134    protected boolean isAllowedToModifyMeta(String metaname, Pattern allowedMetaFieldsPattern)
    11051135    {
    11061136    if(allowedMetaFieldsPattern == null) { // null when user has edit permissions, so they can set any meta
     
    11531183            ///logger.info("### metaname: " + metaname);
    11541184           
    1155             if(!isAllowedToSetMeta(metaname, filterFields)) {
     1185            if(!isAllowedToModifyMeta(metaname, filterFields)) {
    11561186                return null;
    11571187            }
     
    11921222    return userHasCollectionEditPermissions(request, params);
    11931223
     1224    }
     1225
     1226    protected boolean userIsAdministrator(Element request, HashMap<String, Serializable> params) {
     1227   
     1228    UserContext context = new UserContext(request);
     1229   
     1230    for (String group : context.getGroups()) {
     1231        // administrator always has permission
     1232        if (group.equals("administrator")) {
     1233        return true;
     1234        }
     1235    }
     1236
     1237    return false;
    11941238    }
    11951239
Note: See TracChangeset for help on using the changeset viewer.