Allow users to be readded if they were ever removed (#1945)

* Allow users to be readded if they were ever removed

* Address review comment

* lint
This commit is contained in:
Tomas Touceda 2021-09-07 13:33:40 -03:00 committed by GitHub
parent a64023a5af
commit a6acb1cd1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 20 additions and 3 deletions

View File

@ -0,0 +1 @@
* Allow host users to be readded.

View File

@ -1,4 +1,5 @@
//+build !windows
//go:build !windows
// +build !windows
package constant

View File

@ -1,4 +1,5 @@
//+build !windows
//go:build !windows
// +build !windows
package platform

View File

@ -1,3 +1,4 @@
//go:build !windows
// +build !windows
package update

View File

@ -1,3 +1,4 @@
//go:build !full
// +build !full
package bindata

View File

@ -863,7 +863,7 @@ func (d *Datastore) SaveHostUsers(host *fleet.Host) error {
insertValues := strings.TrimSuffix(strings.Repeat("(?, ?, ?, ?, ?),", len(host.Users)), ",")
insertSql := fmt.Sprintf(
`INSERT IGNORE INTO host_users (host_id, uid, username, user_type, groupname) VALUES %s`,
`INSERT INTO host_users (host_id, uid, username, user_type, groupname) VALUES %s ON DUPLICATE KEY UPDATE removed_at=NULL`,
insertValues,
)
if _, err := d.writer.Exec(insertSql, insertArgs...); err != nil {

View File

@ -1340,6 +1340,18 @@ func TestSaveUsers(t *testing.T) {
require.Nil(t, err)
require.Len(t, host.Users, 1)
assert.Equal(t, host.Users[0].Uid, u2.Uid)
// readd u1
host.Users = []fleet.HostUser{u1, u2}
host.Modified = true
err = ds.SaveHost(host)
require.Nil(t, err)
host, err = ds.Host(host.ID)
require.Nil(t, err)
require.Len(t, host.Users, 2)
test.ElementsMatchSkipID(t, host.Users, []fleet.HostUser{u1, u2})
}
func TestSaveUsersWithoutUid(t *testing.T) {