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);
    }
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 ) ; } JsonObject perms = getPermissionsObject ( ) ; 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 ) ) ; client . couchDbClient . executeToResponse ( put ) ; }
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>