feat: enable sharing with visitor

This commit is contained in:
hexxa 2021-09-24 14:15:19 +08:00 committed by Hexxa
parent d440cbe477
commit 2ccdf0e287
7 changed files with 73 additions and 48 deletions

View file

@ -35,11 +35,11 @@ export class Panes extends React.Component<Props, State, {}> {
render() {
let displaying = this.props.panes.displaying;
if (!this.props.login.authed) {
// if (!this.props.login.authed) {
// TODO: use constant instead
// TODO: control this with props
displaying = "login";
}
// displaying = "login";
// }
let panesMap: Map<string, JSX.Element> = Map({});
if (this.props.login.userRole !== roleVisitor) {
@ -60,7 +60,7 @@ export class Panes extends React.Component<Props, State, {}> {
/>
);
}
if (this.props.login.userRole === "admin") {
if (this.props.login.userRole === roleAdmin) {
panesMap = panesMap.set(
"admin",
<AdminPane

View file

@ -540,11 +540,16 @@ func (h *FileHandlers) Download(c *gin.Context) {
c.JSON(q.ErrResp(c, 403, q.ErrAccessDenied))
return
}
userID := c.MustGet(q.UserIDParam).(string)
userIDInt, err := strconv.ParseUint(userID, 10, 64)
if err != nil {
c.JSON(q.ErrResp(c, 500, err))
return
var err error
userIDInt := userstore.VisitorID
if role != userstore.VisitorRole {
userID := c.MustGet(q.UserIDParam).(string)
userIDInt, err = strconv.ParseUint(userID, 10, 64)
if err != nil {
c.JSON(q.ErrResp(c, 500, err))
return
}
}
// TODO: when sharing is introduced, move following logics to a separeted method

View file

@ -107,6 +107,7 @@ func NewMultiUsersSvc(cfg gocfg.ICfg, deps *depidx.Deps) (*MultiUsersSvc, error)
apiRuleCname(userstore.VisitorRole, "GET", "/v1/users/isauthed"): true,
apiRuleCname(userstore.VisitorRole, "GET", "/v1/users/self"): true,
apiRuleCname(userstore.VisitorRole, "GET", "/v1/fs/files"): true,
apiRuleCname(userstore.VisitorRole, "GET", "/v1/fs/dirs"): true,
apiRuleCname(userstore.VisitorRole, "OPTIONS", "/v1/settings/health"): true,
apiRuleCname(userstore.VisitorRole, "GET", "/v1/captchas/"): true,
apiRuleCname(userstore.VisitorRole, "GET", "/v1/captchas/imgs"): true,

View file

@ -94,7 +94,7 @@ func DefaultConfig() (string, error) {
},
Server: &ServerCfg{
Debug: false,
Host: "0.0.0.0",
Host: "127.0.0.1",
Port: 8686,
ReadTimeout: 2000,
WriteTimeout: 1000 * 3600 * 24, // 1 day

View file

@ -240,7 +240,7 @@ func TestUsersHandlers(t *testing.T) {
t.Fatal(resp.StatusCode)
}
if len(lsResp.Users) != 3 {
if len(lsResp.Users) != 4 {
t.Fatal(fmt.Errorf("incorrect users size (%d)", len(lsResp.Users)))
}
for _, user := range lsResp.Users {
@ -306,7 +306,7 @@ func TestUsersHandlers(t *testing.T) {
} else if resp.StatusCode != 200 {
t.Fatal(resp.StatusCode)
}
if len(lsResp.Users) != 2 {
if len(lsResp.Users) != 3 {
t.Fatal(fmt.Errorf("incorrect users size (%d)", len(lsResp.Users)))
}

View file

@ -19,10 +19,14 @@ const (
UsersNs = "users"
RoleListNs = "roleList"
InitTimeKey = "initTime"
VisitorID = uint64(1)
VisitorName = "visitor"
defaultSpaceLimit = 1024 * 1024 * 1024 // 1GB
defaultUploadSpeedLimit = 50 * 1024 * 1024 // 50MB
defaultDownloadSpeedLimit = 50 * 1024 * 1024 // 50MB
visitorUploadSpeedLimit = 10 * 1024 * 1024 // 50MB
visitorDownloadSpeedLimit = 10 * 1024 * 1024 // 50MB
)
var (
@ -99,7 +103,7 @@ func NewKVUserStore(store kvstore.IKVStore) (*KVUserStore, error) {
func (us *KVUserStore) Init(rootName, rootPwd string) error {
var err error
err = us.AddUser(&User{
admin := &User{
ID: 0,
Name: rootName,
Pwd: rootPwd,
@ -109,9 +113,24 @@ func (us *KVUserStore) Init(rootName, rootPwd string) error {
UploadSpeedLimit: defaultUploadSpeedLimit,
DownloadSpeedLimit: defaultDownloadSpeedLimit,
},
})
if err != nil {
return err
}
visitor := &User{
ID: VisitorID,
Name: VisitorName,
Pwd: rootPwd,
Role: VisitorRole,
Quota: &Quota{
SpaceLimit: 0,
UploadSpeedLimit: visitorUploadSpeedLimit,
DownloadSpeedLimit: visitorDownloadSpeedLimit,
},
}
for _, user := range []*User{admin, visitor} {
err = us.AddUser(user)
if err != nil {
return err
}
}
for _, role := range []string{AdminRole, UserRole, VisitorRole} {