mirror of
https://github.com/ddworken/hishtory.git
synced 2025-06-21 04:17:45 +02:00
Allow register new device when exceed user limit when user already exist (#181)
This commit is contained in:
parent
199307f74a
commit
8c4ede4186
@ -153,6 +153,19 @@ func (db *DB) DistinctUsers(ctx context.Context) (int64, error) {
|
|||||||
return numDistinctUsers, nil
|
return numDistinctUsers, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (db *DB) UserAlreadyExist(ctx context.Context, userID string) (bool, error) {
|
||||||
|
var cnt int64
|
||||||
|
tx := db.WithContext(ctx).Table("devices").Where("user_id = ?", userID).Count(&cnt)
|
||||||
|
if tx.Error != nil {
|
||||||
|
return false, fmt.Errorf("tx.Error: %w", tx.Error)
|
||||||
|
}
|
||||||
|
|
||||||
|
if cnt > 0 {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (db *DB) DumpRequestCreate(ctx context.Context, req *shared.DumpRequest) error {
|
func (db *DB) DumpRequestCreate(ctx context.Context, req *shared.DumpRequest) error {
|
||||||
tx := db.WithContext(ctx).Create(req)
|
tx := db.WithContext(ctx).Create(req)
|
||||||
if tx.Error != nil {
|
if tx.Error != nil {
|
||||||
|
@ -201,7 +201,17 @@ func (s *Server) apiDownloadHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) apiRegisterHandler(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) apiRegisterHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
userId := getRequiredQueryParam(r, "user_id")
|
||||||
|
deviceId := getRequiredQueryParam(r, "device_id")
|
||||||
|
isIntegrationTestDevice := getOptionalQueryParam(r, "is_integration_test_device", false) == "true"
|
||||||
|
|
||||||
if getMaximumNumberOfAllowedUsers() < math.MaxInt {
|
if getMaximumNumberOfAllowedUsers() < math.MaxInt {
|
||||||
|
userAlreadyExist, err := s.db.UserAlreadyExist(r.Context(), userId)
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Errorf("db.UserAlreadyExist: %w", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
if !userAlreadyExist {
|
||||||
numDistinctUsers, err := s.db.DistinctUsers(r.Context())
|
numDistinctUsers, err := s.db.DistinctUsers(r.Context())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(fmt.Errorf("db.DistinctUsers: %w", err))
|
panic(fmt.Errorf("db.DistinctUsers: %w", err))
|
||||||
@ -210,9 +220,7 @@ func (s *Server) apiRegisterHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
panic(fmt.Sprintf("Refusing to allow registration of new device since there are currently %d users and this server allows a max of %d users", numDistinctUsers, getMaximumNumberOfAllowedUsers()))
|
panic(fmt.Sprintf("Refusing to allow registration of new device since there are currently %d users and this server allows a max of %d users", numDistinctUsers, getMaximumNumberOfAllowedUsers()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
userId := getRequiredQueryParam(r, "user_id")
|
}
|
||||||
deviceId := getRequiredQueryParam(r, "device_id")
|
|
||||||
isIntegrationTestDevice := getOptionalQueryParam(r, "is_integration_test_device", false) == "true"
|
|
||||||
|
|
||||||
existingDevicesCount, err := s.db.CountDevicesForUser(r.Context(), userId)
|
existingDevicesCount, err := s.db.CountDevicesForUser(r.Context(), userId)
|
||||||
checkGormError(err)
|
checkGormError(err)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user