Skip to content

Commit

Permalink
feat: report feature flags in /whoami
Browse files Browse the repository at this point in the history
  • Loading branch information
KernelDeimos committed Aug 15, 2024
1 parent 8e39f41 commit 4561b89
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/backend/src/routers/whoami.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ const WHOAMI_GET = eggspress('/whoami', {

// Get whoami values from other services
const svc_whoami = req.services.get('whoami');
const provider_details = await svc_whoami.get_details({ user: req.user });
const provider_details = await svc_whoami.get_details({
user: req.user,
actor: actor,
});
Object.assign(details, provider_details);

if ( ! is_user ) {
Expand Down
33 changes: 33 additions & 0 deletions src/backend/src/services/FeatureFlagService.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@ const BaseService = require("./BaseService");
* are enabled or disabled for the current user.
*/
class FeatureFlagService extends BaseService {
_construct () {
this.known_flags = new Map();
}
register (name, spec) {
this.known_flags.set(name, spec);
}
async _init () {
const svc_detailProvider = this.services.get('whoami');
svc_detailProvider.register_provider(async (context, out) => {
console.log(`\x1B[36;1mCALLED\x1B[0m`);
if ( ! context.actor ) return;
out.feature_flags = await this.get_summary(context.actor);
});
}
async check (...a) {
// allows binding call with multiple options objects;
// the last argument is the permission to check
Expand All @@ -25,6 +39,9 @@ class FeatureFlagService extends BaseService {
return { options, value };
})();

if ( ! this.known_flags.has(permission) ) {
this.known_flags.set(permission, true);
}


const actor = options.actor ?? Context.get('actor');
Expand All @@ -35,6 +52,22 @@ class FeatureFlagService extends BaseService {
if ( l.length === 0 ) return false;
return true;
}

async get_summary (actor) {
const summary = {};
for ( const [key, value] of this.known_flags.entries() ) {
if ( value.$ === 'config-flag' ) {
summary[key] = value.value;
continue;
}
const svc_permission = this.services.get('permission');
const reading = await svc_permission.scan(actor, `feature:`);
const l = PermissionUtil.reading_to_options(reading);
summary[key] = l.length > 0;
}

return summary;
}
}

module.exports = {
Expand Down
7 changes: 7 additions & 0 deletions src/backend/src/services/ShareService.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ class ShareService extends BaseService {

async _init () {
this.db = await this.services.get('database').get(DB_WRITE, 'share');

// registry "share" as a feature flag so gui is informed
// about whether or not a user has access to this feature
const svc_featureFlag = this.services.get('feature-flag');
svc_featureFlag.register('share', {
$: 'permission-flag'
});
}

['__on_install.routes'] (_, { app }) {
Expand Down
7 changes: 7 additions & 0 deletions src/backend/src/services/auth/ACLService.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ const { AppUnderUserActorType, UserActorType, Actor, SystemActorType, AccessToke
const { PermissionUtil } = require("./PermissionService");

class ACLService extends BaseService {
async _init () {
const svc_featureFlag = this.services.get('feature-flag');
svc_featureFlag.register('public-folders', {
$: 'config-flag',
value: this.global_config.enable_public_folders ?? false,
});
}
async check (actor, resource, mode) {
const ld = (Context.get('logdent') ?? 0) + 1;
return await Context.get().sub({ logdent: ld }).arun(async () => {
Expand Down

0 comments on commit 4561b89

Please sign in to comment.