mirror of
https://github.com/go-gitea/gitea
synced 2025-12-07 13:28:25 +00:00
fix
This commit is contained in:
@@ -0,0 +1,184 @@
|
||||
// Copyright 2014 The Gogs Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
// Render user setting page (email, website modify)
|
||||
func Setting(ctx *middleware.Context, form auth.UpdateProfileForm) {
|
||||
ctx.Data["Title"] = "Setting"
|
||||
ctx.Data["PageIsUserSetting"] = true // For navbar arrow.
|
||||
ctx.Data["IsUserPageSetting"] = true // For setting nav highlight.
|
||||
|
||||
user := ctx.User
|
||||
ctx.Data["Owner"] = user
|
||||
|
||||
if ctx.Req.Method == "GET" || ctx.HasError() {
|
||||
ctx.HTML(200, "user/setting")
|
||||
return
|
||||
}
|
||||
|
||||
// Check if user name has been changed.
|
||||
if user.Name != form.UserName {
|
||||
isExist, err := models.IsUserExist(form.UserName)
|
||||
if err != nil {
|
||||
ctx.Handle(404, "user.Setting(update: check existence)", err)
|
||||
return
|
||||
} else if isExist {
|
||||
ctx.RenderWithErr("User name has been taken.", "user/setting", &form)
|
||||
return
|
||||
} else if err = models.ChangeUserName(user, form.UserName); err != nil {
|
||||
ctx.Handle(404, "user.Setting(change user name)", err)
|
||||
return
|
||||
}
|
||||
log.Trace("%s User name changed: %s -> %s", ctx.Req.RequestURI, user.Name, form.UserName)
|
||||
|
||||
user.Name = form.UserName
|
||||
}
|
||||
|
||||
user.Email = form.Email
|
||||
user.Website = form.Website
|
||||
user.Location = form.Location
|
||||
user.Avatar = base.EncodeMd5(form.Avatar)
|
||||
user.AvatarEmail = form.Avatar
|
||||
if err := models.UpdateUser(user); err != nil {
|
||||
ctx.Handle(200, "setting.Setting", err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Data["IsSuccess"] = true
|
||||
ctx.HTML(200, "user/setting")
|
||||
log.Trace("%s User setting updated: %s", ctx.Req.RequestURI, ctx.User.LowerName)
|
||||
}
|
||||
|
||||
func SettingPassword(ctx *middleware.Context, form auth.UpdatePasswdForm) {
|
||||
ctx.Data["Title"] = "Password"
|
||||
ctx.Data["PageIsUserSetting"] = true
|
||||
ctx.Data["IsUserPageSettingPasswd"] = true
|
||||
|
||||
if ctx.Req.Method == "GET" {
|
||||
ctx.HTML(200, "user/password")
|
||||
return
|
||||
}
|
||||
|
||||
user := ctx.User
|
||||
newUser := &models.User{Passwd: form.NewPasswd}
|
||||
newUser.EncodePasswd()
|
||||
if user.Passwd != newUser.Passwd {
|
||||
ctx.Data["HasError"] = true
|
||||
ctx.Data["ErrorMsg"] = "Old password is not correct"
|
||||
} else if form.NewPasswd != form.RetypePasswd {
|
||||
ctx.Data["HasError"] = true
|
||||
ctx.Data["ErrorMsg"] = "New password and re-type password are not same"
|
||||
} else {
|
||||
newUser.Salt = models.GetUserSalt()
|
||||
user.Passwd = newUser.Passwd
|
||||
if err := models.UpdateUser(user); err != nil {
|
||||
ctx.Handle(200, "setting.SettingPassword", err)
|
||||
return
|
||||
}
|
||||
ctx.Data["IsSuccess"] = true
|
||||
}
|
||||
|
||||
ctx.Data["Owner"] = user
|
||||
ctx.HTML(200, "user/password")
|
||||
log.Trace("%s User password updated: %s", ctx.Req.RequestURI, ctx.User.LowerName)
|
||||
}
|
||||
|
||||
func SettingSSHKeys(ctx *middleware.Context, form auth.AddSSHKeyForm) {
|
||||
ctx.Data["Title"] = "SSH Keys"
|
||||
|
||||
// Delete SSH key.
|
||||
if ctx.Req.Method == "DELETE" || ctx.Query("_method") == "DELETE" {
|
||||
id, err := strconv.ParseInt(ctx.Query("id"), 10, 64)
|
||||
if err != nil {
|
||||
log.Error("ssh.DelPublicKey: %v", err)
|
||||
ctx.JSON(200, map[string]interface{}{
|
||||
"ok": false,
|
||||
"err": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
k := &models.PublicKey{
|
||||
Id: id,
|
||||
OwnerId: ctx.User.Id,
|
||||
}
|
||||
|
||||
if err = models.DeletePublicKey(k); err != nil {
|
||||
log.Error("ssh.DelPublicKey: %v", err)
|
||||
ctx.JSON(200, map[string]interface{}{
|
||||
"ok": false,
|
||||
"err": err.Error(),
|
||||
})
|
||||
} else {
|
||||
log.Trace("%s User SSH key deleted: %s", ctx.Req.RequestURI, ctx.User.LowerName)
|
||||
ctx.JSON(200, map[string]interface{}{
|
||||
"ok": true,
|
||||
})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Add new SSH key.
|
||||
if ctx.Req.Method == "POST" {
|
||||
if hasErr, ok := ctx.Data["HasError"]; ok && hasErr.(bool) {
|
||||
ctx.HTML(200, "user/publickey")
|
||||
return
|
||||
}
|
||||
|
||||
k := &models.PublicKey{OwnerId: ctx.User.Id,
|
||||
Name: form.KeyName,
|
||||
Content: form.KeyContent,
|
||||
}
|
||||
|
||||
if err := models.AddPublicKey(k); err != nil {
|
||||
if err.Error() == models.ErrKeyAlreadyExist.Error() {
|
||||
ctx.RenderWithErr("Public key name has been used", "user/publickey", &form)
|
||||
return
|
||||
}
|
||||
ctx.Handle(200, "ssh.AddPublicKey", err)
|
||||
log.Trace("%s User SSH key added: %s", ctx.Req.RequestURI, ctx.User.LowerName)
|
||||
return
|
||||
} else {
|
||||
ctx.Data["AddSSHKeySuccess"] = true
|
||||
}
|
||||
}
|
||||
|
||||
// List existed SSH keys.
|
||||
keys, err := models.ListPublicKey(ctx.User.Id)
|
||||
if err != nil {
|
||||
ctx.Handle(200, "ssh.ListPublicKey", err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Data["PageIsUserSetting"] = true
|
||||
ctx.Data["IsUserPageSettingSSH"] = true
|
||||
ctx.Data["Keys"] = keys
|
||||
ctx.HTML(200, "user/publickey")
|
||||
}
|
||||
|
||||
func SettingNotification(ctx *middleware.Context) {
|
||||
// TODO: user setting notification
|
||||
ctx.Data["Title"] = "Notification"
|
||||
ctx.Data["PageIsUserSetting"] = true
|
||||
ctx.Data["IsUserPageSettingNotify"] = true
|
||||
ctx.HTML(200, "user/notification")
|
||||
}
|
||||
|
||||
func SettingSecurity(ctx *middleware.Context) {
|
||||
// TODO: user setting security
|
||||
ctx.Data["Title"] = "Security"
|
||||
ctx.Data["PageIsUserSetting"] = true
|
||||
ctx.Data["IsUserPageSettingSecurity"] = true
|
||||
ctx.HTML(200, "user/security")
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
// Copyright 2014 The Gogs Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"code.google.com/p/goauth2/oauth"
|
||||
|
||||
"github.com/gogits/gogs/models"
|
||||
"github.com/gogits/gogs/modules/base"
|
||||
"github.com/gogits/gogs/modules/log"
|
||||
"github.com/gogits/gogs/modules/middleware"
|
||||
"github.com/gogits/gogs/modules/oauth2"
|
||||
)
|
||||
|
||||
type SocialConnector interface {
|
||||
Identity() string
|
||||
Type() int
|
||||
Name() string
|
||||
Email() string
|
||||
Token() string
|
||||
}
|
||||
|
||||
type SocialGithub struct {
|
||||
data struct {
|
||||
Id int `json:"id"`
|
||||
Name string `json:"login"`
|
||||
Email string `json:"email"`
|
||||
}
|
||||
WebToken *oauth.Token
|
||||
}
|
||||
|
||||
func (s *SocialGithub) Identity() string {
|
||||
return strconv.Itoa(s.data.Id)
|
||||
}
|
||||
|
||||
func (s *SocialGithub) Type() int {
|
||||
return models.OT_GITHUB
|
||||
}
|
||||
|
||||
func (s *SocialGithub) Name() string {
|
||||
return s.data.Name
|
||||
}
|
||||
|
||||
func (s *SocialGithub) Email() string {
|
||||
return s.data.Email
|
||||
}
|
||||
|
||||
func (s *SocialGithub) Token() string {
|
||||
data, _ := json.Marshal(s.WebToken)
|
||||
return string(data)
|
||||
}
|
||||
|
||||
// Github API refer: https://developer.github.com/v3/users/
|
||||
func (s *SocialGithub) Update() error {
|
||||
scope := "https://api.github.com/user"
|
||||
transport := &oauth.Transport{
|
||||
Token: s.WebToken,
|
||||
}
|
||||
log.Debug("update github info")
|
||||
r, err := transport.Client().Get(scope)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer r.Body.Close()
|
||||
return json.NewDecoder(r.Body).Decode(&s.data)
|
||||
}
|
||||
|
||||
func extractPath(next string) string {
|
||||
n, err := url.Parse(next)
|
||||
if err != nil {
|
||||
return "/"
|
||||
}
|
||||
return n.Path
|
||||
}
|
||||
|
||||
// github && google && ...
|
||||
func SocialSignIn(ctx *middleware.Context, tokens oauth2.Tokens) {
|
||||
var socid int64
|
||||
var ok bool
|
||||
next := extractPath(ctx.Query("next"))
|
||||
log.Debug("social signed check %s", next)
|
||||
if socid, ok = ctx.Session.Get("socialId").(int64); ok && socid != 0 {
|
||||
// already login
|
||||
ctx.Redirect(next)
|
||||
log.Info("login soc id: %v", socid)
|
||||
return
|
||||
}
|
||||
config := &oauth.Config{
|
||||
//ClientId: base.OauthService.Github.ClientId,
|
||||
//ClientSecret: base.OauthService.Github.ClientSecret, // FIXME: I don't know why compile error here
|
||||
ClientId: "09383403ff2dc16daaa1",
|
||||
ClientSecret: "0e4aa0c3630df396cdcea01a9d45cacf79925fea",
|
||||
RedirectURL: strings.TrimSuffix(base.AppUrl, "/") + ctx.Req.URL.RequestURI(),
|
||||
Scope: base.OauthService.GitHub.Scopes,
|
||||
AuthURL: "https://github.com/login/oauth/authorize",
|
||||
TokenURL: "https://github.com/login/oauth/access_token",
|
||||
}
|
||||
transport := &oauth.Transport{
|
||||
Config: config,
|
||||
Transport: http.DefaultTransport,
|
||||
}
|
||||
code := ctx.Query("code")
|
||||
if code == "" {
|
||||
// redirect to social login page
|
||||
ctx.Redirect(config.AuthCodeURL(next))
|
||||
return
|
||||
}
|
||||
|
||||
// handle call back
|
||||
tk, err := transport.Exchange(code)
|
||||
if err != nil {
|
||||
log.Error("oauth2 handle callback error: %v", err)
|
||||
return // FIXME, need error page 501
|
||||
}
|
||||
next = extractPath(ctx.Query("state"))
|
||||
log.Debug("success token: %v", tk)
|
||||
|
||||
gh := &SocialGithub{WebToken: tk}
|
||||
if err = gh.Update(); err != nil {
|
||||
// FIXME: handle error page 501
|
||||
log.Error("connect with github error: %s", err)
|
||||
return
|
||||
}
|
||||
var soc SocialConnector = gh
|
||||
log.Info("login: %s", soc.Name())
|
||||
oa, err := models.GetOauth2(soc.Identity())
|
||||
switch err {
|
||||
case nil:
|
||||
ctx.Session.Set("userId", oa.User.Id)
|
||||
ctx.Session.Set("userName", oa.User.Name)
|
||||
case models.ErrOauth2RecordNotExists:
|
||||
oa = &models.Oauth2{}
|
||||
oa.Uid = 0
|
||||
oa.Type = soc.Type()
|
||||
oa.Token = soc.Token()
|
||||
oa.Identity = soc.Identity()
|
||||
log.Debug("oa: %v", oa)
|
||||
if err = models.AddOauth2(oa); err != nil {
|
||||
log.Error("add oauth2 %v", err) // 501
|
||||
return
|
||||
}
|
||||
case models.ErrOauth2NotAssociatedWithUser:
|
||||
// ignore it. judge in /usr/login page
|
||||
default:
|
||||
log.Error(err.Error()) // FIXME: handle error page
|
||||
return
|
||||
}
|
||||
ctx.Session.Set("socialId", oa.Id)
|
||||
log.Debug("socialId: %v", oa.Id)
|
||||
ctx.Redirect(next)
|
||||
}
|
||||
@@ -0,0 +1,514 @@
|
||||
// Copyright 2014 The Gogs Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/go-martini/martini"
|
||||
|
||||
"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/mailer"
|
||||
"github.com/gogits/gogs/modules/middleware"
|
||||
)
|
||||
|
||||
func Dashboard(ctx *middleware.Context) {
|
||||
ctx.Data["Title"] = "Dashboard"
|
||||
ctx.Data["PageIsUserDashboard"] = true
|
||||
repos, err := models.GetRepositories(&models.User{Id: ctx.User.Id})
|
||||
if err != nil {
|
||||
ctx.Handle(200, "user.Dashboard", err)
|
||||
return
|
||||
}
|
||||
ctx.Data["MyRepos"] = repos
|
||||
|
||||
feeds, err := models.GetFeeds(ctx.User.Id, 0, false)
|
||||
if err != nil {
|
||||
ctx.Handle(200, "user.Dashboard", err)
|
||||
return
|
||||
}
|
||||
ctx.Data["Feeds"] = feeds
|
||||
ctx.HTML(200, "user/dashboard")
|
||||
}
|
||||
|
||||
func Profile(ctx *middleware.Context, params martini.Params) {
|
||||
ctx.Data["Title"] = "Profile"
|
||||
|
||||
// TODO: Need to check view self or others.
|
||||
user, err := models.GetUserByName(params["username"])
|
||||
if err != nil {
|
||||
ctx.Handle(200, "user.Profile", err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Data["Owner"] = user
|
||||
|
||||
tab := ctx.Query("tab")
|
||||
ctx.Data["TabName"] = tab
|
||||
|
||||
switch tab {
|
||||
case "activity":
|
||||
feeds, err := models.GetFeeds(user.Id, 0, true)
|
||||
if err != nil {
|
||||
ctx.Handle(200, "user.Profile", err)
|
||||
return
|
||||
}
|
||||
ctx.Data["Feeds"] = feeds
|
||||
default:
|
||||
repos, err := models.GetRepositories(user)
|
||||
if err != nil {
|
||||
ctx.Handle(200, "user.Profile", err)
|
||||
return
|
||||
}
|
||||
ctx.Data["Repos"] = repos
|
||||
}
|
||||
|
||||
ctx.Data["PageIsUserProfile"] = true
|
||||
ctx.HTML(200, "user/profile")
|
||||
}
|
||||
|
||||
func SignIn(ctx *middleware.Context, form auth.LogInForm) {
|
||||
ctx.Data["Title"] = "Log In"
|
||||
|
||||
if ctx.Req.Method == "GET" {
|
||||
if base.OauthService != nil {
|
||||
ctx.Data["OauthEnabled"] = true
|
||||
ctx.Data["OauthGitHubEnabled"] = base.OauthService.GitHub.Enabled
|
||||
}
|
||||
|
||||
// Check auto-login.
|
||||
userName := ctx.GetCookie(base.CookieUserName)
|
||||
if len(userName) == 0 {
|
||||
ctx.HTML(200, "user/signin")
|
||||
return
|
||||
}
|
||||
|
||||
isSucceed := false
|
||||
defer func() {
|
||||
if !isSucceed {
|
||||
log.Trace("%s auto-login cookie cleared: %s", ctx.Req.RequestURI, userName)
|
||||
ctx.SetCookie(base.CookieUserName, "", -1)
|
||||
ctx.SetCookie(base.CookieRememberName, "", -1)
|
||||
}
|
||||
}()
|
||||
|
||||
user, err := models.GetUserByName(userName)
|
||||
if err != nil {
|
||||
ctx.HTML(200, "user/signin")
|
||||
return
|
||||
}
|
||||
|
||||
secret := base.EncodeMd5(user.Rands + user.Passwd)
|
||||
value, _ := ctx.GetSecureCookie(secret, base.CookieRememberName)
|
||||
if value != user.Name {
|
||||
ctx.HTML(200, "user/signin")
|
||||
return
|
||||
}
|
||||
|
||||
isSucceed = true
|
||||
ctx.Session.Set("userId", user.Id)
|
||||
ctx.Session.Set("userName", user.Name)
|
||||
redirectTo, _ := url.QueryUnescape(ctx.GetCookie("redirect_to"))
|
||||
if len(redirectTo) > 0 {
|
||||
ctx.SetCookie("redirect_to", "", -1)
|
||||
ctx.Redirect(redirectTo)
|
||||
} else {
|
||||
ctx.Redirect("/")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if ctx.HasError() {
|
||||
ctx.HTML(200, "user/signin")
|
||||
return
|
||||
}
|
||||
|
||||
user, err := models.LoginUserPlain(form.UserName, form.Password)
|
||||
if err != nil {
|
||||
if err == models.ErrUserNotExist {
|
||||
log.Trace("%s Log in failed: %s/%s", ctx.Req.RequestURI, form.UserName, form.Password)
|
||||
ctx.RenderWithErr("Username or password is not correct", "user/signin", &form)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Handle(200, "user.SignIn", err)
|
||||
return
|
||||
}
|
||||
|
||||
if form.Remember == "on" {
|
||||
secret := base.EncodeMd5(user.Rands + user.Passwd)
|
||||
days := 86400 * base.LogInRememberDays
|
||||
ctx.SetCookie(base.CookieUserName, user.Name, days)
|
||||
ctx.SetSecureCookie(secret, base.CookieRememberName, user.Name, days)
|
||||
}
|
||||
|
||||
ctx.Session.Set("userId", user.Id)
|
||||
ctx.Session.Set("userName", user.Name)
|
||||
redirectTo, _ := url.QueryUnescape(ctx.GetCookie("redirect_to"))
|
||||
if len(redirectTo) > 0 {
|
||||
ctx.SetCookie("redirect_to", "", -1)
|
||||
ctx.Redirect(redirectTo)
|
||||
} else {
|
||||
ctx.Redirect("/")
|
||||
}
|
||||
}
|
||||
|
||||
func SignOut(ctx *middleware.Context) {
|
||||
ctx.Session.Delete("userId")
|
||||
ctx.Session.Delete("userName")
|
||||
ctx.SetCookie(base.CookieUserName, "", -1)
|
||||
ctx.SetCookie(base.CookieRememberName, "", -1)
|
||||
ctx.Redirect("/")
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
if form.Password != form.RetypePasswd {
|
||||
ctx.Data["HasError"] = true
|
||||
ctx.Data["Err_Password"] = true
|
||||
ctx.Data["Err_RetypePasswd"] = true
|
||||
ctx.Data["ErrorMsg"] = "Password and re-type password are not same"
|
||||
auth.AssignForm(form, ctx.Data)
|
||||
}
|
||||
|
||||
if ctx.HasError() {
|
||||
ctx.HTML(200, "user/signup")
|
||||
return
|
||||
}
|
||||
|
||||
u := &models.User{
|
||||
Name: form.UserName,
|
||||
Email: form.Email,
|
||||
Passwd: form.Password,
|
||||
IsActive: !base.Service.RegisterEmailConfirm,
|
||||
}
|
||||
|
||||
var err error
|
||||
if u, err = models.RegisterUser(u); err != nil {
|
||||
switch err {
|
||||
case models.ErrUserAlreadyExist:
|
||||
ctx.RenderWithErr("Username has been already taken", "user/signup", &form)
|
||||
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)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
log.Trace("%s User created: %s", ctx.Req.RequestURI, strings.ToLower(form.UserName))
|
||||
|
||||
// Send confirmation e-mail.
|
||||
if base.Service.RegisterEmailConfirm && u.Id > 1 {
|
||||
mailer.SendRegisterMail(ctx.Render, u)
|
||||
ctx.Data["IsSendRegisterMail"] = true
|
||||
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")
|
||||
}
|
||||
|
||||
func Delete(ctx *middleware.Context) {
|
||||
ctx.Data["Title"] = "Delete Account"
|
||||
ctx.Data["PageIsUserSetting"] = true
|
||||
ctx.Data["IsUserPageSettingDelete"] = true
|
||||
|
||||
if ctx.Req.Method == "GET" {
|
||||
ctx.HTML(200, "user/delete")
|
||||
return
|
||||
}
|
||||
|
||||
tmpUser := models.User{Passwd: ctx.Query("password")}
|
||||
tmpUser.EncodePasswd()
|
||||
if len(tmpUser.Passwd) == 0 || tmpUser.Passwd != ctx.User.Passwd {
|
||||
ctx.Data["HasError"] = true
|
||||
ctx.Data["ErrorMsg"] = "Password is not correct. Make sure you are owner of this account."
|
||||
} else {
|
||||
if err := models.DeleteUser(ctx.User); err != nil {
|
||||
ctx.Data["HasError"] = true
|
||||
switch err {
|
||||
case models.ErrUserOwnRepos:
|
||||
ctx.Data["ErrorMsg"] = "Your account still have ownership of repository, you have to delete or transfer them first."
|
||||
default:
|
||||
ctx.Handle(200, "user.Delete", err)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
ctx.Redirect("/")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
ctx.HTML(200, "user/delete")
|
||||
}
|
||||
|
||||
const (
|
||||
TPL_FEED = `<i class="icon fa fa-%s"></i>
|
||||
<div class="info"><span class="meta">%s</span><br>%s</div>`
|
||||
)
|
||||
|
||||
func Feeds(ctx *middleware.Context, form auth.FeedsForm) {
|
||||
actions, err := models.GetFeeds(form.UserId, form.Page*20, false)
|
||||
if err != nil {
|
||||
ctx.JSON(500, err)
|
||||
}
|
||||
|
||||
feeds := make([]string, len(actions))
|
||||
for i := range actions {
|
||||
feeds[i] = fmt.Sprintf(TPL_FEED, base.ActionIcon(actions[i].OpType),
|
||||
base.TimeSince(actions[i].Created), base.ActionDesc(actions[i]))
|
||||
}
|
||||
ctx.JSON(200, &feeds)
|
||||
}
|
||||
|
||||
func Issues(ctx *middleware.Context) {
|
||||
ctx.Data["Title"] = "Your Issues"
|
||||
ctx.Data["ViewType"] = "all"
|
||||
|
||||
page, _ := base.StrTo(ctx.Query("page")).Int()
|
||||
repoId, _ := base.StrTo(ctx.Query("repoid")).Int64()
|
||||
|
||||
ctx.Data["RepoId"] = repoId
|
||||
|
||||
var posterId int64 = 0
|
||||
if ctx.Query("type") == "created_by" {
|
||||
posterId = ctx.User.Id
|
||||
ctx.Data["ViewType"] = "created_by"
|
||||
}
|
||||
|
||||
// Get all repositories.
|
||||
repos, err := models.GetRepositories(ctx.User)
|
||||
if err != nil {
|
||||
ctx.Handle(200, "user.Issues(get repositories)", err)
|
||||
return
|
||||
}
|
||||
|
||||
showRepos := make([]models.Repository, 0, len(repos))
|
||||
|
||||
isShowClosed := ctx.Query("state") == "closed"
|
||||
var closedIssueCount, createdByCount, allIssueCount int
|
||||
|
||||
// Get all issues.
|
||||
allIssues := make([]models.Issue, 0, 5*len(repos))
|
||||
for i, repo := range repos {
|
||||
issues, err := models.GetIssues(0, repo.Id, posterId, 0, page, isShowClosed, false, "", "")
|
||||
if err != nil {
|
||||
ctx.Handle(200, "user.Issues(get issues)", err)
|
||||
return
|
||||
}
|
||||
|
||||
allIssueCount += repo.NumIssues
|
||||
closedIssueCount += repo.NumClosedIssues
|
||||
|
||||
// Set repository information to issues.
|
||||
for j := range issues {
|
||||
issues[j].Repo = &repos[i]
|
||||
}
|
||||
allIssues = append(allIssues, issues...)
|
||||
|
||||
repos[i].NumOpenIssues = repo.NumIssues - repo.NumClosedIssues
|
||||
if repos[i].NumOpenIssues > 0 {
|
||||
showRepos = append(showRepos, repos[i])
|
||||
}
|
||||
}
|
||||
|
||||
showIssues := make([]models.Issue, 0, len(allIssues))
|
||||
ctx.Data["IsShowClosed"] = isShowClosed
|
||||
|
||||
// Get posters and filter issues.
|
||||
for i := range allIssues {
|
||||
u, err := models.GetUserById(allIssues[i].PosterId)
|
||||
if err != nil {
|
||||
ctx.Handle(200, "user.Issues(get poster): %v", err)
|
||||
return
|
||||
}
|
||||
allIssues[i].Poster = u
|
||||
if u.Id == ctx.User.Id {
|
||||
createdByCount++
|
||||
}
|
||||
|
||||
if repoId > 0 && repoId != allIssues[i].Repo.Id {
|
||||
continue
|
||||
}
|
||||
|
||||
if isShowClosed == allIssues[i].IsClosed {
|
||||
showIssues = append(showIssues, allIssues[i])
|
||||
}
|
||||
}
|
||||
|
||||
ctx.Data["Repos"] = showRepos
|
||||
ctx.Data["Issues"] = showIssues
|
||||
ctx.Data["AllIssueCount"] = allIssueCount
|
||||
ctx.Data["ClosedIssueCount"] = closedIssueCount
|
||||
ctx.Data["OpenIssueCount"] = allIssueCount - closedIssueCount
|
||||
ctx.Data["CreatedByCount"] = createdByCount
|
||||
ctx.HTML(200, "issue/user")
|
||||
}
|
||||
|
||||
func Pulls(ctx *middleware.Context) {
|
||||
ctx.HTML(200, "user/pulls")
|
||||
}
|
||||
|
||||
func Stars(ctx *middleware.Context) {
|
||||
ctx.HTML(200, "user/stars")
|
||||
}
|
||||
|
||||
func Activate(ctx *middleware.Context) {
|
||||
code := ctx.Query("code")
|
||||
if len(code) == 0 {
|
||||
ctx.Data["IsActivatePage"] = true
|
||||
if ctx.User.IsActive {
|
||||
ctx.Handle(404, "user.Activate", nil)
|
||||
return
|
||||
}
|
||||
// Resend confirmation e-mail.
|
||||
if base.Service.RegisterEmailConfirm {
|
||||
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)
|
||||
|
||||
if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil {
|
||||
log.Error("Set cache(MailResendLimit) fail: %v", err)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
ctx.Data["ServiceNotEnabled"] = true
|
||||
}
|
||||
ctx.HTML(200, "user/active")
|
||||
return
|
||||
}
|
||||
|
||||
// Verify code.
|
||||
if user := models.VerifyUserActiveCode(code); user != nil {
|
||||
user.IsActive = true
|
||||
user.Rands = models.GetUserSalt()
|
||||
if err := models.UpdateUser(user); err != nil {
|
||||
ctx.Handle(404, "user.Activate", err)
|
||||
return
|
||||
}
|
||||
|
||||
log.Trace("%s User activated: %s", ctx.Req.RequestURI, user.Name)
|
||||
|
||||
ctx.Session.Set("userId", user.Id)
|
||||
ctx.Session.Set("userName", user.Name)
|
||||
ctx.Redirect("/")
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Data["IsActivateFailed"] = true
|
||||
ctx.HTML(200, "user/active")
|
||||
}
|
||||
|
||||
func ForgotPasswd(ctx *middleware.Context) {
|
||||
ctx.Data["Title"] = "Forgot Password"
|
||||
|
||||
if base.MailService == nil {
|
||||
ctx.Data["IsResetDisable"] = true
|
||||
ctx.HTML(200, "user/forgot_passwd")
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Data["IsResetRequest"] = true
|
||||
if ctx.Req.Method == "GET" {
|
||||
ctx.HTML(200, "user/forgot_passwd")
|
||||
return
|
||||
}
|
||||
|
||||
email := ctx.Query("email")
|
||||
u, err := models.GetUserByEmail(email)
|
||||
if err != nil {
|
||||
if err == models.ErrUserNotExist {
|
||||
ctx.RenderWithErr("This e-mail address does not associate to any account.", "user/forgot_passwd", nil)
|
||||
} else {
|
||||
ctx.Handle(404, "user.ResetPasswd(check existence)", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if ctx.Cache.IsExist("MailResendLimit_" + u.LowerName) {
|
||||
ctx.Data["ResendLimited"] = true
|
||||
ctx.HTML(200, "user/forgot_passwd")
|
||||
return
|
||||
}
|
||||
|
||||
mailer.SendResetPasswdMail(ctx.Render, u)
|
||||
if err = ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
|
||||
log.Error("Set cache(MailResendLimit) fail: %v", err)
|
||||
}
|
||||
|
||||
ctx.Data["Email"] = email
|
||||
ctx.Data["Hours"] = base.Service.ActiveCodeLives / 60
|
||||
ctx.Data["IsResetSent"] = true
|
||||
ctx.HTML(200, "user/forgot_passwd")
|
||||
}
|
||||
|
||||
func ResetPasswd(ctx *middleware.Context) {
|
||||
code := ctx.Query("code")
|
||||
if len(code) == 0 {
|
||||
ctx.Error(404)
|
||||
return
|
||||
}
|
||||
ctx.Data["Code"] = code
|
||||
|
||||
if ctx.Req.Method == "GET" {
|
||||
ctx.Data["IsResetForm"] = true
|
||||
ctx.HTML(200, "user/reset_passwd")
|
||||
return
|
||||
}
|
||||
|
||||
if u := models.VerifyUserActiveCode(code); u != nil {
|
||||
// Validate password length.
|
||||
passwd := ctx.Query("passwd")
|
||||
if len(passwd) < 6 || len(passwd) > 30 {
|
||||
ctx.Data["IsResetForm"] = true
|
||||
ctx.RenderWithErr("Password length should be in 6 and 30.", "user/reset_passwd", nil)
|
||||
return
|
||||
}
|
||||
|
||||
u.Passwd = passwd
|
||||
u.Rands = models.GetUserSalt()
|
||||
u.Salt = models.GetUserSalt()
|
||||
u.EncodePasswd()
|
||||
if err := models.UpdateUser(u); err != nil {
|
||||
ctx.Handle(404, "user.ResetPasswd(UpdateUser)", err)
|
||||
return
|
||||
}
|
||||
|
||||
log.Trace("%s User password reset: %s", ctx.Req.RequestURI, u.Name)
|
||||
ctx.Redirect("/user/login")
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Data["IsResetFailed"] = true
|
||||
ctx.HTML(200, "user/reset_passwd")
|
||||
}
|
||||
Reference in New Issue
Block a user