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:"-"`
}
// 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
func GetListsByNamespaceID(nID int64) (lists []*List, err error) {
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
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
}

View file

@ -26,8 +26,7 @@ func CreateOrUpdateList(list *List) (err error) {
return
}
*list, err = GetListByID(list.ID)
err = list.ReadOne()
return
}
@ -35,8 +34,7 @@ func CreateOrUpdateList(list *List) (err error) {
// Update implements the update method of CRUDable
func (l *List) Update() (err error) {
// Check if it exists
_, err = GetListByID(l.ID)
if err != nil {
if err = l.GetSimpleByID(); err != nil {
return
}

View file

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

View file

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

View file

@ -6,7 +6,8 @@ func (i *ListTask) CanDelete(doer *User) bool {
lI, _ := GetListTaskByID(i.ID)
// 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)
}
@ -16,13 +17,15 @@ func (i *ListTask) CanUpdate(doer *User) bool {
lI, _ := GetListTaskByID(i.ID)
// 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)
}
// CanCreate determines if a user has the right to create a list task
func (i *ListTask) CanCreate(doer *User) bool {
// 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)
}

View file

@ -47,14 +47,20 @@ func (l *List) CanRead(user *User) bool {
// CanDelete checks if the user can delete a list
func (l *List) CanDelete(doer *User) bool {
list, _ := GetListByID(l.ID)
return list.IsAdmin(doer)
if err := l.GetSimpleByID(); err != nil {
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
func (l *List) CanUpdate(doer *User) bool {
list, _ := GetListByID(l.ID)
return list.CanWrite(doer)
if err := l.GetSimpleByID(); err != nil {
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

View file

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

View file

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

View file

@ -15,9 +15,9 @@ func (tl *TeamList) Create(doer *User) (err error) {
}
// Check if the list exists
_, err = GetListByID(tl.ListID)
if err != nil {
return
l := &List{ID: tl.ListID}
if err := l.GetSimpleByID(); err != nil {
return err
}
// 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
func (tl *TeamList) ReadAll(user *User) (interface{}, error) {
// Check if the user can read the namespace
l, err := GetListByID(tl.ListID)
if err != nil {
l := &List{ID: tl.ListID}
if err := l.GetSimpleByID(); err != nil {
return nil, err
}
if !l.CanRead(user) {
@ -13,7 +13,7 @@ func (tl *TeamList) ReadAll(user *User) (interface{}, error) {
// Get the teams
all := []*teamWithRight{}
err = x.
err := x.
Table("teams").
Join("INNER", "team_list", "team_id = teams.id").
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
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)
}
// CanDelete checks if the user can delete a team <-> list relation
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)
}
// CanUpdate checks if the user can update a team <-> list relation
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)
}