Skip to content

Commit 870c052

Browse files
authored
[VET-6374] Stardog 12 Permission changes for Designer (#320)
* [VET-6374] Stardog 12 Permission changes for Designer * skip test until Stardog v12 is released
1 parent 8a4421a commit 870c052

3 files changed

Lines changed: 147 additions & 7 deletions

File tree

lib/index.d.ts

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

12321232
type Action =
1233+
| 'ALL'
12331234
| 'CREATE'
12341235
| 'DELETE'
1235-
| 'READ'
1236-
| 'WRITE'
1236+
| 'EXECUTE'
12371237
| 'GRANT'
1238+
| 'READ'
12381239
| 'REVOKE'
1239-
| 'EXECUTE';
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: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
// TODO skipped until Stardog v12 is released
13+
describe.skip('checkPermission()', () => {
14+
const database = generateDatabaseName();
15+
let conn;
16+
17+
beforeAll(seedDatabase(database));
18+
afterAll(dropDatabase(database));
19+
20+
beforeEach(() => {
21+
conn = ConnectionFactory();
22+
});
23+
24+
it('should return true for a permission the admin user has', () =>
25+
user.checkPermission(conn, 'READ', 'db', database).then(res => {
26+
expect(res.status).toBe(200);
27+
expect(res.body.hasPermission).toBe(true);
28+
}));
29+
30+
it('should return false for a permission a new user does not have', () => {
31+
const username = generateRandomString();
32+
const password = generateRandomString();
33+
34+
return user
35+
.create(conn, { username, password })
36+
.then(res => {
37+
expect(res.status).toBe(201);
38+
// Create a new connection for the unprivileged user
39+
const userConn = new Connection({
40+
username,
41+
password,
42+
endpoint: conn.uri(),
43+
});
44+
return user.checkPermission(userConn, 'WRITE', 'db', database);
45+
})
46+
.then(res => {
47+
expect(res.status).toBe(200);
48+
expect(res.body.hasPermission).toBe(false);
49+
});
50+
});
51+
52+
it('should return true after assigning a permission to a user', () => {
53+
const username = generateRandomString();
54+
const password = generateRandomString();
55+
const userConn = new Connection({
56+
username,
57+
password,
58+
endpoint: conn.uri(),
59+
});
60+
const permission = {
61+
action: 'WRITE',
62+
resourceType: 'db',
63+
resources: [database],
64+
};
65+
66+
return user
67+
.create(conn, { username, password })
68+
.then(res => {
69+
expect(res.status).toBe(201);
70+
// First verify the user does not have the permission
71+
return user.checkPermission(userConn, 'WRITE', 'db', database);
72+
})
73+
.then(res => {
74+
expect(res.status).toBe(200);
75+
expect(res.body.hasPermission).toBe(false);
76+
// Now assign the permission
77+
return user.assignPermission(conn, username, permission);
78+
})
79+
.then(res => {
80+
expect(res.status).toBe(201);
81+
// Verify the user now has the permission
82+
return user.checkPermission(userConn, 'WRITE', 'db', database);
83+
})
84+
.then(res => {
85+
expect(res.status).toBe(200);
86+
expect(res.body.hasPermission).toBe(true);
87+
});
88+
});
89+
});

0 commit comments

Comments
 (0)