Skip to content

Commit 37458cd

Browse files
committed
feat: add user with cli
1 parent 34190a6 commit 37458cd

File tree

6 files changed

+124
-4
lines changed

6 files changed

+124
-4
lines changed

api/src/store/modules/config.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,15 @@ export const config = createSlice({
211211
setWanAccess(state, action: PayloadAction<'yes' | 'no'>) {
212212
state.remote.wanaccess = action.payload;
213213
},
214+
addSsoUser(state, action: PayloadAction<string>) {
215+
// First check if state already has ID, otherwise append it
216+
if (state.remote.ssoSubIds.includes(action.payload)) {
217+
return;
218+
}
219+
const stateAsArray = state.remote.ssoSubIds.split(',');
220+
stateAsArray.push(action.payload);
221+
state.remote.ssoSubIds = stateAsArray.join(',');
222+
}
214223
},
215224
extraReducers(builder) {
216225
builder.addCase(loadConfigFile.pending, (state) => {
@@ -284,6 +293,7 @@ export const config = createSlice({
284293
const { actions, reducer } = config;
285294

286295
export const {
296+
addSsoUser,
287297
updateUserConfig,
288298
updateAccessTokens,
289299
updateAllowedOrigins,

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
import { Module } from '@nestjs/common';
22

3+
import { InquirerService } from 'nest-commander';
4+
35
import { ConfigCommand } from '@app/unraid-api/cli/config.command';
46
import { KeyCommand } from '@app/unraid-api/cli/key.command';
57
import { LogService } from '@app/unraid-api/cli/log.service';
68
import { LogsCommand } from '@app/unraid-api/cli/logs.command';
79
import { ReportCommand } from '@app/unraid-api/cli/report.command';
810
import { RestartCommand } from '@app/unraid-api/cli/restart.command';
9-
import { SSOCommand } from '@app/unraid-api/cli/sso.command';
11+
import { AddSSOUserCommand } from '@app/unraid-api/cli/sso/add-sso-user.command';
12+
import { AddSSOUserQuestionSet } from '@app/unraid-api/cli/sso/add-sso-user.questions';
13+
import { SSOCommand } from '@app/unraid-api/cli/sso/sso.command';
14+
import { ValidateTokenCommand } from '@app/unraid-api/cli/sso/validate-token.command';
1015
import { StartCommand } from '@app/unraid-api/cli/start.command';
1116
import { StatusCommand } from '@app/unraid-api/cli/status.command';
1217
import { StopCommand } from '@app/unraid-api/cli/stop.command';
1318
import { SwitchEnvCommand } from '@app/unraid-api/cli/switch-env.command';
1419
import { VersionCommand } from '@app/unraid-api/cli/version.command';
15-
import { ValidateTokenCommand } from '@app/unraid-api/cli/validate-token.command';
1620

1721
@Module({
1822
providers: [
23+
AddSSOUserCommand,
24+
AddSSOUserQuestionSet,
1925
LogService,
2026
StartCommand,
2127
StopCommand,
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { Injectable } from '@nestjs/common';
2+
3+
import { CommandRunner, InquirerService, Option, SubCommand } from 'nest-commander';
4+
5+
import { store } from '@app/store/index';
6+
import { addSsoUser, loadConfigFile } from '@app/store/modules/config';
7+
import { writeConfigSync } from '@app/store/sync/config-disk-sync';
8+
import { LogService } from '@app/unraid-api/cli/log.service';
9+
import { AddSSOUserQuestionSet } from '@app/unraid-api/cli/sso/add-sso-user.questions';
10+
11+
interface AddSSOUserCommandOptions {
12+
disclaimer: string;
13+
username: string;
14+
}
15+
16+
@Injectable()
17+
@SubCommand({
18+
name: 'add-user',
19+
aliases: ['add', 'a'],
20+
description: 'Add a user for SSO',
21+
})
22+
export class AddSSOUserCommand extends CommandRunner {
23+
constructor(
24+
private readonly logger: LogService,
25+
private readonly inquirerService: InquirerService
26+
) {
27+
super();
28+
}
29+
30+
async run(_input: string[], options: AddSSOUserCommandOptions): Promise<void> {
31+
options = await this.inquirerService.prompt(AddSSOUserQuestionSet.name, options);
32+
33+
if (options.disclaimer === 'y') {
34+
await store.dispatch(loadConfigFile());
35+
store.dispatch(addSsoUser(options.username));
36+
writeConfigSync('flash');
37+
this.logger.info('User added ' + options.username);
38+
}
39+
}
40+
41+
@Option({
42+
flags: '--username [username]',
43+
description: 'Cognito Username',
44+
})
45+
parseUsername(val: string) {
46+
return val;
47+
}
48+
49+
@Option({
50+
flags: '--disclaimer [disclaimer]',
51+
description: 'Disclaimer (y/n)',
52+
})
53+
parseDisclaimer(val: string) {
54+
return val;
55+
}
56+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { Question, QuestionSet } from 'nest-commander';
2+
3+
4+
5+
6+
7+
@QuestionSet({ name: 'add-user' })
8+
export class AddSSOUserQuestionSet {
9+
static name = 'add-user';
10+
11+
@Question({
12+
message: 'Are you sure you wish to add a user for SSO - this will enable single sign on in Unraid and has certain security implications? (y/n)',
13+
name: 'disclaimer',
14+
validate(input) {
15+
if (!input) {
16+
return 'Please provide a response';
17+
}
18+
if (!['y', 'n'].includes(input.toLowerCase())) {
19+
return 'Please provide a valid response';
20+
}
21+
if (input.toLowerCase() === 'n') {
22+
process.exit(1);
23+
}
24+
return true;
25+
},
26+
})
27+
parseDisclaimer(val: string) {
28+
return val;
29+
}
30+
31+
@Question({
32+
message: 'What is the cognito username (NOT YOUR UNRAID USERNAME)? Find it in your Unraid Account at https://account.unraid.net',
33+
name: 'username',
34+
validate(input) {
35+
if (!input) {
36+
return 'Username is required';
37+
}
38+
if (!/^[a-zA-Z0-9-]+$/.test(input)) {
39+
return 'Username must be alphanumeric and can include dashes.';
40+
}
41+
return true;
42+
},
43+
})
44+
parseName(val: string) {
45+
return val;
46+
}
47+
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ import { Injectable } from '@nestjs/common';
33
import { Command, CommandRunner } from 'nest-commander';
44

55
import { LogService } from '@app/unraid-api/cli/log.service';
6-
import { ValidateTokenCommand } from '@app/unraid-api/cli/validate-token.command';
6+
import { ValidateTokenCommand } from '@app/unraid-api/cli/sso/validate-token.command';
7+
import { AddSSOUserCommand } from '@app/unraid-api/cli/sso/add-sso-user.command';
78

89
@Injectable()
910
@Command({
1011
name: 'sso',
1112
description: 'Main Command to Configure / Validate SSO Tokens',
12-
subCommands: [ValidateTokenCommand],
13+
subCommands: [ValidateTokenCommand, AddSSOUserCommand],
1314
})
1415
export class SSOCommand extends CommandRunner {
1516
constructor(private readonly logger: LogService) {
File renamed without changes.

0 commit comments

Comments
 (0)