Search in sources :

Example 1 with Storage

use of org.collectionspace.csp.api.persistence.Storage in project application by collectionspace.

the class WebLoginStatus method testlogin.

public void testlogin(Request in) throws UIException {
    UIRequest request = in.getUIRequest();
    try {
        Storage storage = in.getStorage();
        JSONObject output = new JSONObject();
        UISession uiSession = request.getSession();
        if (uiSession != null && uiSession.getValue(UISession.USERID) != null) {
            if (uiSession.getValue(UISession.USERID).equals("")) {
                output.put("login", false);
            } else {
                JSONObject perms = null;
                // See if there is a cache of the permissions for this user and tenant.
                String userId = (String) uiSession.getValue(UISession.USERID);
                String tenantId = (String) uiSession.getValue(UISession.TENANT);
                perms = findPermsInCache(userId, tenantId);
                boolean fFoundInCache;
                if (perms != null) {
                    fFoundInCache = true;
                } else {
                    fFoundInCache = false;
                    perms = getPermissions(storage);
                }
                if (perms.has("permissions")) {
                    // Will only slow down edge case of user with no roles.
                    if (!fFoundInCache) {
                        addPermsToCache(userId, tenantId, perms);
                    }
                    output.put("permissions", perms.getJSONObject("permissions"));
                    output.put("csid", perms.getString("csid"));
                    output.put("screenName", perms.getString("screenName"));
                    output.put("userId", perms.getString("userId"));
                    output.put("login", true);
                    int maxInterval = 0;
                    UIRequest uir = in.getUIRequest();
                    if (uir != null) {
                        HttpSession httpSession = request.getHttpSession();
                        if (httpSession != null) {
                            maxInterval = httpSession.getMaxInactiveInterval();
                        }
                    }
                    // Need to consider the shorter of session timeout and cookie expiry.
                    // cookie life is in minutes, so convert to seconds.
                    int cookieLife = 60 * spec.getAdminData().getCookieLife();
                    if (maxInterval == 0 || maxInterval >= cookieLife) {
                        maxInterval = cookieLife;
                    }
                    output.put("maxInactive", maxInterval);
                } else {
                    output.put("login", false);
                    output.put("message", "no roles associated with this user");
                }
            }
        } else {
            output.put("login", false);
        }
        request.sendJSONResponse(output);
    } catch (JSONException x) {
        throw new UIException("Failed to parse json: " + x.getMessage(), x);
    } catch (ExistException x) {
        // failed login test
        throw new UIException("Existence exception: ", x);
    } catch (UnimplementedException x) {
        throw new UIException("Unimplemented exception: ", x);
    } catch (UnderlyingStorageException x) {
        UIException uiexception = new UIException(x.getMessage(), x.getStatus(), x.getUrl(), x);
        request.sendJSONResponse(uiexception.getJSON());
    }
}
Also used : HttpSession(javax.servlet.http.HttpSession) JSONException(org.json.JSONException) ExistException(org.collectionspace.csp.api.persistence.ExistException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) UIRequest(org.collectionspace.csp.api.ui.UIRequest) Storage(org.collectionspace.csp.api.persistence.Storage) JSONObject(org.json.JSONObject) UISession(org.collectionspace.csp.api.ui.UISession) UIException(org.collectionspace.csp.api.ui.UIException) UnimplementedException(org.collectionspace.csp.api.persistence.UnimplementedException)

Example 2 with Storage

use of org.collectionspace.csp.api.persistence.Storage in project application by collectionspace.

the class TestPermissions method testRoles.

// permissions
// create user
// list all permissions for a role
// add permission to a role
// change permission action in a role
@Test
public void testRoles() throws Exception {
    // create user
    JSONObject userdata = createUser("user1.json");
    String userId = userdata.getString("userId");
    String csidId = userdata.getString("accountId");
    assertFalse(userdata == null);
    // list roles for user
    // cspace-services/authorization/roles/{csid}/permroles/xxx
    Storage ss;
    ss = makeServicesStorage();
    JSONObject data = ss.getPathsJSON("accounts/" + userId + "/accountrole", null);
    String[] roleperms = (String[]) data.get("listItems");
    log.info(data.toString());
    if (roleperms.length > 0) {
        log.info("has roles already");
    }
    // create a role
    JSONObject roledata = createRole("role.json");
    String role = roledata.getString("roleId");
    log.info(roledata.toString());
    assertFalse(roledata == null);
    // add a role
    JSONObject addroledata = new JSONObject();
    addroledata.put("roleId", role);
    addroledata.put("roleName", roledata.getString("roleName"));
    JSONArray rolearray = new JSONArray();
    rolearray.put(addroledata);
    JSONObject addrole = new JSONObject();
    addrole.put("role", rolearray);
    addrole.put("account", userdata);
    log.info(addrole.toString());
    // add permissions to role
    String path = ss.autocreateJSON("userrole", addrole, null);
    log.info(path);
    assertNotNull(path);
    // delete role
    ss.deleteJSON("role/" + role);
    try {
        ss.retrieveJSON("role/" + role, new JSONObject());
        // XXX use JUnit exception annotation
        assertFalse(true);
    } catch (UnderlyingStorageException e) {
        // XXX use JUnit exception annotation
        assertTrue(true);
    }
    // delete user
    ss.deleteJSON("users/" + csidId);
}
Also used : Storage(org.collectionspace.csp.api.persistence.Storage) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) Test(org.junit.Test)

Example 3 with Storage

use of org.collectionspace.csp.api.persistence.Storage in project application by collectionspace.

the class TestServiceThroughAPI method testGetId.

@Test
public void testGetId() throws Exception {
    Storage ss = makeServicesStorage();
    JSONObject jo = ss.retrieveJSON("id/intake", new JSONObject());
    assertTrue(jo.getString("next").startsWith("IN" + getCurrentYear() + "."));
    jo = ss.retrieveJSON("id/accession", new JSONObject());
    assertTrue(jo.getString("next").startsWith(getCurrentYear() + ".1."));
}
Also used : Storage(org.collectionspace.csp.api.persistence.Storage) JSONObject(org.json.JSONObject) Test(org.junit.Test)

Example 4 with Storage

use of org.collectionspace.csp.api.persistence.Storage in project application by collectionspace.

the class TestServiceThroughAPI method testMini.

@Test
public void testMini() throws Exception {
    Storage ss = makeServicesStorage();
    String p1 = ss.autocreateJSON("intake/", getJSON("intake.json"), null);
    JSONObject mini = ss.retrieveJSON("intake/" + p1 + "/view", new JSONObject());
    assertEquals("TEST4", mini.getString("summary"));
    assertEquals("IN2010.337", mini.getString("number"));
    ss.deleteJSON("intake/" + p1);
    try {
        ss.retrieveJSON("intake/" + p1, new JSONObject());
        // XXX use JUnit exception annotation
        assertFalse(true);
    } catch (Exception e) {
        // XXX use JUnit exception annotation
        assertTrue(true);
    }
}
Also used : Storage(org.collectionspace.csp.api.persistence.Storage) JSONObject(org.json.JSONObject) ExistException(org.collectionspace.csp.api.persistence.ExistException) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException) Test(org.junit.Test)

Example 5 with Storage

use of org.collectionspace.csp.api.persistence.Storage in project application by collectionspace.

the class TestRelations method testRelationsThroughAPI.

@Test
public void testRelationsThroughAPI() throws Exception {
    Storage ss = makeServicesStorage();
    // create 3 objects
    String obj1 = makeRecord(ss, "A");
    String obj2 = makeRecord(ss, "B");
    String obj3 = makeRecord(ss, "C");
    // relate obj1 and obj2
    String path = relate(ss, obj1, obj2);
    // relate  obj2 and obj3
    String path2 = relate(ss, obj2, obj3);
    // test relationship
    JSONObject data2 = ss.retrieveJSON("relations/main/" + path, new JSONObject());
    assertTrue(JSONUtils.checkJSONEquiv("CollectionObject/" + obj1, data2.getString("src")));
    assertTrue(JSONUtils.checkJSONEquiv("CollectionObject/" + obj2, data2.getString("dst")));
    assertTrue(JSONUtils.checkJSONEquiv("affects", data2.getString("type")));
    // update
    updaterelate(ss, path, obj1, obj3);
    // get
    JSONObject data3 = ss.retrieveJSON("relations/main/" + path, new JSONObject());
    assertTrue(JSONUtils.checkJSONEquiv("CollectionObject/" + obj1, data3.getString("src")));
    assertTrue(JSONUtils.checkJSONEquiv("CollectionObject/" + obj3, data3.getString("dst")));
    assertTrue(JSONUtils.checkJSONEquiv("affects", data3.getString("type")));
    // get list
    JSONObject searchRestriction = new JSONObject();
    searchRestriction.put("src", "collection-object/" + obj2);
    searchRestriction.put("type", "affects");
    // simple list
    // XXX CSPACE-1080 - will need to update if this is improved
    JSONObject datalist = ss.getPathsJSON("relations/main", searchRestriction);
    int truecount = 0;
    String[] paths = (String[]) datalist.get("listItems");
    log.info(datalist.toString());
    JSONObject pagination = datalist.getJSONObject("pagination");
    boolean pagbool = false;
    for (int i = 0; i < paths.length; i++) {
        if (paths[i].equals(path) || paths[i].equals(path2)) {
            truecount++;
        }
    }
    assertTrue(truecount > 0);
    // delete
    ss.deleteJSON("/relations/main/" + path);
    ss.deleteJSON("/relations/main/" + path2);
    // delete objects
    ss.deleteJSON("collection-object/" + obj1);
    ss.deleteJSON("collection-object/" + obj2);
    ss.deleteJSON("collection-object/" + obj3);
}
Also used : Storage(org.collectionspace.csp.api.persistence.Storage) JSONObject(org.json.JSONObject) Test(org.junit.Test)

Aggregations

Storage (org.collectionspace.csp.api.persistence.Storage)15 JSONObject (org.json.JSONObject)13 Test (org.junit.Test)10 ExistException (org.collectionspace.csp.api.persistence.ExistException)8 UnderlyingStorageException (org.collectionspace.csp.api.persistence.UnderlyingStorageException)7 UnimplementedException (org.collectionspace.csp.api.persistence.UnimplementedException)5 JSONException (org.json.JSONException)5 ConnectionException (org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)4 CSPDependencyException (org.collectionspace.csp.api.core.CSPDependencyException)4 IOException (java.io.IOException)2 JSONArray (org.json.JSONArray)2 HttpSession (javax.servlet.http.HttpSession)1 ReturnedDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument)1 UIException (org.collectionspace.csp.api.ui.UIException)1 UIRequest (org.collectionspace.csp.api.ui.UIRequest)1 UISession (org.collectionspace.csp.api.ui.UISession)1