1
0

Added methods to revoke a users access to a list

This commit is contained in:
konrad
2018-08-30 18:52:12 +02:00
committed by kolaente
parent b1c3e92f66
commit efaa277751
5 changed files with 87 additions and 1 deletions

View File

@ -530,3 +530,19 @@ func IsErrUserAlreadyHasAccess(err error) bool {
func (err ErrUserAlreadyHasAccess) Error() string {
return fmt.Sprintf("This user already has access to that list. [User ID: %d, List ID: %d]", err.UserID, err.ListID)
}
// ErrUserDoesNotHaveAccessToList represents an error, where the user is not the owner of that List (used i.e. when deleting a List)
type ErrUserDoesNotHaveAccessToList struct {
ListID int64
UserID int64
}
// IsErrUserDoesNotHaveAccessToList checks if an error is a ErrListDoesNotExist.
func IsErrUserDoesNotHaveAccessToList(err error) bool {
_, ok := err.(ErrUserDoesNotHaveAccessToList)
return ok
}
func (err ErrUserDoesNotHaveAccessToList) Error() string {
return fmt.Sprintf("You need to have access to this List to do that [ListID: %d, UserID: %d]", err.ListID, err.UserID)
}

View File

@ -0,0 +1,25 @@
package models
// Delete deletes a list <-> user relation
func (lu *ListUser) Delete() (err error) {
// Check if the user exists
_, _, err = GetUserByID(lu.UserID)
if err != nil {
return
}
// Check if the user has access to the list
has, err := x.Where("user_id = ? AND list_id = ?", lu.UserID, lu.ListID).
Get(&ListUser{})
if err != nil {
return
}
if !has {
return ErrUserDoesNotHaveAccessToList{ListID: lu.ListID, UserID: lu.UserID}
}
_, err = x.Where("user_id = ? AND list_id = ?", lu.UserID, lu.ListID).
Delete(&ListUser{})
return
}

View File

@ -32,3 +32,10 @@ func (lu *ListUser) CanCreate(doer *User) bool {
l, _ := GetListByID(lu.ListID)
return l.CanWrite(doer)
}
// CanDelete checks if the user can delete a user <-> list relation
func (lu *ListUser) CanDelete(doer *User) bool {
// Get the list and check if the user has write access on it
l, _ := GetListByID(lu.ListID)
return l.CanWrite(doer)
}