Add admin add user
This commit is contained in:
		
							parent
							
								
									5373a3093e
								
							
						
					
					
						commit
						c1576b4c40
					
				
					 9 changed files with 204 additions and 15 deletions
				
			
		|  | @ -19,36 +19,36 @@ import ( | |||
| var ( | ||||
| 	orm *xorm.Engine | ||||
| 
 | ||||
| 	dbCfg struct { | ||||
| 	DbCfg struct { | ||||
| 		Type, Host, Name, User, Pwd, Path, SslMode string | ||||
| 	} | ||||
| ) | ||||
| 
 | ||||
| func LoadModelsConfig() { | ||||
| 	dbCfg.Type = base.Cfg.MustValue("database", "DB_TYPE") | ||||
| 	dbCfg.Host = base.Cfg.MustValue("database", "HOST") | ||||
| 	dbCfg.Name = base.Cfg.MustValue("database", "NAME") | ||||
| 	dbCfg.User = base.Cfg.MustValue("database", "USER") | ||||
| 	dbCfg.Pwd = base.Cfg.MustValue("database", "PASSWD") | ||||
| 	dbCfg.Path = base.Cfg.MustValue("database", "PATH", "data/gogs.db") | ||||
| 	dbCfg.SslMode = base.Cfg.MustValue("database", "SSL_MODE") | ||||
| 	DbCfg.Type = base.Cfg.MustValue("database", "DB_TYPE") | ||||
| 	DbCfg.Host = base.Cfg.MustValue("database", "HOST") | ||||
| 	DbCfg.Name = base.Cfg.MustValue("database", "NAME") | ||||
| 	DbCfg.User = base.Cfg.MustValue("database", "USER") | ||||
| 	DbCfg.Pwd = base.Cfg.MustValue("database", "PASSWD") | ||||
| 	DbCfg.SslMode = base.Cfg.MustValue("database", "SSL_MODE") | ||||
| 	DbCfg.Path = base.Cfg.MustValue("database", "PATH", "data/gogs.db") | ||||
| } | ||||
| 
 | ||||
| func setEngine() { | ||||
| 
 | ||||
| 	var err error | ||||
| 	switch dbCfg.Type { | ||||
| 	switch DbCfg.Type { | ||||
| 	case "mysql": | ||||
| 		orm, err = xorm.NewEngine("mysql", fmt.Sprintf("%s:%s@%s/%s?charset=utf8", | ||||
| 			dbCfg.User, dbCfg.Pwd, dbCfg.Host, dbCfg.Name)) | ||||
| 			DbCfg.User, DbCfg.Pwd, DbCfg.Host, DbCfg.Name)) | ||||
| 	case "postgres": | ||||
| 		orm, err = xorm.NewEngine("postgres", fmt.Sprintf("user=%s password=%s dbname=%s sslmode=%s", | ||||
| 			dbCfg.User, dbCfg.Pwd, dbCfg.Name, dbCfg.SslMode)) | ||||
| 			DbCfg.User, DbCfg.Pwd, DbCfg.Name, DbCfg.SslMode)) | ||||
| 	case "sqlite3": | ||||
| 		os.MkdirAll(path.Dir(dbCfg.Path), os.ModePerm) | ||||
| 		orm, err = xorm.NewEngine("sqlite3", dbCfg.Path) | ||||
| 		os.MkdirAll(path.Dir(DbCfg.Path), os.ModePerm) | ||||
| 		orm, err = xorm.NewEngine("sqlite3", DbCfg.Path) | ||||
| 	default: | ||||
| 		fmt.Printf("Unknown database type: %s\n", dbCfg.Type) | ||||
| 		fmt.Printf("Unknown database type: %s\n", DbCfg.Type) | ||||
| 		os.Exit(2) | ||||
| 	} | ||||
| 	if err != nil { | ||||
|  |  | |||
|  | @ -107,7 +107,7 @@ func IsRepositoryExist(user *User, repoName string) (bool, error) { | |||
| 
 | ||||
| var ( | ||||
| 	// Define as all lower case!!
 | ||||
| 	illegalPatterns = []string{"[.][Gg][Ii][Tt]", "user", "help", "stars", "issues", "pulls", "commits", "admin", "repo", "template"} | ||||
| 	illegalPatterns = []string{"[.][Gg][Ii][Tt]", "user", "help", "stars", "issues", "pulls", "commits", "admin", "repo", "template", "admin"} | ||||
| ) | ||||
| 
 | ||||
| // IsLegalName returns false if name contains illegal characters.
 | ||||
|  |  | |||
|  | @ -32,6 +32,7 @@ var ( | |||
| 	AppUrl       string | ||||
| 	Domain       string | ||||
| 	SecretKey    string | ||||
| 	RunUser      string | ||||
| 	RepoRootPath string | ||||
| 
 | ||||
| 	Cfg         *goconfig.ConfigFile | ||||
|  | @ -179,6 +180,7 @@ func NewConfigContext() { | |||
| 	AppUrl = Cfg.MustValue("server", "ROOT_URL") | ||||
| 	Domain = Cfg.MustValue("server", "DOMAIN") | ||||
| 	SecretKey = Cfg.MustValue("security", "SECRET_KEY") | ||||
| 	RunUser = Cfg.MustValue("", "RUN_USER") | ||||
| 
 | ||||
| 	// Determine and create root git reposiroty path.
 | ||||
| 	RepoRootPath = Cfg.MustValue("repository", "ROOT") | ||||
|  |  | |||
|  | @ -5,7 +5,12 @@ | |||
| package admin | ||||
| 
 | ||||
| import ( | ||||
| 	"strings" | ||||
| 
 | ||||
| 	"github.com/codegangsta/martini" | ||||
| 
 | ||||
| 	"github.com/gogits/gogs/models" | ||||
| 	"github.com/gogits/gogs/modules/base" | ||||
| 	"github.com/gogits/gogs/modules/middleware" | ||||
| ) | ||||
| 
 | ||||
|  | @ -45,5 +50,18 @@ func Repositories(ctx *middleware.Context) { | |||
| func Config(ctx *middleware.Context) { | ||||
| 	ctx.Data["Title"] = "Server Configuration" | ||||
| 	ctx.Data["PageIsConfig"] = true | ||||
| 
 | ||||
| 	ctx.Data["AppUrl"] = base.AppUrl | ||||
| 	ctx.Data["Domain"] = base.Domain | ||||
| 	ctx.Data["RunUser"] = base.RunUser | ||||
| 	ctx.Data["RunMode"] = strings.Title(martini.Env) | ||||
| 	ctx.Data["RepoRootPath"] = base.RepoRootPath | ||||
| 
 | ||||
| 	ctx.Data["Service"] = base.Service | ||||
| 
 | ||||
| 	ctx.Data["DbCfg"] = models.DbCfg | ||||
| 
 | ||||
| 	ctx.Data["Mailer"] = base.MailService | ||||
| 
 | ||||
| 	ctx.HTML(200, "admin/config") | ||||
| } | ||||
|  |  | |||
							
								
								
									
										63
									
								
								routers/admin/user.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										63
									
								
								routers/admin/user.go
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,63 @@ | |||
| // Copyright 2014 The Gogs Authors. All rights reserved.
 | ||||
| // Use of this source code is governed by a MIT-style
 | ||||
| // license that can be found in the LICENSE file.
 | ||||
| 
 | ||||
| package admin | ||||
| 
 | ||||
| import ( | ||||
| 	"strings" | ||||
| 
 | ||||
| 	"github.com/gogits/gogs/models" | ||||
| 	"github.com/gogits/gogs/modules/auth" | ||||
| 	"github.com/gogits/gogs/modules/log" | ||||
| 	"github.com/gogits/gogs/modules/middleware" | ||||
| ) | ||||
| 
 | ||||
| func NewUser(ctx *middleware.Context, form auth.RegisterForm) { | ||||
| 	ctx.Data["Title"] = "New Account" | ||||
| 
 | ||||
| 	if ctx.Req.Method == "GET" { | ||||
| 		ctx.HTML(200, "admin/users/new") | ||||
| 		return | ||||
| 	} | ||||
| 
 | ||||
| 	if form.Password != form.RetypePasswd { | ||||
| 		ctx.Data["HasError"] = true | ||||
| 		ctx.Data["Err_Password"] = true | ||||
| 		ctx.Data["Err_RetypePasswd"] = true | ||||
| 		ctx.Data["ErrorMsg"] = "Password and re-type password are not same" | ||||
| 		auth.AssignForm(form, ctx.Data) | ||||
| 	} | ||||
| 
 | ||||
| 	if ctx.HasError() { | ||||
| 		ctx.HTML(200, "admin/users/new") | ||||
| 		return | ||||
| 	} | ||||
| 
 | ||||
| 	u := &models.User{ | ||||
| 		Name:     form.UserName, | ||||
| 		Email:    form.Email, | ||||
| 		Passwd:   form.Password, | ||||
| 		IsActive: true, | ||||
| 	} | ||||
| 
 | ||||
| 	var err error | ||||
| 	if u, err = models.RegisterUser(u); err != nil { | ||||
| 		switch err { | ||||
| 		case models.ErrUserAlreadyExist: | ||||
| 			ctx.RenderWithErr("Username has been already taken", "admin/users/new", &form) | ||||
| 		case models.ErrEmailAlreadyUsed: | ||||
| 			ctx.RenderWithErr("E-mail address has been already used", "admin/users/new", &form) | ||||
| 		case models.ErrUserNameIllegal: | ||||
| 			ctx.RenderWithErr(models.ErrRepoNameIllegal.Error(), "admin/users/new", &form) | ||||
| 		default: | ||||
| 			ctx.Handle(200, "admin.user.NewUser", err) | ||||
| 		} | ||||
| 		return | ||||
| 	} | ||||
| 
 | ||||
| 	log.Trace("%s User created by admin(%s): %s", ctx.Req.RequestURI, | ||||
| 		ctx.User.LowerName, strings.ToLower(form.UserName)) | ||||
| 
 | ||||
| 	ctx.Redirect("/admin/users") | ||||
| } | ||||
|  | @ -9,7 +9,57 @@ | |||
|             </div> | ||||
| 
 | ||||
|             <div class="panel-body"> | ||||
|                 <div><b>Application Name:</b> {{AppName}}</div> | ||||
|                 <div><b>Application Version:</b> {{AppVer}}</div> | ||||
|                 <div><b>Application URL:</b> {{.AppUrl}}</div> | ||||
|                 <div><b>Domain:</b> {{.Domain}}</div> | ||||
|                 <hr/> | ||||
|                 <div><b>Run User:</b> {{.RunUser}}</div> | ||||
|                 <div><b>Run Mode:</b> {{.RunMode}}</div> | ||||
|                 <hr/> | ||||
|                 <div><b>Repository Root Path:</b> {{.RepoRootPath}}</div> | ||||
|             </div> | ||||
|         </div> | ||||
| 
 | ||||
|         <div class="panel panel-default"> | ||||
|             <div class="panel-heading"> | ||||
|                 Database Configuration | ||||
|             </div> | ||||
| 
 | ||||
|             <div class="panel-body"> | ||||
|                 <div><b>Type:</b> {{.DbCfg.Type}}</div> | ||||
|                 <div><b>Host:</b> {{.DbCfg.Host}}</div> | ||||
|                 <div><b>Name:</b> {{.DbCfg.Name}}</div> | ||||
|                 <div><b>User:</b> {{.DbCfg.User}}</div> | ||||
|                 <div><b>SslMode:</b> {{.DbCfg.SslMode}} (for "postgres" only)</div> | ||||
|                 <div><b>Path:</b> {{.DbCfg.Path}} (for "sqlite3" only)</div> | ||||
|             </div> | ||||
|         </div> | ||||
| 
 | ||||
|         <div class="panel panel-default"> | ||||
|             <div class="panel-heading"> | ||||
|                 Service Configuration | ||||
|             </div> | ||||
| 
 | ||||
|             <div class="panel-body"> | ||||
|                 <div><b>Register Email Confirmation:</b> <i class="fa fa{{if .Service.RegisterEmailConfirm}}-check{{end}}-square-o"></i></div> | ||||
|                 <div><b>Disenable Registeration:</b> <i class="fa fa{{if .Service.DisenableRegisteration}}-check{{end}}-square-o"></i></div> | ||||
|                 <div><b>Require Sign In View:</b> <i class="fa fa{{if .Service.RequireSignInView}}-check{{end}}-square-o"></i></div> | ||||
|                 <hr/> | ||||
|                 <div><b>Active Code Lives:</b> {{.Service.ActiveCodeLives}} minutes</div> | ||||
|                 <div><b>Reset Password Code Lives:</b> {{.Service.ResetPwdCodeLives}} minutes</div> | ||||
|             </div> | ||||
|         </div> | ||||
| 
 | ||||
|         <div class="panel panel-default"> | ||||
|             <div class="panel-heading"> | ||||
|                 Mailer Configuration | ||||
|             </div> | ||||
| 
 | ||||
|             <div class="panel-body"> | ||||
|                 <div><b>Name:</b> {{.Mailer.Name}}</div> | ||||
|                 <div><b>Host:</b> {{.Mailer.Host}}</div> | ||||
|                 <div><b>User:</b> {{.Mailer.User}}</div> | ||||
|             </div> | ||||
|         </div> | ||||
|     </div> | ||||
|  |  | |||
|  | @ -9,6 +9,7 @@ | |||
|             </div> | ||||
| 
 | ||||
|             <div class="panel-body"> | ||||
|                 <a href="/admin/users/new" class="btn btn-primary">New Account</a> | ||||
|                 <table class="table table-striped"> | ||||
|                     <thead> | ||||
|                         <tr> | ||||
|  |  | |||
							
								
								
									
										54
									
								
								templates/admin/users/new.tmpl
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								templates/admin/users/new.tmpl
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,54 @@ | |||
| {{template "base/head" .}} | ||||
| {{template "base/navbar" .}} | ||||
| <div id="gogs-body" class="container" data-page="admin"> | ||||
|     {{template "admin/nav" .}} | ||||
|     <div id="gogs-admin-container" class="col-md-9"> | ||||
|         <div class="panel panel-default"> | ||||
|             <div class="panel-heading"> | ||||
|                 New Account | ||||
|             </div> | ||||
| 
 | ||||
|             <div class="panel-body"> | ||||
|             	<br/> | ||||
| 				<form action="/admin/users/new" method="post" class="form-horizontal"> | ||||
| 				    <div class="alert alert-danger form-error{{if .HasError}}{{else}} hidden{{end}}">{{.ErrorMsg}}</div> | ||||
| 					<div class="form-group {{if .Err_UserName}}has-error has-feedback{{end}}"> | ||||
| 						<label class="col-md-4 control-label">Username: </label> | ||||
| 						<div class="col-md-6"> | ||||
| 							<input name="username" class="form-control" placeholder="Type account's username" value="{{.username}}" required="required"> | ||||
| 						</div> | ||||
| 					</div> | ||||
| 
 | ||||
| 					<div class="form-group {{if .Err_Email}}has-error has-feedback{{end}}"> | ||||
| 						<label class="col-md-4 control-label">Email: </label> | ||||
| 						<div class="col-md-6"> | ||||
| 							<input name="email" class="form-control" placeholder="Type account's e-mail address" value="{{.email}}" required="required" title="Email is not valid"> | ||||
| 						</div> | ||||
| 					</div> | ||||
| 
 | ||||
| 					<div class="form-group {{if .Err_Password}}has-error has-feedback{{end}}"> | ||||
| 						<label class="col-md-4 control-label">Password: </label> | ||||
| 						<div class="col-md-6"> | ||||
| 							<input name="passwd" type="password" class="form-control" placeholder="Type account's password" required="required" title="Password must contain at least 6 characters"> | ||||
| 						</div> | ||||
| 					</div> | ||||
| 
 | ||||
| 					<div class="form-group {{if .Err_RetypePasswd}}has-error has-feedback{{end}}"> | ||||
| 						<label class="col-md-4 control-label">Re-type: </label> | ||||
| 						<div class="col-md-6"> | ||||
| 							<input name="retypepasswd" type="password" class="form-control" placeholder="Re-type account's password" required="required" title="Re-type Password must be same to Password"> | ||||
| 						</div> | ||||
| 					</div> | ||||
| 
 | ||||
| 					<div class="form-group"> | ||||
| 					    <div class="col-md-offset-4 col-md-6"> | ||||
| 					    	<button type="submit" class="btn btn-lg btn-primary">Create new account</button> | ||||
| 					    </div> | ||||
| 					</div> | ||||
| 				</form> | ||||
|             </div> | ||||
|         </div> | ||||
| 
 | ||||
| 	</div> | ||||
| </div> | ||||
| {{template "base/footer" .}} | ||||
							
								
								
									
										1
									
								
								web.go
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								web.go
									
									
									
									
									
								
							|  | @ -117,6 +117,7 @@ func runWeb(*cli.Context) { | |||
| 	adminReq := middleware.AdminRequire() | ||||
| 	m.Get("/admin", reqSignIn, adminReq, admin.Dashboard) | ||||
| 	m.Get("/admin/users", reqSignIn, adminReq, admin.Users) | ||||
| 	m.Any("/admin/users/new", reqSignIn, adminReq, binding.BindIgnErr(auth.RegisterForm{}), admin.NewUser) | ||||
| 	m.Get("/admin/repos", reqSignIn, adminReq, admin.Repositories) | ||||
| 	m.Get("/admin/config", reqSignIn, adminReq, admin.Config) | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
		Reference in a new issue