implemented update method via param binder
This commit is contained in:
parent
9e75e9b73b
commit
9979b7e321
12 changed files with 25 additions and 36 deletions
|
@ -5,6 +5,6 @@ type CRUDable interface {
|
||||||
Create(*User) error
|
Create(*User) error
|
||||||
ReadOne() error
|
ReadOne() error
|
||||||
ReadAll(*User) (interface{}, error)
|
ReadAll(*User) (interface{}, error)
|
||||||
Update(int64) error
|
Update() error
|
||||||
Delete() error
|
Delete() error
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,9 +20,7 @@ func CreateOrUpdateList(list *List) (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update implements the update method of CRUDable
|
// Update implements the update method of CRUDable
|
||||||
func (l *List) Update(id int64) (err error) {
|
func (l *List) Update() (err error) {
|
||||||
l.ID = id
|
|
||||||
|
|
||||||
// Check if it exists
|
// Check if it exists
|
||||||
_, err = GetListByID(l.ID)
|
_, err = GetListByID(l.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -2,7 +2,7 @@ package models
|
||||||
|
|
||||||
// ListItem represents an item in a todolist
|
// ListItem represents an item in a todolist
|
||||||
type ListItem struct {
|
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"`
|
Text string `xorm:"varchar(250)" json:"text"`
|
||||||
Description string `xorm:"varchar(250)" json:"description"`
|
Description string `xorm:"varchar(250)" json:"description"`
|
||||||
Done bool `json:"done"`
|
Done bool `json:"done"`
|
||||||
|
|
|
@ -9,11 +9,9 @@ func (i *ListItem) Create(doer *User) (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update updates a list item
|
// Update updates a list item
|
||||||
func (i *ListItem) Update(ID int64) (err error) {
|
func (i *ListItem) Update() (err error) {
|
||||||
i.ID = ID
|
|
||||||
|
|
||||||
// Check if the item exists
|
// Check if the item exists
|
||||||
_, err = GetListItemByID(ID)
|
_, err = GetListItemByID(i.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,9 +11,9 @@ func (i *ListItem) CanDelete(doer *User) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
// CanUpdate determines if a user has the right to update a list item
|
// 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
|
// Get the item
|
||||||
lI, _ := GetListItemByID(id)
|
lI, _ := GetListItemByID(i.ID)
|
||||||
|
|
||||||
// A user can update an item if he has write acces to its list
|
// A user can update an item if he has write acces to its list
|
||||||
list, _ := GetListByID(lI.ListID)
|
list, _ := GetListByID(lI.ListID)
|
||||||
|
|
|
@ -87,8 +87,8 @@ func (l *List) CanDelete(doer *User) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
// CanUpdate checks if the user can update a list
|
// CanUpdate checks if the user can update a list
|
||||||
func (l *List) CanUpdate(doer *User, id int64) bool {
|
func (l *List) CanUpdate(doer *User) bool {
|
||||||
list, _ := GetListByID(id)
|
list, _ := GetListByID(l.ID)
|
||||||
return list.CanWrite(doer)
|
return list.CanWrite(doer)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -71,8 +71,8 @@ func (n *Namespace) CanRead(user *User) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
// CanUpdate checks if the user can update the namespace
|
// CanUpdate checks if the user can update the namespace
|
||||||
func (n *Namespace) CanUpdate(user *User, id int64) bool {
|
func (n *Namespace) CanUpdate(user *User) bool {
|
||||||
nn, _ := GetNamespaceByID(id)
|
nn, _ := GetNamespaceByID(n.ID)
|
||||||
return nn.IsAdmin(user)
|
return nn.IsAdmin(user)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,15 +1,14 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
// Update implements the update method via the interface
|
// 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
|
// Check if we have at least a name
|
||||||
if n.Name == "" {
|
if n.Name == "" {
|
||||||
return ErrNamespaceNameCannotBeEmpty{NamespaceID: id}
|
return ErrNamespaceNameCannotBeEmpty{NamespaceID: n.ID}
|
||||||
}
|
}
|
||||||
n.ID = id
|
|
||||||
|
|
||||||
// Check if the namespace exists
|
// Check if the namespace exists
|
||||||
currentNamespace, err := GetNamespaceByID(id)
|
currentNamespace, err := GetNamespaceByID(n.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,6 @@ type Rights interface {
|
||||||
CanWrite(*User) bool
|
CanWrite(*User) bool
|
||||||
CanRead(*User) bool
|
CanRead(*User) bool
|
||||||
CanDelete(*User) bool
|
CanDelete(*User) bool
|
||||||
CanUpdate(*User, int64) bool
|
CanUpdate(*User) bool
|
||||||
CanCreate(*User) bool
|
CanCreate(*User) bool
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,10 +7,10 @@ func (t *Team) CanCreate(user *User) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
// CanUpdate checks if the user can update a team
|
// 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
|
// 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("user_id = ?", user.ID).
|
||||||
And("is_admin = ?", true).
|
And("is_admin = ?", true).
|
||||||
Get(&TeamMember{})
|
Get(&TeamMember{})
|
||||||
|
|
|
@ -1,25 +1,25 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
// Update is the handler to create a team
|
// 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
|
// Check if we have a name
|
||||||
if t.Name == "" {
|
if t.Name == "" {
|
||||||
return ErrTeamNameCannotBeEmpty{}
|
return ErrTeamNameCannotBeEmpty{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if the team exists
|
// Check if the team exists
|
||||||
_, err = GetTeamByID(id)
|
_, err = GetTeamByID(t.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = x.ID(id).Update(t)
|
_, err = x.ID(t.ID).Update(t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the newly updated team
|
// Get the newly updated team
|
||||||
*t, err = GetTeamByID(id)
|
*t, err = GetTeamByID(t.ID)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,28 +13,22 @@ func (c *WebHandler) UpdateWeb(ctx echo.Context) error {
|
||||||
p := reflect.ValueOf(c.CObject).Elem()
|
p := reflect.ValueOf(c.CObject).Elem()
|
||||||
p.Set(reflect.Zero(p.Type()))
|
p.Set(reflect.Zero(p.Type()))
|
||||||
|
|
||||||
// Get the object
|
// Get the object & bind params to struct
|
||||||
if err := ctx.Bind(&c.CObject); err != nil {
|
if err := ParamBinder(c.CObject, ctx); err != nil {
|
||||||
return echo.NewHTTPError(http.StatusBadRequest, "No or invalid model provided.")
|
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
|
// Check if the user has the right to do that
|
||||||
currentUser, err := models.GetCurrentUser(ctx)
|
currentUser, err := models.GetCurrentUser(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return echo.NewHTTPError(http.StatusInternalServerError, "Could not determine the current user.")
|
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)
|
return echo.NewHTTPError(http.StatusForbidden)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Do the update
|
// Do the update
|
||||||
err = c.CObject.Update(id)
|
err = c.CObject.Update()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if models.IsErrNeedToBeListAdmin(err) {
|
if models.IsErrNeedToBeListAdmin(err) {
|
||||||
return echo.NewHTTPError(http.StatusForbidden, "You need to be list admin to do that.")
|
return echo.NewHTTPError(http.StatusForbidden, "You need to be list admin to do that.")
|
||||||
|
|
Loading…
Reference in a new issue