users system prototyping
This commit is contained in:
@ -5,15 +5,15 @@ function wrap_bsearch<T extends Entry>(index: number, result: T): WrapResult<T,
|
||||
return wrap(result, index)
|
||||
}
|
||||
|
||||
export default function directory_search<T extends Entry>(entry_collection: T[], file_name: string): WrapResult<T, number> | undefined {
|
||||
export default function directory_search<T extends Entry>(dir_files: T[], file_name: string): WrapResult<T, number> | 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<file_name) {
|
||||
start = median+1
|
||||
} else {
|
||||
|
@ -12,7 +12,6 @@ const enum Permissions {
|
||||
r = 1<<0,
|
||||
w = 1<<1,
|
||||
x = 1<<2,
|
||||
none = 1<<3,
|
||||
}
|
||||
const enum ROOT_ID {
|
||||
TRUNK = "/",
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { ROOT_ID } from "./main";
|
||||
|
||||
import Crypto, { SHA256_String } from "../crypto/generate";
|
||||
import Crypto, { type SHA256_String } from "../crypto/generate";
|
||||
import wrap, { type WrapResult } from "./wrap";
|
||||
|
||||
const enum SysGroups {
|
||||
@ -12,85 +12,177 @@ const enum GroupSearch {
|
||||
WheelResult,
|
||||
UsersResult,
|
||||
}
|
||||
const enum UserMoveStatus {
|
||||
Ok,
|
||||
RootBlocked,
|
||||
MovingNonExistentUser,
|
||||
AlreadyInWheel,
|
||||
AlreadyInUsers,
|
||||
}
|
||||
const enum SettingUser {
|
||||
Ok,
|
||||
AlreadyLoggedIn,
|
||||
UserDoesNotExist
|
||||
}
|
||||
|
||||
type WrapUserSearch = WrapResult<User | undefined, GroupSearch>
|
||||
type Group = User[]
|
||||
type User_Index = [User, number]
|
||||
type WrapUserSearch = WrapResult<User_Index | undefined, GroupSearch>
|
||||
|
||||
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<group_t.length; i++) {
|
||||
if (group_t[i].get_uname() === uname) {
|
||||
return [group_t[i], i]
|
||||
}
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
function groups_find_user(uname: string): WrapUserSearch {
|
||||
const exist_in_wheel = group_iter_for_user(uname, groups.wheel)
|
||||
if (exist_in_wheel) {
|
||||
return wrap_user_search(GroupSearch.WheelResult, exist_in_wheel)
|
||||
}
|
||||
const exist_in_users = users.find(user => 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<group.length; i++) {
|
||||
if (group[i].get_uname() === user_name) {
|
||||
group.splice(i, 1)
|
||||
function group_remove(uname: string, group_t: Group): boolean {
|
||||
if (uname !== ROOT_ID.NAME) {
|
||||
const find_user = group_iter_for_user(uname, group_t)
|
||||
if (find_user) {
|
||||
group_t.splice(find_user[1], 1)
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
function group_wheel_add(new_user: User): GroupSearch {
|
||||
return group_add(new_user, wheel)
|
||||
function group_user_move(uname: string, new_group: SysGroups): UserMoveStatus {
|
||||
if (uname === ROOT_ID.NAME) { return UserMoveStatus.RootBlocked }
|
||||
|
||||
const find_in_group = groups_find_user(uname)
|
||||
if (find_in_group.status === GroupSearch.NotFound) { return UserMoveStatus.MovingNonExistentUser }
|
||||
|
||||
if (new_group === SysGroups.Wheel) {
|
||||
if (find_in_group.status === GroupSearch.WheelResult) { return UserMoveStatus.AlreadyInWheel }
|
||||
|
||||
groups.wheel.push(groups.users.splice((find_in_group.result as User_Index)[1], 1)[0])
|
||||
} else if (new_group === SysGroups.Users) {
|
||||
if (find_in_group.status === GroupSearch.UsersResult) { return UserMoveStatus.AlreadyInUsers }
|
||||
|
||||
groups.users.push(groups.wheel.splice((find_in_group.result as User_Index)[1], 1)[0])
|
||||
}
|
||||
return UserMoveStatus.Ok
|
||||
}
|
||||
|
||||
function group_wheel_remove(user_name: string): boolean {
|
||||
return group_remove(user_name, wheel)
|
||||
function group_wheel_add(new_user: User): GroupSearch {
|
||||
return group_add(new_user, groups.wheel)
|
||||
}
|
||||
|
||||
function group_wheel_remove(uname: string): boolean {
|
||||
return group_remove(uname, groups.wheel)
|
||||
}
|
||||
|
||||
function group_users_add(new_user: User): GroupSearch {
|
||||
return group_add(new_user, users)
|
||||
return group_add(new_user, groups.users)
|
||||
}
|
||||
|
||||
function group_users_remove(user_name: string): boolean {
|
||||
return group_remove(user_name, users)
|
||||
function group_users_remove(uname: string): boolean {
|
||||
return group_remove(uname, groups.users)
|
||||
}
|
||||
|
||||
class user_lib {
|
||||
public static current: string = ROOT_ID.NAME
|
||||
public static current_sys_user: User;
|
||||
|
||||
public static get_sys_user(): User {
|
||||
return groups.together().find(user => 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<void> {
|
||||
@ -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,
|
||||
}
|
Reference in New Issue
Block a user