Skip to content

Commit

Permalink
refactor: Add telemetry for RBAC (no-changelog) (#8056)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivov authored Dec 19, 2023
1 parent a895ee8 commit 38d1336
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 1 deletion.
19 changes: 19 additions & 0 deletions packages/cli/src/databases/repositories/role.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,23 @@ export class RoleRepository extends Repository<Role> {
async findRole(scope: RoleScopes, name: RoleNames) {
return this.findOne({ where: { scope, name } });
}

/**
* Counts the number of users in each role, e.g. `{ admin: 2, member: 6, owner: 1 }`
*/
async countUsersByRole() {
type Row = { role_name: string; count: number };

const rows: Row[] = await this.createQueryBuilder('role')
.select('role.name')
.addSelect('COUNT(user.id)', 'count')
.innerJoin('user', 'user', 'role.id = user.globalRoleId')
.groupBy('role.name')
.getRawMany();

return rows.reduce<Record<string, number>>((acc, item) => {
acc[item.role_name] = item.count;
return acc;
}, {});
}
}
2 changes: 2 additions & 0 deletions packages/cli/src/telemetry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { License } from '@/License';
import { N8N_VERSION } from '@/constants';
import { WorkflowRepository } from '@db/repositories/workflow.repository';
import { SourceControlPreferencesService } from '../environments/sourceControl/sourceControlPreferences.service.ee';
import { RoleRepository } from '@/databases/repositories/role.repository';

type ExecutionTrackDataKey = 'manual_error' | 'manual_success' | 'prod_error' | 'prod_success';

Expand Down Expand Up @@ -110,6 +111,7 @@ export class Telemetry {
plan_name_current: this.license.getPlanName(),
quota: this.license.getTriggerLimit(),
usage: await this.workflowRepository.getActiveTriggerCount(),
role_count: await Container.get(RoleRepository).countUsersByRole(),
source_control_set_up: Container.get(SourceControlPreferencesService).isSourceControlSetup(),
branchName: sourceControlPreferences.branchName,
read_only_instance: sourceControlPreferences.branchReadOnly,
Expand Down
37 changes: 37 additions & 0 deletions packages/cli/test/integration/role.repository.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { createAdmin, createMember, createOwner } from './shared/db/users';
import * as testDb from './shared/testDb';
import { RoleRepository } from '@/databases/repositories/role.repository';
import Container from 'typedi';

describe('RoleRepository', () => {
let roleRepository: RoleRepository;

beforeAll(async () => {
await testDb.init();

roleRepository = Container.get(RoleRepository);

await testDb.truncate(['User']);
});

afterAll(async () => {
await testDb.terminate();
});

describe('countUsersByRole()', () => {
test('should return the number of users in each role', async () => {
await Promise.all([
createOwner(),
createAdmin(),
createAdmin(),
createMember(),
createMember(),
createMember(),
]);

const usersByRole = await roleRepository.countUsersByRole();

expect(usersByRole).toStrictEqual({ admin: 2, member: 3, owner: 1 });
});
});
});
1 change: 1 addition & 0 deletions packages/editor-ui/src/Interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1772,6 +1772,7 @@ export interface ExternalSecretsProviderWithProperties extends ExternalSecretsPr
}

export type CloudUpdateLinkSourceType =
| 'advanced-permissions'
| 'canvas-nav'
| 'custom-data-filter'
| 'workflow_sharing'
Expand Down
2 changes: 1 addition & 1 deletion packages/editor-ui/src/components/InviteUsersModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ export default defineComponent({
}
},
goToUpgradeAdvancedPermissions() {
void this.uiStore.goToUpgrade('settings-users', 'upgrade-advanced-permissions');
void this.uiStore.goToUpgrade('advanced-permissions', 'upgrade-advanced-permissions');
},
},
});
Expand Down

0 comments on commit 38d1336

Please sign in to comment.