Commit 3264b7cb authored by Lakr Aream's avatar Lakr Aream 👼🏽

完成删除

parent ff4b0f2f
......@@ -53,8 +53,8 @@ database:
Any request with root ticket signed will be accept even from unauthorized ip address, changed it to 00000 to disable root sign after setup is a good way to reduce attack surface. You can create an admin account use root ticket thu it can take over all requests.
- TIMESTAMP:
Time stamp should be time intercal since 1970 to now, if you are using Date().getTime() then you need to / 1000
> var timestamp = Int(Date().timeIntervalSince1970)
Time stamp should be time intercal since 1970 to now, if you are using Apple API timeIntervalSince1970 then you need to * 1000
> var timestamp = Int(Date().timeIntervalSince1970) * 1000
### Signing Request With Ticket
......@@ -183,11 +183,11 @@ TABLE `Candiay`.`users` (
"name": "Lakr233", # any name with a-z A-Z 0-9
"passwordSHA256": "sha256edpassword", # password could be any, but we only use a SHA256 hash
"role": "admin", # admin, developer, user
"timestamp": 1585910739 # timestamp since 1970
"timestamp": 1585910739000 # timestamp since 1970
}
```
- Examples:
> http://www.candiay.backend/users/c?action=create&from=root&name=Lakr233&passwordSHA256=sha256edpassword&role=admin&timestamp=1585910739
> http://www.candiay.backend/users/c?action=create&from=root&name=Lakr233&passwordSHA256=sha256edpassword&role=admin&timestamp=1585910739000
- Call Back Example
```
{
......@@ -209,15 +209,16 @@ TABLE `Candiay`.`users` (
"action": "delete", # action
"from": "root", # user name
"name": "Lakr233", # any name with a-z A-Z 0-9
"timestamp": 1585910739 # timestamp since 1970
"timestamp": 1585910739000 # timestamp since 1970
}
```
- Examples:
> http://www.candiay.backend/users/c?action=delete&from=root&name=Lakr233&timestamp=1585910739
> http://www.candiay.backend/users/c?action=delete&from=root&name=Lakr233&timestamp=1585910739000
- Call Back Example
```
{
"result": "success", # success, failed, invaildSign
"tips": "exists if failed"
}
```
</details>
......@@ -234,11 +235,11 @@ TABLE `Candiay`.`users` (
"action": "modify.name", # .name, .password
"from": "Lakr233", # user name
"meta": "QAQ", # change to
"timestamp": 1585910739 # timestamp since 1970
"timestamp": 1585910739000 # timestamp since 1970
}
```
- Examples:
> http://www.candiay.backend/users/c?action=modify.name&from=Lakr233&meta=QAQ&timestamp=1585910739
> http://www.candiay.backend/users/c?action=modify.name&from=Lakr233&meta=QAQ&timestamp=1585910739000
- Call Back Example
```
{
......@@ -258,11 +259,11 @@ TABLE `Candiay`.`users` (
{
"action": "list", # action
"from": "Lakr233", # user name
"timestamp": 1585910739 # timestamp since 1970
"timestamp": 1585910739000 # timestamp since 1970
}
```
- Examples:
> http://www.candiay.backend/users/c?action=list&from=Lakr233&timestamp=1585910739
> http://www.candiay.backend/users/c?action=list&from=Lakr233&timestamp=1585910739000
- Call Back Example
```
{
......@@ -284,11 +285,11 @@ TABLE `Candiay`.`users` (
"action": "info", # action
"from": "Lakr233", # user name
"to": "Lakr233", # user to be checked
"timestamp": 1585910739 # timestamp since 1970
"timestamp": 1585910739000 # timestamp since 1970
}
```
- Examples:
> http://www.candiay.backend/users/c?action=info&from=Lakr233&to=Lakr233&timestamp=1585910739
> http://www.candiay.backend/users/c?action=info&from=Lakr233&to=Lakr233&timestamp=1585910739000
- Call Back Example
```
{
......
......@@ -54,7 +54,7 @@ export class RequestManager {
const signed = require('crypto').createHmac('sha1', token.toLowerCase()).update(Buffer.from(signString, 'utf-8')).digest("hex");
if (signed === clientSignResult) {
// Sign is ok, then check timestamp
const current = (new Date()).getTime() / 1000
const current = (new Date()).getTime()
const gap = current - clientTimeStamp
if (gap < 0 || gap > ConfigManager.shared.kikik) {
callback(false)
......@@ -104,7 +104,7 @@ export class RequestManager {
// Doing ~~Steave~~ Jobs
const actionTypeRaw = String(params["action"])
switch (actionTypeRaw.toLowerCase()) {
case "create":
case "create": {
UserManager.shared.readUserType(params["from"], (role) => {
if (role !== "root" && role !== "admin") {
const ro: { [key: string] : any } = {
......@@ -136,13 +136,46 @@ export class RequestManager {
})
})
return
default:
}
case "delete": {
UserManager.shared.readUserType(params["from"], (role) => {
if (role !== "root" && params["from"] !== params["name"]) {
const ro: { [key: string] : any } = {
"code": 401,
"context": "Unauthorized user performing authorized operation"
}
contextContaienr = ro
return
}
const user2delete = params["name"]
UserManager.shared.deleteUser(user2delete, (err) => {
if (err) {
const item = { "result": "failed", "tips": err }
const ro: { [key: string] : any } = {
"code": 418,
"context": JSON.stringify(item)
}
contextContaienr = ro
} else {
const item = { "result": "success" }
const ro: { [key: string] : any } = {
"code": 200,
"context": JSON.stringify(item)
}
contextContaienr = ro
}
})
})
return
}
default:{
const resolveObject: { [key: string] : any } = {
"code": 404,
"context": "User Management Action " + actionTypeRaw + " Not Found"
}
contextContaienr = resolveObject
return
}
}
})
......
......@@ -23,6 +23,7 @@ export class UserManager {
if (this.userListCache === undefined || this.userListCache.length < 1) {
// reload from database
this.userListCacheLastUpdate = -10000
console.log("Update user list from sql now");
this.reloadUserListFromDataBaseIfNeeded((users) => {
this.userListCache = users
if (callback !== null) {
......@@ -38,8 +39,8 @@ export class UserManager {
}
private async reloadUserListFromDataBaseIfNeeded(callback: ((users: User[]) => void) = null ) {
const gap = (new Date()).getTime() / 1000 - this.userListCacheLastUpdate
if (gap > 60) {
const gap = (new Date()).getTime() - this.userListCacheLastUpdate
if (gap > 60000) {
DataBase.shared.readObjectsFromTable("users", (err: any, ret: any) => {
const obj = Object(ret)
if (err) {
......@@ -61,11 +62,15 @@ export class UserManager {
ulist.push(u)
}
this.userListCache = ulist
this.userListCacheLastUpdate = (new Date()).getTime() / 1000
this.userListCacheLastUpdate = (new Date()).getTime()
if (callback != null) {
callback(ulist)
}
})
} else {
if (callback != null) {
callback(this.userListCache)
}
}
}
......@@ -85,7 +90,7 @@ export class UserManager {
})
}
public readTokenFromUser(withName: string, callback: (token: string) => void) {
public readTokenFromUser(withName: string, callback: (token: string | undefined) => void) {
if (withName === "root") {
callback(ConfigManager.shared.rootTicket)
return
......@@ -93,11 +98,13 @@ export class UserManager {
this.getUserFromList(withName, (item) => {
if (item !== undefined) {
callback(item.token)
return
}
})
callback(undefined)
}
public readUserType(withName: string | undefined, callback: (type: string) => void) {
public readUserType(withName: string | undefined, callback: (type: string | undefined) => void) {
// root, admin, developer, user
if (withName === "root" || withName === null || withName === undefined || withName === "") {
callback("root")
......@@ -106,8 +113,10 @@ export class UserManager {
this.getUserFromList(withName, (item) => {
if (item !== undefined) {
callback(item.role)
return
}
})
callback(undefined)
}
public createUser(name: string, passsha: string, role: string, callback: (errTip: string | undefined) => void) {
......@@ -131,7 +140,7 @@ export class UserManager {
}
const base = DataBase.shared.base
const uuid = uuidv4()
const timeStamp = (new Date()).getTime() / 1000
const timeStamp = (new Date()).getTime()
const cmd = "INSERT INTO `" + base + "`.`users`(`name`, `pass`, `role`, `token`, `guard`) VALUES ('" + name + "', '" + passsha + "', '" + role + "', '" + uuid + "', " + timeStamp + ")"
DataBase.shared.queryWithCommand(cmd, (err, ret) => {
if (err) {
......@@ -143,4 +152,23 @@ export class UserManager {
})
}
public deleteUser(name: string, callback: (errTip: string | undefined) => void) {
// tip should be undefined if operation succeed
this.getUserFromList(name, (item) => {
if (item === undefined) {
callback("User not found")
return
}
const base = DataBase.shared.base
const cmd = "DELETE FROM `" + base + "`.`users` WHERE name='" + name + "'"
DataBase.shared.queryWithCommand(cmd, (err, ret) => {
if (err) {
callback("mysql returns error: " + String(err))
} else {
callback(undefined)
}
})
})
}
}
\ No newline at end of file
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