From a5ee53a151151cce430ba0dcfd3c6820be2a06f9 Mon Sep 17 00:00:00 2001 From: rhpidfyre Date: Mon, 12 May 2025 19:36:32 -0400 Subject: [PATCH] users system prototyping --- src/rt/rfwfs/index.ts | 8 +-- src/rt/rfwfs/main.ts | 7 +- src/rt/rfwfs/users.ts | 148 +++++++++++++++++++++++++++++++++++------- 3 files changed, 130 insertions(+), 33 deletions(-) diff --git a/src/rt/rfwfs/index.ts b/src/rt/rfwfs/index.ts index 795f9df..df3b73e 100644 --- a/src/rt/rfwfs/index.ts +++ b/src/rt/rfwfs/index.ts @@ -5,15 +5,15 @@ function wrap_bsearch(index: number, result: T): WrapResult(entry_collection: T[], file_name: string): WrapResult | undefined { +export default function directory_search(dir_files: T[], file_name: string): WrapResult | undefined { let start = 0 - let end = entry_collection.length-1 + let end = dir_files.length-1 while (start<=end) { const median = (start+end)>>1 - const median_name = entry_collection[median].name.inner + const median_name = dir_files[median].name.inner if (median_name === file_name) { - return wrap_bsearch(median, entry_collection[median]) + return wrap_bsearch(median, dir_files[median]) } else if (median_name +type Group = User[] +type User_Index = [User, number] +type WrapUserSearch = WrapResult -const wheel: User[] = [] -const users: User[] = [] +type WheelAndUsers = Group +interface Groups { + wheel: Group, + users: Group, + together: () => WheelAndUsers +} -function wrap_user_search(status: GroupSearch, result?: User): WrapUserSearch { +const groups: Groups = { + wheel: [], + users: [], + together: function() { + return [...this.wheel, ...this.users] + } +} + +function wrap_user_search(status: GroupSearch, result?: User_Index): WrapUserSearch { return wrap(result, status) } -function groups_find_user(user_name: string): WrapUserSearch { - const exist_in_wheel = wheel.find(user => user.get_uname() === user_name) +function group_iter_for_user(uname: string, group_t: Group): User_Index | undefined { + for (let i = 0; i user.get_uname() === user_name) + const exist_in_users = group_iter_for_user(uname, groups.users) if (exist_in_users) { return wrap_user_search(GroupSearch.UsersResult, exist_in_users) } return wrap_user_search(GroupSearch.NotFound) } -function group_add(new_user: User, group: User[]): GroupSearch { +function group_add(new_user: User, group_t: Group): GroupSearch { const dups = groups_find_user(new_user.get_uname()) if (dups.status === GroupSearch.NotFound) { - group.push(new_user) + group_t.push(new_user) } return dups.status } -function group_remove(user_name: string, group: User[]): boolean { - for (let i = 0; i user.is_logged_in()) as User + } + + public static set_sys_user(uname: string): SettingUser { + const found_user = groups_find_user(uname) + const result_user_i = found_user.result + if (!result_user_i) { return SettingUser.UserDoesNotExist } + if (result_user_i[0].is_logged_in()) { return SettingUser.AlreadyLoggedIn } + + user_lib.current_sys_user = result_user_i[0] + return SettingUser.Ok + } } class User extends user_lib { + private current: boolean; private name: string; private password?: SHA256_String; constructor(name: string, password?: SHA256_String) { super() this.name = name + this.current = name === ROOT_ID.NAME this.password = password } + public is_logged_in(): boolean { + return this.current + } + + public login(password?: SHA256_String): boolean { + if (password !== this.password) { return false } + + for (const other_user of groups.together()) { + if (other_user.is_logged_in()) { + other_user.current = false + this.current = true + break + } + } + return this.current + } + public get_uname() { return this.name } - public set_uname(new_username: string) { - const search = groups_find_user(new_username) + public set_uname(new_uname: string): GroupSearch { + const search = groups_find_user(new_uname) if (search.status === GroupSearch.NotFound) { - + this.name = new_uname } + return search.status + } + + public get_password(): SHA256_String | undefined { + return this.password } public async set_password(new_password?: string): Promise { @@ -102,11 +194,17 @@ class User extends user_lib { } } +groups.wheel.push( + new User(ROOT_ID.NAME), + new User("rhpidfyre", "9025f0dd51ed4f2b2dc2791b6a15eff804555c283bac50d1d8923c18a51f977b") +) + export default User export { group_wheel_remove, group_users_remove, group_wheel_add, group_users_add, + group_user_move, SysGroups, } \ No newline at end of file