Page: Manage social accounts

This commit is contained in:
Unknwon 2014-08-09 17:25:02 -07:00
parent 08c6d07aad
commit 78defd238c
14 changed files with 148 additions and 123 deletions

View file

@ -14,7 +14,7 @@ import (
"github.com/gogits/gogs/modules/auth"
"github.com/gogits/gogs/modules/base"
"github.com/gogits/gogs/modules/log"
// "github.com/gogits/gogs/modules/mailer"
"github.com/gogits/gogs/modules/mailer"
"github.com/gogits/gogs/modules/middleware"
"github.com/gogits/gogs/modules/setting"
)
@ -157,23 +157,22 @@ func SignOut(ctx *middleware.Context) {
}
func oauthSignUp(ctx *middleware.Context, sid int64) {
// ctx.Data["Title"] = "OAuth Sign Up"
// ctx.Data["PageIsSignUp"] = true
ctx.Data["Title"] = ctx.Tr("sign_up")
// if _, err := models.GetOauth2ById(sid); err != nil {
// if err == models.ErrOauth2RecordNotExist {
// ctx.Handle(404, "user.oauthSignUp(GetOauth2ById)", err)
// } else {
// ctx.Handle(500, "user.oauthSignUp(GetOauth2ById)", err)
// }
// return
// }
if _, err := models.GetOauth2ById(sid); err != nil {
if err == models.ErrOauth2RecordNotExist {
ctx.Handle(404, "GetOauth2ById", err)
} else {
ctx.Handle(500, "GetOauth2ById", err)
}
return
}
// ctx.Data["IsSocialLogin"] = true
// ctx.Data["username"] = strings.Replace(ctx.Session.Get("socialName").(string), " ", "", -1)
// ctx.Data["email"] = ctx.Session.Get("socialEmail")
// log.Trace("user.oauthSignUp(social ID): %v", ctx.Session.Get("socialId"))
// ctx.HTML(200, SIGNUP)
ctx.Data["IsSocialLogin"] = true
ctx.Data["uname"] = strings.Replace(ctx.Session.Get("socialName").(string), " ", "", -1)
ctx.Data["email"] = ctx.Session.Get("socialEmail")
log.Trace("social ID: %v", ctx.Session.Get("socialId"))
ctx.HTML(200, SIGNUP)
}
func SignUp(ctx *middleware.Context) {
@ -202,10 +201,10 @@ func SignUpPost(ctx *middleware.Context, cpt *captcha.Captcha, form auth.Registe
}
isOauth := false
// sid, isOauth := ctx.Session.Get("socialId").(int64)
// if isOauth {
// ctx.Data["IsSocialLogin"] = true
// }
sid, isOauth := ctx.Session.Get("socialId").(int64)
if isOauth {
ctx.Data["IsSocialLogin"] = true
}
// May redirect from home page.
if ctx.Query("from") == "home" {
@ -268,28 +267,28 @@ func SignUpPost(ctx *middleware.Context, cpt *captcha.Captcha, form auth.Registe
log.Trace("Account created: %s", u.Name)
// Bind social account.
// if isOauth {
// if err = models.BindUserOauth2(u.Id, sid); err != nil {
// ctx.Handle(500, "user.SignUp(BindUserOauth2)", err)
// return
// }
// ctx.Session.Delete("socialId")
// log.Trace("%s OAuth binded: %s -> %d", ctx.Req.RequestURI, form.UserName, sid)
// }
if isOauth {
if err := models.BindUserOauth2(u.Id, sid); err != nil {
ctx.Handle(500, "BindUserOauth2", err)
return
}
ctx.Session.Delete("socialId")
log.Trace("%s OAuth binded: %s -> %d", ctx.Req.RequestURI, form.UserName, sid)
}
// Send confirmation e-mail, no need for social account.
// if !isOauth && setting.Service.RegisterEmailConfirm && u.Id > 1 {
// mailer.SendRegisterMail(ctx.Render, u)
// ctx.Data["IsSendRegisterMail"] = true
// ctx.Data["Email"] = u.Email
// ctx.Data["Hours"] = setting.Service.ActiveCodeLives / 60
// ctx.HTML(200, "user/activate")
if !isOauth && setting.Service.RegisterEmailConfirm && u.Id > 1 {
mailer.SendRegisterMail(ctx.Render, u)
ctx.Data["IsSendRegisterMail"] = true
ctx.Data["Email"] = u.Email
ctx.Data["Hours"] = setting.Service.ActiveCodeLives / 60
ctx.HTML(200, "user/activate")
// if err = ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
// log.Error("Set cache(MailResendLimit) fail: %v", err)
// }
// return
// }
if err := ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
log.Error(4, "Set cache(MailResendLimit) fail: %v", err)
}
return
}
ctx.Redirect("/user/login")
}

View file

@ -200,36 +200,29 @@ func SettingsSSHKeysPost(ctx *middleware.Context, form auth.AddSSHKeyForm) {
ctx.HTML(200, SETTINGS_SSH_KEYS)
}
// func SettingSocial(ctx *middleware.Context) {
// ctx.Data["Title"] = "Social Account"
// ctx.Data["PageIsUserSetting"] = true
// ctx.Data["IsUserPageSettingSocial"] = true
// // Unbind social account.
// remove, _ := base.StrTo(ctx.Query("remove")).Int64()
// if remove > 0 {
// if err := models.DeleteOauth2ById(remove); err != nil {
// ctx.Handle(500, "user.SettingSocial(DeleteOauth2ById)", err)
// return
// }
// ctx.Flash.Success("OAuth2 has been unbinded.")
// ctx.Redirect("/user/settings/social")
// return
// }
// var err error
// ctx.Data["Socials"], err = models.GetOauthByUserId(ctx.User.Id)
// if err != nil {
// ctx.Handle(500, "user.SettingSocial(GetOauthByUserId)", err)
// return
// }
// ctx.HTML(200, SOCIAL)
// }
func SettingsSocial(ctx *middleware.Context) {
ctx.Data["Title"] = ctx.Tr("settings")
ctx.Data["PageIsUserSettings"] = true
ctx.Data["PageIsSettingsSocial"] = true
// Unbind social account.
remove, _ := com.StrTo(ctx.Query("remove")).Int64()
if remove > 0 {
if err := models.DeleteOauth2ById(remove); err != nil {
ctx.Handle(500, "DeleteOauth2ById", err)
return
}
ctx.Flash.Success(ctx.Tr("settings.unbind_success"))
ctx.Redirect("/user/settings/social")
return
}
socials, err := models.GetOauthByUserId(ctx.User.Id)
if err != nil {
ctx.Handle(500, "GetOauthByUserId", err)
return
}
ctx.Data["Socials"] = socials
ctx.HTML(200, SETTINGS_SOCIAL)
}

View file

@ -10,6 +10,7 @@ import (
"fmt"
"net/url"
"strings"
"time"
"github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/log"
@ -67,8 +68,8 @@ func SocialSignIn(ctx *middleware.Context) {
oa, err := models.GetOauth2(ui.Identity)
switch err {
case nil:
ctx.Session.Set("userId", oa.User.Id)
ctx.Session.Set("userName", oa.User.Name)
ctx.Session.Set("uid", oa.User.Id)
ctx.Session.Set("uname", oa.User.Name)
case models.ErrOauth2RecordNotExist:
raw, _ := json.Marshal(tk)
oa = &models.Oauth2{
@ -89,6 +90,11 @@ func SocialSignIn(ctx *middleware.Context) {
return
}
oa.Updated = time.Now()
if err = models.UpdateOauth2(oa); err != nil {
log.Error(4, "UpdateOauth2: %v", err)
}
ctx.Session.Set("socialId", oa.Id)
ctx.Session.Set("socialName", ui.Name)
ctx.Session.Set("socialEmail", ui.Email)