chore: 🤖 adds support for development capabilities toggle
This commit is contained in:
3 files changed
+71
-23
No files matched your search
@@ -1,8 +1,15 @@
|
||||
import factory from '../generated/factories/account';
|
||||
import { random, internet } from 'faker';
|
||||
import permissions from '../helpers/permissions';
|
||||
|
||||
export default factory.extend({
|
||||
authorized_actions: () => ['no-op', 'read', 'update', 'delete'],
|
||||
authorized_actions: () =>
|
||||
permissions.authorizedActionsFor('account') || [
|
||||
'no-op',
|
||||
'read',
|
||||
'update',
|
||||
'delete',
|
||||
],
|
||||
attributes() {
|
||||
switch (this.type) {
|
||||
case 'password':
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
import { copy } from 'ember-copy';
|
||||
|
||||
/**
|
||||
* Allows development-time toggling of resource permissions via the window
|
||||
* querystring. Pass a JSON object into the query to toggle permissions.
|
||||
* Note that the value must be a valid JSON object in order to be parsed.
|
||||
*
|
||||
* @example
|
||||
* ?authorized_actions={"account": ["read"]}
|
||||
*
|
||||
* Within a Mirage factory, pass the actions into the mock:
|
||||
*
|
||||
* @example
|
||||
* import { Factory } from 'ember-cli-mirage';
|
||||
* import permissions from '../helpers/permissions';
|
||||
*
|
||||
* export default Factory.extend({
|
||||
* authorized_actions: () =>
|
||||
* permissions.authorizedActionsFor('account') ||
|
||||
* ['no-op', 'read', 'update', 'delete']
|
||||
* });
|
||||
*/
|
||||
class ToggledCapabilities {
|
||||
// =attributes
|
||||
|
||||
// Get the query string and drop the leading '?'
|
||||
#rawQuery = window.location.search.substring(1);
|
||||
|
||||
// Split the querystring into key/value pairs
|
||||
#queryPairs = this.#rawQuery.split('&').map((item) => item.split('='));
|
||||
|
||||
// Find the value of a pair with key `authorized_actions`
|
||||
// Safe-ish when the item is not present
|
||||
#rawAuthorizedActions = (this.#queryPairs.find(
|
||||
(item) => item[0] === 'authorized_actions'
|
||||
) || [])[1];
|
||||
|
||||
// Safe-ish when the value is not present
|
||||
#authorizedActions =
|
||||
JSON.parse(
|
||||
decodeURIComponent(this.#rawAuthorizedActions || '') || 'null'
|
||||
) || {};
|
||||
|
||||
// =methods
|
||||
|
||||
/**
|
||||
* Returns authorized actions for the specified field, if any are defined
|
||||
* in the `authorized_actions` query. Returns undefined otherwise.
|
||||
* @return {?string[]}
|
||||
*/
|
||||
authorizedActionsFor(field) {
|
||||
if (this.#authorizedActions[field]) {
|
||||
return copy(this.#authorizedActions[field], true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default new ToggledCapabilities();
|
||||
Reference in new issue
Block a user