forked from mastepanoski/keycloak-authz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProtectedResource.js
64 lines (56 loc) · 1.71 KB
/
ProtectedResource.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const UMAResource = require('./UMAResource'),
HttpResource = require('./HttpResource');
class ProtectedResource extends HttpResource {
constructor(authzClient) {
super(authzClient);
}
create(resource) {
if (!resource || !resource.name) throw new Error('Resource is required');
return this.post(
'/authz/protection/resource_set',
resource.serialize(),
this._client.realm
).then(({ body }) => {
resource.setId(body['_id']);
return resource;
});
}
update(resource) {
if (!resource || !resource.name) throw new Error('Resource is required');
return this.put(
'/authz/protection/resource_set/' + resource.id,
resource.serialize(),
this._client.realm
).then(() => {
return resource;
});
}
findById(id) {
if (!id) throw new Error('Id is required');
return this.get(`/authz/protection/resource_set/${id}`, this._client.realm)
.then(({ body }) => {
if (!body) return false;
return new UMAResource(body);
})
.catch(() => false);
}
findByFilter(filter) {
if (!filter) throw new Error('Filter is required');
return this.get(`/authz/protection/resource_set`, { filter }, this._client.realm).then(
({ body }) => {
return Promise.all(body.map(id => this.findById(id)));
}
);
}
findAll(deep = false) {
return this.get(`/authz/protection/resource_set`, this._client.realm).then(({ body }) => {
if (!deep) return body;
return Promise.all(body.map(id => this.findById(id)));
});
}
deleteById(id) {
if (!id) throw new Error('Id is required');
return this.delete('/authz/protection/resource_set/' + id);
}
}
module.exports = ProtectedResource;