Skip to content
Snippets Groups Projects
Commit 5e37d411 authored by michael.minelli's avatar michael.minelli
Browse files

CheckAccesses => Move to independent function

parent b7b20b66
Branches
Tags
No related merge requests found
...@@ -22,16 +22,6 @@ class EnonceCreateCommand extends CommanderCommand { ...@@ -22,16 +22,6 @@ class EnonceCreateCommand extends CommanderCommand {
.action(this.commandAction.bind(this)); .action(this.commandAction.bind(this));
} }
private async checkAccesses(): Promise<boolean> {
let sessionResult = await SessionManager.testSession(true, [ 'teachingStaff' ]);
if ( !sessionResult || !sessionResult.teachingStaff ) {
return false;
}
return (await GitlabManager.testToken(true)).every(result => result);
}
protected async commandAction(options: any): Promise<void> { protected async commandAction(options: any): Promise<void> {
let members!: Array<GitlabUser> | false; let members!: Array<GitlabUser> | false;
let templateIdOrNamespace: string | null = null; let templateIdOrNamespace: string | null = null;
...@@ -40,7 +30,7 @@ class EnonceCreateCommand extends CommanderCommand { ...@@ -40,7 +30,7 @@ class EnonceCreateCommand extends CommanderCommand {
{ {
console.log(chalk.cyan('Please wait while we verify and retrieve data...')); console.log(chalk.cyan('Please wait while we verify and retrieve data...'));
if ( !await this.checkAccesses() ) { if ( !await AccessesHelper.checkTeachingStaff() ) {
return; return;
} }
......
...@@ -21,16 +21,6 @@ class ExerciceCreateCommand extends CommanderCommand { ...@@ -21,16 +21,6 @@ class ExerciceCreateCommand extends CommanderCommand {
.action(this.commandAction.bind(this)); .action(this.commandAction.bind(this));
} }
private async checkAccesses(): Promise<boolean> {
let sessionResult = await SessionManager.testSession(true, [ 'student' ]);
if ( !sessionResult ) {
return false;
}
return (await GitlabManager.testToken(true)).every(result => result);
}
protected async commandAction(options: any): Promise<void> { protected async commandAction(options: any): Promise<void> {
let members!: Array<GitlabUser> | false; let members!: Array<GitlabUser> | false;
let enonce!: Enonce | undefined; let enonce!: Enonce | undefined;
...@@ -39,7 +29,7 @@ class ExerciceCreateCommand extends CommanderCommand { ...@@ -39,7 +29,7 @@ class ExerciceCreateCommand extends CommanderCommand {
{ {
console.log(chalk.cyan('Please wait while we verify and retrieve data...')); console.log(chalk.cyan('Please wait while we verify and retrieve data...'));
if ( !await this.checkAccesses() ) { if ( !await AccessesHelper.checkStudent() ) {
return; return;
} }
......
import SessionManager from '../managers/SessionManager';
import GitlabManager from '../managers/GitlabManager';
class AccessesHelper {
async checkStudent(): Promise<boolean> {
let sessionResult = await SessionManager.testSession(true, [ 'student' ]);
if ( !sessionResult ) {
return false;
}
return (await GitlabManager.testToken(true)).every(result => result);
}
async checkTeachingStaff(): Promise<boolean> {
let sessionResult = await SessionManager.testSession(true, [ 'teachingStaff' ]);
if ( !sessionResult || !sessionResult.teachingStaff ) {
return false;
}
return (await GitlabManager.testToken(true)).every(result => result);
}
};
export default new AccessesHelper();
\ No newline at end of file
...@@ -70,17 +70,15 @@ class SessionManager { ...@@ -70,17 +70,15 @@ class SessionManager {
this.token = ''; this.token = '';
} }
checkPermissions(verbose: boolean = true, indent: number = 8, ...checkPermissions: Array<string>): Permissions { checkPermissions(verbose: boolean = true, indent: number = 8, checkPermissions: Array<string> | null = []): Permissions {
const hasPermission = (permissionPredicate: () => boolean, verboseText: string): boolean => { const hasPermission = (permissionPredicate: () => boolean, verboseText: string): boolean => {
const spinner: ora.Ora = ora({
text : verboseText,
indent: indent
});
let isAllowed: boolean = this.profile.id !== -1 && permissionPredicate(); let isAllowed: boolean = this.profile.id !== -1 && permissionPredicate();
if ( verbose ) { if ( verbose ) {
spinner.start(); const spinner: ora.Ora = ora({
text : verboseText,
indent: indent
}).start();
isAllowed ? spinner.succeed() : spinner.fail(); isAllowed ? spinner.succeed() : spinner.fail();
} }
...@@ -88,13 +86,13 @@ class SessionManager { ...@@ -88,13 +86,13 @@ class SessionManager {
}; };
return { return {
teachingStaff: checkPermissions.length == 0 || checkPermissions.includes('teachingStaff') ? hasPermission(() => this.profile.isTeachingStaff, 'Teaching staff permissions') : false, teachingStaff: checkPermissions && (checkPermissions.length == 0 || checkPermissions.includes('teachingStaff')) ? hasPermission(() => this.profile.isTeachingStaff, 'Teaching staff permissions') : false,
student : checkPermissions.length == 0 || checkPermissions.includes('student') ? hasPermission(() => true, 'Student permissions') : false student : checkPermissions && (checkPermissions.length == 0 || checkPermissions.includes('student')) ? hasPermission(() => true, 'Student permissions') : false
}; };
} }
async testSession(verbose: boolean = true, checkPermissions: Array<string> = []): Promise<false | Permissions> { async testSession(verbose: boolean = true, checkPermissions: Array<string> | null = []): Promise<false | Permissions> {
if ( verbose ) { if ( verbose ) {
ora('Checking Dojo session: ').start().info(); ora('Checking Dojo session: ').start().info();
} }
...@@ -123,7 +121,7 @@ class SessionManager { ...@@ -123,7 +121,7 @@ class SessionManager {
return false; return false;
} }
return this.checkPermissions(verbose, 8, ...checkPermissions); return this.checkPermissions(verbose, 8, checkPermissions);
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment