123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443 |
- package user
- import (
- "net/url"
- "strings"
- "github.com/macaron-contrib/captcha"
- "github.com/gogits/gogs/models"
- "github.com/gogits/gogs/modules/auth"
- "github.com/gogits/gogs/modules/base"
- "github.com/gogits/gogs/modules/log"
-
- "github.com/gogits/gogs/modules/middleware"
- "github.com/gogits/gogs/modules/setting"
- )
- const (
- SIGNIN base.TplName = "user/signin"
- SIGNUP base.TplName = "user/signup"
- DELETE base.TplName = "user/delete"
- ACTIVATE base.TplName = "user/activate"
- FORGOT_PASSWORD base.TplName = "user/forgot_passwd"
- RESET_PASSWORD base.TplName = "user/reset_passwd"
- )
- func SignIn(ctx *middleware.Context) {
- ctx.Data["Title"] = ctx.Tr("sign_in")
-
-
-
-
-
-
-
-
-
-
- uname := ctx.GetCookie(setting.CookieUserName)
- if len(uname) == 0 {
- ctx.HTML(200, SIGNIN)
- return
- }
- isSucceed := false
- defer func() {
- if !isSucceed {
- log.Trace("auto-login cookie cleared: %s", uname)
- ctx.SetCookie(setting.CookieUserName, "", -1)
- ctx.SetCookie(setting.CookieRememberName, "", -1)
- return
- }
- }()
- u, err := models.GetUserByName(uname)
- if err != nil {
- if err != models.ErrUserNotExist {
- ctx.Handle(500, "GetUserByName", err)
- }
- return
- }
- if val, _ := ctx.GetSuperSecureCookie(
- base.EncodeMd5(u.Rands+u.Passwd), setting.CookieRememberName); val != u.Name {
- ctx.HTML(200, SIGNIN)
- return
- }
- isSucceed = true
- ctx.Session.Set("uid", u.Id)
- ctx.Session.Set("uname", u.Name)
- if redirectTo, _ := url.QueryUnescape(ctx.GetCookie("redirect_to")); len(redirectTo) > 0 {
- ctx.SetCookie("redirect_to", "", -1)
- ctx.Redirect(redirectTo)
- return
- }
- ctx.Redirect("/")
- }
- func SignInPost(ctx *middleware.Context, form auth.SignInForm) {
- ctx.Data["Title"] = ctx.Tr("sign_in")
-
-
-
-
-
-
-
- if ctx.HasError() {
- ctx.HTML(200, SIGNIN)
- return
- }
- u, err := models.UserSignIn(form.UserName, form.Password)
- if err != nil {
- if err == models.ErrUserNotExist {
- ctx.RenderWithErr(ctx.Tr("form.username_password_incorrect"), SIGNIN, &form)
- return
- }
- ctx.Handle(500, "UserSignIn", err)
- return
- }
- if form.Remember {
- days := 86400 * setting.LogInRememberDays
- ctx.SetCookie(setting.CookieUserName, u.Name, days)
- ctx.SetSuperSecureCookie(base.EncodeMd5(u.Rands+u.Passwd),
- setting.CookieRememberName, u.Name, days)
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- ctx.Session.Set("uid", u.Id)
- ctx.Session.Set("uname", u.Name)
- if redirectTo, _ := url.QueryUnescape(ctx.GetCookie("redirect_to")); len(redirectTo) > 0 {
- ctx.SetCookie("redirect_to", "", -1)
- ctx.Redirect(redirectTo)
- return
- }
- ctx.Redirect("/")
- }
- func SignOut(ctx *middleware.Context) {
- ctx.Session.Delete("uid")
- ctx.Session.Delete("uname")
-
-
-
- ctx.SetCookie(setting.CookieUserName, "", -1)
- ctx.SetCookie(setting.CookieRememberName, "", -1)
- ctx.Redirect("/")
- }
- func SignUp(ctx *middleware.Context) {
- ctx.Data["Title"] = ctx.Tr("sign_up")
- if setting.Service.DisableRegistration {
- ctx.Data["DisableRegistration"] = true
- ctx.HTML(200, SIGNUP)
- return
- }
-
-
-
-
- ctx.HTML(200, SIGNUP)
- }
- func SignUpPost(ctx *middleware.Context, cpt *captcha.Captcha, form auth.RegisterForm) {
- ctx.Data["Title"] = ctx.Tr("sign_up")
- if setting.Service.DisableRegistration {
- ctx.Error(403)
- return
- }
- isOauth := false
-
-
-
-
-
- if ctx.Query("from") == "home" {
-
- ctx.Data["Err_UserName"] = false
- ctx.Data["Err_Email"] = false
-
- uname := ctx.Query("uname")
- i := strings.Index(uname, "@")
- if i > -1 {
- ctx.Data["email"] = uname
- ctx.Data["uname"] = uname[:i]
- } else {
- ctx.Data["uname"] = uname
- }
- ctx.Data["password"] = ctx.Query("password")
- ctx.HTML(200, SIGNUP)
- return
- }
- if ctx.HasError() {
- ctx.HTML(200, SIGNUP)
- return
- }
- if !cpt.VerifyReq(ctx.Req) {
- ctx.Data["Err_Captcha"] = true
- ctx.RenderWithErr(ctx.Tr("form.captcha_incorrect"), SIGNUP, &form)
- return
- } else if form.Password != form.Retype {
- ctx.Data["Err_Password"] = true
- ctx.RenderWithErr(ctx.Tr("form.password_not_match"), SIGNUP, &form)
- return
- }
- u := &models.User{
- Name: form.UserName,
- Email: form.Email,
- Passwd: form.Password,
- IsActive: !setting.Service.RegisterEmailConfirm || isOauth,
- }
- if err := models.CreateUser(u); err != nil {
- switch err {
- case models.ErrUserAlreadyExist:
- ctx.Data["Err_UserName"] = true
- ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), SIGNUP, &form)
- case models.ErrEmailAlreadyUsed:
- ctx.Data["Err_Email"] = true
- ctx.RenderWithErr(ctx.Tr("form.email_been_used"), SIGNUP, &form)
- case models.ErrUserNameIllegal:
- ctx.Data["Err_UserName"] = true
- ctx.RenderWithErr(ctx.Tr("form.illegal_username"), SIGNUP, &form)
- default:
- ctx.Handle(500, "CreateUser", err)
- }
- return
- }
- log.Trace("Account created: %s", u.Name)
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- ctx.Redirect("/user/login")
- }
- func Activate(ctx *middleware.Context) {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
- func ForgotPasswd(ctx *middleware.Context) {
- ctx.Data["Title"] = "Forgot Password"
- if setting.MailService == nil {
- ctx.Data["IsResetDisable"] = true
- ctx.HTML(200, FORGOT_PASSWORD)
- return
- }
- ctx.Data["IsResetRequest"] = true
- ctx.HTML(200, FORGOT_PASSWORD)
- }
- func ForgotPasswdPost(ctx *middleware.Context) {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
- func ResetPasswd(ctx *middleware.Context) {
- ctx.Data["Title"] = "Reset Password"
- code := ctx.Query("code")
- if len(code) == 0 {
- ctx.Error(404)
- return
- }
- ctx.Data["Code"] = code
- ctx.Data["IsResetForm"] = true
- ctx.HTML(200, RESET_PASSWORD)
- }
- func ResetPasswdPost(ctx *middleware.Context) {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
|