Skip to content

Commit 108c772

Browse files
author
mpv1989
committed
Add user permission api changes
1 parent 2c2ba99 commit 108c772

File tree

9 files changed

+180
-18
lines changed

9 files changed

+180
-18
lines changed

ChangeLog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
v4.2.2 (xxxx-xx-xx)
2+
---------------------------
3+
* added ArangoDatabase.grantAccess(String, Permissions)
4+
* added ArangoCollection.grantAccess(String, Permissions)
5+
16
v4.2.1 (2017-06-20)
27
---------------------------
38
* fixed deserializing of internal field _id

src/main/java/com/arangodb/ArangoCollection.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.arangodb.entity.DocumentUpdateEntity;
3535
import com.arangodb.entity.IndexEntity;
3636
import com.arangodb.entity.MultiDocumentEntity;
37+
import com.arangodb.entity.Permissions;
3738
import com.arangodb.internal.ArangoExecutorSync;
3839
import com.arangodb.internal.InternalArangoCollection;
3940
import com.arangodb.internal.velocystream.internal.ConnectionSync;
@@ -796,4 +797,21 @@ public CollectionRevisionEntity getRevision() throws ArangoDBException {
796797
return executor.execute(getRevisionRequest(), CollectionRevisionEntity.class);
797798
}
798799

800+
/**
801+
* Grants or revoke access to the collection for user user. You need permission to the _system database in order to
802+
* execute this call.
803+
*
804+
* @see <a href=
805+
* "https://docs.arangodb.com/current/HTTP/UserManagement/index.html#grant-or-revoke-collection-access"> API
806+
* Documentation</a>
807+
* @param user
808+
* The name of the user
809+
* @param permissions
810+
* The permissions the user grant
811+
* @throws ArangoDBException
812+
*/
813+
public void grantAccess(final String user, final Permissions permissions) throws ArangoDBException {
814+
executor.execute(grantAccessRequest(user, permissions), Void.class);
815+
}
816+
799817
}

src/main/java/com/arangodb/ArangoDatabase.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.arangodb.entity.EdgeDefinition;
3434
import com.arangodb.entity.GraphEntity;
3535
import com.arangodb.entity.IndexEntity;
36+
import com.arangodb.entity.Permissions;
3637
import com.arangodb.entity.QueryCachePropertiesEntity;
3738
import com.arangodb.entity.QueryEntity;
3839
import com.arangodb.entity.QueryTrackingPropertiesEntity;
@@ -214,31 +215,51 @@ public Boolean drop() throws ArangoDBException {
214215
}
215216

216217
/**
217-
* Grants access to the database dbname for user user. You need permission to the _system database in order to
218+
* Grants or revoke access to the database for user user. You need permission to the _system database in order to
218219
* execute this call.
219220
*
220221
* @see <a href= "https://docs.arangodb.com/current/HTTP/UserManagement/index.html#grant-or-revoke-database-access">
221222
* API Documentation</a>
222223
* @param user
223224
* The name of the user
225+
* @param permissions
226+
* The permissions the user grant
224227
* @throws ArangoDBException
225228
*/
229+
public void grantAccess(final String user, final Permissions permissions) throws ArangoDBException {
230+
executor.execute(grantAccessRequest(user, permissions), Void.class);
231+
}
232+
233+
/**
234+
* Grants access to the database for user user. You need permission to the _system database in order to execute this
235+
* call.
236+
*
237+
* @deprecated use {@link #grantAccess(String, Permissions)} instead
238+
* @see <a href= "https://docs.arangodb.com/current/HTTP/UserManagement/index.html#grant-or-revoke-database-access">
239+
* API Documentation</a>
240+
* @param user
241+
* The name of the user
242+
* @throws ArangoDBException
243+
*/
244+
@Deprecated
226245
public void grantAccess(final String user) throws ArangoDBException {
227-
executor.execute(grantAccessRequest(user), Void.class);
246+
executor.execute(grantAccessRequest(user, Permissions.RW), Void.class);
228247
}
229248

230249
/**
231250
* Revokes access to the database dbname for user user. You need permission to the _system database in order to
232251
* execute this call.
233252
*
253+
* @deprecated use {@link #grantAccess(String, Permissions)} instead
234254
* @see <a href= "https://docs.arangodb.com/current/HTTP/UserManagement/index.html#grant-or-revoke-database-access">
235255
* API Documentation</a>
236256
* @param user
237257
* The name of the user
238258
* @throws ArangoDBException
239259
*/
260+
@Deprecated
240261
public void revokeAccess(final String user) throws ArangoDBException {
241-
executor.execute(revokeAccessRequest(user), Void.class);
262+
executor.execute(grantAccessRequest(user, Permissions.NONE), Void.class);
242263
}
243264

244265
/**
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* DISCLAIMER
3+
*
4+
* Copyright 2017 ArangoDB GmbH, Cologne, Germany
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
*/
20+
21+
package com.arangodb.entity;
22+
23+
/**
24+
* @author Mark Vollmary
25+
*
26+
*/
27+
public enum Permissions {
28+
29+
/**
30+
* read and write access
31+
*/
32+
RW,
33+
/**
34+
* read-only access
35+
*/
36+
RO,
37+
NONE;
38+
39+
@Override
40+
public String toString() {
41+
return super.toString().toLowerCase();
42+
}
43+
44+
}

src/main/java/com/arangodb/internal/ArangoDBConstants.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,6 @@ public class ArangoDBConstants {
8989
public static final String COLLECTIONS = "collections";
9090
public static final String EXCLUDE_SYSTEM = "excludeSystem";
9191
public static final String USER = "user";
92-
public static final String RW = "rw";
93-
public static final String NONE = "none";
9492
public static final String DATABASE = "database";
9593
public static final String CURRENT = "current";
9694
public static final String INDEXES = "indexes";

src/main/java/com/arangodb/internal/InternalArangoCollection.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.arangodb.entity.ErrorEntity;
3535
import com.arangodb.entity.IndexEntity;
3636
import com.arangodb.entity.MultiDocumentEntity;
37+
import com.arangodb.entity.Permissions;
3738
import com.arangodb.internal.ArangoExecutor.ResponseDeserializer;
3839
import com.arangodb.internal.velocystream.internal.Connection;
3940
import com.arangodb.model.CollectionPropertiesOptions;
@@ -52,6 +53,7 @@
5253
import com.arangodb.model.OptionsBuilder;
5354
import com.arangodb.model.PersistentIndexOptions;
5455
import com.arangodb.model.SkiplistIndexOptions;
56+
import com.arangodb.model.UserAccessOptions;
5557
import com.arangodb.util.ArangoSerializer;
5658
import com.arangodb.velocypack.Type;
5759
import com.arangodb.velocypack.VPackSlice;
@@ -620,4 +622,11 @@ protected Request getRevisionRequest() {
620622
return new Request(db.name(), RequestType.GET,
621623
executor.createPath(ArangoDBConstants.PATH_API_COLLECTION, name, ArangoDBConstants.REVISION));
622624
}
625+
626+
protected Request grantAccessRequest(final String user, final Permissions permissions) {
627+
return new Request(ArangoDBConstants.SYSTEM, RequestType.PUT,
628+
executor.createPath(ArangoDBConstants.PATH_API_USER, user, ArangoDBConstants.DATABASE, db.name(), name))
629+
.setBody(
630+
util().serialize(OptionsBuilder.build(new UserAccessOptions(), permissions.toString())));
631+
}
623632
}

src/main/java/com/arangodb/internal/InternalArangoDatabase.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.arangodb.entity.EdgeDefinition;
3131
import com.arangodb.entity.GraphEntity;
3232
import com.arangodb.entity.PathEntity;
33+
import com.arangodb.entity.Permissions;
3334
import com.arangodb.entity.QueryCachePropertiesEntity;
3435
import com.arangodb.entity.QueryTrackingPropertiesEntity;
3536
import com.arangodb.entity.TraversalEntity;
@@ -131,16 +132,10 @@ public Boolean deserialize(final Response response) throws VPackException {
131132
};
132133
}
133134

134-
protected Request grantAccessRequest(final String user) {
135-
return new Request(ArangoDBConstants.SYSTEM, RequestType.PUT,
136-
executor.createPath(ArangoDBConstants.PATH_API_USER, user, ArangoDBConstants.DATABASE, name))
137-
.setBody(util().serialize(OptionsBuilder.build(new UserAccessOptions(), ArangoDBConstants.RW)));
138-
}
139-
140-
protected Request revokeAccessRequest(final String user) {
135+
protected Request grantAccessRequest(final String user, final Permissions permissions) {
141136
return new Request(ArangoDBConstants.SYSTEM, RequestType.PUT,
142137
executor.createPath(ArangoDBConstants.PATH_API_USER, user, ArangoDBConstants.DATABASE, name)).setBody(
143-
util().serialize(OptionsBuilder.build(new UserAccessOptions(), ArangoDBConstants.NONE)));
138+
util().serialize(OptionsBuilder.build(new UserAccessOptions(), permissions.toString())));
144139
}
145140

146141
protected Request queryRequest(

src/test/java/com/arangodb/ArangoCollectionTest.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import com.arangodb.entity.IndexEntity;
6060
import com.arangodb.entity.IndexType;
6161
import com.arangodb.entity.MultiDocumentEntity;
62+
import com.arangodb.entity.Permissions;
6263
import com.arangodb.entity.ServerRole;
6364
import com.arangodb.model.CollectionCreateOptions;
6465
import com.arangodb.model.CollectionPropertiesOptions;
@@ -1784,4 +1785,54 @@ public void alreadyUrlEncodedkey() {
17841785
assertThat(doc.getKey(), is(key));
17851786
}
17861787

1788+
@Test
1789+
public void grantAccessRW() {
1790+
try {
1791+
arangoDB.createUser("user1", "1234", null);
1792+
db.collection(COLLECTION_NAME).grantAccess("user1", Permissions.RW);
1793+
} finally {
1794+
arangoDB.deleteUser("user1");
1795+
}
1796+
}
1797+
1798+
@Test
1799+
public void grantAccessRO() {
1800+
try {
1801+
arangoDB.createUser("user1", "1234", null);
1802+
db.collection(COLLECTION_NAME).grantAccess("user1", Permissions.RO);
1803+
} finally {
1804+
arangoDB.deleteUser("user1");
1805+
}
1806+
}
1807+
1808+
@Test
1809+
public void grantAccessNONE() {
1810+
try {
1811+
arangoDB.createUser("user1", "1234", null);
1812+
db.collection(COLLECTION_NAME).grantAccess("user1", Permissions.NONE);
1813+
} finally {
1814+
arangoDB.deleteUser("user1");
1815+
}
1816+
}
1817+
1818+
@Test(expected = ArangoDBException.class)
1819+
public void grantAccessUserNotFound() {
1820+
db.collection(COLLECTION_NAME).grantAccess("user1", Permissions.RW);
1821+
}
1822+
1823+
@Test
1824+
public void revokeAccess() {
1825+
try {
1826+
arangoDB.createUser("user1", "1234", null);
1827+
db.collection(COLLECTION_NAME).grantAccess("user1", Permissions.NONE);
1828+
} finally {
1829+
arangoDB.deleteUser("user1");
1830+
}
1831+
}
1832+
1833+
@Test(expected = ArangoDBException.class)
1834+
public void revokeAccessUserNotFound() {
1835+
db.collection(COLLECTION_NAME).grantAccess("user1", Permissions.NONE);
1836+
}
1837+
17871838
}

src/test/java/com/arangodb/ArangoDatabaseTest.java

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import com.arangodb.entity.GraphEntity;
6363
import com.arangodb.entity.IndexEntity;
6464
import com.arangodb.entity.PathEntity;
65+
import com.arangodb.entity.Permissions;
6566
import com.arangodb.entity.QueryCachePropertiesEntity;
6667
import com.arangodb.entity.QueryCachePropertiesEntity.CacheMode;
6768
import com.arangodb.entity.QueryEntity;
@@ -303,33 +304,53 @@ public void getCollectionsExcludeSystem() {
303304
}
304305

305306
@Test
306-
public void grantAccess() {
307+
public void grantAccessRW() {
307308
try {
308309
arangoDB.createUser("user1", "1234", null);
309-
db.grantAccess("user1");
310+
db.grantAccess("user1", Permissions.RW);
311+
} finally {
312+
arangoDB.deleteUser("user1");
313+
}
314+
}
315+
316+
@Test
317+
public void grantAccessRO() {
318+
try {
319+
arangoDB.createUser("user1", "1234", null);
320+
db.grantAccess("user1", Permissions.RO);
321+
} finally {
322+
arangoDB.deleteUser("user1");
323+
}
324+
}
325+
326+
@Test
327+
public void grantAccessNONE() {
328+
try {
329+
arangoDB.createUser("user1", "1234", null);
330+
db.grantAccess("user1", Permissions.NONE);
310331
} finally {
311332
arangoDB.deleteUser("user1");
312333
}
313334
}
314335

315336
@Test(expected = ArangoDBException.class)
316337
public void grantAccessUserNotFound() {
317-
db.grantAccess("user1");
338+
db.grantAccess("user1", Permissions.RW);
318339
}
319340

320341
@Test
321342
public void revokeAccess() {
322343
try {
323344
arangoDB.createUser("user1", "1234", null);
324-
db.revokeAccess("user1");
345+
db.grantAccess("user1", Permissions.NONE);
325346
} finally {
326347
arangoDB.deleteUser("user1");
327348
}
328349
}
329350

330351
@Test(expected = ArangoDBException.class)
331352
public void revokeAccessUserNotFound() {
332-
db.revokeAccess("user1");
353+
db.grantAccess("user1", Permissions.NONE);
333354
}
334355

335356
@Test

0 commit comments

Comments
 (0)