From d1c0fac13518ab814dfaec51f9944468760bf14b Mon Sep 17 00:00:00 2001 From: Rowan Seymour Date: Thu, 16 Jun 2022 13:19:02 -0500 Subject: [PATCH] Use new orgmembership table to load users --- core/models/users.go | 34 ++++++++-------------------------- 1 file changed, 8 insertions(+), 26 deletions(-) diff --git a/core/models/users.go b/core/models/users.go index 121e50e2e..8eb41f2f7 100644 --- a/core/models/users.go +++ b/core/models/users.go @@ -60,7 +60,7 @@ type User struct { Email string `json:"email"` FirstName string `json:"first_name"` LastName string `json:"last_name"` - Role UserRole `json:"role"` + Role UserRole `json:"role_code"` Team *Team `json:"team"` } } @@ -94,32 +94,14 @@ func (u *User) Team() *Team { var _ assets.User = (*User)(nil) const selectOrgUsersSQL = ` -SELECT ROW_TO_JSON(r) FROM (SELECT - u.id AS "id", - u.email AS "email", - u.first_name as "first_name", - u.last_name as "last_name", - o.role AS "role", - row_to_json(team_struct) AS team -FROM - auth_user u -INNER JOIN ( - SELECT user_id, 'A' AS "role" FROM orgs_org_administrators WHERE org_id = $1 - UNION - SELECT user_id, 'E' AS "role" FROM orgs_org_editors WHERE org_id = $1 - UNION - SELECT user_id, 'V' AS "role" FROM orgs_org_viewers WHERE org_id = $1 - UNION - SELECT user_id, 'T' AS "role" FROM orgs_org_agents WHERE org_id = $1 - UNION - SELECT user_id, 'S' AS "role" FROM orgs_org_surveyors WHERE org_id = $1 -) o ON o.user_id = u.id -LEFT JOIN orgs_usersettings s ON s.user_id = u.id +SELECT ROW_TO_JSON(r) FROM ( + SELECT u.id, u.email, u.first_name, u.last_name, m.role_code, row_to_json(team_struct) AS team + FROM orgs_orgmembership m + INNER JOIN auth_user u ON u.id = m.user_id + LEFT JOIN orgs_usersettings s ON s.user_id = u.id LEFT JOIN LATERAL (SELECT id, uuid, name FROM tickets_team WHERE tickets_team.id = s.team_id) AS team_struct ON True -WHERE - u.is_active = TRUE -ORDER BY - u.email ASC + WHERE m.org_id = $1 AND u.is_active = TRUE + ORDER BY u.email ASC ) r;` // loadUsers loads all the users for the passed in org