From c2673cd416a9275e68735adf771244dad33df2df Mon Sep 17 00:00:00 2001 From: hexxa Date: Tue, 24 Aug 2021 21:35:12 +0800 Subject: [PATCH] feat(client): support SetUser api --- src/client/users.go | 12 ++++++++++ src/client/web/src/client/index.ts | 1 + src/client/web/src/client/users.ts | 21 ++++++++++++++++-- src/client/web/src/client/users_mock.ts | 10 ++++++++- src/client/web/src/components/pane_admin.tsx | 22 ++++++++++++++----- .../web/src/components/state_updater.ts | 6 +++++ 6 files changed, 64 insertions(+), 8 deletions(-) diff --git a/src/client/users.go b/src/client/users.go index 3b12e5f..c4252d5 100644 --- a/src/client/users.go +++ b/src/client/users.go @@ -7,6 +7,7 @@ import ( "github.com/ihexxa/quickshare/src/handlers" "github.com/ihexxa/quickshare/src/handlers/multiusers" + "github.com/ihexxa/quickshare/src/userstore" "github.com/parnurzeal/gorequest" ) @@ -52,6 +53,17 @@ func (cl *SingleUserClient) SetPwd(oldPwd, newPwd string, token *http.Cookie) (* End() } +func (cl *SingleUserClient) SetUser(ID uint64, role string, quota *userstore.Quota, token *http.Cookie) (*http.Response, string, []error) { + return cl.r.Patch(cl.url("/v1/users/")). + Send(multiusers.SetUserReq{ + ID: ID, + Role: role, + Quota: quota, + }). + AddCookie(token). + End() +} + func (cl *SingleUserClient) AddUser(name, pwd, role string, token *http.Cookie) (*http.Response, *multiusers.AddUserResp, []error) { resp, body, errs := cl.r.Post(cl.url("/v1/users/")). AddCookie(token). diff --git a/src/client/web/src/client/index.ts b/src/client/web/src/client/index.ts index bbada68..9375ba2 100644 --- a/src/client/web/src/client/index.ts +++ b/src/client/web/src/client/index.ts @@ -64,6 +64,7 @@ export interface IUsersClient { isAuthed: () => Promise; self: () => Promise; setPwd: (oldPwd: string, newPwd: string) => Promise; + setUser: (id: string, role: string, quota: Quota) => Promise; forceSetPwd: (userID: string, newPwd: string) => Promise; addUser: (name: string, pwd: string, role: string) => Promise; delUser: (userID: string) => Promise; diff --git a/src/client/web/src/client/users.ts b/src/client/web/src/client/users.ts index 5d427eb..59e9c53 100644 --- a/src/client/web/src/client/users.ts +++ b/src/client/web/src/client/users.ts @@ -1,11 +1,16 @@ -import { BaseClient, Response, userIDParam } from "./"; +import { BaseClient, Response, userIDParam, Quota } from "./"; export class UsersClient extends BaseClient { constructor(url: string) { super(url); } - login = (user: string, pwd: string, captchaId: string, captchaInput:string): Promise => { + login = ( + user: string, + pwd: string, + captchaId: string, + captchaInput: string + ): Promise => { return this.do({ method: "post", url: `${this.url}/v1/users/login`, @@ -43,6 +48,18 @@ export class UsersClient extends BaseClient { }); }; + setUser = (id: string, role: string, quota: Quota): Promise => { + return this.do({ + method: "patch", + url: `${this.url}/v1/users/`, + data: { + id, + role, + quota, + }, + }); + }; + forceSetPwd = (userID: string, newPwd: string): Promise => { return this.do({ method: "patch", diff --git a/src/client/web/src/client/users_mock.ts b/src/client/web/src/client/users_mock.ts index fb9eca3..13d920a 100644 --- a/src/client/web/src/client/users_mock.ts +++ b/src/client/web/src/client/users_mock.ts @@ -1,11 +1,13 @@ // TODO: replace this with jest mocks -import { Response } from "./"; +import { Response, Quota } from "./"; + export interface UsersClientResps { loginMockResp: Response; logoutMockResp: Response; isAuthedMockResp: Response; setPwdMockResp: Response; + setUserMockResp: Response; forceSetPwdMockResp: Response; addUserMockResp: Response; delUserMockResp: Response; @@ -22,6 +24,7 @@ export const resps = { logoutMockResp: { status: 200, statusText: "", data: {} }, isAuthedMockResp: { status: 200, statusText: "", data: {} }, setPwdMockResp: { status: 200, statusText: "", data: {} }, + setUserMockResp: { status: 200, statusText: "", data: {} }, forceSetPwdMockResp: { status: 200, statusText: "", data: {} }, addUserMockResp: { status: 200, statusText: "", data: {} }, delUserMockResp: { status: 200, statusText: "", data: {} }, @@ -119,6 +122,11 @@ export class MockUsersClient { return this.wrapPromise(this.resps.setPwdMockResp); }; + setUser = (id: string, role: string, quota: Quota): Promise => { + return this.wrapPromise(this.resps.setUserMockResp); + }; + + forceSetPwd = (userID: string, newPwd: string): Promise => { return this.wrapPromise(this.resps.forceSetPwdMockResp); }; diff --git a/src/client/web/src/components/pane_admin.tsx b/src/client/web/src/components/pane_admin.tsx index 7c91400..ae65234 100644 --- a/src/client/web/src/components/pane_admin.tsx +++ b/src/client/web/src/components/pane_admin.tsx @@ -109,7 +109,19 @@ export class UserForm extends React.Component< }); }; - setUser = () => {}; + setUser = async () => { + return updater() + .setUser(this.props.id, this.props.role, this.props.quota) + .then((ok: boolean) => { + if (!ok) { + alert("failed to set user"); + } + return updater().listUsers(); + }) + .then(() => { + this.props.update(updater().updateAdmin); + }); + }; delUser = () => { updater() @@ -121,7 +133,7 @@ export class UserForm extends React.Component< return updater().listUsers(); }) .then((_: boolean) => { - this.props.update(updater().updatePanes); + this.props.update(updater().updateAdmin); }); }; @@ -319,7 +331,7 @@ export class AdminPane extends React.Component { return updater().listRoles(); }) .then(() => { - this.props.update(updater().updatePanes); + this.props.update(updater().updateAdmin); }); }; @@ -341,7 +353,7 @@ export class AdminPane extends React.Component { return updater().listRoles(); }) .then(() => { - this.props.update(updater().updatePanes); + this.props.update(updater().updateAdmin); }); }; @@ -372,7 +384,7 @@ export class AdminPane extends React.Component { return updater().listUsers(); }) .then(() => { - this.props.update(updater().updatePanes); + this.props.update(updater().updateAdmin); }); }; diff --git a/src/client/web/src/components/state_updater.ts b/src/client/web/src/components/state_updater.ts index e8f76a4..c5d8260 100644 --- a/src/client/web/src/components/state_updater.ts +++ b/src/client/web/src/components/state_updater.ts @@ -10,6 +10,7 @@ import { IFilesClient, MetadataResp, UploadInfo, + Quota, } from "../client"; import { FilesClient } from "../client/files"; import { UsersClient } from "../client/users"; @@ -218,6 +219,11 @@ export class Updater { return resp.status === 200; }; + setUser = async (userID: string, role: string, quota: Quota): Promise => { + const resp = await this.usersClient.setUser(userID, role, quota); + return resp.status === 200; + }; + setRole = async (userID: string, role: string): Promise => { const resp = await this.usersClient.delUser(userID); return resp.status === 200;