Adapt count tags for managers

This commit is contained in:
Christoph Kluge 2023-01-30 13:43:12 +01:00
parent b2aed2f16b
commit 2590ce7e44
2 changed files with 15 additions and 5 deletions

View File

@ -58,7 +58,7 @@ func (r *JobRepository) CreateTag(tagType string, tagName string) (tagId int64,
return res.LastInsertId() 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) tags = make([]schema.Tag, 0, 100)
xrows, err := r.DB.Queryx("SELECT * FROM tag") xrows, err := r.DB.Queryx("SELECT * FROM tag")
if err != nil { if err != nil {
@ -77,9 +77,12 @@ func (r *JobRepository) CountTags(user *string) (tags []schema.Tag, counts map[s
From("tag t"). From("tag t").
LeftJoin("jobtag jt ON t.id = jt.tag_id"). LeftJoin("jobtag jt ON t.id = jt.tag_id").
GroupBy("t.tag_name") 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) 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() rows, err := q.RunWith(r.stmtCache).Query()
if err != nil { if err != nil {

View File

@ -144,12 +144,19 @@ func setupAnalysisRoute(i InfoType, r *http.Request) InfoType {
func setupTaglistRoute(i InfoType, r *http.Request) InfoType { func setupTaglistRoute(i InfoType, r *http.Request) InfoType {
var username *string = nil var username *string = nil
var project *string = nil
jobRepo := repository.GetJobRepository() 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 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{}) tagMap := make(map[string][]map[string]interface{})
if err != nil { if err != nil {
log.Errorf("GetTags failed: %s", err.Error()) log.Errorf("GetTags failed: %s", err.Error())