Skip to content

Commit

Permalink
fix(user): allow deleting a user if they have a default project
Browse files Browse the repository at this point in the history
Resolves #78
  • Loading branch information
kolaente committed Aug 23, 2023
1 parent 40037f2 commit acb03c4
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 3 deletions.
9 changes: 9 additions & 0 deletions pkg/db/fixtures/projects.yml
Expand Up @@ -315,3 +315,12 @@
position: 1
updated: 2018-12-02 15:13:12
created: 2018-12-01 15:13:12
-
id: 37
title: Project 37
description: Lorem Ipsum
identifier: test37
owner_id: 16
position: 1
updated: 2018-12-02 15:13:12
created: 2018-12-01 15:13:12
8 changes: 8 additions & 0 deletions pkg/db/fixtures/users.yml
Expand Up @@ -118,3 +118,11 @@
issuer: local
updated: 2018-12-02 15:13:12
created: 2018-12-01 15:13:12
- id: 16
username: 'user16'
password: '$2a$14$dcadBoMBL9jQoOcZK8Fju.cy0Ptx2oZECkKLnaa8ekRoTFe1w7To.' # 1234
email: 'user16@example.com'
issuer: local
default_project_id: 37
updated: 2018-12-02 15:13:12
created: 2018-12-01 15:13:12
13 changes: 12 additions & 1 deletion pkg/models/project.go
Expand Up @@ -960,7 +960,8 @@ func (p *Project) Delete(s *xorm.Session, a web.Auth) (err error) {
if err != nil {
return err
}
if isDefaultProject {
// Owners should be allowed to delete the default project
if isDefaultProject && p.OwnerID != a.GetID() {
return &ErrCannotDeleteDefaultProject{ProjectID: p.ID}
}

Expand Down Expand Up @@ -988,6 +989,16 @@ func (p *Project) Delete(s *xorm.Session, a web.Auth) (err error) {
return
}

// If we're deleting a default project, remove it as default
if isDefaultProject {
_, err = s.Where("default_project_id = ?", p.ID).
Cols("default_project_id").
Update(&user.User{DefaultProjectID: 0})
if err != nil {
return
}
}

// Delete the project
_, err = s.ID(p.ID).Delete(&Project{})
if err != nil {
Expand Down
13 changes: 13 additions & 0 deletions pkg/models/user_delete_test.go
Expand Up @@ -58,4 +58,17 @@ func TestDeleteUser(t *testing.T) {
assert.NoError(t, err)
// No assertions for deleted projects since that user doesn't have any
})
t.Run("user with a default project", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
notifications.Fake()

u := &user.User{ID: 16}
err := DeleteUser(s, u)

assert.NoError(t, err)
db.AssertMissing(t, "users", map[string]interface{}{"id": u.ID})
db.AssertMissing(t, "projects", map[string]interface{}{"id": 37}) // only user16 had access to this project, and it was their default
})
}
4 changes: 2 additions & 2 deletions pkg/user/user_test.go
Expand Up @@ -399,7 +399,7 @@ func TestListUsers(t *testing.T) {

all, err := ListAllUsers(s)
assert.NoError(t, err)
assert.Len(t, all, 15)
assert.Len(t, all, 16)
})
t.Run("no search term", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
Expand Down Expand Up @@ -512,7 +512,7 @@ func TestListUsers(t *testing.T) {
MatchFuzzily: true,
})
assert.NoError(t, err)
assert.Len(t, all, 15)
assert.Len(t, all, 16)
})
}

Expand Down

0 comments on commit acb03c4

Please sign in to comment.