Soft delete users (#476)

* Update Makefile

* Optionally load test DB from env variable

* Add option to soft-delete a user

This allows users to be marked as deleted.
An additional middleware checks if a user is deleted or deactivated and rejects
requests for those as StatusUnauthorized.

note: the middelware adds an additional DB query to load the user.
This commit is contained in:
Michael Bumann
2024-01-09 19:38:01 +02:00
committed by GitHub
parent a6f493ec20
commit e33693398e
10 changed files with 163 additions and 14 deletions

View File

@@ -78,7 +78,7 @@ func (svc *LndhubService) CreateUser(ctx context.Context, login string, password
return user, err
}
func (svc *LndhubService) UpdateUser(ctx context.Context, userId int64, login *string, password *string, deactivated *bool) (user *models.User, err error) {
func (svc *LndhubService) UpdateUser(ctx context.Context, userId int64, login *string, password *string, deactivated *bool, deleted *bool) (user *models.User, err error) {
user, err = svc.FindUser(ctx, userId)
if err != nil {
return nil, err
@@ -99,6 +99,14 @@ func (svc *LndhubService) UpdateUser(ctx context.Context, userId int64, login *s
if deactivated != nil {
user.Deactivated = *deactivated
}
// if a user gets deleted we mark it as deactivated and deleted
// un-deleting it is not supported currently
if deleted != nil {
if *deleted == true {
user.Deactivated = true
user.Deleted = true
}
}
_, err = svc.DB.NewUpdate().Model(user).WherePK().Exec(ctx)
if err != nil {
return nil, err