Changeset 25092 for main/trunk


Ignore:
Timestamp:
2012-02-15T15:14:09+13:00 (12 years ago)
Author:
sjm84
Message:

Adding in security capabilities for Greenstone 3

Location:
main/trunk/greenstone3/src/java/org/greenstone/gsdl3
Files:
5 edited

Legend:

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

    r25054 r25092  
    1313import javax.servlet.*;
    1414import javax.servlet.http.*;
     15
     16import java.security.Principal;
     17import java.util.Collection;
    1518import java.util.Enumeration;
    1619import java.util.ArrayList;
     
    9699     */
    97100    protected Hashtable session_ids_table = new Hashtable();
    98 
     101   
    99102    /**
    100103     * the maximum interval that the cached info remains in session_ids_table
     
    356359    {
    357360        logUsageInfo(request);
    358        
    359         Map<String,String[]> queryMap = request.getParameterMap();
     361
     362        Map<String, String[]> queryMap = request.getParameterMap();
    360363        if (queryMap != null)
    361364        {
     
    473476        String subaction = request.getParameter(GSParams.SUBACTION);
    474477        String collection = request.getParameter(GSParams.COLLECTION);
     478        String document = request.getParameter(GSParams.DOCUMENT);
    475479        String service = request.getParameter(GSParams.SERVICE);
    476480
     
    695699                        response.setHeader(name, value);
    696700                    }
     701                }
     702            }
     703        }
     704
     705        //Check if we need to login or logout
     706        Map<String, String[]> params = request.getParameterMap();
     707        String[] username = params.get("username");
     708        String[] password = params.get("password");
     709        String[] logout = params.get("logout");
     710
     711        if (logout != null)
     712        {
     713            request.logout();
     714        }
     715
     716        if (username != null && password != null)
     717        {
     718            if (request.getAuthType() != null)
     719            {
     720                request.logout();
     721            }
     722            request.login(username[0], password[0]);
     723        }
     724       
     725        if(request.getAuthType() != null)
     726        {
     727            Element userInformation = this.doc.createElement("userInformation");
     728            xml_request.appendChild(userInformation);
     729            userInformation.setAttribute("username", request.getUserPrincipal().getName());
     730        }
     731           
     732
     733        //If we are in a collection-related page then make sure this user is allowed to access it
     734        if (collection != null && !collection.equals(""))
     735        {
     736            //Get the security info for this collection
     737            Element securityMessage = this.doc.createElement(GSXML.MESSAGE_ELEM);
     738            Element securityRequest = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_SECURITY, collection, userContext);
     739            securityMessage.appendChild(securityRequest);
     740            if (document != null && !document.equals(""))
     741            {
     742                securityRequest.setAttribute("oid", document);
     743            }
     744
     745            Element securityResponse = (Element) GSXML.getChildByTagName(this.recept.process(securityMessage), GSXML.RESPONSE_ELEM);
     746            ArrayList<String> groups = GSXML.getGroupsFromSecurityResponse(securityResponse);
     747
     748            //If guests are not allowed to access this page then check to see if the user is in a group that is allowed to access the page
     749            if (!groups.contains(""))
     750            {
     751                boolean found = false;
     752                for (String group : groups)
     753                {
     754                    if (request.isUserInRole(group))
     755                    {
     756                        found = true;
     757                        break;
     758                    }
     759                }
     760
     761                //The current user is not allowed to access the page so produce a login page
     762                if (!found)
     763                {
     764                    Element loginPageMessage = this.doc.createElement(GSXML.MESSAGE_ELEM);
     765                    Element loginPageRequest = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PAGE, "", userContext);
     766                    loginPageRequest.setAttribute(GSXML.ACTION_ATT, "p");
     767                    loginPageRequest.setAttribute(GSXML.SUBACTION_ATT, "login");
     768                    loginPageRequest.setAttribute(GSXML.OUTPUT_ATT, "html");
     769                    loginPageMessage.appendChild(loginPageRequest);
     770
     771                    Element paramList = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
     772                    loginPageRequest.appendChild(paramList);
     773                   
     774                    Element messageParam = this.doc.createElement(GSXML.PARAM_ELEM);
     775                    messageParam.setAttribute(GSXML.NAME_ATT, "loginMessage");
     776                    if(request.getAuthType() == null)
     777                    {
     778                        messageParam.setAttribute(GSXML.VALUE_ATT, "Please log in to view this page");
     779                    }
     780                    else
     781                    {
     782                        messageParam.setAttribute(GSXML.VALUE_ATT, "You are not in the correct group to view this page");
     783                    }
     784                    paramList.appendChild(messageParam);
     785                   
     786                    Element urlParam = this.doc.createElement(GSXML.PARAM_ELEM);
     787                    urlParam.setAttribute(GSXML.NAME_ATT, "redirectURL");
     788                    urlParam.setAttribute(GSXML.VALUE_ATT, this.getServletName() + "?" + request.getQueryString().replace("&", "&amp;"));
     789                    paramList.appendChild(urlParam);
     790
     791                    Node loginPageResponse = this.recept.process(loginPageMessage);
     792                    out.println(this.converter.getPrettyString(loginPageResponse));
     793
     794                    return;
    697795                }
    698796            }
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/collection/Collection.java

    r24583 r25092  
    3030
    3131import java.io.*;
    32 import java.io.File;
    33 import java.util.HashMap;
    3432import java.util.*;
    3533
     
    7068    /** earliestDatestamp of this collection. Necessary for OAI */
    7169    protected long earliestDatestamp = 0;
     70
     71    /** Stores the default accessibility of guest users */
     72    protected boolean _publicAccess = true;
     73    /** Stores the scope of any security rules (either collection or document) */
     74    protected boolean _securityScopeCollection = true;
     75
     76    protected HashMap<String, ArrayList<Element>> _documentSets = new HashMap<String, ArrayList<Element>>();
     77
     78    protected ArrayList<HashMap<String, ArrayList<String>>> _securityExceptions = new ArrayList<HashMap<String, ArrayList<String>>>();
    7279
    7380    /**
     
    125132            col_type = search.getAttribute(GSXML.TYPE_ATT);
    126133        }
    127        
     134
    128135        Element browse = (Element) GSXML.getChildByTagName(coll_config_xml, GSXML.INFODB_ELEM);
    129136        if (browse != null)
     
    138145        // process the metadata and display items
    139146        findAndLoadInfo(coll_config_xml, build_config_xml);
     147
     148        loadSecurityInformation(coll_config_xml);
    140149
    141150        // now do the services
     
    207216    protected Element loadBuildConfigFile()
    208217    {
    209 
    210218        File build_config_file = new File(GSFile.collectionBuildConfigFile(this.site_home, this.cluster_name));
    211219        if (!build_config_file.exists())
     
    232240    protected boolean findAndLoadInfo(Element coll_config_xml, Element build_config_xml)
    233241    {
    234 
    235         // metadata
    236242        Element meta_list = (Element) GSXML.getChildByTagName(coll_config_xml, GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
    237243        addMetadata(meta_list);
     
    293299        return true;
    294300
     301    }
     302
     303    protected void loadSecurityInformation(Element coll_config_xml)
     304    {
     305        Element securityBlock = (Element) GSXML.getChildByTagName(coll_config_xml, GSXML.SECURITY_ELEM);
     306
     307        if(securityBlock == null)
     308        {
     309            return;
     310        }
     311       
     312        String scope = securityBlock.getAttribute(GSXML.SCOPE_ATT);
     313        String defaultAccess = securityBlock.getAttribute(GSXML.DEFAULT_ACCESS_ATT);
     314
     315        if (defaultAccess.toLowerCase().equals("public"))
     316        {
     317            _publicAccess = true;
     318        }
     319        else if (defaultAccess.toLowerCase().equals("private"))
     320        {
     321            _publicAccess = false;
     322        }
     323        else
     324        {
     325            logger.warn("Default access for collection " + this.cluster_name + " is neither public or private, assuming public");
     326        }
     327
     328        if (scope.toLowerCase().equals("collection"))
     329        {
     330            _securityScopeCollection = true;
     331        }
     332        else if (scope.toLowerCase().equals("documents") || scope.toLowerCase().equals("document"))
     333        {
     334            _securityScopeCollection = false;
     335        }
     336        else
     337        {
     338            logger.warn("Security scope is neither collection or document, assuming collection");
     339        }
     340
     341        NodeList exceptions = GSXML.getChildrenByTagName(securityBlock, GSXML.EXCEPTION_ELEM);
     342
     343        if (exceptions.getLength() > 0)
     344        {
     345            if (!_securityScopeCollection)
     346            {
     347                NodeList documentSetElems = GSXML.getChildrenByTagName(securityBlock, GSXML.DOCUMENT_SET_ELEM);
     348                for (int i = 0; i < documentSetElems.getLength(); i++)
     349                {
     350                    Element documentSet = (Element) documentSetElems.item(i);
     351                    String setName = documentSet.getAttribute(GSXML.NAME_ATT);
     352                    NodeList matchStatements = GSXML.getChildrenByTagName(documentSet, GSXML.MATCH_ELEM);
     353                    ArrayList<Element> matchStatementList = new ArrayList<Element>();
     354                    for (int j = 0; j < matchStatements.getLength(); j++)
     355                    {
     356                        matchStatementList.add((Element) matchStatements.item(j));
     357                    }
     358                    _documentSets.put(setName, matchStatementList);
     359                }
     360            }
     361
     362            for (int i = 0; i < exceptions.getLength(); i++)
     363            {
     364                HashMap<String, ArrayList<String>> securityException = new HashMap<String, ArrayList<String>>();
     365                ArrayList<String> exceptionGroups = new ArrayList<String>();
     366                ArrayList<String> exceptionSets = new ArrayList<String>();
     367
     368                Element exception = (Element) exceptions.item(i);
     369                NodeList groups = GSXML.getChildrenByTagName(exception, GSXML.GROUP_ELEM);
     370                for (int j = 0; j < groups.getLength(); j++)
     371                {
     372                    Element group = (Element) groups.item(j);
     373                    String groupName = group.getAttribute(GSXML.NAME_ATT);
     374                    exceptionGroups.add(groupName);
     375                }
     376                NodeList docSets = GSXML.getChildrenByTagName(exception, GSXML.DOCUMENT_SET_ELEM);
     377                for (int j = 0; j < docSets.getLength(); j++)
     378                {
     379                    Element docSet = (Element) docSets.item(j);
     380                    String docSetName = docSet.getAttribute(GSXML.NAME_ATT);
     381                    exceptionSets.add(docSetName);
     382                }
     383
     384                securityException.put("groups", exceptionGroups);
     385                securityException.put("sets", exceptionSets);
     386                _securityExceptions.add(securityException);
     387            }
     388        }
    295389    }
    296390
     
    431525                classifier = request.getAttribute("classifier");
    432526            }
    433            
     527
    434528            // check for version file
    435529            String directory = new File(GSFile.collectionConfigFile(this.site_home, this.cluster_name)).getParent() + File.separator;
     
    661755            }
    662756        }
     757        else if (type.equals(GSXML.REQUEST_TYPE_SECURITY))
     758        {
     759            String oid = request.getAttribute("oid");
     760            ArrayList<String> groups = getPermittedGroups(oid);
     761           
     762            Element groupList = this.doc.createElement(GSXML.GROUP_ELEM + GSXML.LIST_MODIFIER);
     763            response.appendChild(groupList);
     764           
     765            for(String groupName : groups)
     766            {
     767                Element group = this.doc.createElement(GSXML.GROUP_ELEM);
     768                groupList.appendChild(group);
     769                group.setAttribute(GSXML.NAME_ATT, groupName);
     770            }
     771        }
    663772        else
    664773        { // unknown type
     
    669778    }
    670779
     780    protected ArrayList<String> getPermittedGroups(String oid)
     781    {
     782        ArrayList<String> groups = new ArrayList<String>();
     783       
     784        if (_securityScopeCollection)
     785        {
     786            if (_publicAccess)
     787            {
     788                groups.add("");
     789            }
     790            else
     791            {
     792                for (HashMap<String, ArrayList<String>> exception : _securityExceptions)
     793                {
     794                    for (String group : exception.get("groups"))
     795                    {
     796                        groups.add(group);
     797                    }
     798                }
     799            }
     800        }
     801        else
     802        {
     803            if(oid != null && !oid.equals(""))
     804            {
     805                boolean inSet = false;
     806                for (HashMap<String, ArrayList<String>> exception : _securityExceptions)
     807                {
     808                    for (String setName : exception.get("sets"))
     809                    {
     810                        if (documentIsInSet(oid, setName))
     811                        {
     812                            inSet = true;
     813                            for (String group : exception.get("groups"))
     814                            {
     815                                groups.add(group);
     816                            }
     817                        }
     818                    }
     819                }
     820               
     821                if(!inSet && _publicAccess)
     822                {
     823                    groups.add("");
     824                }
     825            }
     826            else
     827            {
     828                groups.add("");
     829            }
     830        }
     831
     832        return groups;
     833    }
     834
     835    protected boolean documentIsInSet(String oid, String setName)
     836    {
     837        ArrayList<Element> matchStatements = _documentSets.get(setName);
     838        if (matchStatements == null || matchStatements.size() == 0)
     839        {
     840            return false;
     841        }
     842
     843        for (Element currentMatchStatement : matchStatements)
     844        {
     845            String fieldName = currentMatchStatement.getAttribute(GSXML.FIELD_ATT);
     846            if (fieldName == null || fieldName.equals(""))
     847            {
     848                fieldName = "oid";
     849            }
     850
     851            String type = currentMatchStatement.getAttribute(GSXML.TYPE_ATT);
     852            if (type == null || type.equals(""))
     853            {
     854                type = "match";
     855            }
     856
     857            String fieldValue = "";
     858            if (!fieldName.equals("oid"))
     859            {
     860                fieldValue = getFieldValue(oid, fieldName);
     861                if(fieldValue == null)
     862                {
     863                    return false;
     864                }
     865            }
     866            else
     867            {
     868                fieldValue = oid;
     869            }
     870
     871            String matchValue = GSXML.getNodeText(currentMatchStatement);
     872            if (type.equals("match"))
     873            {
     874                if(matchValue.equals(fieldValue))
     875                {
     876                    return true;
     877                }
     878            }
     879            else if (type.equals("regex"))
     880            {
     881                if(fieldValue.matches(matchValue))
     882                {
     883                    return true;
     884                }
     885            }
     886            else
     887            {
     888                logger.warn("Unknown type of match specified in security block of collection " + this.cluster_name + ".");
     889            }
     890        }
     891
     892        return false;
     893    }
     894
     895    protected String getFieldValue(String oid, String fieldName)
     896    {
     897        Element metadataMessage = this.doc.createElement(GSXML.MESSAGE_ELEM);
     898        Element metadataRequest = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, this.cluster_name + "/DocumentMetadataRetrieve", new UserContext());
     899        metadataMessage.appendChild(metadataRequest);
     900
     901        Element paramList = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
     902        metadataRequest.appendChild(paramList);
     903       
     904        Element param = this.doc.createElement(GSXML.PARAM_ELEM);
     905        paramList.appendChild(param);
     906       
     907        param.setAttribute(GSXML.NAME_ATT, "metadata");
     908        param.setAttribute(GSXML.VALUE_ATT, fieldName);
     909       
     910        Element docList = this.doc.createElement(GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER);
     911        metadataRequest.appendChild(docList);
     912       
     913        Element doc = this.doc.createElement(GSXML.DOC_NODE_ELEM);
     914        docList.appendChild(doc);
     915       
     916        doc.setAttribute(GSXML.NODE_ID_ATT, oid);
     917       
     918        Element response = (Element) this.router.process(metadataMessage);
     919        NodeList metadataElems = response.getElementsByTagName(GSXML.METADATA_ELEM);
     920       
     921        if(metadataElems.getLength() > 0)
     922        {
     923            Element metadata = (Element) metadataElems.item(0);
     924            return GSXML.getNodeText(metadata);
     925        }
     926       
     927        return null;
     928    }
    671929}
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/core/Receptionist.java

    r25090 r25092  
    170170    public Node process(Node message_node)
    171171    {
    172 
    173172        Element message = this.converter.nodeToElement(message_node);
    174173
     
    182181        // check the request type
    183182        String type = request.getAttribute(GSXML.TYPE_ATT); // returns "" if no att of this name
     183        if(type.equals(GSXML.REQUEST_TYPE_SECURITY))
     184        {
     185            return this.mr.process(message);
     186        }
     187       
    184188        if (!type.equals(GSXML.REQUEST_TYPE_PAGE))
    185189        {
     
    235239        //logger.info(a+" mesa=" + this.converter.getPrettyString(message));
    236240        // get the page data from the action
     241
    237242        Node action_response = a.process(message);
    238243
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/DerbyWrapper.java

    r24983 r25092  
    2727import java.sql.SQLWarning;
    2828import java.sql.Statement;
     29import java.util.ArrayList;
     30import java.util.HashMap;
    2931import java.util.Properties;
    3032
     
    3537    static final String USERSDB = "usersDB";
    3638    static final String USERS = "users";
     39    static final String ROLES = "roles";
    3740    private Connection conn = null;
    3841    private Statement state = null;
     
    98101    }
    99102
    100     public void createDatabase() throws SQLException
    101     {
    102         conn.setAutoCommit(false);
    103         state.execute("create table users (username varchar(40) not null, password varchar(40) not null, groups varchar(500), accountstatus varchar(10), comment varchar(100), primary key(username))");
    104         //ystem.out.println("table users created successfully!");
    105         state.execute("insert into " + USERS + " values ('admin', 'admin', 'administrator,all-collections-editor', 'true', 'change the password for this account as soon as possible')");
    106         conn.commit();
     103    public void createDatabase()
     104    {
     105        try
     106        {
     107            conn.setAutoCommit(false);
     108            state.execute("create table users (username varchar(40) not null, password varchar(40) not null, accountstatus varchar(10), comment varchar(100), primary key(username))");
     109            state.execute("create table roles (username varchar(40) not null, role varchar(40) not null, primary key (username, role))");
     110            //ystem.out.println("table users created successfully!");
     111            state.execute("insert into " + USERS + " values ('admin', 'admin', 'true', 'change the password for this account as soon as possible')");
     112            state.execute("insert into " + ROLES + " values ('admin', 'administrator')");
     113            state.execute("insert into " + ROLES + " values ('admin', 'all-collections-editor')");
     114            conn.commit();
     115        }
     116        catch(Exception ex)
     117        {
     118            ex.printStackTrace();
     119        }
    107120    }
    108121
     
    110123    {
    111124        UserQueryResult userQueryResult = new UserQueryResult();
    112         String sql_list_all_user = "select username, password, groups, accountstatus, comment from " + USERS;
    113 
     125        String sql_list_all_user = "SELECT username, password, accountstatus, comment FROM " + USERS;
     126
     127        ArrayList<HashMap<String, String>> users = new ArrayList<HashMap<String,String>>();
    114128        ResultSet rs = state.executeQuery(sql_list_all_user);
    115129        while (rs.next())
    116130        {
    117             String returned_username = rs.getString("username");
    118             String returned_password = rs.getString("password");
    119             String returned_groups = rs.getString("groups");
    120             String returned_accountstatus = rs.getString("accountstatus");
    121             String returned_comment = rs.getString("comment");
    122             userQueryResult.addUserTerm(returned_username, returned_password, returned_groups, returned_accountstatus, returned_comment);
    123         }
     131            HashMap<String, String> user = new HashMap<String, String>();
     132            user.put("username", rs.getString("username"));
     133            user.put("password", rs.getString("password"));
     134            user.put("as", rs.getString("accountstatus"));
     135            user.put("comment", rs.getString("comment"));
     136           
     137            users.add(user);
     138        }
     139       
     140        for(HashMap<String, String> user : users)
     141        {
     142            ResultSet gs = state.executeQuery("SELECT role FROM roles WHERE username = '" + user.get("username") + "'");
     143            String group = "";
     144            while(gs.next())
     145            {
     146                if(!group.equals(""))
     147                {
     148                    group += ",";
     149                }
     150                group += gs.getString("role");
     151            }
     152            userQueryResult.addUserTerm(user.get("username"), user.get("password"), group, user.get("as"), user.get("comment"));
     153        }
     154       
    124155        if (userQueryResult.getSize() == 0)
    125156        {
     
    133164    }
    134165
    135     public String addUser(String username, String password, String groups, String accountstatus, String comment) throws SQLException
    136     {
    137         conn.setAutoCommit(false);
    138         String sql_insert_user = "insert into " + USERS + " values ('" + username + "', '" + password + "', '" + groups + "', '" + accountstatus + "', '" + comment + "')";
    139         try
    140         {
     166    public String addUser(String username, String password, String groups, String accountstatus, String comment)
     167    {
     168        try
     169        {
     170            conn.setAutoCommit(false);
     171            String sql_insert_user = "insert into " + USERS + " values ('" + username + "', '" + password + "', '" + accountstatus + "', '" + comment + "')";
    141172            state.execute(sql_insert_user);
     173           
     174            String[] groupArray = groups.split(",");
     175            for(String g : groupArray)
     176            {
     177                String sql_insert_group = "insert into " + ROLES + " values ('" + username + "', '" + g + "')";
     178                state.execute(sql_insert_group);
     179            }
     180           
    142181            conn.commit();
    143182        }
     
    157196            return "Error:" + e.getMessage();
    158197        }
     198       
    159199        return "succeed";
    160200    }
    161201
    162     public String deleteUser(String del_username) throws SQLException
    163     {
    164         conn.setAutoCommit(false);
    165         String sql_delete_user = "delete from " + USERS + " where username='" + del_username + "'";
    166         try
    167         {
     202    public String deleteUser(String del_username)
     203    {
     204        try
     205        {
     206            conn.setAutoCommit(false);
     207            String sql_delete_user = "delete from " + USERS + " where username='" + del_username + "'";
     208            String sql_delete_groups = "delete from " + ROLES + " where username='" + del_username + "'";
    168209            state.execute(sql_delete_user);
     210            state.execute(sql_delete_groups);
    169211            conn.commit();
    170212        }
     
    192234        {
    193235            state.execute("delete from " + USERS);
     236            state.execute("delete from " + ROLES);
    194237            conn.commit();
    195238        }
     
    216259
    217260        conn.setAutoCommit(false);
    218         String sql_find_user = "SELECT  username, password, groups, accountstatus, comment FROM " + USERS;
     261        String sql_find_user = "SELECT  username, password, accountstatus, comment FROM " + USERS;
    219262        String append_sql = "";
    220263
     
    238281            sql_find_user += append_sql;
    239282        }
     283       
     284        ArrayList<HashMap<String, String>> users = new ArrayList<HashMap<String,String>>();
    240285        ResultSet rs = state.executeQuery(sql_find_user);
    241286        while (rs.next())
    242287        {
    243             String returned_username = rs.getString("username");
    244             //System.out.println("returned_username :" + returned_username);
    245             String returned_password = rs.getString("password");
    246             //System.out.println("returned_password :" + returned_password);
    247             String returned_groups = rs.getString("groups");
    248             //System.out.println("returned_groups :" + returned_groups);
    249             String returned_accountstatus = rs.getString("accountstatus");
    250             //System.out.println("returned_accountstatus :" + returned_accountstatus);
    251             String returned_comment = rs.getString("comment");
    252             //System.out.println("returned_comment :" + returned_comment);
    253             userQueryResult.addUserTerm(returned_username, returned_password, returned_groups, returned_accountstatus, returned_comment);
    254             //System.out.println(userQueryResult.toString());
     288            HashMap<String, String> user = new HashMap<String, String>();
     289            user.put("username", rs.getString("username"));
     290            user.put("password", rs.getString("password"));
     291            user.put("as", rs.getString("accountstatus"));
     292            user.put("comment", rs.getString("comment"));
     293           
     294            users.add(user);
    255295        }
    256296        conn.commit();
     297       
     298        for(HashMap<String, String> user : users)
     299        {
     300            ResultSet gs = state.executeQuery("SELECT role FROM " + ROLES + " WHERE username = '" + user.get("username") + "'");
     301           
     302            String group = "";
     303            while(gs.next())
     304            {
     305                if(!group.equals(""))
     306                {
     307                    group += ",";
     308                }
     309                group += gs.getString("role");
     310            }
     311           
     312            System.out.println("GROUP = " + group);
     313           
     314            userQueryResult.addUserTerm(user.get("username"), user.get("password"), group, user.get("as"), user.get("comment"));
     315        }
     316       
    257317        if (userQueryResult.getSize() > 0)
    258318        {
     
    266326    }
    267327
    268     public String modifyUserInfo(String username, String new_password, String groups, String accountstatus, String comment) throws SQLException
    269     {
    270         conn.setAutoCommit(false);
    271         String sql_modify_user_info = "update " + USERS + " set ";
    272         if (new_password != null && !new_password.equals(""))
    273         {
    274             sql_modify_user_info += "password='" + new_password + "'";
    275         }
    276 
    277         if (groups != null && accountstatus != null && comment != null)
    278         {
    279             sql_modify_user_info += ", groups='" + groups + "'" + ", accountstatus='" + accountstatus + "'" + ", comment='" + comment + "'";
    280         }
    281         sql_modify_user_info += " where username='" + username + "'";
    282 
    283         try
    284         {
     328    public String modifyUserInfo(String username, String new_password, String groups, String accountstatus, String comment)
     329    {
     330        try
     331        {
     332            conn.setAutoCommit(false);
     333            String sql_modify_user_info = "update " + USERS + " set ";
     334            if (new_password != null && !new_password.equals(""))
     335            {
     336                sql_modify_user_info += "password='" + new_password + "'";
     337            }
     338   
     339            if (accountstatus != null && comment != null)
     340            {
     341                sql_modify_user_info += ", accountstatus='" + accountstatus + "'" + ", comment='" + comment + "'";
     342            }
     343            sql_modify_user_info += " where username='" + username + "'";
    285344            System.out.println(sql_modify_user_info);
    286345            state.execute(sql_modify_user_info);
     346           
     347            String sql_delete_groups = "delete from " + ROLES + " where username='" + username + "'";
     348            state.execute(sql_delete_groups);
     349           
     350            String[] groupsArray = groups.split(",");
     351            for(String g : groupsArray)
     352            {
     353                String sql_insert_group = "insert into " + ROLES + " values ('" + username + "', '" + g + "')";
     354                state.execute(sql_insert_group);
     355            }
     356           
    287357            conn.commit();
    288358        }
     
    307377    {
    308378        UserQueryResult userQueryResult = new UserQueryResult();
    309         String sql_list_all_user = "select username, password, groups, accountstatus, comment from " + USERS;
     379        String sql_list_all_user = "select username, password, accountstatus, comment from " + USERS;
    310380        ResultSet rs = state.executeQuery(sql_list_all_user);
    311381
     
    318388            String returned_accountstatus = rs.getString("accountstatus");
    319389            System.out.println("<enabled>" + returned_accountstatus);
    320             String returned_groups = rs.getString("groups");
     390            ResultSet groupsSet = state.executeQuery("SELECT role FROM " + ROLES + " WHERE username = '" + returned_username + "'");
     391            String returned_groups = "";
     392            while(groupsSet.next())
     393            {
     394                if(!returned_groups.equals(""))
     395                {
     396                    returned_groups += ",";
     397                }
     398                returned_groups += groupsSet.getString("role");
     399            }
    321400            System.out.println("<groups>" + returned_groups);
    322401            String returned_password = rot13(rs.getString("password"));
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/GSXML.java

    r24993 r25092  
    209209    // save the format string
    210210    public static final String REQUEST_TYPE_FORMAT_STRING = "formatString";
     211    // check credentials
     212    public static final String REQUEST_TYPE_SECURITY = "security";
    211213
    212214    // service types
     
    254256    public static final String ERROR = "error";
    255257
     258    //security tags and attributes
     259    public static final String SECURITY_ELEM = "security";
     260    public static final String SCOPE_ATT = "scope";
     261    public static final String DEFAULT_ACCESS_ATT = "default_access";
     262    public static final String EXCEPTION_ELEM = "exception";
     263    public static final String DOCUMENT_SET_ELEM = "documentSet";
     264    public static final String GROUP_ELEM = "group";
     265    public static final String MATCH_ELEM = "match";
     266    public static final String FIELD_ATT = "field";
     267
    256268    /**
    257269     * takes a list of elements, and returns an array of strings of the values
     
    10901102    private static void xmlNodeToString(StringBuffer sb, Node e, int depth, boolean printText)
    10911103    {
    1092 
     1104        if(e == null)
     1105        {
     1106            return;
     1107        }
     1108       
    10931109        for (int i = 0; i < depth; i++)
    10941110            sb.append(' ');
     
    12241240        }
    12251241    }
     1242
     1243    public static ArrayList<String> getGroupsFromSecurityResponse(Element securityResponse)
     1244    {
     1245        ArrayList<String> groups = new ArrayList<String>();
     1246       
     1247        Element groupList = (Element) GSXML.getChildByTagName(securityResponse, GSXML.GROUP_ELEM + GSXML.LIST_MODIFIER);
     1248        if(groupList == null)
     1249        {
     1250            return groups;
     1251        }
     1252       
     1253        NodeList groupElems = GSXML.getChildrenByTagName(groupList, GSXML.GROUP_ELEM);
     1254       
     1255        for(int i = 0; i < groupElems.getLength(); i++)
     1256        {
     1257            Element groupElem = (Element) groupElems.item(i);
     1258            groups.add(groupElem.getAttribute(GSXML.NAME_ATT));
     1259        }
     1260       
     1261        return groups;
     1262    }
    12261263}
Note: See TracChangeset for help on using the changeset viewer.