Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forbid to create user without full UserInfo #234

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/server/usermanager/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,4 @@ externalDocs:
host: 127.0.0.1:8080
basePath: /
schemes:
- http
- http
8 changes: 8 additions & 0 deletions internal/server/usermanager/localmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,14 @@ func (manager *localManager) WriteUserInfo(u UserInfo) (err error) {
if err != nil {
return err
}
k, v := bucket.Cursor().First()
// Registration required full UserInfo
if k == nil && v == nil {
if u.SessionsCap == nil || u.UpRate == nil || u.DownRate == nil ||
u.UpCredit == nil || u.DownCredit == nil || u.ExpiryTime == nil {
return ErrInvalidUserInfo
}
}
if u.SessionsCap != nil {
if err = bucket.Put([]byte("SessionsCap"), i32ToB(*u.SessionsCap)); err != nil {
return err
Expand Down
14 changes: 10 additions & 4 deletions internal/server/usermanager/localmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,16 @@ func TestLocalManager_WriteUserInfo(t *testing.T) {
mgr, cleaner := makeManager(t)
defer cleaner()

err := mgr.WriteUserInfo(mockUserInfo)
if err != nil {
t.Error(err)
}
/* New user with partial UserInfo */
err := mgr.WriteUserInfo(UserInfo{
UID: mockUID,
SessionsCap: JustInt32(*mockUserInfo.SessionsCap + 1),
})
assert.Error(t, err, ErrInvalidUserInfo)

/* New user with full UserInfo */
err = mgr.WriteUserInfo(mockUserInfo)
assert.NoError(t, err)

got, err := mgr.GetUserInfo(mockUID)
assert.NoError(t, err)
Expand Down
1 change: 1 addition & 0 deletions internal/server/usermanager/usermanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ var ErrMangerIsVoid = errors.New("cannot perform operation with user manager as
var ErrNoUpCredit = errors.New("No upload credit left")
var ErrNoDownCredit = errors.New("No download credit left")
var ErrUserExpired = errors.New("User has expired")
var ErrInvalidUserInfo = errors.New("Invalid user info")

type UserManager interface {
AuthenticateUser([]byte) (int64, int64, error)
Expand Down