Finish verify email

This commit is contained in:
Unknown 2014-03-19 12:50:44 -04:00
parent c6e12d2568
commit 35d473f04a
13 changed files with 182 additions and 62 deletions

View file

@ -131,15 +131,15 @@ func newMailService() {
}
}
func newRegisterService() {
func newRegisterMailService() {
if !Cfg.MustBool("service", "REGISTER_EMAIL_CONFIRM") {
return
} else if MailService == nil {
log.Warn("Register Service: Mail Service is not enabled")
log.Warn("Register Mail Service: Mail Service is not enabled")
return
}
Service.RegisterEmailConfirm = true
log.Info("Register Service Enabled")
log.Info("Register Mail Service Enabled")
}
func init() {
@ -177,5 +177,5 @@ func init() {
newService()
newLogService()
newMailService()
newRegisterService()
newRegisterMailService()
}

View file

@ -8,6 +8,7 @@ import (
"container/list"
"fmt"
"html/template"
"strings"
"time"
)
@ -54,4 +55,7 @@ var TemplateFuncs template.FuncMap = map[string]interface{}{
"ActionDesc": ActionDesc,
"DateFormat": DateFormat,
"List": List,
"Mail2Domain": func(mail string) string {
return "mail." + strings.Split(mail, "@")[1]
},
}

View file

@ -36,6 +36,35 @@ func GetRandomString(n int) string {
return string(bytes)
}
// verify time limit code
func VerifyTimeLimitCode(data string, minutes int, code string) bool {
if len(code) <= 18 {
return false
}
// split code
start := code[:12]
lives := code[12:18]
if d, err := StrTo(lives).Int(); err == nil {
minutes = d
}
// right active code
retCode := CreateTimeLimitCode(data, minutes, start)
if retCode == code && minutes > 0 {
// check time is expired or not
before, _ := DateParse(start, "YmdHi")
now := time.Now()
if before.Add(time.Minute*time.Duration(minutes)).Unix() > now.Unix() {
return true
}
}
return false
}
const TimeLimitCodeLength = 12 + 6 + 40
// create a time limit code
// code format: 12 length date time string + 6 minutes string + 40 sha1 encoded string
func CreateTimeLimitCode(data string, minutes int, startInf interface{}) string {
@ -283,16 +312,24 @@ func DateFormat(t time.Time, format string) string {
return t.Format(format)
}
type argInt []int
// convert string to specify type
func (a argInt) Get(i int, args ...int) (r int) {
if i >= 0 && i < len(a) {
r = a[i]
type StrTo string
func (f StrTo) Exist() bool {
return string(f) != string(0x1E)
}
func (f StrTo) Int() (int, error) {
v, err := strconv.ParseInt(f.String(), 10, 32)
return int(v), err
}
func (f StrTo) String() string {
if f.Exist() {
return string(f)
}
if len(args) > 0 {
r = args[0]
}
return
return ""
}
// convert any type to string
@ -334,6 +371,18 @@ func ToStr(value interface{}, args ...int) (s string) {
return s
}
type argInt []int
func (a argInt) Get(i int, args ...int) (r int) {
if i >= 0 && i < len(a) {
r = a[i]
}
if len(args) > 0 {
r = args[0]
}
return
}
type Actioner interface {
GetOpType() int
GetActUserName() string

View file

@ -37,9 +37,9 @@ func GetMailTmplData(user *models.User) map[interface{}]interface{} {
// create a time limit code for user active
func CreateUserActiveCode(user *models.User, startInf interface{}) string {
hours := base.Service.ActiveCodeLives / 60
minutes := base.Service.ActiveCodeLives
data := base.ToStr(user.Id) + user.Email + user.LowerName + user.Passwd + user.Rands
code := base.CreateTimeLimitCode(data, hours, startInf)
code := base.CreateTimeLimitCode(data, minutes, startInf)
// add tail hex username
code += hex.EncodeToString([]byte(user.LowerName))
@ -70,11 +70,11 @@ func SendRegisterMail(r *middleware.Render, user *models.User) {
func SendActiveMail(r *middleware.Render, user *models.User) {
code := CreateUserActiveCode(user, nil)
subject := "Verify your email address"
subject := "Verify your e-mail address"
data := GetMailTmplData(user)
data["Code"] = code
body, err := r.HTMLString("mail/auth/active_email.html", data)
body, err := r.HTMLString("mail/auth/active_email", data)
if err != nil {
log.Error("mail.SendActiveMail(fail to render): %v", err)
return

View file

@ -6,6 +6,8 @@ package middleware
import (
"github.com/codegangsta/martini"
"github.com/gogits/gogs/modules/base"
)
// SignInRequire requires user to sign in.
@ -16,10 +18,10 @@ func SignInRequire(redirect bool) martini.Handler {
ctx.Redirect("/")
}
return
} else if !ctx.User.IsActive {
// ctx.Data["Title"] = "Activate Your Account"
// ctx.Render.HTML(200, "user/active", ctx.Data)
// return
} else if !ctx.User.IsActive && base.Service.RegisterEmailConfirm {
ctx.Data["Title"] = "Activate Your Account"
ctx.Render.HTML(200, "user/active", ctx.Data)
return
}
}
}