Skip to content

Commit 25c0384

Browse files
authored
Merge pull request openimsdk#86 from icey-yu/fix-userID-check
Fix user id check
2 parents be1abf7 + c4eaa9e commit 25c0384

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

user/user.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package user
1717
import (
1818
"errors"
1919
"github.com/openimsdk/protocol/constant"
20+
"github.com/openimsdk/protocol/util/datautil"
2021
)
2122

2223
func (x *GetAllUserIDReq) Check() error {
@@ -140,11 +141,19 @@ func (x *GetPaginationUsersReq) Check() error {
140141

141142
func (x *UserRegisterReq) Check() error {
142143
if x.Users == nil {
143-
return errors.New("Users are empty")
144+
return errors.New("users are empty")
145+
}
146+
for _, u := range x.Users {
147+
switch {
148+
case u == nil:
149+
return errors.New("user is empty")
150+
case datautil.IsLegalUserID(u.UserID):
151+
return errors.New("userID is legal")
152+
}
144153
}
145154
for _, u := range x.Users {
146155
if u.Nickname == "" {
147-
return errors.New("User name is empty")
156+
return errors.New("user name is empty")
148157
}
149158
}
150159

util/datautil/string.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package datautil
2+
3+
import "unicode"
4+
5+
// IsLegalUserID check if str is legal userID(Only contain letter/number/_).
6+
func IsLegalUserID(str string) bool {
7+
for _, r := range str {
8+
if !IsAlphanumeric(r) && r != '_' {
9+
return false
10+
}
11+
}
12+
return true
13+
}
14+
15+
// IsAlphanumeric check if b is a letter or number
16+
func IsAlphanumeric(b rune) bool {
17+
if !unicode.IsLetter(b) && !unicode.IsDigit(b) {
18+
return false
19+
}
20+
return true
21+
}

0 commit comments

Comments
 (0)