mirror of
https://github.com/usememos/memos.git
synced 2025-06-05 22:09:59 +02:00
chore: update userinfo validator (#868)
* chore: update userinfo validator * chore: update actions * chore: update
This commit is contained in:
20
.github/workflows/backend-tests-default.yml
vendored
Normal file
20
.github/workflows/backend-tests-default.yml
vendored
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
name: Backend Test
|
||||||
|
|
||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
- "release/v*.*.*"
|
||||||
|
paths:
|
||||||
|
- "web/**"
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
go-static-checks:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- run: 'echo "Not required"'
|
||||||
|
|
||||||
|
go-tests:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- run: 'echo "Not required"'
|
4
.github/workflows/backend-tests.yml
vendored
4
.github/workflows/backend-tests.yml
vendored
@ -1,12 +1,10 @@
|
|||||||
name: Backend Test
|
name: Backend Test
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
pull_request:
|
||||||
branches:
|
branches:
|
||||||
- main
|
- main
|
||||||
- "release/v*.*.*"
|
- "release/v*.*.*"
|
||||||
pull_request:
|
|
||||||
branches: [main]
|
|
||||||
paths-ignore:
|
paths-ignore:
|
||||||
- "web/**"
|
- "web/**"
|
||||||
|
|
||||||
|
25
.github/workflows/frontend-tests-default.yml
vendored
Normal file
25
.github/workflows/frontend-tests-default.yml
vendored
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
name: Frontend Test
|
||||||
|
|
||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
- "release/v*.*.*"
|
||||||
|
paths-ignore:
|
||||||
|
- "web/**"
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
eslint-checks:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- run: 'echo "Not required"'
|
||||||
|
|
||||||
|
jest-tests:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- run: 'echo "Not required"'
|
||||||
|
|
||||||
|
frontend-build:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- run: 'echo "Not required"'
|
4
.github/workflows/frontend-tests.yml
vendored
4
.github/workflows/frontend-tests.yml
vendored
@ -1,12 +1,10 @@
|
|||||||
name: Frontend Test
|
name: Frontend Test
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
pull_request:
|
||||||
branches:
|
branches:
|
||||||
- main
|
- main
|
||||||
- "release/v*.*.*"
|
- "release/v*.*.*"
|
||||||
pull_request:
|
|
||||||
branches: [main]
|
|
||||||
paths:
|
paths:
|
||||||
- "web/**"
|
- "web/**"
|
||||||
|
|
||||||
|
41
api/user.go
41
api/user.go
@ -2,6 +2,8 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/usememos/memos/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Role is the type of a role.
|
// Role is the type of a role.
|
||||||
@ -61,9 +63,23 @@ func (create UserCreate) Validate() error {
|
|||||||
if len(create.Username) < 4 {
|
if len(create.Username) < 4 {
|
||||||
return fmt.Errorf("username is too short, minimum length is 4")
|
return fmt.Errorf("username is too short, minimum length is 4")
|
||||||
}
|
}
|
||||||
|
if len(create.Username) > 32 {
|
||||||
|
return fmt.Errorf("username is too long, maximum length is 32")
|
||||||
|
}
|
||||||
if len(create.Password) < 4 {
|
if len(create.Password) < 4 {
|
||||||
return fmt.Errorf("password is too short, minimum length is 4")
|
return fmt.Errorf("password is too short, minimum length is 4")
|
||||||
}
|
}
|
||||||
|
if len(create.Nickname) > 64 {
|
||||||
|
return fmt.Errorf("nickname is too long, maximum length is 64")
|
||||||
|
}
|
||||||
|
if create.Email != "" {
|
||||||
|
if len(create.Email) > 256 {
|
||||||
|
return fmt.Errorf("email is too long, maximum length is 256")
|
||||||
|
}
|
||||||
|
if common.ValidateEmail(create.Email) {
|
||||||
|
return fmt.Errorf("invalid email format")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -85,6 +101,31 @@ type UserPatch struct {
|
|||||||
OpenID *string
|
OpenID *string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (patch UserPatch) Validate() error {
|
||||||
|
if patch.Username != nil && len(*patch.Username) < 4 {
|
||||||
|
return fmt.Errorf("username is too short, minimum length is 4")
|
||||||
|
}
|
||||||
|
if patch.Username != nil && len(*patch.Username) > 32 {
|
||||||
|
return fmt.Errorf("username is too long, maximum length is 32")
|
||||||
|
}
|
||||||
|
if patch.Password != nil && len(*patch.Password) < 4 {
|
||||||
|
return fmt.Errorf("password is too short, minimum length is 4")
|
||||||
|
}
|
||||||
|
if patch.Nickname != nil && len(*patch.Nickname) > 64 {
|
||||||
|
return fmt.Errorf("nickname is too long, maximum length is 64")
|
||||||
|
}
|
||||||
|
if patch.Email != nil {
|
||||||
|
if len(*patch.Email) > 256 {
|
||||||
|
return fmt.Errorf("email is too long, maximum length is 256")
|
||||||
|
}
|
||||||
|
if common.ValidateEmail(*patch.Email) {
|
||||||
|
return fmt.Errorf("invalid email format")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
type UserFind struct {
|
type UserFind struct {
|
||||||
ID *int `json:"id"`
|
ID *int `json:"id"`
|
||||||
|
|
||||||
|
@ -198,9 +198,8 @@ func (s *Server) registerUserRoutes(g *echo.Group) {
|
|||||||
if err := json.NewDecoder(c.Request().Body).Decode(userPatch); err != nil {
|
if err := json.NewDecoder(c.Request().Body).Decode(userPatch); err != nil {
|
||||||
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch user request").SetInternal(err)
|
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch user request").SetInternal(err)
|
||||||
}
|
}
|
||||||
|
if err := userPatch.Validate(); err != nil {
|
||||||
if userPatch.Email != nil && *userPatch.Email != "" && !common.ValidateEmail(*userPatch.Email) {
|
return echo.NewHTTPError(http.StatusBadRequest, "Invalid user patch format.").SetInternal(err)
|
||||||
return echo.NewHTTPError(http.StatusBadRequest, "Invalid email format")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if userPatch.Password != nil && *userPatch.Password != "" {
|
if userPatch.Password != nil && *userPatch.Password != "" {
|
||||||
|
Reference in New Issue
Block a user