session manager login and logout implemented - untested, may contain bugs
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { type HistoryState } from "./history_state";
|
||||
import type { ShellState } from "./shell_state";
|
||||
import { createShellState, type ShellState } from "./shell_state";
|
||||
import { createShellError, type LoginError, type UserNotFoundError } from "./util/errors";
|
||||
|
||||
export interface UserInfo {
|
||||
readonly username: string;
|
||||
@@ -32,3 +33,41 @@ export function createInitialSessionState(user: UserInfo): SessionState {
|
||||
export function getCurrentSession(state: ShellState): SessionState {
|
||||
return state.sessionStack[state.sessionStack.length - 1]!;
|
||||
}
|
||||
|
||||
export function login(
|
||||
state: ShellState,
|
||||
username: string,
|
||||
password: string,
|
||||
passwdMap: ReadonlyMap<number, string>,
|
||||
): ShellState {
|
||||
const user = state.users.values().find(value => value.username === username);
|
||||
if (!user) {
|
||||
throw createShellError<UserNotFoundError>("UserNotFound", "The specified user does not exist", { username });
|
||||
}
|
||||
|
||||
const passwd = passwdMap.get(user.uid);
|
||||
if (!passwd) {
|
||||
throw createShellError<LoginError>("LoginError", "The user does not have a password mapped to their uid", {
|
||||
user: user.uid,
|
||||
});
|
||||
}
|
||||
|
||||
// Change this to hashed passwords later on
|
||||
if (!(password === passwd)) {
|
||||
throw createShellError<LoginError>("LoginError", "The password is incorrect", { user: user.uid });
|
||||
}
|
||||
|
||||
const stack = [...state.sessionStack];
|
||||
stack.push(createInitialSessionState(user));
|
||||
return createShellState(state.filesystem, state.users, stack);
|
||||
}
|
||||
|
||||
export function logout(state: ShellState): ShellState {
|
||||
if (state.sessionStack.length > 1) {
|
||||
const stack = [...state.sessionStack];
|
||||
stack.pop();
|
||||
|
||||
return createShellState(state.filesystem, state.users, stack);
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
||||
@@ -21,6 +21,18 @@ export function createInitialShellState(
|
||||
});
|
||||
}
|
||||
|
||||
export function createShellState(
|
||||
filesystem: FileSystem,
|
||||
users: ReadonlyMap<number, UserInfo>,
|
||||
sessionStack: ReadonlyArray<SessionState>,
|
||||
): ShellState {
|
||||
return Object.freeze({
|
||||
filesystem,
|
||||
users,
|
||||
sessionStack: Object.freeze(sessionStack),
|
||||
});
|
||||
}
|
||||
|
||||
export function withCwd(state: ShellState, inode: number): ShellState {
|
||||
const session = getCurrentSession(state);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user