Skip to content

Commit bbd809b

Browse files
committed
feat: remove sso user command
1 parent 6be3af8 commit bbd809b

File tree

6 files changed

+84
-9
lines changed

6 files changed

+84
-9
lines changed

api/src/store/modules/config.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { F_OK } from 'constants';
2-
import { randomBytes } from 'crypto';
32
import { writeFileSync } from 'fs';
43
import { access } from 'fs/promises';
54

@@ -24,7 +23,6 @@ import { setupRemoteAccessThunk } from '@app/store/actions/setup-remote-access';
2423
import { FileLoadStatus } from '@app/store/types';
2524
import { type RecursivePartial } from '@app/types';
2625
import { type MyServersConfig, type MyServersConfigMemory } from '@app/types/my-servers-config';
27-
import { isFulfilled } from '@app/utils';
2826

2927
export type SliceState = {
3028
status: FileLoadStatus;
@@ -219,7 +217,18 @@ export const config = createSlice({
219217
const stateAsArray = state.remote.ssoSubIds.split(',');
220218
stateAsArray.push(action.payload);
221219
state.remote.ssoSubIds = stateAsArray.join(',');
222-
}
220+
},
221+
removeSsoUser(state, action: PayloadAction<string | null>) {
222+
if (action.payload === null) {
223+
state.remote.ssoSubIds = '';
224+
return;
225+
}
226+
if (!state.remote.ssoSubIds.includes(action.payload)) {
227+
return;
228+
}
229+
const stateAsArray = state.remote.ssoSubIds.split(',').filter((id) => id !== action.payload);
230+
state.remote.ssoSubIds = stateAsArray.join(',');
231+
},
223232
},
224233
extraReducers(builder) {
225234
builder.addCase(loadConfigFile.pending, (state) => {
@@ -300,12 +309,14 @@ export const {
300309
setUpnpState,
301310
setWanPortToValue,
302311
setWanAccess,
312+
removeSsoUser,
303313
} = actions;
304314

305315
/**
306316
* Actions that should trigger a flash write
307317
*/
308318
export const configUpdateActionsFlash = isAnyOf(
319+
addSsoUser,
309320
updateUserConfig,
310321
updateAccessTokens,
311322
updateAllowedOrigins,
@@ -314,7 +325,8 @@ export const configUpdateActionsFlash = isAnyOf(
314325
setWanAccess,
315326
setupRemoteAccessThunk.fulfilled,
316327
logoutUser.fulfilled,
317-
loginUser.fulfilled
328+
loginUser.fulfilled,
329+
removeSsoUser
318330
);
319331

320332
/**

api/src/unraid-api/cli/cli.module.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@ import { StatusCommand } from '@app/unraid-api/cli/status.command';
1717
import { StopCommand } from '@app/unraid-api/cli/stop.command';
1818
import { SwitchEnvCommand } from '@app/unraid-api/cli/switch-env.command';
1919
import { VersionCommand } from '@app/unraid-api/cli/version.command';
20+
import { RemoveSSOUserCommand } from '@app/unraid-api/cli/sso/remove-sso-user.command';
21+
import { RemoveSSOUserQuestionSet } from '@app/unraid-api/cli/sso/remove-sso-user.questions';
2022

2123
@Module({
2224
providers: [
2325
AddSSOUserCommand,
2426
AddSSOUserQuestionSet,
27+
RemoveSSOUserCommand,
28+
RemoveSSOUserQuestionSet,
2529
LogService,
2630
StartCommand,
2731
StopCommand,

api/src/unraid-api/cli/sso/add-sso-user.questions.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import { Question, QuestionSet } from 'nest-commander';
22
import { v4 as uuidv4 } from 'uuid';
33

4-
5-
6-
7-
84
@QuestionSet({ name: 'add-user' })
95
export class AddSSOUserQuestionSet {
106
static name = 'add-user';
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { Injectable } from '@nestjs/common';
2+
3+
import { CommandRunner, InquirerService, Option, OptionChoiceFor, SubCommand } from 'nest-commander';
4+
5+
import { store } from '@app/store/index';
6+
import { loadConfigFile, removeSsoUser } from '@app/store/modules/config';
7+
import { LogService } from '@app/unraid-api/cli/log.service';
8+
import { RemoveSSOUserQuestionSet } from '@app/unraid-api/cli/sso/remove-sso-user.questions';
9+
10+
interface RemoveSSOUserCommandOptions {
11+
username: string;
12+
}
13+
14+
@Injectable()
15+
@SubCommand({
16+
name: 'remove-user',
17+
aliases: ['remove', 'r'],
18+
description: 'Remove a user (or all users) from SSO',
19+
})
20+
export class RemoveSSOUserCommand extends CommandRunner {
21+
constructor(
22+
private readonly logger: LogService,
23+
private readonly inquirerService: InquirerService
24+
) {
25+
super();
26+
}
27+
public async run(_input: string[], options: RemoveSSOUserCommandOptions): Promise<void> {
28+
await store.dispatch(loadConfigFile());
29+
console.log('options', options);
30+
options = await this.inquirerService.prompt(RemoveSSOUserQuestionSet.name, options);
31+
store.dispatch(removeSsoUser(options.username === 'all' ? null : options.username));
32+
this.logger.info('User/s removed ' + options.username);
33+
}
34+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { ChoicesFor, Question, QuestionSet, } from 'nest-commander';
2+
3+
import { store } from '@app/store/index';
4+
import { loadConfigFile } from '@app/store/modules/config';
5+
6+
7+
@QuestionSet({ name: 'remove-user' })
8+
export class RemoveSSOUserQuestionSet {
9+
static name = 'remove-user';
10+
11+
@Question({
12+
message: `Please select from the following list of users to remove from SSO, or enter all to remove all users from SSO.\n`,
13+
name: 'username',
14+
type: 'list',
15+
})
16+
parseName(val: string) {
17+
return val;
18+
}
19+
20+
@ChoicesFor({ name: 'username' })
21+
async choicesForUsername() {
22+
await store.dispatch(loadConfigFile());
23+
const users = store.getState().config.remote.ssoSubIds.split(',').filter((user) => user !== '');
24+
25+
users.push('all');
26+
return users;
27+
}
28+
}

api/src/unraid-api/cli/sso/sso.command.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import { Command, CommandRunner } from 'nest-commander';
55
import { LogService } from '@app/unraid-api/cli/log.service';
66
import { ValidateTokenCommand } from '@app/unraid-api/cli/sso/validate-token.command';
77
import { AddSSOUserCommand } from '@app/unraid-api/cli/sso/add-sso-user.command';
8+
import { RemoveSSOUserCommand } from '@app/unraid-api/cli/sso/remove-sso-user.command';
89

910
@Injectable()
1011
@Command({
1112
name: 'sso',
1213
description: 'Main Command to Configure / Validate SSO Tokens',
13-
subCommands: [ValidateTokenCommand, AddSSOUserCommand],
14+
subCommands: [ValidateTokenCommand, AddSSOUserCommand, RemoveSSOUserCommand],
1415
})
1516
export class SSOCommand extends CommandRunner {
1617
constructor(private readonly logger: LogService) {

0 commit comments

Comments
 (0)