Added extra function to get a list without tasks or user objects

This commit is contained in:
konrad 2018-10-06 13:05:29 +02:00
parent 53a7f2e6a7
commit a108ed689d
No known key found for this signature in database
GPG Key ID: F40E70337AB24C9B
12 changed files with 97 additions and 60 deletions

View File

@ -18,33 +18,6 @@ type List struct {
Rights `xorm:"-" json:"-"` Rights `xorm:"-" json:"-"`
} }
// GetListByID returns a list by its ID
func GetListByID(id int64) (list List, err error) {
if id < 1 {
return list, ErrListDoesNotExist{ID: id}
}
exists, err := x.ID(id).Get(&list)
if err != nil {
return list, err
}
if !exists {
return list, ErrListDoesNotExist{ID: id}
}
// Get list tasks
list.Tasks, err = GetTasksByListID(list.ID)
if err != nil {
return list, err
}
// Get list owner
list.Owner, err = GetUserByID(list.OwnerID)
return list, err
}
// GetListsByNamespaceID gets all lists in a namespace // GetListsByNamespaceID gets all lists in a namespace
func GetListsByNamespaceID(nID int64) (lists []*List, err error) { func GetListsByNamespaceID(nID int64) (lists []*List, err error) {
err = x.Where("namespace_id = ?", nID).Find(&lists) err = x.Where("namespace_id = ?", nID).Find(&lists)
@ -87,7 +60,41 @@ func (l *List) ReadAll(user *User) (interface{}, error) {
// ReadOne gets one list by its ID // ReadOne gets one list by its ID
func (l *List) ReadOne() (err error) { func (l *List) ReadOne() (err error) {
*l, err = GetListByID(l.ID) err = l.GetSimpleByID()
if err != nil {
return err
}
// Get list tasks
l.Tasks, err = GetTasksByListID(l.ID)
if err != nil {
return err
}
// Get list owner
l.Owner, err = GetUserByID(l.OwnerID)
return
}
// GetSimple gets a list with only the basic items, aka no tasks or user objects. Returns an error if the list does not exist.
func (l *List) GetSimpleByID() (err error) {
if l.ID < 1 {
return ErrListDoesNotExist{ID: l.ID}
}
// We need to re-init our list object, because otherwise xorm creates a "where for every item in that list object,
// leading to not finding anything if the id is good, but for example the title is different.
id := l.ID
*l = List{}
exists, err := x.Where("id = ?", id).Get(l)
if err != nil {
return
}
if !exists {
return ErrListDoesNotExist{ID: l.ID}
}
return return
} }

View File

@ -26,8 +26,7 @@ func CreateOrUpdateList(list *List) (err error) {
return return
} }
*list, err = GetListByID(list.ID) err = list.ReadOne()
return return
} }
@ -35,8 +34,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() (err error) { func (l *List) Update() (err error) {
// Check if it exists // Check if it exists
_, err = GetListByID(l.ID) if err = l.GetSimpleByID(); err != nil {
if err != nil {
return return
} }

View File

@ -3,8 +3,7 @@ package models
// Delete implements the delete method of CRUDable // Delete implements the delete method of CRUDable
func (l *List) Delete() (err error) { func (l *List) Delete() (err error) {
// Check if the list exists // Check if the list exists
_, err = GetListByID(l.ID) if err = l.GetSimpleByID(); err != nil {
if err != nil {
return return
} }

View File

@ -14,8 +14,8 @@ func (i *ListTask) Create(doer *User) (err error) {
} }
// Check if the list exists // Check if the list exists
_, err = GetListByID(i.ListID) l := &List{ID: i.ListID}
if err != nil { if err = l.GetSimpleByID(); err != nil {
return return
} }

View File

@ -6,7 +6,8 @@ func (i *ListTask) CanDelete(doer *User) bool {
lI, _ := GetListTaskByID(i.ID) lI, _ := GetListTaskByID(i.ID)
// A user can delete an task if he has write acces to its list // A user can delete an task if he has write acces to its list
list, _ := GetListByID(lI.ListID) list := &List{ID: lI.ListID}
list.ReadOne()
return list.CanWrite(doer) return list.CanWrite(doer)
} }
@ -16,13 +17,15 @@ func (i *ListTask) CanUpdate(doer *User) bool {
lI, _ := GetListTaskByID(i.ID) lI, _ := GetListTaskByID(i.ID)
// A user can update an task if he has write acces to its list // A user can update an task if he has write acces to its list
list, _ := GetListByID(lI.ListID) list := &List{ID: lI.ListID}
list.ReadOne()
return list.CanWrite(doer) return list.CanWrite(doer)
} }
// CanCreate determines if a user has the right to create a list task // CanCreate determines if a user has the right to create a list task
func (i *ListTask) CanCreate(doer *User) bool { func (i *ListTask) CanCreate(doer *User) bool {
// A user can create an task if he has write acces to its list // A user can create an task if he has write acces to its list
list, _ := GetListByID(i.ListID) list := &List{ID: i.ListID}
list.ReadOne()
return list.CanWrite(doer) return list.CanWrite(doer)
} }

View File

@ -47,14 +47,20 @@ func (l *List) CanRead(user *User) bool {
// CanDelete checks if the user can delete a list // CanDelete checks if the user can delete a list
func (l *List) CanDelete(doer *User) bool { func (l *List) CanDelete(doer *User) bool {
list, _ := GetListByID(l.ID) if err := l.GetSimpleByID(); err != nil {
return list.IsAdmin(doer) Log.Error("Error occured during CanDelete for List: %s", err)
return false
}
return l.IsAdmin(doer)
} }
// CanUpdate checks if the user can update a list // CanUpdate checks if the user can update a list
func (l *List) CanUpdate(doer *User) bool { func (l *List) CanUpdate(doer *User) bool {
list, _ := GetListByID(l.ID) if err := l.GetSimpleByID(); err != nil {
return list.CanWrite(doer) Log.Error("Error occured during CanUpdate for List: %s", err)
return false
}
return l.CanWrite(doer)
} }
// CanCreate checks if the user can update a list // CanCreate checks if the user can update a list

View File

@ -9,8 +9,8 @@ func (ul *ListUser) Create(user *User) (err error) {
} }
// Check if the list exists // Check if the list exists
l, err := GetListByID(ul.ListID) l := &List{ID: ul.ListID}
if err != nil { if err = l.GetSimpleByID(); err != nil {
return return
} }

View File

@ -3,8 +3,8 @@ package models
// ReadAll gets all users who have access to a list // ReadAll gets all users who have access to a list
func (ul *ListUser) ReadAll(user *User) (interface{}, error) { func (ul *ListUser) ReadAll(user *User) (interface{}, error) {
// Check if the user has access to the list // Check if the user has access to the list
l, err := GetListByID(ul.ListID) l := &List{ID: ul.ListID}
if err != nil { if err := l.GetSimpleByID(); err != nil {
return nil, err return nil, err
} }
if !l.CanRead(user) { if !l.CanRead(user) {
@ -13,7 +13,7 @@ func (ul *ListUser) ReadAll(user *User) (interface{}, error) {
// Get all users // Get all users
all := []*userWithRight{} all := []*userWithRight{}
err = x. err := x.
Join("INNER", "users_list", "user_id = users.id"). Join("INNER", "users_list", "user_id = users.id").
Where("users_list.list_id = ?", ul.ListID). Where("users_list.list_id = ?", ul.ListID).
Find(&all) Find(&all)

View File

@ -29,20 +29,32 @@ func (r UserRight) isValid() error {
// CanCreate checks if the user can create a new user <-> list relation // CanCreate checks if the user can create a new user <-> list relation
func (lu *ListUser) CanCreate(doer *User) bool { func (lu *ListUser) CanCreate(doer *User) bool {
// Get the list and check if the user has write access on it // Get the list and check if the user has write access on it
l, _ := GetListByID(lu.ListID) l := List{ID: lu.ListID}
if err := l.GetSimpleByID(); err != nil {
Log.Error("Error occured during CanCreate for ListUser: %s", err)
return false
}
return l.CanWrite(doer) return l.CanWrite(doer)
} }
// CanDelete checks if the user can delete a user <-> list relation // CanDelete checks if the user can delete a user <-> list relation
func (lu *ListUser) CanDelete(doer *User) bool { func (lu *ListUser) CanDelete(doer *User) bool {
// Get the list and check if the user has write access on it // Get the list and check if the user has write access on it
l, _ := GetListByID(lu.ListID) l := List{ID: lu.ListID}
if err := l.GetSimpleByID(); err != nil {
Log.Error("Error occured during CanDelete for ListUser: %s", err)
return false
}
return l.CanWrite(doer) return l.CanWrite(doer)
} }
// CanUpdate checks if the user can update a user <-> list relation // CanUpdate checks if the user can update a user <-> list relation
func (lu *ListUser) CanUpdate(doer *User) bool { func (lu *ListUser) CanUpdate(doer *User) bool {
// Get the list and check if the user has write access on it // Get the list and check if the user has write access on it
l, _ := GetListByID(lu.ListID) l := List{ID: lu.ListID}
if err := l.GetSimpleByID(); err != nil {
Log.Error("Error occured during CanUpdate for ListUser: %s", err)
return false
}
return l.CanWrite(doer) return l.CanWrite(doer)
} }

View File

@ -15,9 +15,9 @@ func (tl *TeamList) Create(doer *User) (err error) {
} }
// Check if the list exists // Check if the list exists
_, err = GetListByID(tl.ListID) l := &List{ID: tl.ListID}
if err != nil { if err := l.GetSimpleByID(); err != nil {
return return err
} }
// Check if the team is already on the list // Check if the team is already on the list

View File

@ -3,8 +3,8 @@ package models
// ReadAll implements the method to read all teams of a list // ReadAll implements the method to read all teams of a list
func (tl *TeamList) ReadAll(user *User) (interface{}, error) { func (tl *TeamList) ReadAll(user *User) (interface{}, error) {
// Check if the user can read the namespace // Check if the user can read the namespace
l, err := GetListByID(tl.ListID) l := &List{ID: tl.ListID}
if err != nil { if err := l.GetSimpleByID(); err != nil {
return nil, err return nil, err
} }
if !l.CanRead(user) { if !l.CanRead(user) {
@ -13,7 +13,7 @@ func (tl *TeamList) ReadAll(user *User) (interface{}, error) {
// Get the teams // Get the teams
all := []*teamWithRight{} all := []*teamWithRight{}
err = x. err := x.
Table("teams"). Table("teams").
Join("INNER", "team_list", "team_id = teams.id"). Join("INNER", "team_list", "team_id = teams.id").
Where("team_list.list_id = ?", tl.ListID). Where("team_list.list_id = ?", tl.ListID).

View File

@ -2,18 +2,30 @@ package models
// CanCreate checks if the user can create a team <-> list relation // CanCreate checks if the user can create a team <-> list relation
func (tl *TeamList) CanCreate(user *User) bool { func (tl *TeamList) CanCreate(user *User) bool {
l, _ := GetListByID(tl.ListID) l := List{ID: tl.ListID}
if err := l.GetSimpleByID(); err != nil {
Log.Error("Error occured during CanCreate for TeamList: %s", err)
return false
}
return l.IsAdmin(user) return l.IsAdmin(user)
} }
// CanDelete checks if the user can delete a team <-> list relation // CanDelete checks if the user can delete a team <-> list relation
func (tl *TeamList) CanDelete(user *User) bool { func (tl *TeamList) CanDelete(user *User) bool {
l, _ := GetListByID(tl.ListID) l := List{ID: tl.ListID}
if err := l.GetSimpleByID(); err != nil {
Log.Error("Error occured during CanDelete for TeamList: %s", err)
return false
}
return l.IsAdmin(user) return l.IsAdmin(user)
} }
// CanUpdate checks if the user can update a team <-> list relation // CanUpdate checks if the user can update a team <-> list relation
func (tl *TeamList) CanUpdate(user *User) bool { func (tl *TeamList) CanUpdate(user *User) bool {
l, _ := GetListByID(tl.ListID) l := List{ID: tl.ListID}
if err := l.GetSimpleByID(); err != nil {
Log.Error("Error occured during CanUpdate for TeamList: %s", err)
return false
}
return l.IsAdmin(user) return l.IsAdmin(user)
} }