summaryrefslogtreecommitdiffstats
path: root/routers/user
diff options
context:
space:
mode:
authorUnknwon <u@gogs.io>2016-03-11 11:56:52 -0500
committerUnknwon <u@gogs.io>2016-03-11 11:56:52 -0500
commit514382e2ebfe6869268aeb919c1fa4d224687e13 (patch)
tree1aa8c4b3b1e771a5dc6f0bdd74567961570efcaa /routers/user
parentcb1eadc2768ea5ffb2967eb4262e96730c3f9ba5 (diff)
downloadgitea-514382e2ebfe6869268aeb919c1fa4d224687e13.tar.gz
gitea-514382e2ebfe6869268aeb919c1fa4d224687e13.zip
Rename module: middleware -> context
Diffstat (limited to 'routers/user')
-rw-r--r--routers/user/auth.go66
-rw-r--r--routers/user/home.go18
-rw-r--r--routers/user/profile.go16
-rw-r--r--routers/user/setting.go38
4 files changed, 89 insertions, 49 deletions
diff --git a/routers/user/auth.go b/routers/user/auth.go
index 3f37b09059..3af87c5844 100644
--- a/routers/user/auth.go
+++ b/routers/user/auth.go
@@ -5,6 +5,7 @@
package user
import (
+ "fmt"
"net/url"
"github.com/go-macaron/captcha"
@@ -12,9 +13,9 @@ import (
"github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/auth"
"github.com/gogits/gogs/modules/base"
+ "github.com/gogits/gogs/modules/context"
"github.com/gogits/gogs/modules/log"
"github.com/gogits/gogs/modules/mailer"
- "github.com/gogits/gogs/modules/middleware"
"github.com/gogits/gogs/modules/setting"
)
@@ -26,11 +27,50 @@ const (
RESET_PASSWORD base.TplName = "user/auth/reset_passwd"
)
-func SignIn(ctx *middleware.Context) {
+// AutoSignIn reads cookie and try to auto-login.
+func AutoSignIn(ctx *context.Context) (bool, error) {
+ if !models.HasEngine {
+ return false, nil
+ }
+
+ uname := ctx.GetCookie(setting.CookieUserName)
+ if len(uname) == 0 {
+ return false, nil
+ }
+
+ isSucceed := false
+ defer func() {
+ if !isSucceed {
+ log.Trace("auto-login cookie cleared: %s", uname)
+ ctx.SetCookie(setting.CookieUserName, "", -1, setting.AppSubUrl)
+ ctx.SetCookie(setting.CookieRememberName, "", -1, setting.AppSubUrl)
+ }
+ }()
+
+ u, err := models.GetUserByName(uname)
+ if err != nil {
+ if !models.IsErrUserNotExist(err) {
+ return false, fmt.Errorf("GetUserByName: %v", err)
+ }
+ return false, nil
+ }
+
+ if val, _ := ctx.GetSuperSecureCookie(
+ base.EncodeMD5(u.Rands+u.Passwd), setting.CookieRememberName); val != u.Name {
+ return false, nil
+ }
+
+ isSucceed = true
+ ctx.Session.Set("uid", u.Id)
+ ctx.Session.Set("uname", u.Name)
+ return true, nil
+}
+
+func SignIn(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("sign_in")
// Check auto-login.
- isSucceed, err := middleware.AutoSignIn(ctx)
+ isSucceed, err := AutoSignIn(ctx)
if err != nil {
ctx.Handle(500, "AutoSignIn", err)
return
@@ -49,7 +89,7 @@ func SignIn(ctx *middleware.Context) {
ctx.HTML(200, SIGNIN)
}
-func SignInPost(ctx *middleware.Context, form auth.SignInForm) {
+func SignInPost(ctx *context.Context, form auth.SignInForm) {
ctx.Data["Title"] = ctx.Tr("sign_in")
if ctx.HasError() {
@@ -85,7 +125,7 @@ func SignInPost(ctx *middleware.Context, form auth.SignInForm) {
ctx.Redirect(setting.AppSubUrl + "/")
}
-func SignOut(ctx *middleware.Context) {
+func SignOut(ctx *context.Context) {
ctx.Session.Delete("uid")
ctx.Session.Delete("uname")
ctx.Session.Delete("socialId")
@@ -96,7 +136,7 @@ func SignOut(ctx *middleware.Context) {
ctx.Redirect(setting.AppSubUrl + "/")
}
-func SignUp(ctx *middleware.Context) {
+func SignUp(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("sign_up")
ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
@@ -110,7 +150,7 @@ func SignUp(ctx *middleware.Context) {
ctx.HTML(200, SIGNUP)
}
-func SignUpPost(ctx *middleware.Context, cpt *captcha.Captcha, form auth.RegisterForm) {
+func SignUpPost(ctx *context.Context, cpt *captcha.Captcha, form auth.RegisterForm) {
ctx.Data["Title"] = ctx.Tr("sign_up")
ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
@@ -191,7 +231,7 @@ func SignUpPost(ctx *middleware.Context, cpt *captcha.Captcha, form auth.Registe
ctx.Redirect(setting.AppSubUrl + "/user/login")
}
-func Activate(ctx *middleware.Context) {
+func Activate(ctx *context.Context) {
code := ctx.Query("code")
if len(code) == 0 {
ctx.Data["IsActivatePage"] = true
@@ -243,7 +283,7 @@ func Activate(ctx *middleware.Context) {
ctx.HTML(200, ACTIVATE)
}
-func ActivateEmail(ctx *middleware.Context) {
+func ActivateEmail(ctx *context.Context) {
code := ctx.Query("code")
email_string := ctx.Query("email")
@@ -261,7 +301,7 @@ func ActivateEmail(ctx *middleware.Context) {
return
}
-func ForgotPasswd(ctx *middleware.Context) {
+func ForgotPasswd(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("auth.forgot_password")
if setting.MailService == nil {
@@ -274,7 +314,7 @@ func ForgotPasswd(ctx *middleware.Context) {
ctx.HTML(200, FORGOT_PASSWORD)
}
-func ForgotPasswdPost(ctx *middleware.Context) {
+func ForgotPasswdPost(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("auth.forgot_password")
if setting.MailService == nil {
@@ -313,7 +353,7 @@ func ForgotPasswdPost(ctx *middleware.Context) {
ctx.HTML(200, FORGOT_PASSWORD)
}
-func ResetPasswd(ctx *middleware.Context) {
+func ResetPasswd(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("auth.reset_password")
code := ctx.Query("code")
@@ -326,7 +366,7 @@ func ResetPasswd(ctx *middleware.Context) {
ctx.HTML(200, RESET_PASSWORD)
}
-func ResetPasswdPost(ctx *middleware.Context) {
+func ResetPasswdPost(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("auth.reset_password")
code := ctx.Query("code")
diff --git a/routers/user/home.go b/routers/user/home.go
index e021a6142b..2a7f219e49 100644
--- a/routers/user/home.go
+++ b/routers/user/home.go
@@ -13,7 +13,7 @@ import (
"github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/base"
- "github.com/gogits/gogs/modules/middleware"
+ "github.com/gogits/gogs/modules/context"
"github.com/gogits/gogs/modules/setting"
)
@@ -24,7 +24,7 @@ const (
ORG_HOME base.TplName = "org/home"
)
-func getDashboardContextUser(ctx *middleware.Context) *models.User {
+func getDashboardContextUser(ctx *context.Context) *models.User {
ctxUser := ctx.User
orgName := ctx.Params(":org")
if len(orgName) > 0 {
@@ -51,7 +51,7 @@ func getDashboardContextUser(ctx *middleware.Context) *models.User {
return ctxUser
}
-func retrieveFeeds(ctx *middleware.Context, ctxUserID, userID, offset int64, isProfile bool) {
+func retrieveFeeds(ctx *context.Context, ctxUserID, userID, offset int64, isProfile bool) {
actions, err := models.GetFeeds(ctxUserID, userID, offset, isProfile)
if err != nil {
ctx.Handle(500, "GetFeeds", err)
@@ -82,7 +82,7 @@ func retrieveFeeds(ctx *middleware.Context, ctxUserID, userID, offset int64, isP
ctx.Data["Feeds"] = feeds
}
-func Dashboard(ctx *middleware.Context) {
+func Dashboard(ctx *context.Context) {
ctxUser := getDashboardContextUser(ctx)
ctx.Data["Title"] = ctxUser.DisplayName() + " - " + ctx.Tr("dashboard")
ctx.Data["PageIsDashboard"] = true
@@ -147,7 +147,7 @@ func Dashboard(ctx *middleware.Context) {
ctx.HTML(200, DASHBOARD)
}
-func Issues(ctx *middleware.Context) {
+func Issues(ctx *context.Context) {
isPullList := ctx.Params(":type") == "pulls"
if isPullList {
ctx.Data["Title"] = ctx.Tr("pull_requests")
@@ -306,7 +306,7 @@ func Issues(ctx *middleware.Context) {
ctx.HTML(200, ISSUES)
}
-func ShowSSHKeys(ctx *middleware.Context, uid int64) {
+func ShowSSHKeys(ctx *context.Context, uid int64) {
keys, err := models.ListPublicKeys(uid)
if err != nil {
ctx.Handle(500, "ListPublicKeys", err)
@@ -321,9 +321,9 @@ func ShowSSHKeys(ctx *middleware.Context, uid int64) {
ctx.PlainText(200, buf.Bytes())
}
-func showOrgProfile(ctx *middleware.Context) {
+func showOrgProfile(ctx *context.Context) {
ctx.SetParams(":org", ctx.Params(":username"))
- middleware.HandleOrgAssignment(ctx)
+ context.HandleOrgAssignment(ctx)
if ctx.Written() {
return
}
@@ -366,7 +366,7 @@ func showOrgProfile(ctx *middleware.Context) {
ctx.HTML(200, ORG_HOME)
}
-func Email2User(ctx *middleware.Context) {
+func Email2User(ctx *context.Context) {
u, err := models.GetUserByEmail(ctx.Query("email"))
if err != nil {
if models.IsErrUserNotExist(err) {
diff --git a/routers/user/profile.go b/routers/user/profile.go
index d0f41262d1..847cffdd4e 100644
--- a/routers/user/profile.go
+++ b/routers/user/profile.go
@@ -11,7 +11,7 @@ import (
"github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/base"
- "github.com/gogits/gogs/modules/middleware"
+ "github.com/gogits/gogs/modules/context"
"github.com/gogits/gogs/modules/setting"
"github.com/gogits/gogs/routers/repo"
)
@@ -21,7 +21,7 @@ const (
STARS base.TplName = "user/meta/stars"
)
-func GetUserByName(ctx *middleware.Context, name string) *models.User {
+func GetUserByName(ctx *context.Context, name string) *models.User {
user, err := models.GetUserByName(name)
if err != nil {
if models.IsErrUserNotExist(err) {
@@ -35,11 +35,11 @@ func GetUserByName(ctx *middleware.Context, name string) *models.User {
}
// GetUserByParams returns user whose name is presented in URL paramenter.
-func GetUserByParams(ctx *middleware.Context) *models.User {
+func GetUserByParams(ctx *context.Context) *models.User {
return GetUserByName(ctx, ctx.Params(":username"))
}
-func Profile(ctx *middleware.Context) {
+func Profile(ctx *context.Context) {
uname := ctx.Params(":username")
// Special handle for FireFox requests favicon.ico.
if uname == "favicon.ico" {
@@ -103,7 +103,7 @@ func Profile(ctx *middleware.Context) {
ctx.HTML(200, PROFILE)
}
-func Followers(ctx *middleware.Context) {
+func Followers(ctx *context.Context) {
u := GetUserByParams(ctx)
if ctx.Written() {
return
@@ -115,7 +115,7 @@ func Followers(ctx *middleware.Context) {
repo.RenderUserCards(ctx, u.NumFollowers, u.GetFollowers, FOLLOWERS)
}
-func Following(ctx *middleware.Context) {
+func Following(ctx *context.Context) {
u := GetUserByParams(ctx)
if ctx.Written() {
return
@@ -127,11 +127,11 @@ func Following(ctx *middleware.Context) {
repo.RenderUserCards(ctx, u.NumFollowing, u.GetFollowing, FOLLOWERS)
}
-func Stars(ctx *middleware.Context) {
+func Stars(ctx *context.Context) {
}
-func Action(ctx *middleware.Context) {
+func Action(ctx *context.Context) {
u := GetUserByParams(ctx)
if ctx.Written() {
return
diff --git a/routers/user/setting.go b/routers/user/setting.go
index c704b67ce0..4b62bb11cc 100644
--- a/routers/user/setting.go
+++ b/routers/user/setting.go
@@ -15,9 +15,9 @@ import (
"github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/auth"
"github.com/gogits/gogs/modules/base"
+ "github.com/gogits/gogs/modules/context"
"github.com/gogits/gogs/modules/log"
"github.com/gogits/gogs/modules/mailer"
- "github.com/gogits/gogs/modules/middleware"
"github.com/gogits/gogs/modules/setting"
)
@@ -33,13 +33,13 @@ const (
SECURITY base.TplName = "user/security"
)
-func Settings(ctx *middleware.Context) {
+func Settings(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("settings")
ctx.Data["PageIsSettingsProfile"] = true
ctx.HTML(200, SETTINGS_PROFILE)
}
-func handleUsernameChange(ctx *middleware.Context, newName string) {
+func handleUsernameChange(ctx *context.Context, newName string) {
// Non-local users are not allowed to change their username.
if len(newName) == 0 || !ctx.User.IsLocal() {
return
@@ -74,7 +74,7 @@ func handleUsernameChange(ctx *middleware.Context, newName string) {
ctx.User.LowerName = strings.ToLower(newName)
}
-func SettingsPost(ctx *middleware.Context, form auth.UpdateProfileForm) {
+func SettingsPost(ctx *context.Context, form auth.UpdateProfileForm) {
ctx.Data["Title"] = ctx.Tr("settings")
ctx.Data["PageIsSettingsProfile"] = true
@@ -107,7 +107,7 @@ func SettingsPost(ctx *middleware.Context, form auth.UpdateProfileForm) {
}
// FIXME: limit size.
-func UpdateAvatarSetting(ctx *middleware.Context, form auth.UploadAvatarForm, ctxUser *models.User) error {
+func UpdateAvatarSetting(ctx *context.Context, form auth.UploadAvatarForm, ctxUser *models.User) error {
ctxUser.UseCustomAvatar = form.Enable
if form.Avatar != nil {
@@ -144,7 +144,7 @@ func UpdateAvatarSetting(ctx *middleware.Context, form auth.UploadAvatarForm, ct
return nil
}
-func SettingsAvatar(ctx *middleware.Context, form auth.UploadAvatarForm) {
+func SettingsAvatar(ctx *context.Context, form auth.UploadAvatarForm) {
if err := UpdateAvatarSetting(ctx, form, ctx.User); err != nil {
ctx.Flash.Error(err.Error())
} else {
@@ -154,7 +154,7 @@ func SettingsAvatar(ctx *middleware.Context, form auth.UploadAvatarForm) {
ctx.Redirect(setting.AppSubUrl + "/user/settings")
}
-func SettingsDeleteAvatar(ctx *middleware.Context) {
+func SettingsDeleteAvatar(ctx *context.Context) {
if err := ctx.User.DeleteAvatar(); err != nil {
ctx.Flash.Error(err.Error())
}
@@ -162,13 +162,13 @@ func SettingsDeleteAvatar(ctx *middleware.Context) {
ctx.Redirect(setting.AppSubUrl + "/user/settings")
}
-func SettingsPassword(ctx *middleware.Context) {
+func SettingsPassword(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("settings")
ctx.Data["PageIsSettingsPassword"] = true
ctx.HTML(200, SETTINGS_PASSWORD)
}
-func SettingsPasswordPost(ctx *middleware.Context, form auth.ChangePasswordForm) {
+func SettingsPasswordPost(ctx *context.Context, form auth.ChangePasswordForm) {
ctx.Data["Title"] = ctx.Tr("settings")
ctx.Data["PageIsSettingsPassword"] = true
@@ -196,7 +196,7 @@ func SettingsPasswordPost(ctx *middleware.Context, form auth.ChangePasswordForm)
ctx.Redirect(setting.AppSubUrl + "/user/settings/password")
}
-func SettingsEmails(ctx *middleware.Context) {
+func SettingsEmails(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("settings")
ctx.Data["PageIsSettingsEmails"] = true
@@ -210,7 +210,7 @@ func SettingsEmails(ctx *middleware.Context) {
ctx.HTML(200, SETTINGS_EMAILS)
}
-func SettingsEmailPost(ctx *middleware.Context, form auth.AddEmailForm) {
+func SettingsEmailPost(ctx *context.Context, form auth.AddEmailForm) {
ctx.Data["Title"] = ctx.Tr("settings")
ctx.Data["PageIsSettingsEmails"] = true
@@ -269,7 +269,7 @@ func SettingsEmailPost(ctx *middleware.Context, form auth.AddEmailForm) {
ctx.Redirect(setting.AppSubUrl + "/user/settings/email")
}
-func DeleteEmail(ctx *middleware.Context) {
+func DeleteEmail(ctx *context.Context) {
if err := models.DeleteEmailAddress(&models.EmailAddress{ID: ctx.QueryInt64("id")}); err != nil {
ctx.Handle(500, "DeleteEmail", err)
return
@@ -282,7 +282,7 @@ func DeleteEmail(ctx *middleware.Context) {
})
}
-func SettingsSSHKeys(ctx *middleware.Context) {
+func SettingsSSHKeys(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("settings")
ctx.Data["PageIsSettingsSSHKeys"] = true
@@ -296,7 +296,7 @@ func SettingsSSHKeys(ctx *middleware.Context) {
ctx.HTML(200, SETTINGS_SSH_KEYS)
}
-func SettingsSSHKeysPost(ctx *middleware.Context, form auth.AddSSHKeyForm) {
+func SettingsSSHKeysPost(ctx *context.Context, form auth.AddSSHKeyForm) {
ctx.Data["Title"] = ctx.Tr("settings")
ctx.Data["PageIsSettingsSSHKeys"] = true
@@ -342,7 +342,7 @@ func SettingsSSHKeysPost(ctx *middleware.Context, form auth.AddSSHKeyForm) {
ctx.Redirect(setting.AppSubUrl + "/user/settings/ssh")
}
-func DeleteSSHKey(ctx *middleware.Context) {
+func DeleteSSHKey(ctx *context.Context) {
if err := models.DeletePublicKey(ctx.User, ctx.QueryInt64("id")); err != nil {
ctx.Flash.Error("DeletePublicKey: " + err.Error())
} else {
@@ -354,7 +354,7 @@ func DeleteSSHKey(ctx *middleware.Context) {
})
}
-func SettingsApplications(ctx *middleware.Context) {
+func SettingsApplications(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("settings")
ctx.Data["PageIsSettingsApplications"] = true
@@ -368,7 +368,7 @@ func SettingsApplications(ctx *middleware.Context) {
ctx.HTML(200, SETTINGS_APPLICATIONS)
}
-func SettingsApplicationsPost(ctx *middleware.Context, form auth.NewAccessTokenForm) {
+func SettingsApplicationsPost(ctx *context.Context, form auth.NewAccessTokenForm) {
ctx.Data["Title"] = ctx.Tr("settings")
ctx.Data["PageIsSettingsApplications"] = true
@@ -398,7 +398,7 @@ func SettingsApplicationsPost(ctx *middleware.Context, form auth.NewAccessTokenF
ctx.Redirect(setting.AppSubUrl + "/user/settings/applications")
}
-func SettingsDeleteApplication(ctx *middleware.Context) {
+func SettingsDeleteApplication(ctx *context.Context) {
if err := models.DeleteAccessTokenByID(ctx.QueryInt64("id")); err != nil {
ctx.Flash.Error("DeleteAccessTokenByID: " + err.Error())
} else {
@@ -410,7 +410,7 @@ func SettingsDeleteApplication(ctx *middleware.Context) {
})
}
-func SettingsDelete(ctx *middleware.Context) {
+func SettingsDelete(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("settings")
ctx.Data["PageIsSettingsDelete"] = true