Search in sources :

Example 1 with Permissions

use of com.cloudant.client.api.model.Permissions in project java-cloudant by cloudant.

the class DatabaseTest method permissionsParsing.

@Test
public void permissionsParsing() throws Exception {
    CloudantClient client = CloudantClientHelper.newMockWebServerClientBuilder(mockWebServer).build();
    Database db = client.database("notarealdb", false);
    // Mock up a request of all permissions
    // for GET _security
    mockWebServer.enqueue(MockWebServerResources.PERMISSIONS);
    // for PUT _security
    mockWebServer.enqueue(MockWebServerResources.JSON_OK);
    db.setPermissions("testUsername", EnumSet.allOf(Permissions.class));
    // Mock up a failing request
    String testError = "test error";
    String testReason = "test reason";
    // for GET _security
    mockWebServer.enqueue(MockWebServerResources.PERMISSIONS);
    mockWebServer.enqueue(new MockResponse().setResponseCode(400).setBody("{\"reason\":\"" + testReason + "\", \"error\":\"" + testError + "\"}"));
    try {
        db.setPermissions("testUsername", EnumSet.allOf(Permissions.class));
    } catch (CouchDbException e) {
        assertEquals(testError, e.getError());
        assertEquals(testReason, e.getReason());
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) CouchDbException(com.cloudant.client.org.lightcouch.CouchDbException) CloudantClient(com.cloudant.client.api.CloudantClient) Database(com.cloudant.client.api.Database) Permissions(com.cloudant.client.api.model.Permissions) Test(org.junit.jupiter.api.Test)

Example 2 with Permissions

use of com.cloudant.client.api.model.Permissions in project java-cloudant by cloudant.

the class DatabaseTest method permissions.

@Test
@RequiresCloudantService
public void permissions() {
    Map<String, EnumSet<Permissions>> userPerms = db.getPermissions();
    assertNotNull(userPerms);
    ApiKey key = account.generateApiKey();
    EnumSet<Permissions> p = EnumSet.<Permissions>of(Permissions._reader, Permissions._writer);
    db.setPermissions(key.getKey(), p);
    userPerms = db.getPermissions();
    assertNotNull(userPerms);
    assertEquals(1, userPerms.size());
    assertEquals(p, userPerms.get(key.getKey()));
    p = EnumSet.noneOf(Permissions.class);
    db.setPermissions(key.getKey(), p);
    userPerms = db.getPermissions();
    assertNotNull(userPerms);
    assertEquals(1, userPerms.size());
    assertEquals(p, userPerms.get(key.getKey()));
}
Also used : ApiKey(com.cloudant.client.api.model.ApiKey) EnumSet(java.util.EnumSet) Permissions(com.cloudant.client.api.model.Permissions) Test(org.junit.jupiter.api.Test) RequiresCloudantService(com.cloudant.test.main.RequiresCloudantService)

Example 3 with Permissions

use of com.cloudant.client.api.model.Permissions in project java-cloudant by cloudant.

the class Database method setPermissions.

/**
 * Set permissions for a user/apiKey on this database.
 * <p>
 * Note this method is only applicable to databases that support the
 * <a target="_blank"
 * href="https://console.bluemix.net/docs/services/Cloudant/api/authorization.html#authorization">
 * Cloudant authorization API
 * </a> such as Cloudant DBaaS. For unsupported databases consider using the /db/_security
 * endpoint.
 * </p>
 * <p>Example usage to set read-only access for a new key on the "example" database:</p>
 * <pre>
 * {@code
 * // generate an API key
 * ApiKey key = client.generateApiKey();
 *
 * // get the "example" database
 * Database db = client.database("example", false);
 *
 * // set read-only permissions
 * db.setPermissions(key.getKey(), EnumSet.<Permissions>of(Permissions._reader));
 * }
 * </pre>
 *
 * @param userNameorApikey the user or key to apply permissions to
 * @param permissions      permissions to grant
 * @throws UnsupportedOperationException if called on a database that does not provide the
 *                                       Cloudant authorization API
 * @see CloudantClient#generateApiKey()
 * @see <a
 * href="https://console.bluemix.net/docs/services/Cloudant/api/authorization.html#roles"
 * target="_blank">Roles</a>
 * @see <a
 * href="https://console.bluemix.net/docs/services/Cloudant/api/authorization.html#modifying-permissions"
 * target="_blank">Modifying permissions</a>
 */
public void setPermissions(String userNameorApikey, EnumSet<Permissions> permissions) {
    assertNotEmpty(userNameorApikey, "userNameorApikey");
    assertNotEmpty(permissions, "permissions");
    final JsonArray jsonPermissions = new JsonArray();
    for (Permissions s : permissions) {
        final JsonPrimitive permission = new JsonPrimitive(s.toString());
        jsonPermissions.add(permission);
    }
    // get existing permissions
    JsonObject perms = getPermissionsObject();
    // now set back
    JsonElement elem = perms.get("cloudant");
    if (elem == null) {
        perms.addProperty("_id", "_security");
        elem = new JsonObject();
        perms.add("cloudant", elem);
    }
    elem.getAsJsonObject().add(userNameorApikey, jsonPermissions);
    HttpConnection put = Http.PUT(apiV2DBSecurityURI, "application/json");
    put.setRequestBody(client.getGson().toJson(perms));
    // CouchDbExceptions will be thrown for non-2XX cases
    client.couchDbClient.executeToResponse(put);
}
Also used : JsonArray(com.google.gson.JsonArray) JsonPrimitive(com.google.gson.JsonPrimitive) HttpConnection(com.cloudant.http.HttpConnection) JsonElement(com.google.gson.JsonElement) Permissions(com.cloudant.client.api.model.Permissions) JsonObject(com.google.gson.JsonObject)

Example 4 with Permissions

use of com.cloudant.client.api.model.Permissions in project java-cloudant by cloudant.

the class SecurityDeserializer method deserialize.

public Map<String, EnumSet<Permissions>> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    Map<String, EnumSet<Permissions>> perms = new HashMap<String, EnumSet<Permissions>>();
    JsonElement elem = json.getAsJsonObject().get("cloudant");
    if (elem == null) {
        return perms;
    }
    Set<Map.Entry<String, JsonElement>> permList = elem.getAsJsonObject().entrySet();
    for (Map.Entry<String, JsonElement> entry : permList) {
        String user = entry.getKey();
        EnumSet<Permissions> p = context.deserialize(entry.getValue(), DeserializationTypes.PERMISSIONS);
        perms.put(user, p);
    }
    return perms;
}
Also used : HashMap(java.util.HashMap) JsonElement(com.google.gson.JsonElement) EnumSet(java.util.EnumSet) Permissions(com.cloudant.client.api.model.Permissions) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

Permissions (com.cloudant.client.api.model.Permissions)4 JsonElement (com.google.gson.JsonElement)2 EnumSet (java.util.EnumSet)2 Test (org.junit.jupiter.api.Test)2 CloudantClient (com.cloudant.client.api.CloudantClient)1 Database (com.cloudant.client.api.Database)1 ApiKey (com.cloudant.client.api.model.ApiKey)1 CouchDbException (com.cloudant.client.org.lightcouch.CouchDbException)1 HttpConnection (com.cloudant.http.HttpConnection)1 RequiresCloudantService (com.cloudant.test.main.RequiresCloudantService)1 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 MockResponse (okhttp3.mockwebserver.MockResponse)1