rawlinp closed pull request #2703: Remove TO Go unnecessary query passing
URL: https://github.com/apache/trafficcontrol/pull/2703
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/traffic_ops/traffic_ops_golang/auth/authorize.go
b/traffic_ops/traffic_ops_golang/auth/authorize.go
index 069e2b150..faf43dc68 100644
--- a/traffic_ops/traffic_ops_golang/auth/authorize.go
+++ b/traffic_ops/traffic_ops_golang/auth/authorize.go
@@ -75,7 +75,22 @@ type key int
const CurrentUserKey key = iota
// GetCurrentUserFromDB - returns the id and privilege level of the given
user along with the username, or -1 as the id, - as the userName and
PrivLevelInvalid if the user doesn't exist, along with a user facing error, a
system error to log, and an error code to return
-func GetCurrentUserFromDB(DB *sqlx.DB, CurrentUserStmt, user string, timeout
time.Duration) (CurrentUser, error, error, int) {
+func GetCurrentUserFromDB(DB *sqlx.DB, user string, timeout time.Duration)
(CurrentUser, error, error, int) {
+ qry := `
+SELECT
+ r.priv_level,
+ u.id,
+ u.username,
+ COALESCE(u.tenant_id, -1) AS tenant_id,
+ ARRAY(SELECT rc.cap_name FROM role_capability AS rc WHERE rc.role_id=r.id)
AS capabilities
+FROM
+ tm_user AS u
+JOIN
+ role AS r ON u.role = r.id
+WHERE
+ u.username = $1
+`
+
var currentUserInfo CurrentUser
if DB == nil {
return CurrentUser{"-", -1, PrivLevelInvalid, TenantIDInvalid,
[]string{}}, nil, errors.New("no db provided to GetCurrentUserFromDB"),
http.StatusInternalServerError
@@ -83,7 +98,7 @@ func GetCurrentUserFromDB(DB *sqlx.DB, CurrentUserStmt, user
string, timeout tim
dbCtx, dbClose := context.WithTimeout(context.Background(), timeout)
defer dbClose()
- err := DB.GetContext(dbCtx, ¤tUserInfo, CurrentUserStmt, user)
+ err := DB.GetContext(dbCtx, ¤tUserInfo, qry, user)
switch {
case err == sql.ErrNoRows:
return CurrentUser{"-", -1, PrivLevelInvalid, TenantIDInvalid,
[]string{}}, errors.New("user not found"), fmt.Errorf("checking user %v info:
user not in database", user), http.StatusUnauthorized
diff --git a/traffic_ops/traffic_ops_golang/routing.go
b/traffic_ops/traffic_ops_golang/routing.go
index 62c52fdad..424686aec 100644
--- a/traffic_ops/traffic_ops_golang/routing.go
+++ b/traffic_ops/traffic_ops_golang/routing.go
@@ -216,9 +216,7 @@ func RegisterRoutes(d ServerData) error {
return err
}
- userInfoStmt := getUserInfoStmt()
-
- authBase := AuthBase{secret: d.Config.Secrets[0],
getCurrentUserInfoStmt: userInfoStmt, override: nil} //we know d.Config.Secrets
is a slice of at least one or start up would fail.
+ authBase := AuthBase{secret: d.Config.Secrets[0], override: nil} //we
know d.Config.Secrets is a slice of at least one or start up would fail.
routes := CreateRouteMap(routeSlice, rawRoutes, authBase,
d.RequestTimeout)
compiledRoutes := CompileRoutes(routes)
getReqID := nextReqIDGetter()
@@ -228,10 +226,6 @@ func RegisterRoutes(d ServerData) error {
return nil
}
-func getUserInfoStmt() string {
- return "SELECT r.priv_level, u.id, u.username, COALESCE(u.tenant_id,
-1) AS tenant_id, ARRAY(SELECT rc.cap_name FROM role_capability AS rc WHERE
rc.role_id=r.id) AS capabilities FROM tm_user AS u JOIN role AS r ON u.role =
r.id WHERE u.username = $1"
-}
-
func use(h http.HandlerFunc, middlewares []Middleware) http.HandlerFunc {
for i := len(middlewares) - 1; i >= 0; i-- { //apply them in reverse
order so they are used in a natural order.
h = middlewares[i](h)
diff --git a/traffic_ops/traffic_ops_golang/wrappers.go
b/traffic_ops/traffic_ops_golang/wrappers.go
index 59f179a76..10f31ca6d 100644
--- a/traffic_ops/traffic_ops_golang/wrappers.go
+++ b/traffic_ops/traffic_ops_golang/wrappers.go
@@ -47,9 +47,8 @@ var ServerName = "traffic_ops_golang" + "/" +
about.About.Version
// AuthBase ...
type AuthBase struct {
- secret string
- getCurrentUserInfoStmt string
- override Middleware
+ secret string
+ override Middleware
}
// GetWrapper ...
@@ -117,7 +116,7 @@ func (a AuthBase) GetWrapper(privLevelRequired int)
Middleware {
handleErr(http.StatusInternalServerError,
errors.New("No config found"))
}
- currentUserInfo, userErr, sysErr, code :=
auth.GetCurrentUserFromDB(DB, a.getCurrentUserInfoStmt, username,
time.Duration(cfg.DBQueryTimeoutSeconds)*time.Second)
+ currentUserInfo, userErr, sysErr, code :=
auth.GetCurrentUserFromDB(DB, username,
time.Duration(cfg.DBQueryTimeoutSeconds)*time.Second)
if userErr != nil || sysErr != nil {
api.HandleErr(w, r, code, userErr, sysErr)
return
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services