From c1576b4c400376f22ec25012a6ca66e9c5539ee4 Mon Sep 17 00:00:00 2001 From: Unknown Date: Fri, 21 Mar 2014 03:27:59 -0400 Subject: [PATCH] Add admin add user --- models/models.go | 28 +++++++-------- models/repo.go | 2 +- modules/base/conf.go | 2 ++ routers/admin/admin.go | 18 ++++++++++ routers/admin/user.go | 63 ++++++++++++++++++++++++++++++++++ templates/admin/config.tmpl | 50 +++++++++++++++++++++++++++ templates/admin/users.tmpl | 1 + templates/admin/users/new.tmpl | 54 +++++++++++++++++++++++++++++ web.go | 1 + 9 files changed, 204 insertions(+), 15 deletions(-) create mode 100644 routers/admin/user.go create mode 100644 templates/admin/users/new.tmpl diff --git a/models/models.go b/models/models.go index bb0015d3de..a4550d7243 100644 --- a/models/models.go +++ b/models/models.go @@ -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 { diff --git a/models/repo.go b/models/repo.go index f252004785..918e5dc84c 100644 --- a/models/repo.go +++ b/models/repo.go @@ -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. diff --git a/modules/base/conf.go b/modules/base/conf.go index 3050b915fa..bf054ec3c5 100644 --- a/modules/base/conf.go +++ b/modules/base/conf.go @@ -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") diff --git a/routers/admin/admin.go b/routers/admin/admin.go index 1095a599b9..547883f7bc 100644 --- a/routers/admin/admin.go +++ b/routers/admin/admin.go @@ -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") } diff --git a/routers/admin/user.go b/routers/admin/user.go new file mode 100644 index 0000000000..9dcc1499e3 --- /dev/null +++ b/routers/admin/user.go @@ -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") +} diff --git a/templates/admin/config.tmpl b/templates/admin/config.tmpl index d209bcdfd9..9593a545f8 100644 --- a/templates/admin/config.tmpl +++ b/templates/admin/config.tmpl @@ -9,7 +9,57 @@
+
Application Name: {{AppName}}
+
Application Version: {{AppVer}}
+
Application URL: {{.AppUrl}}
+
Domain: {{.Domain}}
+
+
Run User: {{.RunUser}}
+
Run Mode: {{.RunMode}}
+
+
Repository Root Path: {{.RepoRootPath}}
+
+ +
+
+ Database Configuration +
+ +
+
Type: {{.DbCfg.Type}}
+
Host: {{.DbCfg.Host}}
+
Name: {{.DbCfg.Name}}
+
User: {{.DbCfg.User}}
+
SslMode: {{.DbCfg.SslMode}} (for "postgres" only)
+
Path: {{.DbCfg.Path}} (for "sqlite3" only)
+
+
+ +
+
+ Service Configuration +
+ +
+
Register Email Confirmation:
+
Disenable Registeration:
+
Require Sign In View:
+
+
Active Code Lives: {{.Service.ActiveCodeLives}} minutes
+
Reset Password Code Lives: {{.Service.ResetPwdCodeLives}} minutes
+
+
+ +
+
+ Mailer Configuration +
+ +
+
Name: {{.Mailer.Name}}
+
Host: {{.Mailer.Host}}
+
User: {{.Mailer.User}}
diff --git a/templates/admin/users.tmpl b/templates/admin/users.tmpl index ae2b5bbb65..d82f04b812 100644 --- a/templates/admin/users.tmpl +++ b/templates/admin/users.tmpl @@ -9,6 +9,7 @@
+ New Account diff --git a/templates/admin/users/new.tmpl b/templates/admin/users/new.tmpl new file mode 100644 index 0000000000..bf59b16a80 --- /dev/null +++ b/templates/admin/users/new.tmpl @@ -0,0 +1,54 @@ +{{template "base/head" .}} +{{template "base/navbar" .}} +
+ {{template "admin/nav" .}} +
+
+
+ New Account +
+ +
+
+
+
{{.ErrorMsg}}
+
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+{{template "base/footer" .}} \ No newline at end of file diff --git a/web.go b/web.go index 6fe838aa78..d6d78afa5d 100644 --- a/web.go +++ b/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)