From 9f9cd6bfc61d82ee0a3d31cee112be7975b8ca86 Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 20 Mar 2014 07:50:26 -0400 Subject: Work on admin --- routers/user/user.go | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'routers/user/user.go') diff --git a/routers/user/user.go b/routers/user/user.go index f495cb13..2b759e41 100644 --- a/routers/user/user.go +++ b/routers/user/user.go @@ -34,7 +34,7 @@ func Dashboard(ctx *middleware.Context) { return } ctx.Data["Feeds"] = feeds - ctx.HTML(200, "user/dashboard", ctx.Data) + ctx.HTML(200, "user/dashboard") } func Profile(ctx *middleware.Context, params martini.Params) { @@ -70,19 +70,19 @@ func Profile(ctx *middleware.Context, params martini.Params) { } ctx.Data["PageIsUserProfile"] = true - ctx.HTML(200, "user/profile", ctx.Data) + ctx.HTML(200, "user/profile") } func SignIn(ctx *middleware.Context, form auth.LogInForm) { ctx.Data["Title"] = "Log In" if ctx.Req.Method == "GET" { - ctx.HTML(200, "user/signin", ctx.Data) + ctx.HTML(200, "user/signin") return } if hasErr, ok := ctx.Data["HasError"]; ok && hasErr.(bool) { - ctx.HTML(200, "user/signin", ctx.Data) + ctx.HTML(200, "user/signin") return } @@ -113,7 +113,7 @@ func SignUp(ctx *middleware.Context, form auth.RegisterForm) { ctx.Data["PageIsSignUp"] = true if ctx.Req.Method == "GET" { - ctx.HTML(200, "user/signup", ctx.Data) + ctx.HTML(200, "user/signup") return } @@ -126,7 +126,7 @@ func SignUp(ctx *middleware.Context, form auth.RegisterForm) { } if ctx.HasError() { - ctx.HTML(200, "user/signup", ctx.Data) + ctx.HTML(200, "user/signup") return } @@ -158,7 +158,7 @@ func SignUp(ctx *middleware.Context, form auth.RegisterForm) { ctx.Data["IsSendRegisterMail"] = true ctx.Data["Email"] = u.Email ctx.Data["Hours"] = base.Service.ActiveCodeLives / 60 - ctx.Render.HTML(200, "user/active", ctx.Data) + ctx.HTML(200, "user/active") return } ctx.Redirect("/user/login") @@ -170,7 +170,7 @@ func Delete(ctx *middleware.Context) { ctx.Data["IsUserPageSettingDelete"] = true if ctx.Req.Method == "GET" { - ctx.HTML(200, "user/delete", ctx.Data) + ctx.HTML(200, "user/delete") return } @@ -195,7 +195,7 @@ func Delete(ctx *middleware.Context) { } } - ctx.HTML(200, "user/delete", ctx.Data) + ctx.HTML(200, "user/delete") } const ( @@ -218,15 +218,15 @@ func Feeds(ctx *middleware.Context, form auth.FeedsForm) { } func Issues(ctx *middleware.Context) { - ctx.HTML(200, "user/issues", ctx.Data) + ctx.HTML(200, "user/issues") } func Pulls(ctx *middleware.Context) { - ctx.HTML(200, "user/pulls", ctx.Data) + ctx.HTML(200, "user/pulls") } func Stars(ctx *middleware.Context) { - ctx.HTML(200, "user/stars", ctx.Data) + ctx.HTML(200, "user/stars") } func Activate(ctx *middleware.Context) { @@ -244,7 +244,7 @@ func Activate(ctx *middleware.Context) { } else { ctx.Data["ServiceNotEnabled"] = true } - ctx.Render.HTML(200, "user/active", ctx.Data) + ctx.HTML(200, "user/active") return } @@ -263,5 +263,5 @@ func Activate(ctx *middleware.Context) { } ctx.Data["IsActivateFailed"] = true - ctx.Render.HTML(200, "user/active", ctx.Data) + ctx.HTML(200, "user/active") } -- cgit v1.2.3 From 4cf6cc63b0679aaf5fe8b74a2aaf0bd92b1f12d3 Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 20 Mar 2014 08:02:14 -0400 Subject: Work on admin --- conf/app.ini | 4 ---- models/user.go | 8 +++++++- modules/base/conf.go | 2 -- modules/middleware/auth.go | 2 +- modules/middleware/context.go | 6 +----- routers/user/user.go | 2 +- 6 files changed, 10 insertions(+), 14 deletions(-) (limited to 'routers/user/user.go') diff --git a/conf/app.ini b/conf/app.ini index 21090ceb..658f7c01 100644 --- a/conf/app.ini +++ b/conf/app.ini @@ -27,10 +27,6 @@ PASSWD = ; For "postgres" only, either "disable", "require" or "verify-full" SSL_MODE = disable -[admin] -; Administor's name, which should be same as the user name you want to authorize -NAME = admin - [security] ; !!CHANGE THIS TO KEEP YOUR USER DATA SAFE!! SECRET_KEY = !#@FDEWREWR&*( diff --git a/models/user.go b/models/user.go index 8f74fd53..fd89af6b 100644 --- a/models/user.go +++ b/models/user.go @@ -137,7 +137,13 @@ func RegisterUser(user *User) (*User, error) { } return nil, err } - return user, nil + + if user.Id == 1 { + user.IsAdmin = true + user.IsActive = true + _, err = orm.Id(user.Id).UseBool().Update(user) + } + return user, err } // get user by erify code diff --git a/modules/base/conf.go b/modules/base/conf.go index c904c5b3..fdbf3ad3 100644 --- a/modules/base/conf.go +++ b/modules/base/conf.go @@ -32,7 +32,6 @@ var ( AppUrl string Domain string SecretKey string - AdminName string Cfg *goconfig.ConfigFile MailService *Mailer ) @@ -174,7 +173,6 @@ func init() { AppUrl = Cfg.MustValue("server", "ROOT_URL") Domain = Cfg.MustValue("server", "DOMAIN") SecretKey = Cfg.MustValue("security", "SECRET_KEY") - AdminName = strings.ToLower(Cfg.MustValue("admin", "NAME")) } func NewServices() { diff --git a/modules/middleware/auth.go b/modules/middleware/auth.go index b67f766b..44033abb 100644 --- a/modules/middleware/auth.go +++ b/modules/middleware/auth.go @@ -39,7 +39,7 @@ func SignOutRequire() martini.Handler { // AdminRequire requires user signed in as administor. func AdminRequire() martini.Handler { return func(ctx *Context) { - if ctx.User.LowerName != base.AdminName && !ctx.User.IsAdmin { + if !ctx.User.IsAdmin { ctx.Error(403) return } diff --git a/modules/middleware/context.go b/modules/middleware/context.go index 744cdfc1..cb3cbabc 100644 --- a/modules/middleware/context.go +++ b/modules/middleware/context.go @@ -14,7 +14,6 @@ import ( "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/auth" - "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/log" ) @@ -112,10 +111,7 @@ func InitContext() martini.Handler { ctx.Data["SignedUser"] = user ctx.Data["SignedUserId"] = user.Id ctx.Data["SignedUserName"] = user.LowerName - - if ctx.User.IsAdmin || ctx.User.LowerName == base.AdminName { - ctx.Data["IsAdmin"] = true - } + ctx.Data["IsAdmin"] = ctx.User.IsAdmin } ctx.Data["PageStartTime"] = time.Now() diff --git a/routers/user/user.go b/routers/user/user.go index 2b759e41..be2c4d38 100644 --- a/routers/user/user.go +++ b/routers/user/user.go @@ -153,7 +153,7 @@ func SignUp(ctx *middleware.Context, form auth.RegisterForm) { log.Trace("%s User created: %s", ctx.Req.RequestURI, strings.ToLower(form.UserName)) // Send confirmation e-mail. - if base.Service.RegisterEmailConfirm { + if base.Service.RegisterEmailConfirm && u.Id > 1 { mailer.SendRegisterMail(ctx.Render, u) ctx.Data["IsSendRegisterMail"] = true ctx.Data["Email"] = u.Email -- cgit v1.2.3 From 3b387336bfc097090d5b03f5b01e136bca56f8fd Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 20 Mar 2014 11:41:24 -0400 Subject: Add Repository/user name filter --- README.md | 2 +- gogs.go | 2 +- models/repo.go | 31 +++++++++++++++++++++++++++++++ models/user.go | 5 +++++ routers/repo/repo.go | 3 +++ routers/repo/single.go | 5 +++++ routers/user/user.go | 8 +++++--- 7 files changed, 51 insertions(+), 5 deletions(-) (limited to 'routers/user/user.go') diff --git a/README.md b/README.md index 3a1023c6..4219e4ed 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Gogs(Go Git Service) is a GitHub-like clone in the Go Programming Language. Since we choose to use pure Go implementation of Git manipulation, Gogs certainly supports **ALL platforms** that Go supports, including Linux, Max OS X, and Windows with **ZERO** dependency. -##### Current version: 0.1.1 Alpha +##### Current version: 0.1.4 Alpha ## Purpose diff --git a/gogs.go b/gogs.go index d32e3908..a600c53d 100644 --- a/gogs.go +++ b/gogs.go @@ -20,7 +20,7 @@ import ( // Test that go1.1 tag above is included in builds. main.go refers to this definition. const go11tag = true -const APP_VER = "0.1.2.0320.1" +const APP_VER = "0.1.3.0320.1" func init() { base.AppVer = APP_VER diff --git a/models/repo.go b/models/repo.go index 052341ff..bcbc5867 100644 --- a/models/repo.go +++ b/models/repo.go @@ -12,6 +12,7 @@ import ( "os" "path" "path/filepath" + "regexp" "strings" "sync" "time" @@ -82,6 +83,7 @@ var ( ErrRepoAlreadyExist = errors.New("Repository already exist") ErrRepoNotExist = errors.New("Repository does not exist") ErrRepoFileNotExist = errors.New("Target Repo file does not exist") + ErrRepoNameIllegal = errors.New("Repository name contains illegal characters") ) func init() { @@ -104,6 +106,15 @@ func init() { os.Exit(2) } } + + // Initialize illegal patterns. + for i := range illegalPatterns[1:] { + pattern := "" + for j := range illegalPatterns[i+1] { + pattern += "[" + string(illegalPatterns[i+1][j]-32) + string(illegalPatterns[i+1][j]) + "]" + } + illegalPatterns[i+1] = pattern + } } // IsRepositoryExist returns true if the repository with given name under user has already existed. @@ -120,8 +131,28 @@ func IsRepositoryExist(user *User, repoName string) (bool, error) { return s.IsDir(), nil } +var ( + // Define as all lower case!! + illegalPatterns = []string{"[.][Gg][Ii][Tt]", "user", "help", "stars", "issues", "pulls", "commits", "admin", "repo", "template"} +) + +// IsLegalName returns false if name contains illegal characters. +func IsLegalName(repoName string) bool { + for _, pattern := range illegalPatterns { + has, _ := regexp.MatchString(pattern, repoName) + if has { + return false + } + } + return true +} + // CreateRepository creates a repository for given user or orgnaziation. func CreateRepository(user *User, repoName, desc, repoLang, license string, private bool, initReadme bool) (*Repository, error) { + if !IsLegalName(repoName) { + return nil, ErrRepoNameIllegal + } + isExist, err := IsRepositoryExist(user, repoName) if err != nil { return nil, err diff --git a/models/user.go b/models/user.go index fd89af6b..990e1954 100644 --- a/models/user.go +++ b/models/user.go @@ -79,6 +79,7 @@ var ( ErrUserAlreadyExist = errors.New("User already exist") ErrUserNotExist = errors.New("User does not exist") ErrEmailAlreadyUsed = errors.New("E-mail already used") + ErrUserNameIllegal = errors.New("User name contains illegal characters") ) // IsUserExist checks if given user name exist, @@ -108,6 +109,10 @@ func GetUserSalt() string { // RegisterUser creates record of a new user. func RegisterUser(user *User) (*User, error) { + if !IsLegalName(user.Name) { + return nil, ErrUserNameIllegal + } + isExist, err := IsUserExist(user.Name) if err != nil { return nil, err diff --git a/routers/repo/repo.go b/routers/repo/repo.go index 556cc434..c83a6df5 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -31,6 +31,9 @@ func Create(ctx *middleware.Context, form auth.CreateRepoForm) { } else if err == models.ErrRepoAlreadyExist { ctx.RenderWithErr("Repository name has already been used", "repo/create", &form) return + } else if err == models.ErrRepoNameIllegal { + ctx.RenderWithErr(models.ErrRepoNameIllegal.Error(), "repo/create", &form) + return } ctx.Handle(200, "repo.Create", err) } diff --git a/routers/repo/single.go b/routers/repo/single.go index f1b15cce..eab49be9 100644 --- a/routers/repo/single.go +++ b/routers/repo/single.go @@ -217,6 +217,11 @@ func Setting(ctx *middleware.Context, params martini.Params) { title = t } + if len(params["branchname"]) == 0 { + params["branchname"] = "master" + } + + ctx.Data["Branchname"] = params["branchname"] ctx.Data["Title"] = title + " - settings" ctx.HTML(200, "repo/setting") } diff --git a/routers/user/user.go b/routers/user/user.go index be2c4d38..ea692259 100644 --- a/routers/user/user.go +++ b/routers/user/user.go @@ -139,11 +139,13 @@ func SignUp(ctx *middleware.Context, form auth.RegisterForm) { var err error if u, err = models.RegisterUser(u); err != nil { - switch err.Error() { - case models.ErrUserAlreadyExist.Error(): + switch err { + case models.ErrUserAlreadyExist: ctx.RenderWithErr("Username has been already taken", "user/signup", &form) - case models.ErrEmailAlreadyUsed.Error(): + case models.ErrEmailAlreadyUsed: ctx.RenderWithErr("E-mail address has been already used", "user/signup", &form) + case models.ErrUserNameIllegal: + ctx.RenderWithErr(models.ErrRepoNameIllegal.Error(), "user/signup", &form) default: ctx.Handle(200, "user.SignUp", err) } -- cgit v1.2.3 From 369ddf76a8ae6916ab72f1fa26c81b44c456c6ea Mon Sep 17 00:00:00 2001 From: Unknown Date: Fri, 21 Mar 2014 01:09:22 -0400 Subject: 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(-) (limited to 'routers/user/user.go') diff --git a/README.md b/README.md index 8c971fdb..3668ae89 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 658f7c01..d38cd1f0 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 b3be0935..107d4b10 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 214d1c76..8df23097 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 f5ceaf76..93f68ced 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 81f32bd5..41b66b69 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 e596d1da..8d95dbea 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 ea692259..40b594ab 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 4522c667..f4834c90 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 c087f268..b690e177 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 9c064d07..d775191a 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 8a7b5e47..a1c3cfbb 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 e60cedec..a49bf114 100644 --- a/templates/user/signin.tmpl +++ b/templates/user/signin.tmpl @@ -32,7 +32,7 @@ diff --git a/templates/user/signup.tmpl b/templates/user/signup.tmpl index 187364de..069d34a5 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 -- cgit v1.2.3 From d40499e7fa3d62655431f160b6909d9751dabe11 Mon Sep 17 00:00:00 2001 From: Unknown Date: Fri, 21 Mar 2014 10:09:57 -0400 Subject: Finsih mail resend limit --- README.md | 2 +- conf/app.ini | 8 +++++++- gogs.go | 2 +- models/models.go | 1 - modules/base/conf.go | 35 +++++++++++++++++++++++++---------- routers/admin/admin.go | 3 +++ routers/user/user.go | 12 ++++++++++-- templates/admin/config.tmpl | 11 +++++++++++ templates/user/active.tmpl | 2 ++ 9 files changed, 60 insertions(+), 16 deletions(-) (limited to 'routers/user/user.go') diff --git a/README.md b/README.md index e78ce8fe..cbd1f588 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Gogs(Go Git Service) is a GitHub-like clone in the Go Programming Language. Since we choose to use pure Go implementation of Git manipulation, Gogs certainly supports **ALL platforms** that Go supports, including Linux, Max OS X, and Windows with **ZERO** dependency. -##### Current version: 0.1.4 Alpha +##### Current version: 0.1.5 Alpha ## Purpose diff --git a/conf/app.ini b/conf/app.ini index 71fe81e8..ecb0d251 100644 --- a/conf/app.ini +++ b/conf/app.ini @@ -61,8 +61,14 @@ USER = PASSWD = [cache] +; Either "memory", "redis", or "memcache", default is "memory" ADAPTER = memory -CONFIG = +; For "memory" only, GC interval in seconds, default is 60 +INTERVAL = 60 +; For "redis" and "memcache", connection host address +; redis: ":6039" +; memcache: "127.0.0.1:11211" +HOST = [log] ; Either "console", "file", "conn" or "smtp", default is "console" diff --git a/gogs.go b/gogs.go index a9f7e6b5..41df7928 100644 --- a/gogs.go +++ b/gogs.go @@ -20,7 +20,7 @@ import ( // Test that go1.2 tag above is included in builds. main.go refers to this definition. const go12tag = true -const APP_VER = "0.1.4.0321" +const APP_VER = "0.1.5.0321" func init() { base.AppVer = APP_VER diff --git a/models/models.go b/models/models.go index a4550d72..8713ff28 100644 --- a/models/models.go +++ b/models/models.go @@ -35,7 +35,6 @@ func LoadModelsConfig() { } func setEngine() { - var err error switch DbCfg.Type { case "mysql": diff --git a/modules/base/conf.go b/modules/base/conf.go index 3962972c..2c3ecd72 100644 --- a/modules/base/conf.go +++ b/modules/base/conf.go @@ -133,6 +133,30 @@ func newLogService() { log.Info("Log Mode: %s(%s)", strings.Title(mode), levelName) } +func newCacheService() { + CacheAdapter = Cfg.MustValue("cache", "ADAPTER", "memory") + + switch CacheAdapter { + case "memory": + CacheConfig = fmt.Sprintf(`{"interval":%d}`, Cfg.MustInt("cache", "INTERVAL", 60)) + case "redis", "memcache": + CacheConfig = fmt.Sprintf(`{"conn":"%s"}`, Cfg.MustValue("cache", "HOST")) + default: + fmt.Printf("Unknown cache adapter: %s\n", CacheAdapter) + os.Exit(2) + } + + var err error + Cache, err = cache.NewCache(CacheAdapter, CacheConfig) + if err != nil { + fmt.Printf("Init cache system failed, adapter: %s, config: %s, %v\n", + CacheAdapter, CacheConfig, err) + os.Exit(2) + } + + log.Info("Cache Service Enabled") +} + func newMailService() { // Check mailer setting. if Cfg.MustBool("mailer", "ENABLED") { @@ -188,16 +212,6 @@ func NewConfigContext() { SecretKey = Cfg.MustValue("security", "SECRET_KEY") RunUser = Cfg.MustValue("", "RUN_USER") - CacheAdapter = Cfg.MustValue("cache", "ADAPTER") - CacheConfig = Cfg.MustValue("cache", "CONFIG") - - Cache, err = cache.NewCache(CacheAdapter, CacheConfig) - if err != nil { - fmt.Printf("Init cache system failed, adapter: %s, config: %s, %v\n", - CacheAdapter, CacheConfig, err) - os.Exit(2) - } - // Determine and create root git reposiroty path. RepoRootPath = Cfg.MustValue("repository", "ROOT") if err = os.MkdirAll(RepoRootPath, os.ModePerm); err != nil { @@ -209,6 +223,7 @@ func NewConfigContext() { func NewServices() { newService() newLogService() + newCacheService() newMailService() newRegisterMailService() } diff --git a/routers/admin/admin.go b/routers/admin/admin.go index 6d3831a9..d70af3c5 100644 --- a/routers/admin/admin.go +++ b/routers/admin/admin.go @@ -67,5 +67,8 @@ func Config(ctx *middleware.Context) { ctx.Data["Mailer"] = base.MailService } + ctx.Data["CacheAdapter"] = base.CacheAdapter + ctx.Data["CacheConfig"] = base.CacheConfig + ctx.HTML(200, "admin/config") } diff --git a/routers/user/user.go b/routers/user/user.go index 40b594ab..d38eb1ce 100644 --- a/routers/user/user.go +++ b/routers/user/user.go @@ -167,6 +167,10 @@ func SignUp(ctx *middleware.Context, form auth.RegisterForm) { ctx.Data["Email"] = u.Email ctx.Data["Hours"] = base.Service.ActiveCodeLives / 60 ctx.HTML(200, "user/active") + + if err = ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil { + log.Error("Set cache(MailResendLimit) fail: %v", err) + } return } ctx.Redirect("/user/login") @@ -247,8 +251,12 @@ func Activate(ctx *middleware.Context) { } // Resend confirmation e-mail. if base.Service.RegisterEmailConfirm { - ctx.Data["Hours"] = base.Service.ActiveCodeLives / 60 - mailer.SendActiveMail(ctx.Render, ctx.User) + if ctx.Cache.IsExist("MailResendLimit_" + ctx.User.LowerName) { + ctx.Data["ResendLimited"] = true + } else { + ctx.Data["Hours"] = base.Service.ActiveCodeLives / 60 + mailer.SendActiveMail(ctx.Render, ctx.User) + } } else { ctx.Data["ServiceNotEnabled"] = true } diff --git a/templates/admin/config.tmpl b/templates/admin/config.tmpl index 7013c201..ad32ec3f 100644 --- a/templates/admin/config.tmpl +++ b/templates/admin/config.tmpl @@ -63,6 +63,17 @@
User: {{.Mailer.User}}
+ +
+
+ Cache Configuration +
+ +
+
Cache Adapter: {{.CacheAdapter}}
+
Cache Config: {{.CacheConfig}}
+
+
{{template "base/footer" .}} \ No newline at end of file diff --git a/templates/user/active.tmpl b/templates/user/active.tmpl index fefd7d3a..47c87a59 100644 --- a/templates/user/active.tmpl +++ b/templates/user/active.tmpl @@ -6,6 +6,8 @@ {{if .IsActivatePage}} {{if .ServiceNotEnabled}}

Sorry, Register Mail Confirmation has been disabled.

+ {{else if .ResendLimited}} +

Sorry, you are sending activation e-mail too frequently, please wait 3 minutes.

{{else}}

New confirmation e-mail has been sent to {{.SignedUser.Email}}, please check your inbox within {{.Hours}} hours to complete your registeration.


-- cgit v1.2.3