aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUnknown <joe2010xtmf@163.com>2014-03-21 03:27:59 -0400
committerUnknown <joe2010xtmf@163.com>2014-03-21 03:27:59 -0400
commitc1576b4c400376f22ec25012a6ca66e9c5539ee4 (patch)
treeb8e682560055e8e91448d701e798edc3450a86c4
parent5373a3093eaf9bc9ced7a6b3335ccf1b17fd343e (diff)
downloadgitea-c1576b4c400376f22ec25012a6ca66e9c5539ee4.tar.gz
gitea-c1576b4c400376f22ec25012a6ca66e9c5539ee4.zip
Add admin add user
-rw-r--r--models/models.go28
-rw-r--r--models/repo.go2
-rw-r--r--modules/base/conf.go2
-rw-r--r--routers/admin/admin.go18
-rw-r--r--routers/admin/user.go63
-rw-r--r--templates/admin/config.tmpl50
-rw-r--r--templates/admin/users.tmpl1
-rw-r--r--templates/admin/users/new.tmpl54
-rw-r--r--web.go1
9 files changed, 204 insertions, 15 deletions
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 @@
</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>
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 @@
</div>
<div class="panel-body">
+ <a href="/admin/users/new" class="btn btn-primary">New Account</a>
<table class="table table-striped">
<thead>
<tr>
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" .}}
+<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" .}} \ 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)