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());
}
}
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()));
}
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);
}
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;
}
Aggregations