Skip to content

Commit

Permalink
Revert "add capabilities service (from #28168)"
Browse files Browse the repository at this point in the history
This reverts commit 3fdafc7.
  • Loading branch information
hashishaw committed Sep 13, 2024
1 parent 2fe433e commit 30aa6e4
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 656 deletions.
57 changes: 11 additions & 46 deletions ui/app/adapters/capabilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,24 @@
import AdapterError from '@ember-data/adapter/error';
import { set } from '@ember/object';
import ApplicationAdapter from './application';
import { sanitizePath, sanitizeStart } from 'core/utils/sanitize-path';
import { sanitizePath } from 'core/utils/sanitize-path';

export default class CapabilitiesAdapter extends ApplicationAdapter {
export default ApplicationAdapter.extend({
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) {
formatPaths(path) {
const { relativeNamespace } = this.namespaceService;
if (!relativeNamespace) {
return path;
return [path];
}
// ensure original path doesn't have leading slash
return `${relativeNamespace}/${sanitizeStart(path)}`;
}
return [`${relativeNamespace}/${path.replace(/^\//, '')}`];
},

async findRecord(store, type, id) {
const paths = [this._formatPath(id)];
const paths = this.formatPaths(id);
return this.ajax(this.buildURL(type), 'POST', {
data: { paths },
namespace: sanitizePath(this.namespaceService.userRootNamespace),
Expand All @@ -40,7 +33,7 @@ export default class CapabilitiesAdapter extends ApplicationAdapter {
}
throw e;
});
}
},

queryRecord(store, type, query) {
const { id } = query;
Expand All @@ -51,33 +44,5 @@ export default class CapabilitiesAdapter extends ApplicationAdapter {
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;
});
}
}
},
});
21 changes: 7 additions & 14 deletions ui/app/serializers/capabilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,14 @@ export default ApplicationSerializer.extend({
primaryKey: 'path',

normalizeResponse(store, primaryModelClass, payload, id, requestType) {
let response;
// queryRecord will already have set path, and we won't have an id here
if (id) payload.path = id;

if (requestType === 'query') {
// each key on the response is a path with an array of capabilities as its value
response = Object.keys(payload.data).map((fullPath) => {
// we use pathMap to normalize a namespace-prefixed path back to the relative path
// this is okay because we clear capabilities when moving between namespaces
const path = payload.pathMap ? payload.pathMap[fullPath] : fullPath;
return { capabilities: payload.data[fullPath], path };
});
} else {
response = { ...payload.data, path: payload.path };
// queryRecord will already have set this, and we won't have an id here
if (id) {
payload.path = id;
}
const response = {
...payload.data,
path: payload.path,
};
return this._super(store, primaryModelClass, response, id, requestType);
},

Expand Down
120 changes: 0 additions & 120 deletions ui/app/services/capabilities.ts

This file was deleted.

Loading

0 comments on commit 30aa6e4

Please sign in to comment.