Fixed check if the user really exists before updating/deleting its rights (#77)

This commit is contained in:
konrad 2019-05-25 10:16:55 +00:00 committed by Gitea
parent 4c4eb76fed
commit 85d08e5697
5 changed files with 35 additions and 21 deletions

View file

@ -217,7 +217,7 @@ Sorry for some of them being in German, I'll tranlate them at some point.
* [x] Reminders should use an extra table so we can make reverse lookups aka "give me all tasks with reminders in this period" which we'll need for things like email reminders notifications * [x] Reminders should use an extra table so we can make reverse lookups aka "give me all tasks with reminders in this period" which we'll need for things like email reminders notifications
* [x] When giving a user access to a list/namespace, they should be reffered to by uuid, not numeric id * [x] When giving a user access to a list/namespace, they should be reffered to by uuid, not numeric id
* [x] Adding users to a team should also use uuid * [x] Adding users to a team should also use uuid
* [ ] Check if the team/user really exist before updating them on lists/namespaces * [x] Check if the team/user really exist before updating them on lists/namespaces
### Linters ### Linters

View file

@ -218,7 +218,7 @@ func TestListUser_ReadAll(t *testing.T) {
func TestListUser_Update(t *testing.T) { func TestListUser_Update(t *testing.T) {
type fields struct { type fields struct {
ID int64 ID int64
UserID int64 Username string
ListID int64 ListID int64
Right Right Right Right
Created int64 Created int64
@ -235,33 +235,33 @@ func TestListUser_Update(t *testing.T) {
{ {
name: "Test Update Normally", name: "Test Update Normally",
fields: fields{ fields: fields{
ListID: 3, ListID: 3,
UserID: 1, Username: "user1",
Right: RightAdmin, Right: RightAdmin,
}, },
}, },
{ {
name: "Test Update to write", name: "Test Update to write",
fields: fields{ fields: fields{
ListID: 3, ListID: 3,
UserID: 1, Username: "user1",
Right: RightWrite, Right: RightWrite,
}, },
}, },
{ {
name: "Test Update to Read", name: "Test Update to Read",
fields: fields{ fields: fields{
ListID: 3, ListID: 3,
UserID: 1, Username: "user1",
Right: RightRead, Right: RightRead,
}, },
}, },
{ {
name: "Test Update with invalid right", name: "Test Update with invalid right",
fields: fields{ fields: fields{
ListID: 3, ListID: 3,
UserID: 1, Username: "user1",
Right: 500, Right: 500,
}, },
wantErr: true, wantErr: true,
errType: IsErrInvalidRight, errType: IsErrInvalidRight,
@ -271,7 +271,7 @@ func TestListUser_Update(t *testing.T) {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
lu := &ListUser{ lu := &ListUser{
ID: tt.fields.ID, ID: tt.fields.ID,
UserID: tt.fields.UserID, Username: tt.fields.Username,
ListID: tt.fields.ListID, ListID: tt.fields.ListID,
Right: tt.fields.Right, Right: tt.fields.Right,
Created: tt.fields.Created, Created: tt.fields.Created,

View file

@ -40,6 +40,13 @@ func (lu *ListUser) Update() (err error) {
return err return err
} }
// Check if the user exists
user, err := GetUserByUsername(lu.Username)
if err != nil {
return err
}
lu.UserID = user.ID
_, err = x. _, err = x.
Where("list_id = ? AND user_id = ?", lu.ListID, lu.UserID). Where("list_id = ? AND user_id = ?", lu.ListID, lu.UserID).
Cols("right"). Cols("right").

View file

@ -218,7 +218,7 @@ func TestNamespaceUser_ReadAll(t *testing.T) {
func TestNamespaceUser_Update(t *testing.T) { func TestNamespaceUser_Update(t *testing.T) {
type fields struct { type fields struct {
ID int64 ID int64
UserID int64 Username string
NamespaceID int64 NamespaceID int64
Right Right Right Right
Created int64 Created int64
@ -236,7 +236,7 @@ func TestNamespaceUser_Update(t *testing.T) {
name: "Test Update Normally", name: "Test Update Normally",
fields: fields{ fields: fields{
NamespaceID: 3, NamespaceID: 3,
UserID: 1, Username: "user1",
Right: RightAdmin, Right: RightAdmin,
}, },
}, },
@ -244,7 +244,7 @@ func TestNamespaceUser_Update(t *testing.T) {
name: "Test Update to write", name: "Test Update to write",
fields: fields{ fields: fields{
NamespaceID: 3, NamespaceID: 3,
UserID: 1, Username: "user1",
Right: RightWrite, Right: RightWrite,
}, },
}, },
@ -252,7 +252,7 @@ func TestNamespaceUser_Update(t *testing.T) {
name: "Test Update to Read", name: "Test Update to Read",
fields: fields{ fields: fields{
NamespaceID: 3, NamespaceID: 3,
UserID: 1, Username: "user1",
Right: RightRead, Right: RightRead,
}, },
}, },
@ -260,7 +260,7 @@ func TestNamespaceUser_Update(t *testing.T) {
name: "Test Update with invalid right", name: "Test Update with invalid right",
fields: fields{ fields: fields{
NamespaceID: 3, NamespaceID: 3,
UserID: 1, Username: "user1",
Right: 500, Right: 500,
}, },
wantErr: true, wantErr: true,
@ -271,7 +271,7 @@ func TestNamespaceUser_Update(t *testing.T) {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
nu := &NamespaceUser{ nu := &NamespaceUser{
ID: tt.fields.ID, ID: tt.fields.ID,
UserID: tt.fields.UserID, Username: tt.fields.Username,
NamespaceID: tt.fields.NamespaceID, NamespaceID: tt.fields.NamespaceID,
Right: tt.fields.Right, Right: tt.fields.Right,
Created: tt.fields.Created, Created: tt.fields.Created,

View file

@ -40,6 +40,13 @@ func (nu *NamespaceUser) Update() (err error) {
return err return err
} }
// Check if the user exists
user, err := GetUserByUsername(nu.Username)
if err != nil {
return err
}
nu.UserID = user.ID
_, err = x. _, err = x.
Where("namespace_id = ? AND user_id = ?", nu.NamespaceID, nu.UserID). Where("namespace_id = ? AND user_id = ?", nu.NamespaceID, nu.UserID).
Cols("right"). Cols("right").