Skip to content

Commit ec1b22c

Browse files
committed
[VET-6374] Stardog 12 Permission changes for Designer
1 parent 8a4421a commit ec1b22c

3 files changed

Lines changed: 150 additions & 11 deletions

File tree

lib/index.d.ts

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,22 +1230,36 @@ declare namespace Stardog {
12301230
}
12311231

12321232
type Action =
1233-
| 'CREATE'
1234-
| 'DELETE'
1235-
| 'READ'
1236-
| 'WRITE'
1237-
| 'GRANT'
1238-
| 'REVOKE'
1239-
| 'EXECUTE';
1233+
| 'all'
1234+
| 'create'
1235+
| 'delete'
1236+
| 'execute'
1237+
| 'grant'
1238+
| 'read'
1239+
| 'revoke'
1240+
| 'write';
12401241

12411242
type ResourceType =
1242-
| 'db'
1243-
| 'user'
1244-
| 'role'
1243+
| '*'
12451244
| 'admin'
1245+
| 'cache'
1246+
| 'cache-target'
1247+
| 'data-source'
1248+
| 'db'
1249+
| 'db-export'
1250+
| 'dbms-admin'
1251+
| 'entity-resolution'
1252+
| 'icv-constraints'
12461253
| 'metadata'
1254+
| 'modeling'
12471255
| 'named-graph'
1248-
| 'icv-constraints';
1256+
| 'permission'
1257+
| 'role'
1258+
| 'role-assignment'
1259+
| 'sensitive-properties'
1260+
| 'stored-query'
1261+
| 'user'
1262+
| 'virtual-graph';
12491263

12501264
/**
12511265
* Gets a list of users.
@@ -1440,6 +1454,21 @@ declare namespace Stardog {
14401454
params?: object
14411455
): Promise<HTTP.Body>;
14421456

1457+
/**
1458+
* Checks if the current user has a specific permission.
1459+
*
1460+
* @param {Connection} conn the Stardog server connection
1461+
* @param {Action} action the action to check
1462+
* @param {ResourceType} resourceType the type of resource
1463+
* @param {string} resource the resource name
1464+
*/
1465+
function checkPermission(
1466+
conn: Connection,
1467+
action: Action,
1468+
resourceType: ResourceType,
1469+
resource: string
1470+
): Promise<HTTP.Body>;
1471+
14431472
/**
14441473
* Specifies whether a user is a superuser.
14451474
*

lib/user/main.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const { httpBody, httpMessage } = require('../response-transforms');
2+
const { encodeQueryString } = require('../utils');
23

34
const list = (conn, params) => {
45
const headers = conn.headers();
@@ -162,6 +163,26 @@ const effectivePermissions = (conn, username, params) => {
162163
).then(httpBody);
163164
};
164165

166+
const checkPermission = (conn, action, resourceType, resource) => {
167+
const headers = conn.headers();
168+
headers.set('Accept', 'application/json');
169+
const queryParams = {
170+
action,
171+
resource_type: resourceType,
172+
resource,
173+
};
174+
return fetch(
175+
conn.request(
176+
'admin',
177+
'permissions',
178+
`check${encodeQueryString(queryParams)}`
179+
),
180+
{
181+
headers,
182+
}
183+
).then(httpBody);
184+
};
185+
165186
const superUser = (conn, username, params) => {
166187
const headers = conn.headers();
167188
headers.set('Accept', 'application/json');
@@ -196,6 +217,7 @@ module.exports = {
196217
assignRole,
197218
assignPermission,
198219
changePassword,
220+
checkPermission,
199221
create,
200222
deletePermission,
201223
effectivePermissions,

test/checkPermission.spec.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/* eslint-env jest */
2+
3+
const { user, Connection } = require('../lib');
4+
const {
5+
seedDatabase,
6+
dropDatabase,
7+
generateDatabaseName,
8+
generateRandomString,
9+
ConnectionFactory,
10+
} = require('./setup-database');
11+
12+
describe('checkPermission()', () => {
13+
const database = generateDatabaseName();
14+
let conn;
15+
16+
beforeAll(seedDatabase(database));
17+
afterAll(dropDatabase(database));
18+
19+
beforeEach(() => {
20+
conn = ConnectionFactory();
21+
});
22+
23+
it('should return true for a permission the admin user has', () =>
24+
user.checkPermission(conn, 'READ', 'db', database).then(res => {
25+
expect(res.status).toBe(200);
26+
expect(res.body.hasPermission).toBe(true);
27+
}));
28+
29+
it('should return false for a permission a new user does not have', () => {
30+
const username = generateRandomString();
31+
const password = generateRandomString();
32+
33+
return user
34+
.create(conn, { username, password })
35+
.then(res => {
36+
expect(res.status).toBe(201);
37+
// Create a new connection for the unprivileged user
38+
const userConn = new Connection({
39+
username,
40+
password,
41+
endpoint: conn.uri(),
42+
});
43+
return user.checkPermission(userConn, 'WRITE', 'db', database);
44+
})
45+
.then(res => {
46+
expect(res.status).toBe(200);
47+
expect(res.body.hasPermission).toBe(false);
48+
});
49+
});
50+
51+
it('should return true after assigning a permission to a user', () => {
52+
const username = generateRandomString();
53+
const password = generateRandomString();
54+
const userConn = new Connection({
55+
username,
56+
password,
57+
endpoint: conn.uri(),
58+
});
59+
const permission = {
60+
action: 'WRITE',
61+
resourceType: 'db',
62+
resources: [database],
63+
};
64+
65+
return user
66+
.create(conn, { username, password })
67+
.then(res => {
68+
expect(res.status).toBe(201);
69+
// First verify the user does not have the permission
70+
return user.checkPermission(userConn, 'WRITE', 'db', database);
71+
})
72+
.then(res => {
73+
expect(res.status).toBe(200);
74+
expect(res.body.hasPermission).toBe(false);
75+
// Now assign the permission
76+
return user.assignPermission(conn, username, permission);
77+
})
78+
.then(res => {
79+
expect(res.status).toBe(201);
80+
// Verify the user now has the permission
81+
return user.checkPermission(userConn, 'WRITE', 'db', database);
82+
})
83+
.then(res => {
84+
expect(res.status).toBe(200);
85+
expect(res.body.hasPermission).toBe(true);
86+
});
87+
});
88+
});

0 commit comments

Comments
 (0)