feat(client): support SetUser api

This commit is contained in:
hexxa 2021-08-24 21:35:12 +08:00 committed by Hexxa
parent f2dd36c630
commit c2673cd416
6 changed files with 64 additions and 8 deletions

View file

@ -7,6 +7,7 @@ import (
"github.com/ihexxa/quickshare/src/handlers" "github.com/ihexxa/quickshare/src/handlers"
"github.com/ihexxa/quickshare/src/handlers/multiusers" "github.com/ihexxa/quickshare/src/handlers/multiusers"
"github.com/ihexxa/quickshare/src/userstore"
"github.com/parnurzeal/gorequest" "github.com/parnurzeal/gorequest"
) )
@ -52,6 +53,17 @@ func (cl *SingleUserClient) SetPwd(oldPwd, newPwd string, token *http.Cookie) (*
End() 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) { 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/")). resp, body, errs := cl.r.Post(cl.url("/v1/users/")).
AddCookie(token). AddCookie(token).

View file

@ -64,6 +64,7 @@ export interface IUsersClient {
isAuthed: () => Promise<Response>; isAuthed: () => Promise<Response>;
self: () => Promise<Response>; self: () => Promise<Response>;
setPwd: (oldPwd: string, newPwd: string) => Promise<Response>; setPwd: (oldPwd: string, newPwd: string) => Promise<Response>;
setUser: (id: string, role: string, quota: Quota) => Promise<Response>;
forceSetPwd: (userID: string, newPwd: string) => Promise<Response>; forceSetPwd: (userID: string, newPwd: string) => Promise<Response>;
addUser: (name: string, pwd: string, role: string) => Promise<Response>; addUser: (name: string, pwd: string, role: string) => Promise<Response>;
delUser: (userID: string) => Promise<Response>; delUser: (userID: string) => Promise<Response>;

View file

@ -1,11 +1,16 @@
import { BaseClient, Response, userIDParam } from "./"; import { BaseClient, Response, userIDParam, Quota } from "./";
export class UsersClient extends BaseClient { export class UsersClient extends BaseClient {
constructor(url: string) { constructor(url: string) {
super(url); super(url);
} }
login = (user: string, pwd: string, captchaId: string, captchaInput:string): Promise<Response> => { login = (
user: string,
pwd: string,
captchaId: string,
captchaInput: string
): Promise<Response> => {
return this.do({ return this.do({
method: "post", method: "post",
url: `${this.url}/v1/users/login`, url: `${this.url}/v1/users/login`,
@ -43,6 +48,18 @@ export class UsersClient extends BaseClient {
}); });
}; };
setUser = (id: string, role: string, quota: Quota): Promise<Response> => {
return this.do({
method: "patch",
url: `${this.url}/v1/users/`,
data: {
id,
role,
quota,
},
});
};
forceSetPwd = (userID: string, newPwd: string): Promise<Response> => { forceSetPwd = (userID: string, newPwd: string): Promise<Response> => {
return this.do({ return this.do({
method: "patch", method: "patch",

View file

@ -1,11 +1,13 @@
// TODO: replace this with jest mocks // TODO: replace this with jest mocks
import { Response } from "./"; import { Response, Quota } from "./";
export interface UsersClientResps { export interface UsersClientResps {
loginMockResp: Response; loginMockResp: Response;
logoutMockResp: Response; logoutMockResp: Response;
isAuthedMockResp: Response; isAuthedMockResp: Response;
setPwdMockResp: Response; setPwdMockResp: Response;
setUserMockResp: Response;
forceSetPwdMockResp: Response; forceSetPwdMockResp: Response;
addUserMockResp: Response; addUserMockResp: Response;
delUserMockResp: Response; delUserMockResp: Response;
@ -22,6 +24,7 @@ export const resps = {
logoutMockResp: { status: 200, statusText: "", data: {} }, logoutMockResp: { status: 200, statusText: "", data: {} },
isAuthedMockResp: { status: 200, statusText: "", data: {} }, isAuthedMockResp: { status: 200, statusText: "", data: {} },
setPwdMockResp: { status: 200, statusText: "", data: {} }, setPwdMockResp: { status: 200, statusText: "", data: {} },
setUserMockResp: { status: 200, statusText: "", data: {} },
forceSetPwdMockResp: { status: 200, statusText: "", data: {} }, forceSetPwdMockResp: { status: 200, statusText: "", data: {} },
addUserMockResp: { status: 200, statusText: "", data: {} }, addUserMockResp: { status: 200, statusText: "", data: {} },
delUserMockResp: { status: 200, statusText: "", data: {} }, delUserMockResp: { status: 200, statusText: "", data: {} },
@ -119,6 +122,11 @@ export class MockUsersClient {
return this.wrapPromise(this.resps.setPwdMockResp); return this.wrapPromise(this.resps.setPwdMockResp);
}; };
setUser = (id: string, role: string, quota: Quota): Promise<Response> => {
return this.wrapPromise(this.resps.setUserMockResp);
};
forceSetPwd = (userID: string, newPwd: string): Promise<Response> => { forceSetPwd = (userID: string, newPwd: string): Promise<Response> => {
return this.wrapPromise(this.resps.forceSetPwdMockResp); return this.wrapPromise(this.resps.forceSetPwdMockResp);
}; };

View file

@ -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 = () => { delUser = () => {
updater() updater()
@ -121,7 +133,7 @@ export class UserForm extends React.Component<
return updater().listUsers(); return updater().listUsers();
}) })
.then((_: boolean) => { .then((_: boolean) => {
this.props.update(updater().updatePanes); this.props.update(updater().updateAdmin);
}); });
}; };
@ -319,7 +331,7 @@ export class AdminPane extends React.Component<Props, State, {}> {
return updater().listRoles(); return updater().listRoles();
}) })
.then(() => { .then(() => {
this.props.update(updater().updatePanes); this.props.update(updater().updateAdmin);
}); });
}; };
@ -341,7 +353,7 @@ export class AdminPane extends React.Component<Props, State, {}> {
return updater().listRoles(); return updater().listRoles();
}) })
.then(() => { .then(() => {
this.props.update(updater().updatePanes); this.props.update(updater().updateAdmin);
}); });
}; };
@ -372,7 +384,7 @@ export class AdminPane extends React.Component<Props, State, {}> {
return updater().listUsers(); return updater().listUsers();
}) })
.then(() => { .then(() => {
this.props.update(updater().updatePanes); this.props.update(updater().updateAdmin);
}); });
}; };

View file

@ -10,6 +10,7 @@ import {
IFilesClient, IFilesClient,
MetadataResp, MetadataResp,
UploadInfo, UploadInfo,
Quota,
} from "../client"; } from "../client";
import { FilesClient } from "../client/files"; import { FilesClient } from "../client/files";
import { UsersClient } from "../client/users"; import { UsersClient } from "../client/users";
@ -218,6 +219,11 @@ export class Updater {
return resp.status === 200; return resp.status === 200;
}; };
setUser = async (userID: string, role: string, quota: Quota): Promise<boolean> => {
const resp = await this.usersClient.setUser(userID, role, quota);
return resp.status === 200;
};
setRole = async (userID: string, role: string): Promise<boolean> => { setRole = async (userID: string, role: string): Promise<boolean> => {
const resp = await this.usersClient.delUser(userID); const resp = await this.usersClient.delUser(userID);
return resp.status === 200; return resp.status === 200;