From 369ddf76a8ae6916ab72f1fa26c81b44c456c6ea Mon Sep 17 00:00:00 2001 From: Unknown Date: Fri, 21 Mar 2014 01:09:22 -0400 Subject: [PATCH] Batch fix --- README.md | 2 +- conf/app.ini | 8 +++++++- models/action.go | 4 ++++ models/models.go | 5 +++++ models/repo.go | 28 +++++++++++++++++++++++++--- modules/base/conf.go | 8 +++++--- modules/base/template.go | 11 ++++++++++- routers/user/user.go | 6 ++++++ templates/admin/repos.tmpl | 4 +++- templates/admin/users.tmpl | 2 +- templates/base/navbar.tmpl | 3 ++- templates/repo/single.tmpl | 2 +- templates/user/signin.tmpl | 2 +- templates/user/signup.tmpl | 4 ++++ 14 files changed, 75 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 8c971fdb85..3668ae8998 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ There are some very good products in this category such as [gitlab](http://gitla - Repository viewer. - Gravatar support. - Mail service(register). -- Supports MySQL and PostgreSQL. +- Supports MySQL, PostgreSQL and SQLite3(binary release only). ## Installation diff --git a/conf/app.ini b/conf/app.ini index 658f7c0151..d38cd1f05e 100644 --- a/conf/app.ini +++ b/conf/app.ini @@ -18,7 +18,7 @@ HTTP_ADDR = HTTP_PORT = 3000 [database] -; Either "mysql" or "postgres", it's your choice +; Either "mysql", "postgres" or "sqlite3"(binary release only), it's your choice DB_TYPE = mysql HOST = NAME = gogs @@ -26,6 +26,10 @@ USER = root PASSWD = ; For "postgres" only, either "disable", "require" or "verify-full" SSL_MODE = disable +; For "sqlite3" only +PATH = data/gogs.db + +[admin] [security] ; !!CHANGE THIS TO KEEP YOUR USER DATA SAFE!! @@ -36,6 +40,8 @@ ACTIVE_CODE_LIVE_MINUTES = 180 RESET_PASSWD_CODE_LIVE_MINUTES = 180 ; User need to confirm e-mail for registration REGISTER_EMAIL_CONFIRM = false +; Does not allow register and admin create account only +DISENABLE_REGISTERATION = false [mailer] ENABLED = false diff --git a/models/action.go b/models/action.go index b3be093533..107d4b1057 100644 --- a/models/action.go +++ b/models/action.go @@ -64,6 +64,10 @@ func CommitRepoAction(userId int64, userName string, watches = append(watches, Watch{UserId: userId}) for i := range watches { + if userId == watches[i].UserId && i > 0 { + continue // Do not add twice in case author watches his/her repository. + } + _, err = orm.InsertOne(&Action{ UserId: watches[i].UserId, ActUserId: userId, diff --git a/models/models.go b/models/models.go index 214d1c767a..8df230975f 100644 --- a/models/models.go +++ b/models/models.go @@ -7,6 +7,7 @@ package models import ( "fmt" "os" + "path" _ "github.com/go-sql-driver/mysql" _ "github.com/lib/pq" @@ -23,6 +24,7 @@ func setEngine() { dbName := base.Cfg.MustValue("database", "NAME") dbUser := base.Cfg.MustValue("database", "USER") dbPwd := base.Cfg.MustValue("database", "PASSWD") + dbPath := base.Cfg.MustValue("database", "PATH", "data/gogs.db") sslMode := base.Cfg.MustValue("database", "SSL_MODE") var err error @@ -33,6 +35,9 @@ func setEngine() { case "postgres": orm, err = xorm.NewEngine("postgres", fmt.Sprintf("user=%s password=%s dbname=%s sslmode=%s", dbUser, dbPwd, dbName, sslMode)) + case "sqlite3": + os.MkdirAll(path.Dir(dbPath), os.ModePerm) + orm, err = xorm.NewEngine("sqlite3", dbPath) default: fmt.Printf("Unknown database type: %s\n", dbType) os.Exit(2) diff --git a/models/repo.go b/models/repo.go index f5ceaf7631..93f68ceddf 100644 --- a/models/repo.go +++ b/models/repo.go @@ -323,11 +323,33 @@ func initRepository(f string, user *User, repo *Repository, initReadme bool, rep return nil } +// UserRepo reporesents a repository with user name. +type UserRepo struct { + *Repository + UserName string +} + // GetRepos returns given number of repository objects with offset. -func GetRepos(num, offset int) ([]Repository, error) { +func GetRepos(num, offset int) ([]UserRepo, error) { repos := make([]Repository, 0, num) - err := orm.Limit(num, offset).Asc("id").Find(&repos) - return repos, err + if err := orm.Limit(num, offset).Asc("id").Find(&repos); err != nil { + return nil, err + } + + urepos := make([]UserRepo, len(repos)) + for i := range repos { + urepos[i].Repository = &repos[i] + u := new(User) + has, err := orm.Id(urepos[i].Repository.OwnerId).Get(u) + if err != nil { + return nil, err + } else if !has { + return nil, ErrUserNotExist + } + urepos[i].UserName = u.Name + } + + return urepos, nil } func RepoPath(userName, repoName string) string { diff --git a/modules/base/conf.go b/modules/base/conf.go index 81f32bd591..41b66b69f8 100644 --- a/modules/base/conf.go +++ b/modules/base/conf.go @@ -39,9 +39,10 @@ var ( ) var Service struct { - RegisterEmailConfirm bool - ActiveCodeLives int - ResetPwdCodeLives int + RegisterEmailConfirm bool + DisenableRegisteration bool + ActiveCodeLives int + ResetPwdCodeLives int } func exeDir() (string, error) { @@ -68,6 +69,7 @@ var logLevels = map[string]string{ func newService() { Service.ActiveCodeLives = Cfg.MustInt("service", "ACTIVE_CODE_LIVE_MINUTES", 180) Service.ResetPwdCodeLives = Cfg.MustInt("service", "RESET_PASSWD_CODE_LIVE_MINUTES", 180) + Service.DisenableRegisteration = Cfg.MustBool("service", "DISENABLE_REGISTERATION", false) } func newLogService() { diff --git a/modules/base/template.go b/modules/base/template.go index e596d1dada..8d95dbeab7 100644 --- a/modules/base/template.go +++ b/modules/base/template.go @@ -33,6 +33,10 @@ func List(l *list.List) chan interface{} { return c } +var mailDomains = map[string]string{ + "gmail.com": "gmail.com", +} + var TemplateFuncs template.FuncMap = map[string]interface{}{ "AppName": func() string { return AppName @@ -56,7 +60,12 @@ var TemplateFuncs template.FuncMap = map[string]interface{}{ "DateFormat": DateFormat, "List": List, "Mail2Domain": func(mail string) string { - return "mail." + strings.Split(mail, "@")[1] + suffix := strings.SplitN(mail, "@", 2)[1] + domain, ok := mailDomains[suffix] + if !ok { + return "mail." + suffix + } + return domain }, "SubStr": func(str string, start, length int) string { return str[start : start+length] diff --git a/routers/user/user.go b/routers/user/user.go index ea6922591e..40b594ab80 100644 --- a/routers/user/user.go +++ b/routers/user/user.go @@ -112,6 +112,12 @@ func SignUp(ctx *middleware.Context, form auth.RegisterForm) { ctx.Data["Title"] = "Sign Up" ctx.Data["PageIsSignUp"] = true + if base.Service.DisenableRegisteration { + ctx.Data["DisenableRegisteration"] = true + ctx.HTML(200, "user/signup") + return + } + if ctx.Req.Method == "GET" { ctx.HTML(200, "user/signup") return diff --git a/templates/admin/repos.tmpl b/templates/admin/repos.tmpl index 4522c66792..f4834c9060 100644 --- a/templates/admin/repos.tmpl +++ b/templates/admin/repos.tmpl @@ -20,6 +20,7 @@ Id + Owner Name Private Watches @@ -31,7 +32,8 @@ {{range .Repos}} {{.Id}} - {{.Name}} + {{.UserName}} + {{.Name}} {{.NumWatches}} {{.NumForks}} diff --git a/templates/admin/users.tmpl b/templates/admin/users.tmpl index c087f268f2..b690e1771e 100644 --- a/templates/admin/users.tmpl +++ b/templates/admin/users.tmpl @@ -32,7 +32,7 @@ {{range .Users}} {{.Id}} - {{.Name}} + {{.Name}} {{.Email}} diff --git a/templates/base/navbar.tmpl b/templates/base/navbar.tmpl index 9c064d07e7..d775191a5a 100644 --- a/templates/base/navbar.tmpl +++ b/templates/base/navbar.tmpl @@ -11,7 +11,8 @@ {{if .IsAdmin}}{{end}} - {{else}}Sign in{{end}} + {{else}}Sign In + Sign Up{{end}} diff --git a/templates/repo/single.tmpl b/templates/repo/single.tmpl index 8a7b5e479b..a1c3cfbb26 100644 --- a/templates/repo/single.tmpl +++ b/templates/repo/single.tmpl @@ -15,7 +15,7 @@ diff --git a/templates/user/signin.tmpl b/templates/user/signin.tmpl index e60cedec88..a49bf11405 100644 --- a/templates/user/signin.tmpl +++ b/templates/user/signin.tmpl @@ -32,7 +32,7 @@
- Social Login + Register new account
diff --git a/templates/user/signup.tmpl b/templates/user/signup.tmpl index 187364de8f..069d34a5b2 100644 --- a/templates/user/signup.tmpl +++ b/templates/user/signup.tmpl @@ -2,6 +2,9 @@ {{template "base/navbar" .}}
+ {{if .DisenableRegisteration}} + Sorry, registeration has been disenabled, you can only get account from administrator. + {{else}}

Sign Up

{{.ErrorMsg}}
+ {{end}} {{template "base/footer" .}} \ No newline at end of file