Skip to content

Commit

Permalink
refactor: Add telemetry for RBAC
Browse files Browse the repository at this point in the history
  • Loading branch information
ivov committed Dec 15, 2023
1 parent 439a22d commit 7d3f388
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
19 changes: 19 additions & 0 deletions packages/cli/src/services/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { RoleService } from '@/services/role.service';
import { ApplicationError, ErrorReporterProxy as ErrorReporter } from 'n8n-workflow';
import type { UserRequest } from '@/requests';
import { InternalServerError } from '@/errors/response-errors/internal-server.error';
import { RoleRepository } from '@/databases/repositories/role.repository';

@Service()
export class UserService {
Expand Down Expand Up @@ -307,4 +308,22 @@ export class UserService {

return { usersInvited, usersCreated: toCreateUsers.map(({ email }) => email) };
}

/**
* Counts the number of users in each role, e.g. `{ admin: 2, member: 6, owner: 1 }`
*/
async countUsersByRole() {
const result: Array<{ role_name: string; count: number }> = await Container.get(RoleRepository)
.createQueryBuilder('role')
.select('role.name')
.addSelect('COUNT(user.id)', 'count')
.innerJoin('user', 'user', 'role.id = user.globalRoleId')
.groupBy('role.name')
.getRawMany();

return result.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 @@ -10,6 +10,7 @@ import { LicenseService } from '@/license/License.service';
import { N8N_VERSION } from '@/constants';
import { SourceControlPreferencesService } from '../environments/sourceControl/sourceControlPreferences.service.ee';
import { InstanceSettings } from 'n8n-core';
import { UserService } from '@/services/user.service';

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

Expand Down Expand Up @@ -108,6 +109,7 @@ export class Telemetry {
plan_name_current: this.license.getPlanName(),
quota: this.license.getTriggerLimit(),
usage: await LicenseService.getActiveTriggerCount(),
role_count: await Container.get(UserService).countUsersByRole(),
source_control_set_up: Container.get(SourceControlPreferencesService).isSourceControlSetup(),
branchName: sourceControlPreferences.branchName,
read_only_instance: sourceControlPreferences.branchReadOnly,
Expand Down
1 change: 1 addition & 0 deletions packages/editor-ui/src/Interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1766,6 +1766,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 7d3f388

Please sign in to comment.