From 2590ce7e445dd6f431c17853616a47d8369cf179 Mon Sep 17 00:00:00 2001 From: Christoph Kluge Date: Mon, 30 Jan 2023 13:43:12 +0100 Subject: [PATCH] Adapt count tags for managers --- internal/repository/tags.go | 9 ++++++--- internal/routerConfig/routes.go | 11 +++++++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/internal/repository/tags.go b/internal/repository/tags.go index 07c9910..d1179ad 100644 --- a/internal/repository/tags.go +++ b/internal/repository/tags.go @@ -58,7 +58,7 @@ func (r *JobRepository) CreateTag(tagType string, tagName string) (tagId int64, return res.LastInsertId() } -func (r *JobRepository) CountTags(user *string) (tags []schema.Tag, counts map[string]int, err error) { +func (r *JobRepository) CountTags(user *string, project *string) (tags []schema.Tag, counts map[string]int, err error) { tags = make([]schema.Tag, 0, 100) xrows, err := r.DB.Queryx("SELECT * FROM tag") if err != nil { @@ -77,9 +77,12 @@ func (r *JobRepository) CountTags(user *string) (tags []schema.Tag, counts map[s From("tag t"). LeftJoin("jobtag jt ON t.id = jt.tag_id"). GroupBy("t.tag_name") - if user != nil { + + if (user != nil && project == nil) { // USER: Only count own jobs q = q.Where("jt.job_id IN (SELECT id FROM job WHERE job.user = ?)", *user) - } + } else if (user != nil && project != nil) { // MANAGER: Count own jobs plus project's jobs + q = q.Where("jt.job_id IN (SELECT id FROM job WHERE job.user = ? OR job.project = ?)", *user, *project) + } // else: ADMIN: Count all jobs rows, err := q.RunWith(r.stmtCache).Query() if err != nil { diff --git a/internal/routerConfig/routes.go b/internal/routerConfig/routes.go index 5fae877..b842a98 100644 --- a/internal/routerConfig/routes.go +++ b/internal/routerConfig/routes.go @@ -144,12 +144,19 @@ func setupAnalysisRoute(i InfoType, r *http.Request) InfoType { func setupTaglistRoute(i InfoType, r *http.Request) InfoType { var username *string = nil + var project *string = nil + jobRepo := repository.GetJobRepository() - if user := auth.GetUser(r.Context()); user != nil && !user.HasRole(auth.RoleAdmin) { + user := auth.GetUser(r.Context()) + + if (user != nil && user.HasNotRoles([]string{auth.RoleAdmin, auth.RoleManager})) { username = &user.Username + } else if (user != nil && user.HasRole(auth.RoleManager)) { + username = &user.Username + project = &user.Project } - tags, counts, err := jobRepo.CountTags(username) + tags, counts, err := jobRepo.CountTags(username, project) tagMap := make(map[string][]map[string]interface{}) if err != nil { log.Errorf("GetTags failed: %s", err.Error())