Skip to content

Commit

Permalink
ignore user limit if user already registered
Browse files Browse the repository at this point in the history
  • Loading branch information
nghduc97 committed Feb 19, 2024
1 parent b95f049 commit 20c113a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
17 changes: 15 additions & 2 deletions backend/server/internal/database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,19 @@ func (db *DB) DistinctUsers(ctx context.Context) (int64, error) {
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 {
tx := db.WithContext(ctx).Create(req)
if tx.Error != nil {
Expand Down Expand Up @@ -409,7 +422,7 @@ func (db *DB) DeepClean(ctx context.Context) error {
FROM devices
GROUP BY user_id
HAVING COUNT(DISTINCT device_id) = 1
)
)
`)
if r.Error != nil {
return fmt.Errorf("failed to create list of single device users: %w", r.Error)
Expand All @@ -419,7 +432,7 @@ func (db *DB) DeepClean(ctx context.Context) error {
SELECT user_id
FROM usage_data
WHERE last_used <= (now() - INTERVAL '180 days')
)
)
`)
if r.Error != nil {
return fmt.Errorf("failed to create list of inactive users: %w", r.Error)
Expand Down
22 changes: 15 additions & 7 deletions backend/server/internal/server/api_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,18 +201,26 @@ func (s *Server) apiDownloadHandler(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 {
numDistinctUsers, err := s.db.DistinctUsers(r.Context())
userAlreadyExist, err := s.db.UserAlreadyExist(r.Context(), userId)
if err != nil {
panic(fmt.Errorf("db.DistinctUsers: %w", err))
panic(fmt.Errorf("db.UserAlreadyExist: %w", err))
}
if numDistinctUsers >= int64(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()))

if !userAlreadyExist {
numDistinctUsers, err := s.db.DistinctUsers(r.Context())
if err != nil {
panic(fmt.Errorf("db.DistinctUsers: %w", err))
}
if numDistinctUsers >= int64(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)
checkGormError(err)
Expand Down

0 comments on commit 20c113a

Please sign in to comment.