Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
canifa_note
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Vũ Hoàng Anh
canifa_note
Commits
c60bb124
Commit
c60bb124
authored
Aug 20, 2022
by
Steven
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chore: update user setting validator
parent
3b1bb4a9
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
62 additions
and
4 deletions
+62
-4
user_setting.go
api/user_setting.go
+52
-0
memo.go
server/memo.go
+4
-1
user.go
server/user.go
+6
-3
No files found.
api/user_setting.go
View file @
c60bb124
package
api
import
(
"encoding/json"
"fmt"
)
type
UserSettingKey
string
const
(
...
...
@@ -20,6 +25,11 @@ func (key UserSettingKey) String() string {
return
""
}
var
(
UserSettingLocaleValue
=
[]
string
{
"en"
,
"zh"
}
UserSettingMemoVisibilityValue
=
[]
Visibility
{
Privite
,
Protected
,
Public
}
)
type
UserSetting
struct
{
UserID
int
Key
UserSettingKey
`json:"key"`
...
...
@@ -33,6 +43,48 @@ type UserSettingUpsert struct {
Value
string
`json:"value"`
}
func
(
upsert
UserSettingUpsert
)
Validate
()
error
{
if
upsert
.
Key
==
UserSettingLocaleKey
{
var
localeValue
string
err
:=
json
.
Unmarshal
([]
byte
(
upsert
.
Value
),
&
localeValue
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to unmarshal user setting locale value"
)
}
invalid
:=
true
for
_
,
value
:=
range
UserSettingLocaleValue
{
if
localeValue
==
value
{
invalid
=
false
break
}
}
if
invalid
{
return
fmt
.
Errorf
(
"invalid user setting locale value"
)
}
}
else
if
upsert
.
Key
==
UserSettingMemoVisibilityKey
{
var
memoVisibilityValue
Visibility
err
:=
json
.
Unmarshal
([]
byte
(
upsert
.
Value
),
&
memoVisibilityValue
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to unmarshal user setting memo visibility value"
)
}
invalid
:=
true
for
_
,
value
:=
range
UserSettingMemoVisibilityValue
{
if
memoVisibilityValue
==
value
{
invalid
=
false
break
}
}
if
invalid
{
return
fmt
.
Errorf
(
"invalid user setting memo visibility value"
)
}
}
else
{
return
fmt
.
Errorf
(
"invalid user setting key"
)
}
return
nil
}
type
UserSettingFind
struct
{
UserID
int
...
...
server/memo.go
View file @
c60bb124
...
...
@@ -43,7 +43,10 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
}
if
userMemoVisibilitySetting
!=
nil
{
memoVisibility
:=
api
.
Privite
json
.
Unmarshal
([]
byte
(
userMemoVisibilitySetting
.
Value
),
&
memoVisibility
)
err
:=
json
.
Unmarshal
([]
byte
(
userMemoVisibilitySetting
.
Value
),
&
memoVisibility
)
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to unmarshal user setting value"
)
.
SetInternal
(
err
)
}
memoCreate
.
Visibility
=
memoVisibility
}
...
...
server/user.go
View file @
c60bb124
...
...
@@ -118,9 +118,8 @@ func (s *Server) registerUserRoutes(g *echo.Group) {
if
err
:=
json
.
NewDecoder
(
c
.
Request
()
.
Body
)
.
Decode
(
userSettingUpsert
);
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"Malformatted post user setting upsert request"
)
.
SetInternal
(
err
)
}
if
userSettingUpsert
.
Key
.
String
()
==
""
{
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"Invalid user setting key"
)
if
err
:=
userSettingUpsert
.
Validate
();
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"Invalid user setting format"
)
.
SetInternal
(
err
)
}
userSettingUpsert
.
UserID
=
userID
...
...
@@ -191,6 +190,10 @@ func (s *Server) registerUserRoutes(g *echo.Group) {
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"Malformatted patch user request"
)
.
SetInternal
(
err
)
}
if
userPatch
.
Email
!=
nil
&&
!
common
.
ValidateEmail
(
*
userPatch
.
Email
)
{
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"Invalid email format"
)
}
if
userPatch
.
Password
!=
nil
&&
*
userPatch
.
Password
!=
""
{
passwordHash
,
err
:=
bcrypt
.
GenerateFromPassword
([]
byte
(
*
userPatch
.
Password
),
bcrypt
.
DefaultCost
)
if
err
!=
nil
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment