-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathcapabilities.js
83 lines (76 loc) · 2.49 KB
/
capabilities.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
import AdapterError from '@ember-data/adapter/error';
import { set } from '@ember/object';
import ApplicationAdapter from './application';
import { sanitizePath, sanitizeStart } from 'core/utils/sanitize-path';
export default class CapabilitiesAdapter extends ApplicationAdapter {
pathForType() {
return 'capabilities-self';
}
/*
users don't always have access to the capabilities-self endpoint in the current namespace,
this can happen when logging in to a namespace and then navigating to a child namespace.
adding "relativeNamespace" to the path and/or "this.namespaceService.userRootNamespace"
to the request header ensures we are querying capabilities-self in the user's root namespace,
which is where they are most likely to have their policy/permissions.
*/
_formatPath(path) {
const { relativeNamespace } = this.namespaceService;
if (!relativeNamespace) {
return path;
}
// ensure original path doesn't have leading slash
return `${relativeNamespace}/${sanitizeStart(path)}`;
}
async findRecord(store, type, id) {
const paths = [this._formatPath(id)];
return this.ajax(this.buildURL(type), 'POST', {
data: { paths },
namespace: sanitizePath(this.namespaceService.userRootNamespace),
}).catch((e) => {
if (e instanceof AdapterError) {
set(e, 'policyPath', 'sys/capabilities-self');
}
throw e;
});
}
queryRecord(store, type, query) {
const { id } = query;
if (!id) {
return;
}
return this.findRecord(store, type, id).then((resp) => {
resp.path = id;
return resp;
});
}
query(store, type, query) {
const pathMap = query?.paths.reduce((mapping, path) => {
const withNs = this._formatPath(path);
if (withNs) {
mapping[withNs] = path;
}
return mapping;
}, {});
return this.ajax(this.buildURL(type), 'POST', {
data: { paths: Object.keys(pathMap) },
namespace: sanitizePath(this.namespaceService.userRootNamespace),
})
.then((queryResult) => {
if (queryResult) {
// send the pathMap with the response so the serializer can normalize the paths to be relative to the namespace
queryResult.pathMap = pathMap;
}
return queryResult;
})
.catch((e) => {
if (e instanceof AdapterError) {
set(e, 'policyPath', 'sys/capabilities-self');
}
throw e;
});
}
}