From 069486d169d7adac902a310e10b6580857735880 Mon Sep 17 00:00:00 2001
From: Unknwon <joe2010xtmf@163.com>
Date: Fri, 5 Dec 2014 17:54:57 -0500
Subject: [PATCH] fix #165

---
 conf/app.ini               |  1 +
 gogs.go                    |  2 +-
 modules/auth/auth.go       | 20 +++++++++++++++++++-
 modules/setting/setting.go | 19 ++++++++++---------
 templates/.VERSION         |  2 +-
 5 files changed, 32 insertions(+), 12 deletions(-)

diff --git a/conf/app.ini b/conf/app.ini
index 0fec297bc..4043cbb63 100644
--- a/conf/app.ini
+++ b/conf/app.ini
@@ -77,6 +77,7 @@ ENABLE_CACHE_AVATAR = false
 ENABLE_NOTIFY_MAIL = false
 ; More detail: https://github.com/gogits/gogs/issues/165
 ENABLE_REVERSE_PROXY_AUTHENTICATION = false
+ENABLE_REVERSE_PROXY_AUTO_REGISTERATION = false
 
 [webhook]
 ; Cron task interval in minutes
diff --git a/gogs.go b/gogs.go
index ec8fa4869..5097309e4 100644
--- a/gogs.go
+++ b/gogs.go
@@ -17,7 +17,7 @@ import (
 	"github.com/gogits/gogs/modules/setting"
 )
 
-const APP_VER = "0.5.8.1130 Beta"
+const APP_VER = "0.5.8.1205 Beta"
 
 func init() {
 	runtime.GOMAXPROCS(runtime.NumCPU())
diff --git a/modules/auth/auth.go b/modules/auth/auth.go
index 302620dbc..5b7276b48 100644
--- a/modules/auth/auth.go
+++ b/modules/auth/auth.go
@@ -60,6 +60,7 @@ func SignedInId(req *http.Request, sess session.Store) int64 {
 }
 
 // SignedInUser returns the user object of signed user.
+// It returns a bool value to indicate whether user uses basic auth or not.
 func SignedInUser(req *http.Request, sess session.Store) (*models.User, bool) {
 	if !models.HasEngine {
 		return nil, false
@@ -75,8 +76,25 @@ func SignedInUser(req *http.Request, sess session.Store) (*models.User, bool) {
 				if err != nil {
 					if err != models.ErrUserNotExist {
 						log.Error(4, "GetUserByName: %v", err)
+						return nil, false
+					}
+
+					// Check if enabled auto-registeration.
+					if setting.Service.EnableReverseProxyAutoRegister {
+						u := &models.User{
+							Name:     webAuthUser,
+							Email:    webAuthUser + "@gogs.io",
+							Passwd:   webAuthUser,
+							IsActive: true,
+						}
+						if err = models.CreateUser(u); err != nil {
+							// FIXME: should I create a system notice?
+							log.Error(4, "CreateUser: %v", err)
+							return nil, false
+						} else {
+							return u, false
+						}
 					}
-					return nil, false
 				}
 				return u, false
 			}
diff --git a/modules/setting/setting.go b/modules/setting/setting.go
index 476fc6aff..b1efc9c7a 100644
--- a/modules/setting/setting.go
+++ b/modules/setting/setting.go
@@ -300,15 +300,15 @@ func NewConfigContext() {
 }
 
 var Service struct {
-	RegisterEmailConfirm   bool
-	DisableRegistration    bool
-	RequireSignInView      bool
-	EnableCacheAvatar      bool
-	EnableNotifyMail       bool
-	EnableReverseProxyAuth bool
-	LdapAuth               bool
-	ActiveCodeLives        int
-	ResetPwdCodeLives      int
+	RegisterEmailConfirm           bool
+	DisableRegistration            bool
+	RequireSignInView              bool
+	EnableCacheAvatar              bool
+	EnableNotifyMail               bool
+	EnableReverseProxyAuth         bool
+	EnableReverseProxyAutoRegister bool
+	ActiveCodeLives                int
+	ResetPwdCodeLives              int
 }
 
 func newService() {
@@ -318,6 +318,7 @@ func newService() {
 	Service.RequireSignInView = Cfg.MustBool("service", "REQUIRE_SIGNIN_VIEW")
 	Service.EnableCacheAvatar = Cfg.MustBool("service", "ENABLE_CACHE_AVATAR")
 	Service.EnableReverseProxyAuth = Cfg.MustBool("service", "ENABLE_REVERSE_PROXY_AUTHENTICATION")
+	Service.EnableReverseProxyAutoRegister = Cfg.MustBool("service", "ENABLE_REVERSE_PROXY_AUTO_REGISTERATION")
 }
 
 var logLevels = map[string]string{
diff --git a/templates/.VERSION b/templates/.VERSION
index 27cf20dd2..9808b6297 100644
--- a/templates/.VERSION
+++ b/templates/.VERSION
@@ -1 +1 @@
-0.5.8.1130 Beta
\ No newline at end of file
+0.5.8.1205 Beta
\ No newline at end of file