Browse Source

Avoid duplicate queries in auth (#827)

Avoid identical making calls to GetUserByID(..) in SignedInUser(..)
Ethan Koenig 2 years ago
parent
commit
e86d935175
1 changed files with 48 additions and 57 deletions
  1. 48 57
      modules/auth/auth.go

+ 48 - 57
modules/auth/auth.go

@@ -69,14 +69,7 @@ func SignedInID(ctx *macaron.Context, sess session.Store) int64 {
69 69
 	uid := sess.Get("uid")
70 70
 	if uid == nil {
71 71
 		return 0
72
-	}
73
-	if id, ok := uid.(int64); ok {
74
-		if _, err := models.GetUserByID(id); err != nil {
75
-			if !models.IsErrUserNotExist(err) {
76
-				log.Error(4, "GetUserById: %v", err)
77
-			}
78
-			return 0
79
-		}
72
+	} else if id, ok := uid.(int64); ok {
80 73
 		return id
81 74
 	}
82 75
 	return 0
@@ -89,66 +82,64 @@ func SignedInUser(ctx *macaron.Context, sess session.Store) (*models.User, bool)
89 82
 		return nil, false
90 83
 	}
91 84
 
92
-	uid := SignedInID(ctx, sess)
85
+	if uid := SignedInID(ctx, sess); uid > 0 {
86
+		user, err := models.GetUserByID(uid)
87
+		if err == nil {
88
+			return user, false
89
+		} else if !models.IsErrUserNotExist(err) {
90
+			log.Error(4, "GetUserById: %v", err)
91
+		}
92
+	}
93 93
 
94
-	if uid <= 0 {
95
-		if setting.Service.EnableReverseProxyAuth {
96
-			webAuthUser := ctx.Req.Header.Get(setting.ReverseProxyAuthUser)
97
-			if len(webAuthUser) > 0 {
98
-				u, err := models.GetUserByName(webAuthUser)
99
-				if err != nil {
100
-					if !models.IsErrUserNotExist(err) {
101
-						log.Error(4, "GetUserByName: %v", err)
102
-						return nil, false
103
-					}
94
+	if setting.Service.EnableReverseProxyAuth {
95
+		webAuthUser := ctx.Req.Header.Get(setting.ReverseProxyAuthUser)
96
+		if len(webAuthUser) > 0 {
97
+			u, err := models.GetUserByName(webAuthUser)
98
+			if err != nil {
99
+				if !models.IsErrUserNotExist(err) {
100
+					log.Error(4, "GetUserByName: %v", err)
101
+					return nil, false
102
+				}
104 103
 
105
-					// Check if enabled auto-registration.
106
-					if setting.Service.EnableReverseProxyAutoRegister {
107
-						u := &models.User{
108
-							Name:     webAuthUser,
109
-							Email:    gouuid.NewV4().String() + "@localhost",
110
-							Passwd:   webAuthUser,
111
-							IsActive: true,
112
-						}
113
-						if err = models.CreateUser(u); err != nil {
114
-							// FIXME: should I create a system notice?
115
-							log.Error(4, "CreateUser: %v", err)
116
-							return nil, false
117
-						}
118
-						return u, false
104
+				// Check if enabled auto-registration.
105
+				if setting.Service.EnableReverseProxyAutoRegister {
106
+					u := &models.User{
107
+						Name:     webAuthUser,
108
+						Email:    gouuid.NewV4().String() + "@localhost",
109
+						Passwd:   webAuthUser,
110
+						IsActive: true,
111
+					}
112
+					if err = models.CreateUser(u); err != nil {
113
+						// FIXME: should I create a system notice?
114
+						log.Error(4, "CreateUser: %v", err)
115
+						return nil, false
119 116
 					}
117
+					return u, false
120 118
 				}
121
-				return u, false
122 119
 			}
120
+			return u, false
123 121
 		}
122
+	}
124 123
 
125
-		// Check with basic auth.
126
-		baHead := ctx.Req.Header.Get("Authorization")
127
-		if len(baHead) > 0 {
128
-			auths := strings.Fields(baHead)
129
-			if len(auths) == 2 && auths[0] == "Basic" {
130
-				uname, passwd, _ := base.BasicAuthDecode(auths[1])
131
-
132
-				u, err := models.UserSignIn(uname, passwd)
133
-				if err != nil {
134
-					if !models.IsErrUserNotExist(err) {
135
-						log.Error(4, "UserSignIn: %v", err)
136
-					}
137
-					return nil, false
138
-				}
124
+	// Check with basic auth.
125
+	baHead := ctx.Req.Header.Get("Authorization")
126
+	if len(baHead) > 0 {
127
+		auths := strings.Fields(baHead)
128
+		if len(auths) == 2 && auths[0] == "Basic" {
129
+			uname, passwd, _ := base.BasicAuthDecode(auths[1])
139 130
 
140
-				return u, true
131
+			u, err := models.UserSignIn(uname, passwd)
132
+			if err != nil {
133
+				if !models.IsErrUserNotExist(err) {
134
+					log.Error(4, "UserSignIn: %v", err)
135
+				}
136
+				return nil, false
141 137
 			}
142
-		}
143
-		return nil, false
144
-	}
145 138
 
146
-	u, err := models.GetUserByID(uid)
147
-	if err != nil {
148
-		log.Error(4, "GetUserById: %v", err)
149
-		return nil, false
139
+			return u, true
140
+		}
150 141
 	}
151
-	return u, false
142
+	return nil, false
152 143
 }
153 144
 
154 145
 // Form form binding interface