From 17fefae8bb0f1550de0a152683380292dac52b3b Mon Sep 17 00:00:00 2001 From: konrad Date: Mon, 14 Jan 2019 21:49:50 +0000 Subject: [PATCH] Simplify list rights check (#50) --- Featurecreep.md | 2 +- pkg/models/list_task_readall.go | 6 +-- pkg/models/list_tasks_rights.go | 66 +++++++++++++++++---------------- 3 files changed, 38 insertions(+), 36 deletions(-) diff --git a/Featurecreep.md b/Featurecreep.md index 72c29eb4..c6f998ff 100644 --- a/Featurecreep.md +++ b/Featurecreep.md @@ -156,7 +156,7 @@ Sorry for some of them being in German, I'll tranlate them at some point. ### Refactor -* [ ] ListTaskRights, sollte überall gleich funktionieren, gibt ja mittlerweile auch eine Methode um liste von nem Task aus zu kriegen oder so +* [x] ListTaskRights, sollte überall gleich funktionieren, gibt ja mittlerweile auch eine Methode um liste von nem Task aus zu kriegen oder so ### Linters diff --git a/pkg/models/list_task_readall.go b/pkg/models/list_task_readall.go index d15b61d4..5e5bc0aa 100644 --- a/pkg/models/list_task_readall.go +++ b/pkg/models/list_task_readall.go @@ -71,14 +71,14 @@ func dummy2() { // @Success 200 {array} models.List "The tasks" // @Failure 500 {object} models.Message "Internal error" // @Router /tasks/all [get] -func (lt *ListTask) ReadAll(search string, a web.Auth, page int) (interface{}, error) { +func (t *ListTask) ReadAll(search string, a web.Auth, page int) (interface{}, error) { u, err := getUserWithError(a) if err != nil { return nil, err } var sortby SortBy - switch lt.Sorting { + switch t.Sorting { case "priority": sortby = SortTasksByPriorityDesc case "prioritydesc": @@ -95,7 +95,7 @@ func (lt *ListTask) ReadAll(search string, a web.Auth, page int) (interface{}, e sortby = SortTasksByUnsorted } - return GetTasksByUser(search, u, page, sortby, time.Unix(lt.StartDateSortUnix, 0), time.Unix(lt.EndDateSortUnix, 0)) + return GetTasksByUser(search, u, page, sortby, time.Unix(t.StartDateSortUnix, 0), time.Unix(t.EndDateSortUnix, 0)) } //GetTasksByUser returns all tasks for a user diff --git a/pkg/models/list_tasks_rights.go b/pkg/models/list_tasks_rights.go index c9b60617..15c83f36 100644 --- a/pkg/models/list_tasks_rights.go +++ b/pkg/models/list_tasks_rights.go @@ -23,49 +23,25 @@ import ( // CanDelete checks if the user can delete an task func (t *ListTask) CanDelete(a web.Auth) bool { - doer := getUserForRights(a) - - // Get the task - lI, err := GetListTaskByID(t.ID) - if err != nil { - log.Log.Error("Error occurred during CanDelete for ListTask: %s", err) - return false - } - - // A user can delete an task if he has write acces to its list - l := &List{ID: lI.ListID} - l.ReadOne() - return l.CanWrite(doer) + return t.canDoListTask(a) } // CanUpdate determines if a user has the right to update a list task func (t *ListTask) CanUpdate(a web.Auth) bool { - doer := getUserForRights(a) - - // Get the task - lI, err := getTaskByIDSimple(t.ID) - if err != nil { - log.Log.Error("Error occurred during CanUpdate (getTaskByIDSimple) for ListTask: %s", err) - return false - } - - // A user can update an task if he has write acces to its list - l := &List{ID: lI.ListID} - err = l.GetSimpleByID() - if err != nil { - log.Log.Error("Error occurred during CanUpdate (ReadOne) for ListTask: %s", err) - return false - } - return l.CanWrite(doer) + return t.canDoListTask(a) } // CanCreate determines if a user has the right to create a list task func (t *ListTask) CanCreate(a web.Auth) bool { doer := getUserForRights(a) - // A user can create an task if he has write acces to its list + // A user can do a task if he has write acces to its list l := &List{ID: t.ListID} - l.ReadOne() + err := l.GetSimpleByID() + if err != nil { + log.Log.Error("Error occurred during CanDelete for ListTask: %s", err) + return false + } return l.CanWrite(doer) } @@ -73,5 +49,31 @@ func (t *ListTask) CanCreate(a web.Auth) bool { func (t *ListTask) CanRead(a web.Auth) bool { // A user can read a task if it has access to the list list := &List{ID: t.ListID} + err := list.GetSimpleByID() + if err != nil { + log.Log.Error("Error occurred during CanRead for ListTask: %s", err) + return false + } return list.CanRead(a) } + +// Helper function to check if a user can do stuff on a list task +func (t *ListTask) canDoListTask(a web.Auth) bool { + doer := getUserForRights(a) + + // Get the task + lI, err := getTaskByIDSimple(t.ID) + if err != nil { + log.Log.Error("Error occurred during canDoListTask (getTaskByIDSimple) for ListTask: %s", err) + return false + } + + // A user can do a task if he has write acces to its list + l := &List{ID: lI.ListID} + err = l.GetSimpleByID() + if err != nil { + log.Log.Error("Error occurred during CanDelete for ListTask: %s", err) + return false + } + return l.CanWrite(doer) +}