fix(user): allow deleting a user if they have a default project
continuous-integration/drone/push Build is failing Details

Resolves https://github.com/go-vikunja/api/issues/78
This commit is contained in:
kolaente 2023-08-23 16:10:51 +02:00
parent 40037f25f2
commit acb03c430e
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
5 changed files with 44 additions and 3 deletions

View File

@ -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

View File

@ -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

View File

@ -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}
}
@ -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 {

View File

@ -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
})
}

View File

@ -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)
@ -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)
})
}