diff --git a/models/crudable.go b/models/crudable.go index 6a7b8fea7..0480e90ed 100644 --- a/models/crudable.go +++ b/models/crudable.go @@ -5,6 +5,6 @@ type CRUDable interface { Create(*User) error ReadOne() error ReadAll(*User) (interface{}, error) - Update(int64) error + Update() error Delete() error } diff --git a/models/list_create_update.go b/models/list_create_update.go index c23896833..ef5dfadad 100644 --- a/models/list_create_update.go +++ b/models/list_create_update.go @@ -20,9 +20,7 @@ func CreateOrUpdateList(list *List) (err error) { } // Update implements the update method of CRUDable -func (l *List) Update(id int64) (err error) { - l.ID = id - +func (l *List) Update() (err error) { // Check if it exists _, err = GetListByID(l.ID) if err != nil { diff --git a/models/list_items.go b/models/list_items.go index 83f6fe698..6d200c6d6 100644 --- a/models/list_items.go +++ b/models/list_items.go @@ -2,7 +2,7 @@ package models // ListItem represents an item in a todolist type ListItem struct { - ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id" param:"listitemid"` + ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id" param:"listitem"` Text string `xorm:"varchar(250)" json:"text"` Description string `xorm:"varchar(250)" json:"description"` Done bool `json:"done"` diff --git a/models/list_items_create_update.go b/models/list_items_create_update.go index 3bb4def13..b91849396 100644 --- a/models/list_items_create_update.go +++ b/models/list_items_create_update.go @@ -9,11 +9,9 @@ func (i *ListItem) Create(doer *User) (err error) { } // Update updates a list item -func (i *ListItem) Update(ID int64) (err error) { - i.ID = ID - +func (i *ListItem) Update() (err error) { // Check if the item exists - _, err = GetListItemByID(ID) + _, err = GetListItemByID(i.ID) if err != nil { return } diff --git a/models/list_items_rights.go b/models/list_items_rights.go index 98fdf0606..1ca08b7c2 100644 --- a/models/list_items_rights.go +++ b/models/list_items_rights.go @@ -11,9 +11,9 @@ func (i *ListItem) CanDelete(doer *User) bool { } // CanUpdate determines if a user has the right to update a list item -func (i *ListItem) CanUpdate(doer *User, id int64) bool { +func (i *ListItem) CanUpdate(doer *User) bool { // Get the item - lI, _ := GetListItemByID(id) + lI, _ := GetListItemByID(i.ID) // A user can update an item if he has write acces to its list list, _ := GetListByID(lI.ListID) diff --git a/models/list_rights.go b/models/list_rights.go index 86510a732..d61b995b4 100644 --- a/models/list_rights.go +++ b/models/list_rights.go @@ -87,8 +87,8 @@ func (l *List) CanDelete(doer *User) bool { } // CanUpdate checks if the user can update a list -func (l *List) CanUpdate(doer *User, id int64) bool { - list, _ := GetListByID(id) +func (l *List) CanUpdate(doer *User) bool { + list, _ := GetListByID(l.ID) return list.CanWrite(doer) } diff --git a/models/namespace_rights.go b/models/namespace_rights.go index 3eb067996..1cdcf070d 100644 --- a/models/namespace_rights.go +++ b/models/namespace_rights.go @@ -71,8 +71,8 @@ func (n *Namespace) CanRead(user *User) bool { } // CanUpdate checks if the user can update the namespace -func (n *Namespace) CanUpdate(user *User, id int64) bool { - nn, _ := GetNamespaceByID(id) +func (n *Namespace) CanUpdate(user *User) bool { + nn, _ := GetNamespaceByID(n.ID) return nn.IsAdmin(user) } diff --git a/models/namespace_update.go b/models/namespace_update.go index a5eaa2e0e..2216bc411 100644 --- a/models/namespace_update.go +++ b/models/namespace_update.go @@ -1,15 +1,14 @@ package models // Update implements the update method via the interface -func (n *Namespace) Update(id int64) (err error) { +func (n *Namespace) Update() (err error) { // Check if we have at least a name if n.Name == "" { - return ErrNamespaceNameCannotBeEmpty{NamespaceID: id} + return ErrNamespaceNameCannotBeEmpty{NamespaceID: n.ID} } - n.ID = id // Check if the namespace exists - currentNamespace, err := GetNamespaceByID(id) + currentNamespace, err := GetNamespaceByID(n.ID) if err != nil { return } diff --git a/models/rights.go b/models/rights.go index 8f47948b7..9d5ce9914 100644 --- a/models/rights.go +++ b/models/rights.go @@ -6,6 +6,6 @@ type Rights interface { CanWrite(*User) bool CanRead(*User) bool CanDelete(*User) bool - CanUpdate(*User, int64) bool + CanUpdate(*User) bool CanCreate(*User) bool } diff --git a/models/teams_rights.go b/models/teams_rights.go index 4ca19780f..1d6457871 100644 --- a/models/teams_rights.go +++ b/models/teams_rights.go @@ -7,10 +7,10 @@ func (t *Team) CanCreate(user *User) bool { } // CanUpdate checks if the user can update a team -func (t *Team) CanUpdate(user *User, id int64) bool { +func (t *Team) CanUpdate(user *User) bool { // Check if the current user is in the team and has admin rights in it - exists, _ := x.Where("team_id = ?", id). + exists, _ := x.Where("team_id = ?", t.ID). And("user_id = ?", user.ID). And("is_admin = ?", true). Get(&TeamMember{}) diff --git a/models/teams_update.go b/models/teams_update.go index 9c9d29460..bcbb44228 100644 --- a/models/teams_update.go +++ b/models/teams_update.go @@ -1,25 +1,25 @@ package models // Update is the handler to create a team -func (t *Team) Update(id int64) (err error) { +func (t *Team) Update() (err error) { // Check if we have a name if t.Name == "" { return ErrTeamNameCannotBeEmpty{} } // Check if the team exists - _, err = GetTeamByID(id) + _, err = GetTeamByID(t.ID) if err != nil { return } - _, err = x.ID(id).Update(t) + _, err = x.ID(t.ID).Update(t) if err != nil { return } // Get the newly updated team - *t, err = GetTeamByID(id) + *t, err = GetTeamByID(t.ID) return } diff --git a/routes/crud/update.go b/routes/crud/update.go index 59124fbb8..6ea065105 100644 --- a/routes/crud/update.go +++ b/routes/crud/update.go @@ -13,28 +13,22 @@ func (c *WebHandler) UpdateWeb(ctx echo.Context) error { p := reflect.ValueOf(c.CObject).Elem() p.Set(reflect.Zero(p.Type())) - // Get the object - if err := ctx.Bind(&c.CObject); err != nil { + // Get the object & bind params to struct + if err := ParamBinder(c.CObject, ctx); err != nil { return echo.NewHTTPError(http.StatusBadRequest, "No or invalid model provided.") } - // Get the ID - id, err := models.GetIntURLParam("id", ctx) - if err != nil { - return echo.NewHTTPError(http.StatusBadRequest, "Invalid ID.") - } - // Check if the user has the right to do that currentUser, err := models.GetCurrentUser(ctx) if err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Could not determine the current user.") } - if !c.CObject.CanUpdate(¤tUser, id) { + if !c.CObject.CanUpdate(¤tUser) { return echo.NewHTTPError(http.StatusForbidden) } // Do the update - err = c.CObject.Update(id) + err = c.CObject.Update() if err != nil { if models.IsErrNeedToBeListAdmin(err) { return echo.NewHTTPError(http.StatusForbidden, "You need to be list admin to do that.")