Commit 9a7aad96 authored by Lakr Aream's avatar Lakr Aream 👼🏽

修复错误 完成枚举用户

parent 3264b7cb
......@@ -235,7 +235,7 @@ TABLE `Candiay`.`users` (
"action": "modify.name", # .name, .password
"from": "Lakr233", # user name
"meta": "QAQ", # change to
"timestamp": 1585910739000 # timestamp since 1970
"timestamp": 1585910739000 # timestamp since 1970
}
```
- Examples:
......
import { ConfigManager } from "./config"
import { UserManager } from "./users"
import { UserManager, User } from "./users"
export class RequestManager {
......@@ -92,19 +92,22 @@ export class RequestManager {
}
console.log("---------------------------------------------");
if (!vaildated) {
const resolveObject: { [key: string] : any } = {
"code": 401,
"context": "Invalid Signature"
}
contextContaienr = resolveObject
return
}
// Doing ~~Steave~~ Jobs
const actionTypeRaw = String(params["action"])
switch (actionTypeRaw.toLowerCase()) {
case "teapot": {
// The server refuses the attempt to brew coffee with a teapot.
return
}
case "create": {
if (!vaildated) {
const resolveObject: { [key: string] : any } = {
"code": 401,
"context": "Invalid Signature"
}
contextContaienr = resolveObject
return
}
UserManager.shared.readUserType(params["from"], (role) => {
if (role !== "root" && role !== "admin") {
const ro: { [key: string] : any } = {
......@@ -138,6 +141,14 @@ export class RequestManager {
return
}
case "delete": {
if (!vaildated) {
const resolveObject: { [key: string] : any } = {
"code": 401,
"context": "Invalid Signature"
}
contextContaienr = resolveObject
return
}
UserManager.shared.readUserType(params["from"], (role) => {
if (role !== "root" && params["from"] !== params["name"]) {
const ro: { [key: string] : any } = {
......@@ -168,20 +179,108 @@ export class RequestManager {
})
return
}
case "list": {
UserManager.shared.readUserType(params["from"], (role) => {
if (role !== null && role !== "" && role !== undefined) {
// a user name was set inside from param and user exists with a role
if (!vaildated) {
const resolveObject: { [key: string] : any } = {
"code": 401,
"context": "Invalid Signature"
}
contextContaienr = resolveObject
return
}
} else if (params["from"] !== "" && params["from"] !== null && params["from"] !== undefined) {
// user doesnt exists with a role, but user was set inside from param
const roresolveObject: { [key: string] : any } = {
"code": 401,
"context": "User not found or not vaild"
}
contextContaienr = roresolveObject
return
}
const userList = UserManager.shared.readUserList((list) => {
const users: { [key: string]: string } = {}
switch (String(role)) {
case "root": {
// everyone
for (let index = 0; index < list.length; index++) {
const element = list[index];
users[element.name] = element.role
}
const ro: { [key: string] : any } = {
"code": 200,
"context": JSON.stringify(users)
}
contextContaienr = ro
}
case "admin": {
// everyone
for (let index = 0; index < list.length; index++) {
const element = list[index];
users[element.name] = element.role
}
const ro: { [key: string] : any } = {
"code": 200,
"context": JSON.stringify(users)
}
contextContaienr = ro
}
case "developer": {
// everyone
for (let index = 0; index < list.length; index++) {
const element = list[index];
users[element.name] = element.role
}
const ro: { [key: string] : any } = {
"code": 200,
"context": JSON.stringify(users)
}
contextContaienr = ro
}
default: {
// can only seen developer
for (let index = 0; index < list.length; index++) {
const element = list[index];
if (element.role === "developer") {
users[element.name] = element.role
}
}
const ro: { [key: string] : any } = {
"code": 200,
"context": JSON.stringify(users)
}
contextContaienr = ro
}
}
})
})
return
}
default:{
const resolveObject: { [key: string] : any } = {
if (!vaildated) {
const ro: { [key: string] : any } = {
"code": 401,
"context": "Invalid Signature"
}
contextContaienr = ro
return
}
const roresolveObject: { [key: string] : any } = {
"code": 404,
"context": "User Management Action " + actionTypeRaw + " Not Found"
}
contextContaienr = resolveObject
contextContaienr = roresolveObject
return
}
}
})
let currentStamp = (new Date()).getTime()
while (contextContaienr === undefined && currentStamp - beginStamp < 50) {
require('deasync').runLoopOnce()
// will sleep up to around 4680ms
while (contextContaienr === undefined && Math.abs((currentStamp - beginStamp) / 1000) < 4) {
require('deasync').sleep(2333)
currentStamp = (new Date()).getTime()
}
......
......@@ -40,7 +40,7 @@ export class UserManager {
private async reloadUserListFromDataBaseIfNeeded(callback: ((users: User[]) => void) = null ) {
const gap = (new Date()).getTime() - this.userListCacheLastUpdate
if (gap > 60000) {
if (gap / 1000 > 60) {
DataBase.shared.readObjectsFromTable("users", (err: any, ret: any) => {
const obj = Object(ret)
if (err) {
......@@ -106,7 +106,7 @@ export class UserManager {
public readUserType(withName: string | undefined, callback: (type: string | undefined) => void) {
// root, admin, developer, user
if (withName === "root" || withName === null || withName === undefined || withName === "") {
if (withName === "root") {
callback("root")
return
}
......@@ -114,9 +114,11 @@ export class UserManager {
if (item !== undefined) {
callback(item.role)
return
} else {
callback(undefined)
return
}
})
callback(undefined)
}
public createUser(name: string, passsha: string, role: string, callback: (errTip: string | undefined) => void) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment