mirror of
https://github.com/go-gitea/gitea
synced 2025-07-22 18:28:37 +00:00
New UI merge in progress
This commit is contained in:
@@ -10,7 +10,8 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-martini/martini"
|
||||
"github.com/Unknwon/com"
|
||||
"github.com/Unknwon/macaron"
|
||||
|
||||
"github.com/gogits/gogs/models"
|
||||
"github.com/gogits/gogs/modules/base"
|
||||
@@ -126,7 +127,7 @@ func Dashboard(ctx *middleware.Context) {
|
||||
ctx.Data["PageIsDashboard"] = true
|
||||
|
||||
// Run operation.
|
||||
op, _ := base.StrTo(ctx.Query("op")).Int()
|
||||
op, _ := com.StrTo(ctx.Query("op")).Int()
|
||||
if op > 0 {
|
||||
var err error
|
||||
var success string
|
||||
@@ -159,7 +160,7 @@ func Users(ctx *middleware.Context) {
|
||||
ctx.Data["Title"] = "User Management"
|
||||
ctx.Data["PageIsUsers"] = true
|
||||
|
||||
p := base.StrTo(ctx.Query("p")).MustInt()
|
||||
p := com.StrTo(ctx.Query("p")).MustInt()
|
||||
if p < 1 {
|
||||
p = 1
|
||||
}
|
||||
@@ -188,7 +189,7 @@ func Repositories(ctx *middleware.Context) {
|
||||
ctx.Data["Title"] = "Repository Management"
|
||||
ctx.Data["PageIsRepos"] = true
|
||||
|
||||
p := base.StrTo(ctx.Query("p")).MustInt()
|
||||
p := com.StrTo(ctx.Query("p")).MustInt()
|
||||
if p < 1 {
|
||||
p = 1
|
||||
}
|
||||
@@ -235,7 +236,7 @@ func Config(ctx *middleware.Context) {
|
||||
ctx.Data["OfflineMode"] = setting.OfflineMode
|
||||
ctx.Data["DisableRouterLog"] = setting.DisableRouterLog
|
||||
ctx.Data["RunUser"] = setting.RunUser
|
||||
ctx.Data["RunMode"] = strings.Title(martini.Env)
|
||||
ctx.Data["RunMode"] = strings.Title(macaron.Env)
|
||||
ctx.Data["RepoRootPath"] = setting.RepoRootPath
|
||||
ctx.Data["StaticRootPath"] = setting.StaticRootPath
|
||||
ctx.Data["LogRootPath"] = setting.LogRootPath
|
||||
|
@@ -7,7 +7,7 @@ package admin
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/go-martini/martini"
|
||||
"github.com/Unknwon/com"
|
||||
"github.com/go-xorm/core"
|
||||
|
||||
"github.com/gogits/gogs/models"
|
||||
@@ -89,13 +89,13 @@ func NewAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) {
|
||||
ctx.Redirect("/admin/auths")
|
||||
}
|
||||
|
||||
func EditAuthSource(ctx *middleware.Context, params martini.Params) {
|
||||
func EditAuthSource(ctx *middleware.Context) {
|
||||
ctx.Data["Title"] = "Edit Authentication"
|
||||
ctx.Data["PageIsAuths"] = true
|
||||
ctx.Data["LoginTypes"] = models.LoginTypes
|
||||
ctx.Data["SMTPAuths"] = models.SMTPAuths
|
||||
|
||||
id, err := base.StrTo(params["authid"]).Int64()
|
||||
id, err := com.StrTo(ctx.Params(":authid")).Int64()
|
||||
if err != nil {
|
||||
ctx.Handle(404, "admin.auths.EditAuthSource", err)
|
||||
return
|
||||
@@ -168,11 +168,11 @@ func EditAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) {
|
||||
ctx.Redirect("/admin/auths")
|
||||
}
|
||||
|
||||
func DeleteAuthSource(ctx *middleware.Context, params martini.Params) {
|
||||
func DeleteAuthSource(ctx *middleware.Context) {
|
||||
ctx.Data["Title"] = "Delete Authentication"
|
||||
ctx.Data["PageIsAuths"] = true
|
||||
|
||||
id, err := base.StrTo(params["authid"]).Int64()
|
||||
id, err := com.StrTo(ctx.Params(":authid")).Int64()
|
||||
if err != nil {
|
||||
ctx.Handle(404, "admin.auths.DeleteAuth", err)
|
||||
return
|
||||
@@ -188,7 +188,7 @@ func DeleteAuthSource(ctx *middleware.Context, params martini.Params) {
|
||||
switch err {
|
||||
case models.ErrAuthenticationUserUsed:
|
||||
ctx.Flash.Error("This authentication still has used by some users, you should move them and then delete again.")
|
||||
ctx.Redirect("/admin/auths/" + params["authid"])
|
||||
ctx.Redirect("/admin/auths/" + ctx.Params(":authid"))
|
||||
default:
|
||||
ctx.Handle(500, "admin.auths.DeleteAuth(DelLoginSource)", err)
|
||||
}
|
||||
|
@@ -7,7 +7,7 @@ package admin
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/go-martini/martini"
|
||||
"github.com/Unknwon/com"
|
||||
|
||||
"github.com/gogits/gogs/models"
|
||||
"github.com/gogits/gogs/modules/auth"
|
||||
@@ -42,7 +42,7 @@ func NewUserPost(ctx *middleware.Context, form auth.RegisterForm) {
|
||||
return
|
||||
}
|
||||
|
||||
if form.Password != form.RetypePasswd {
|
||||
if form.Password != form.Retype {
|
||||
ctx.Data["Err_Password"] = true
|
||||
ctx.Data["Err_RetypePasswd"] = true
|
||||
ctx.RenderWithErr("Password and re-type password are not same.", "admin/users/new", &form)
|
||||
@@ -60,14 +60,13 @@ func NewUserPost(ctx *middleware.Context, form auth.RegisterForm) {
|
||||
if len(form.LoginType) > 0 {
|
||||
// NOTE: need rewrite.
|
||||
fields := strings.Split(form.LoginType, "-")
|
||||
tp, _ := base.StrTo(fields[0]).Int()
|
||||
tp, _ := com.StrTo(fields[0]).Int()
|
||||
u.LoginType = models.LoginType(tp)
|
||||
u.LoginSource, _ = base.StrTo(fields[1]).Int64()
|
||||
u.LoginSource, _ = com.StrTo(fields[1]).Int64()
|
||||
u.LoginName = form.LoginName
|
||||
}
|
||||
|
||||
var err error
|
||||
if u, err = models.CreateUser(u); err != nil {
|
||||
if err := models.CreateUser(u); err != nil {
|
||||
switch err {
|
||||
case models.ErrUserAlreadyExist:
|
||||
ctx.RenderWithErr("Username has been already taken", USER_NEW, &form)
|
||||
@@ -87,11 +86,11 @@ func NewUserPost(ctx *middleware.Context, form auth.RegisterForm) {
|
||||
ctx.Redirect("/admin/users")
|
||||
}
|
||||
|
||||
func EditUser(ctx *middleware.Context, params martini.Params) {
|
||||
func EditUser(ctx *middleware.Context) {
|
||||
ctx.Data["Title"] = "Edit Account"
|
||||
ctx.Data["PageIsUsers"] = true
|
||||
|
||||
uid, err := base.StrTo(params["userid"]).Int()
|
||||
uid, err := com.StrTo(ctx.Params(":userid")).Int()
|
||||
if err != nil {
|
||||
ctx.Handle(404, "admin.user.EditUser", err)
|
||||
return
|
||||
@@ -113,11 +112,11 @@ func EditUser(ctx *middleware.Context, params martini.Params) {
|
||||
ctx.HTML(200, USER_EDIT)
|
||||
}
|
||||
|
||||
func EditUserPost(ctx *middleware.Context, params martini.Params, form auth.AdminEditUserForm) {
|
||||
func EditUserPost(ctx *middleware.Context, form auth.AdminEditUserForm) {
|
||||
ctx.Data["Title"] = "Edit Account"
|
||||
ctx.Data["PageIsUsers"] = true
|
||||
|
||||
uid, err := base.StrTo(params["userid"]).Int()
|
||||
uid, err := com.StrTo(ctx.Params(":userid")).Int()
|
||||
if err != nil {
|
||||
ctx.Handle(404, "admin.user.EditUserPost", err)
|
||||
return
|
||||
@@ -134,7 +133,7 @@ func EditUserPost(ctx *middleware.Context, params martini.Params, form auth.Admi
|
||||
return
|
||||
}
|
||||
|
||||
if (form.Passwd != "") {
|
||||
if form.Passwd != "" {
|
||||
u.Passwd = form.Passwd
|
||||
u.Rands = models.GetUserSalt()
|
||||
u.Salt = models.GetUserSalt()
|
||||
@@ -157,15 +156,15 @@ func EditUserPost(ctx *middleware.Context, params martini.Params, form auth.Admi
|
||||
|
||||
ctx.Data["User"] = u
|
||||
ctx.Flash.Success("Account profile has been successfully updated.")
|
||||
ctx.Redirect("/admin/users/" + params["userid"])
|
||||
ctx.Redirect("/admin/users/" + ctx.Params(":userid"))
|
||||
}
|
||||
|
||||
func DeleteUser(ctx *middleware.Context, params martini.Params) {
|
||||
func DeleteUser(ctx *middleware.Context) {
|
||||
ctx.Data["Title"] = "Delete Account"
|
||||
ctx.Data["PageIsUsers"] = true
|
||||
|
||||
//log.Info("delete")
|
||||
uid, err := base.StrTo(params["userid"]).Int()
|
||||
uid, err := com.StrTo(ctx.Params(":userid")).Int()
|
||||
if err != nil {
|
||||
ctx.Handle(404, "admin.user.DeleteUser", err)
|
||||
return
|
||||
@@ -181,7 +180,7 @@ func DeleteUser(ctx *middleware.Context, params martini.Params) {
|
||||
switch err {
|
||||
case models.ErrUserOwnRepos:
|
||||
ctx.Flash.Error("This account still has ownership of repository, owner has to delete or transfer them first.")
|
||||
ctx.Redirect("/admin/users/" + params["userid"])
|
||||
ctx.Redirect("/admin/users/" + ctx.Params(":userid"))
|
||||
default:
|
||||
ctx.Handle(500, "admin.user.DeleteUser", err)
|
||||
}
|
||||
|
@@ -5,8 +5,9 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"github.com/Unknwon/com"
|
||||
|
||||
"github.com/gogits/gogs/models"
|
||||
"github.com/gogits/gogs/modules/base"
|
||||
"github.com/gogits/gogs/modules/middleware"
|
||||
)
|
||||
|
||||
@@ -17,7 +18,7 @@ type user struct {
|
||||
|
||||
func SearchUsers(ctx *middleware.Context) {
|
||||
q := ctx.Query("q")
|
||||
limit, err := base.StrTo(ctx.Query("limit")).Int()
|
||||
limit, err := com.StrTo(ctx.Query("limit")).Int()
|
||||
if err != nil {
|
||||
limit = 10
|
||||
}
|
||||
|
@@ -7,10 +7,10 @@ package dev
|
||||
import (
|
||||
"net/http/pprof"
|
||||
|
||||
"github.com/go-martini/martini"
|
||||
"github.com/Unknwon/macaron"
|
||||
)
|
||||
|
||||
func RegisterDebugRoutes(r martini.Router) {
|
||||
func RegisterDebugRoutes(r *macaron.Macaron) {
|
||||
r.Any("/debug/pprof/cmdline", pprof.Cmdline)
|
||||
r.Any("/debug/pprof/profile", pprof.Profile)
|
||||
r.Any("/debug/pprof/symbol", pprof.Symbol)
|
||||
|
@@ -5,15 +5,13 @@
|
||||
package dev
|
||||
|
||||
import (
|
||||
"github.com/go-martini/martini"
|
||||
|
||||
"github.com/gogits/gogs/models"
|
||||
"github.com/gogits/gogs/modules/base"
|
||||
"github.com/gogits/gogs/modules/middleware"
|
||||
"github.com/gogits/gogs/modules/setting"
|
||||
)
|
||||
|
||||
func TemplatePreview(ctx *middleware.Context, params martini.Params) {
|
||||
func TemplatePreview(ctx *middleware.Context) {
|
||||
ctx.Data["User"] = models.User{Name: "Unknown"}
|
||||
ctx.Data["AppName"] = setting.AppName
|
||||
ctx.Data["AppVer"] = setting.AppVer
|
||||
@@ -23,5 +21,5 @@ func TemplatePreview(ctx *middleware.Context, params martini.Params) {
|
||||
ctx.Data["ActiveCodeLives"] = setting.Service.ActiveCodeLives / 60
|
||||
ctx.Data["ResetPwdCodeLives"] = setting.Service.ResetPwdCodeLives / 60
|
||||
ctx.Data["CurDbValue"] = ""
|
||||
ctx.HTML(200, base.TplName(params["_1"]))
|
||||
ctx.HTML(200, base.TplName(ctx.Params("*")))
|
||||
}
|
||||
|
@@ -5,7 +5,6 @@
|
||||
package routers
|
||||
|
||||
import (
|
||||
"github.com/gogits/gogs/models"
|
||||
"github.com/gogits/gogs/modules/base"
|
||||
"github.com/gogits/gogs/modules/middleware"
|
||||
"github.com/gogits/gogs/modules/setting"
|
||||
@@ -23,33 +22,17 @@ func Home(ctx *middleware.Context) {
|
||||
}
|
||||
|
||||
// Check auto-login.
|
||||
userName := ctx.GetCookie(setting.CookieUserName)
|
||||
if len(userName) != 0 {
|
||||
uname := ctx.GetCookie(setting.CookieUserName)
|
||||
if len(uname) != 0 {
|
||||
ctx.Redirect("/user/login")
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Data["PageIsHome"] = true
|
||||
|
||||
// Show recent updated repositories for new visitors.
|
||||
repos, err := models.GetRecentUpdatedRepositories()
|
||||
if err != nil {
|
||||
ctx.Handle(500, "dashboard.Home(GetRecentUpdatedRepositories)", err)
|
||||
return
|
||||
}
|
||||
|
||||
for _, repo := range repos {
|
||||
if err = repo.GetOwner(); err != nil {
|
||||
ctx.Handle(500, "dashboard.Home(GetOwner)", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
ctx.Data["Repos"] = repos
|
||||
ctx.HTML(200, HOME)
|
||||
}
|
||||
|
||||
func NotFound(ctx *middleware.Context) {
|
||||
ctx.Data["Title"] = "Page Not Found"
|
||||
ctx.Data["PageIsNotFound"] = true
|
||||
ctx.Handle(404, "home.NotFound", nil)
|
||||
}
|
@@ -11,8 +11,9 @@ import (
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/Unknwon/com"
|
||||
"github.com/Unknwon/goconfig"
|
||||
"github.com/go-martini/martini"
|
||||
"github.com/Unknwon/macaron"
|
||||
"github.com/go-xorm/xorm"
|
||||
|
||||
"github.com/gogits/gogs/models"
|
||||
@@ -33,12 +34,12 @@ const (
|
||||
func checkRunMode() {
|
||||
switch setting.Cfg.MustValue("", "RUN_MODE") {
|
||||
case "prod":
|
||||
martini.Env = martini.Prod
|
||||
macaron.Env = macaron.PROD
|
||||
setting.ProdMode = true
|
||||
case "test":
|
||||
martini.Env = martini.Test
|
||||
macaron.Env = macaron.TEST
|
||||
}
|
||||
log.Info("Run Mode: %s", strings.Title(martini.Env))
|
||||
log.Info("Run Mode: %s", strings.Title(macaron.Env))
|
||||
}
|
||||
|
||||
func NewServices() {
|
||||
@@ -59,7 +60,7 @@ func GlobalInit() {
|
||||
|
||||
if setting.InstallLock {
|
||||
if err := models.NewEngine(); err != nil {
|
||||
log.Fatal("Fail to initialize ORM engine: %v", err)
|
||||
log.Fatal(4, "Fail to initialize ORM engine: %v", err)
|
||||
}
|
||||
|
||||
models.HasEngine = true
|
||||
@@ -210,8 +211,8 @@ func InstallPost(ctx *middleware.Context, form auth.InstallForm) {
|
||||
setting.Cfg.SetValue("mailer", "USER", form.SmtpEmail)
|
||||
setting.Cfg.SetValue("mailer", "PASSWD", form.SmtpPasswd)
|
||||
|
||||
setting.Cfg.SetValue("service", "REGISTER_EMAIL_CONFIRM", base.ToStr(form.RegisterConfirm == "on"))
|
||||
setting.Cfg.SetValue("service", "ENABLE_NOTIFY_MAIL", base.ToStr(form.MailNotify == "on"))
|
||||
setting.Cfg.SetValue("service", "REGISTER_EMAIL_CONFIRM", com.ToStr(form.RegisterConfirm == "on"))
|
||||
setting.Cfg.SetValue("service", "ENABLE_NOTIFY_MAIL", com.ToStr(form.MailNotify == "on"))
|
||||
}
|
||||
|
||||
setting.Cfg.SetValue("", "RUN_MODE", "prod")
|
||||
@@ -227,7 +228,7 @@ func InstallPost(ctx *middleware.Context, form auth.InstallForm) {
|
||||
GlobalInit()
|
||||
|
||||
// Create admin account.
|
||||
if _, err := models.CreateUser(&models.User{Name: form.AdminName, Email: form.AdminEmail, Passwd: form.AdminPasswd,
|
||||
if err := models.CreateUser(&models.User{Name: form.AdminName, Email: form.AdminEmail, Passwd: form.AdminPasswd,
|
||||
IsAdmin: true, IsActive: true}); err != nil {
|
||||
if err != models.ErrUserAlreadyExist {
|
||||
setting.InstallLock = false
|
||||
|
@@ -7,7 +7,7 @@ package org
|
||||
import (
|
||||
"github.com/go-martini/martini"
|
||||
|
||||
"github.com/gogits/gogs/models"
|
||||
"github.com/gogits/gogs-ng/models"
|
||||
"github.com/gogits/gogs/modules/auth"
|
||||
"github.com/gogits/gogs/modules/base"
|
||||
"github.com/gogits/gogs/modules/log"
|
||||
@@ -114,11 +114,11 @@ func Dashboard(ctx *middleware.Context, params martini.Params) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := ctx.User.GetOrganizations(); err != nil {
|
||||
ctx.Handle(500, "home.Dashboard(GetOrganizations)", err)
|
||||
return
|
||||
}
|
||||
ctx.Data["Orgs"] = ctx.User.Orgs
|
||||
// if err := ctx.User.GetOrganizations(); err != nil {
|
||||
// ctx.Handle(500, "home.Dashboard(GetOrganizations)", err)
|
||||
// return
|
||||
// }
|
||||
// ctx.Data["Orgs"] = ctx.User.Orgs
|
||||
ctx.Data["ContextUser"] = org
|
||||
|
||||
ctx.Data["MyRepos"], err = models.GetRepositories(org.Id, true)
|
||||
|
@@ -5,8 +5,6 @@
|
||||
package repo
|
||||
|
||||
import (
|
||||
"github.com/go-martini/martini"
|
||||
|
||||
"github.com/gogits/gogs/modules/base"
|
||||
"github.com/gogits/gogs/modules/middleware"
|
||||
)
|
||||
@@ -15,7 +13,7 @@ const (
|
||||
BRANCH base.TplName = "repo/branch"
|
||||
)
|
||||
|
||||
func Branches(ctx *middleware.Context, params martini.Params) {
|
||||
func Branches(ctx *middleware.Context) {
|
||||
ctx.Data["Title"] = "Branches"
|
||||
ctx.Data["IsRepoToolbarBranches"] = true
|
||||
|
||||
|
@@ -4,222 +4,224 @@
|
||||
|
||||
package repo
|
||||
|
||||
import (
|
||||
"path"
|
||||
// import (
|
||||
// "path"
|
||||
|
||||
"github.com/go-martini/martini"
|
||||
// "github.com/Unknwon/com"
|
||||
// "github.com/go-martini/martini"
|
||||
|
||||
"github.com/gogits/gogs/models"
|
||||
"github.com/gogits/gogs/modules/base"
|
||||
"github.com/gogits/gogs/modules/middleware"
|
||||
)
|
||||
// "github.com/gogits/gogs/models"
|
||||
// "github.com/gogits/gogs/modules/base"
|
||||
// "github.com/gogits/gogs/modules/middleware"
|
||||
// )
|
||||
|
||||
const (
|
||||
COMMITS base.TplName = "repo/commits"
|
||||
DIFF base.TplName = "repo/diff"
|
||||
)
|
||||
// const (
|
||||
// COMMITS base.TplName = "repo/commits"
|
||||
// DIFF base.TplName = "repo/diff"
|
||||
// )
|
||||
|
||||
func Commits(ctx *middleware.Context, params martini.Params) {
|
||||
ctx.Data["IsRepoToolbarCommits"] = true
|
||||
// func Commits(ctx *middleware.Context, params martini.Params) {
|
||||
// ctx.Data["IsRepoToolbarCommits"] = true
|
||||
|
||||
userName := ctx.Repo.Owner.Name
|
||||
repoName := ctx.Repo.Repository.Name
|
||||
// userName := ctx.Repo.Owner.Name
|
||||
// repoName := ctx.Repo.Repository.Name
|
||||
|
||||
brs, err := ctx.Repo.GitRepo.GetBranches()
|
||||
if err != nil {
|
||||
ctx.Handle(500, "repo.Commits(GetBranches)", err)
|
||||
return
|
||||
} else if len(brs) == 0 {
|
||||
ctx.Handle(404, "repo.Commits(GetBranches)", nil)
|
||||
return
|
||||
}
|
||||
// brs, err := ctx.Repo.GitRepo.GetBranches()
|
||||
// if err != nil {
|
||||
// ctx.Handle(500, "repo.Commits(GetBranches)", err)
|
||||
// return
|
||||
// } else if len(brs) == 0 {
|
||||
// ctx.Handle(404, "repo.Commits(GetBranches)", nil)
|
||||
// return
|
||||
// }
|
||||
|
||||
commitsCount, err := ctx.Repo.Commit.CommitsCount()
|
||||
if err != nil {
|
||||
ctx.Handle(500, "repo.Commits(GetCommitsCount)", err)
|
||||
return
|
||||
}
|
||||
// commitsCount, err := ctx.Repo.Commit.CommitsCount()
|
||||
// if err != nil {
|
||||
// ctx.Handle(500, "repo.Commits(GetCommitsCount)", err)
|
||||
// return
|
||||
// }
|
||||
|
||||
// Calculate and validate page number.
|
||||
page, _ := base.StrTo(ctx.Query("p")).Int()
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
lastPage := page - 1
|
||||
if lastPage < 0 {
|
||||
lastPage = 0
|
||||
}
|
||||
nextPage := page + 1
|
||||
if nextPage*50 > commitsCount {
|
||||
nextPage = 0
|
||||
}
|
||||
// // Calculate and validate page number.
|
||||
// page, _ := com.StrTo(ctx.Query("p")).Int()
|
||||
// if page < 1 {
|
||||
// page = 1
|
||||
// }
|
||||
// lastPage := page - 1
|
||||
// if lastPage < 0 {
|
||||
// lastPage = 0
|
||||
// }
|
||||
// nextPage := page + 1
|
||||
// if nextPage*50 > commitsCount {
|
||||
// nextPage = 0
|
||||
// }
|
||||
|
||||
// Both `git log branchName` and `git log commitId` work.
|
||||
ctx.Data["Commits"], err = ctx.Repo.Commit.CommitsByRange(page)
|
||||
if err != nil {
|
||||
ctx.Handle(500, "repo.Commits(CommitsByRange)", err)
|
||||
return
|
||||
}
|
||||
// // Both `git log branchName` and `git log commitId` work.
|
||||
// // ctx.Data["Commits"], err = ctx.Repo.Commit.CommitsByRange(page)
|
||||
// // if err != nil {
|
||||
// // ctx.Handle(500, "repo.Commits(CommitsByRange)", err)
|
||||
// // return
|
||||
// // }
|
||||
|
||||
ctx.Data["Username"] = userName
|
||||
ctx.Data["Reponame"] = repoName
|
||||
ctx.Data["CommitCount"] = commitsCount
|
||||
ctx.Data["LastPageNum"] = lastPage
|
||||
ctx.Data["NextPageNum"] = nextPage
|
||||
ctx.HTML(200, COMMITS)
|
||||
}
|
||||
// ctx.Data["Username"] = userName
|
||||
// ctx.Data["Reponame"] = repoName
|
||||
// ctx.Data["CommitCount"] = commitsCount
|
||||
// ctx.Data["LastPageNum"] = lastPage
|
||||
// ctx.Data["NextPageNum"] = nextPage
|
||||
// ctx.HTML(200, COMMITS)
|
||||
// }
|
||||
|
||||
func SearchCommits(ctx *middleware.Context, params martini.Params) {
|
||||
ctx.Data["IsSearchPage"] = true
|
||||
ctx.Data["IsRepoToolbarCommits"] = true
|
||||
// func SearchCommits(ctx *middleware.Context, params martini.Params) {
|
||||
// ctx.Data["IsSearchPage"] = true
|
||||
// ctx.Data["IsRepoToolbarCommits"] = true
|
||||
|
||||
keyword := ctx.Query("q")
|
||||
if len(keyword) == 0 {
|
||||
ctx.Redirect(ctx.Repo.RepoLink + "/commits/" + ctx.Repo.BranchName)
|
||||
return
|
||||
}
|
||||
// keyword := ctx.Query("q")
|
||||
// if len(keyword) == 0 {
|
||||
// ctx.Redirect(ctx.Repo.RepoLink + "/commits/" + ctx.Repo.BranchName)
|
||||
// return
|
||||
// }
|
||||
|
||||
userName := params["username"]
|
||||
repoName := params["reponame"]
|
||||
// userName := params["username"]
|
||||
// repoName := params["reponame"]
|
||||
|
||||
brs, err := ctx.Repo.GitRepo.GetBranches()
|
||||
if err != nil {
|
||||
ctx.Handle(500, "repo.SearchCommits(GetBranches)", err)
|
||||
return
|
||||
} else if len(brs) == 0 {
|
||||
ctx.Handle(404, "repo.SearchCommits(GetBranches)", nil)
|
||||
return
|
||||
}
|
||||
// brs, err := ctx.Repo.GitRepo.GetBranches()
|
||||
// if err != nil {
|
||||
// ctx.Handle(500, "repo.SearchCommits(GetBranches)", err)
|
||||
// return
|
||||
// } else if len(brs) == 0 {
|
||||
// ctx.Handle(404, "repo.SearchCommits(GetBranches)", nil)
|
||||
// return
|
||||
// }
|
||||
|
||||
commits, err := ctx.Repo.Commit.SearchCommits(keyword)
|
||||
if err != nil {
|
||||
ctx.Handle(500, "repo.SearchCommits(SearchCommits)", err)
|
||||
return
|
||||
}
|
||||
// // commits, err := ctx.Repo.Commit.SearchCommits(keyword)
|
||||
// // if err != nil {
|
||||
// // ctx.Handle(500, "repo.SearchCommits(SearchCommits)", err)
|
||||
// // return
|
||||
// // }
|
||||
|
||||
ctx.Data["Keyword"] = keyword
|
||||
ctx.Data["Username"] = userName
|
||||
ctx.Data["Reponame"] = repoName
|
||||
ctx.Data["CommitCount"] = commits.Len()
|
||||
ctx.Data["Commits"] = commits
|
||||
ctx.HTML(200, COMMITS)
|
||||
}
|
||||
// ctx.Data["Keyword"] = keyword
|
||||
// ctx.Data["Username"] = userName
|
||||
// ctx.Data["Reponame"] = repoName
|
||||
// // ctx.Data["CommitCount"] = commits.Len()
|
||||
// // ctx.Data["Commits"] = commits
|
||||
// ctx.HTML(200, COMMITS)
|
||||
// }
|
||||
|
||||
func Diff(ctx *middleware.Context, params martini.Params) {
|
||||
ctx.Data["IsRepoToolbarCommits"] = true
|
||||
// func Diff(ctx *middleware.Context, params martini.Params) {
|
||||
// ctx.Data["IsRepoToolbarCommits"] = true
|
||||
|
||||
userName := ctx.Repo.Owner.Name
|
||||
repoName := ctx.Repo.Repository.Name
|
||||
commitId := ctx.Repo.CommitId
|
||||
// userName := ctx.Repo.Owner.Name
|
||||
// repoName := ctx.Repo.Repository.Name
|
||||
// commitId := ctx.Repo.CommitId
|
||||
|
||||
commit := ctx.Repo.Commit
|
||||
// commit := ctx.Repo.Commit
|
||||
|
||||
diff, err := models.GetDiff(models.RepoPath(userName, repoName), commitId)
|
||||
if err != nil {
|
||||
ctx.Handle(404, "repo.Diff(GetDiff)", err)
|
||||
return
|
||||
}
|
||||
// diff, err := models.GetDiff(models.RepoPath(userName, repoName), commitId)
|
||||
// if err != nil {
|
||||
// ctx.Handle(404, "repo.Diff(GetDiff)", err)
|
||||
// return
|
||||
// }
|
||||
|
||||
isImageFile := func(name string) bool {
|
||||
blob, err := ctx.Repo.Commit.GetBlobByPath(name)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
// isImageFile := func(name string) bool {
|
||||
// // blob, err := ctx.Repo.Commit.GetBlobByPath(name)
|
||||
// // if err != nil {
|
||||
// // return false
|
||||
// // }
|
||||
|
||||
dataRc, err := blob.Data()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
buf := make([]byte, 1024)
|
||||
n, _ := dataRc.Read(buf)
|
||||
if n > 0 {
|
||||
buf = buf[:n]
|
||||
}
|
||||
dataRc.Close()
|
||||
_, isImage := base.IsImageFile(buf)
|
||||
return isImage
|
||||
}
|
||||
// // dataRc, err := blob.Data()
|
||||
// // if err != nil {
|
||||
// // return false
|
||||
// // }
|
||||
// // buf := make([]byte, 1024)
|
||||
// // n, _ := dataRc.Read(buf)
|
||||
// // if n > 0 {
|
||||
// // buf = buf[:n]
|
||||
// // }
|
||||
// // dataRc.Close()
|
||||
// // _, isImage := base.IsImageFile(buf)
|
||||
// // return isImage
|
||||
// return false
|
||||
// }
|
||||
|
||||
parents := make([]string, commit.ParentCount())
|
||||
for i := 0; i < commit.ParentCount(); i++ {
|
||||
sha, err := commit.ParentId(i)
|
||||
parents[i] = sha.String()
|
||||
if err != nil {
|
||||
ctx.Handle(404, "repo.Diff", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
// parents := make([]string, commit.ParentCount())
|
||||
// for i := 0; i < commit.ParentCount(); i++ {
|
||||
// sha, err := commit.ParentId(i)
|
||||
// parents[i] = sha.String()
|
||||
// if err != nil {
|
||||
// ctx.Handle(404, "repo.Diff", err)
|
||||
// return
|
||||
// }
|
||||
// }
|
||||
|
||||
ctx.Data["Username"] = userName
|
||||
ctx.Data["Reponame"] = repoName
|
||||
ctx.Data["IsImageFile"] = isImageFile
|
||||
ctx.Data["Title"] = commit.Summary() + " · " + base.ShortSha(commitId)
|
||||
ctx.Data["Commit"] = commit
|
||||
ctx.Data["Diff"] = diff
|
||||
ctx.Data["Parents"] = parents
|
||||
ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
|
||||
ctx.Data["SourcePath"] = "/" + path.Join(userName, repoName, "src", commitId)
|
||||
ctx.Data["RawPath"] = "/" + path.Join(userName, repoName, "raw", commitId)
|
||||
ctx.HTML(200, DIFF)
|
||||
}
|
||||
// ctx.Data["Username"] = userName
|
||||
// ctx.Data["Reponame"] = repoName
|
||||
// ctx.Data["IsImageFile"] = isImageFile
|
||||
// ctx.Data["Title"] = commit.Summary() + " · " + base.ShortSha(commitId)
|
||||
// ctx.Data["Commit"] = commit
|
||||
// ctx.Data["Diff"] = diff
|
||||
// ctx.Data["Parents"] = parents
|
||||
// ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
|
||||
// ctx.Data["SourcePath"] = "/" + path.Join(userName, repoName, "src", commitId)
|
||||
// ctx.Data["RawPath"] = "/" + path.Join(userName, repoName, "raw", commitId)
|
||||
// ctx.HTML(200, DIFF)
|
||||
// }
|
||||
|
||||
func FileHistory(ctx *middleware.Context, params martini.Params) {
|
||||
ctx.Data["IsRepoToolbarCommits"] = true
|
||||
// func FileHistory(ctx *middleware.Context, params martini.Params) {
|
||||
// ctx.Data["IsRepoToolbarCommits"] = true
|
||||
|
||||
fileName := params["_1"]
|
||||
if len(fileName) == 0 {
|
||||
Commits(ctx, params)
|
||||
return
|
||||
}
|
||||
// fileName := params["_1"]
|
||||
// if len(fileName) == 0 {
|
||||
// Commits(ctx, params)
|
||||
// return
|
||||
// }
|
||||
|
||||
userName := ctx.Repo.Owner.Name
|
||||
repoName := ctx.Repo.Repository.Name
|
||||
branchName := params["branchname"]
|
||||
// userName := ctx.Repo.Owner.Name
|
||||
// repoName := ctx.Repo.Repository.Name
|
||||
// branchName := params["branchname"]
|
||||
|
||||
brs, err := ctx.Repo.GitRepo.GetBranches()
|
||||
if err != nil {
|
||||
ctx.Handle(500, "repo.FileHistory", err)
|
||||
return
|
||||
} else if len(brs) == 0 {
|
||||
ctx.Handle(404, "repo.FileHistory", nil)
|
||||
return
|
||||
}
|
||||
// brs, err := ctx.Repo.GitRepo.GetBranches()
|
||||
// if err != nil {
|
||||
// ctx.Handle(500, "repo.FileHistory", err)
|
||||
// return
|
||||
// } else if len(brs) == 0 {
|
||||
// ctx.Handle(404, "repo.FileHistory", nil)
|
||||
// return
|
||||
// }
|
||||
|
||||
commitsCount, err := ctx.Repo.GitRepo.FileCommitsCount(branchName, fileName)
|
||||
if err != nil {
|
||||
ctx.Handle(500, "repo.FileHistory(GetCommitsCount)", err)
|
||||
return
|
||||
} else if commitsCount == 0 {
|
||||
ctx.Handle(404, "repo.FileHistory", nil)
|
||||
return
|
||||
}
|
||||
// // commitsCount, err := ctx.Repo.GitRepo.FileCommitsCount(branchName, fileName)
|
||||
// // if err != nil {
|
||||
// // ctx.Handle(500, "repo.FileHistory(GetCommitsCount)", err)
|
||||
// // return
|
||||
// // } else if commitsCount == 0 {
|
||||
// // ctx.Handle(404, "repo.FileHistory", nil)
|
||||
// // return
|
||||
// // }
|
||||
|
||||
// Calculate and validate page number.
|
||||
page, _ := base.StrTo(ctx.Query("p")).Int()
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
lastPage := page - 1
|
||||
if lastPage < 0 {
|
||||
lastPage = 0
|
||||
}
|
||||
nextPage := page + 1
|
||||
if nextPage*50 > commitsCount {
|
||||
nextPage = 0
|
||||
}
|
||||
// // Calculate and validate page number.
|
||||
// // page, _ := base.StrTo(ctx.Query("p")).Int()
|
||||
// // if page < 1 {
|
||||
// // page = 1
|
||||
// // }
|
||||
// // lastPage := page - 1
|
||||
// // if lastPage < 0 {
|
||||
// // lastPage = 0
|
||||
// // }
|
||||
// // nextPage := page + 1
|
||||
// // if nextPage*50 > commitsCount {
|
||||
// // nextPage = 0
|
||||
// // }
|
||||
|
||||
ctx.Data["Commits"], err = ctx.Repo.GitRepo.CommitsByFileAndRange(
|
||||
branchName, fileName, page)
|
||||
if err != nil {
|
||||
ctx.Handle(500, "repo.FileHistory(CommitsByRange)", err)
|
||||
return
|
||||
}
|
||||
// // ctx.Data["Commits"], err = ctx.Repo.GitRepo.CommitsByFileAndRange(
|
||||
// // branchName, fileName, page)
|
||||
// // if err != nil {
|
||||
// // ctx.Handle(500, "repo.FileHistory(CommitsByRange)", err)
|
||||
// // return
|
||||
// // }
|
||||
|
||||
ctx.Data["Username"] = userName
|
||||
ctx.Data["Reponame"] = repoName
|
||||
ctx.Data["FileName"] = fileName
|
||||
ctx.Data["CommitCount"] = commitsCount
|
||||
ctx.Data["LastPageNum"] = lastPage
|
||||
ctx.Data["NextPageNum"] = nextPage
|
||||
ctx.HTML(200, COMMITS)
|
||||
}
|
||||
// ctx.Data["Username"] = userName
|
||||
// ctx.Data["Reponame"] = repoName
|
||||
// ctx.Data["FileName"] = fileName
|
||||
// // ctx.Data["CommitCount"] = commitsCount
|
||||
// // ctx.Data["LastPageNum"] = lastPage
|
||||
// // ctx.Data["NextPageNum"] = nextPage
|
||||
// ctx.HTML(200, COMMITS)
|
||||
// }
|
||||
|
@@ -5,101 +5,50 @@
|
||||
package repo
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
// "io"
|
||||
// "os"
|
||||
// "path/filepath"
|
||||
|
||||
"github.com/Unknwon/com"
|
||||
"github.com/go-martini/martini"
|
||||
// "github.com/Unknwon/com"
|
||||
|
||||
"github.com/gogits/git"
|
||||
// "github.com/gogits/git"
|
||||
|
||||
"github.com/gogits/gogs/modules/base"
|
||||
// "github.com/gogits/gogs/modules/base"
|
||||
"github.com/gogits/gogs/modules/middleware"
|
||||
)
|
||||
|
||||
func SingleDownload(ctx *middleware.Context, params martini.Params) {
|
||||
treename := params["_1"]
|
||||
func SingleDownload(ctx *middleware.Context) {
|
||||
// treename := params["_1"]
|
||||
|
||||
blob, err := ctx.Repo.Commit.GetBlobByPath(treename)
|
||||
if err != nil {
|
||||
ctx.Handle(500, "repo.SingleDownload(GetBlobByPath)", err)
|
||||
return
|
||||
}
|
||||
// blob, err := ctx.Repo.Commit.GetBlobByPath(treename)
|
||||
// if err != nil {
|
||||
// ctx.Handle(500, "repo.SingleDownload(GetBlobByPath)", err)
|
||||
// return
|
||||
// }
|
||||
|
||||
dataRc, err := blob.Data()
|
||||
if err != nil {
|
||||
ctx.Handle(500, "repo.SingleDownload(Data)", err)
|
||||
return
|
||||
}
|
||||
// dataRc, err := blob.Data()
|
||||
// if err != nil {
|
||||
// ctx.Handle(500, "repo.SingleDownload(Data)", err)
|
||||
// return
|
||||
// }
|
||||
|
||||
buf := make([]byte, 1024)
|
||||
n, _ := dataRc.Read(buf)
|
||||
if n > 0 {
|
||||
buf = buf[:n]
|
||||
}
|
||||
// buf := make([]byte, 1024)
|
||||
// n, _ := dataRc.Read(buf)
|
||||
// if n > 0 {
|
||||
// buf = buf[:n]
|
||||
// }
|
||||
|
||||
defer func() {
|
||||
dataRc.Close()
|
||||
}()
|
||||
// defer func() {
|
||||
// dataRc.Close()
|
||||
// }()
|
||||
|
||||
contentType, isTextFile := base.IsTextFile(buf)
|
||||
_, isImageFile := base.IsImageFile(buf)
|
||||
ctx.Res.Header().Set("Content-Type", contentType)
|
||||
if !isTextFile && !isImageFile {
|
||||
ctx.Res.Header().Set("Content-Disposition", "attachment; filename="+filepath.Base(treename))
|
||||
ctx.Res.Header().Set("Content-Transfer-Encoding", "binary")
|
||||
}
|
||||
ctx.Res.Write(buf)
|
||||
io.Copy(ctx.Res, dataRc)
|
||||
}
|
||||
|
||||
func ZipDownload(ctx *middleware.Context, params martini.Params) {
|
||||
commitId := ctx.Repo.CommitId
|
||||
archivesPath := filepath.Join(ctx.Repo.GitRepo.Path, "archives/zip")
|
||||
if !com.IsDir(archivesPath) {
|
||||
if err := os.MkdirAll(archivesPath, 0755); err != nil {
|
||||
ctx.Handle(500, "ZipDownload -> os.Mkdir(archivesPath)", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
archivePath := filepath.Join(archivesPath, commitId+".zip")
|
||||
|
||||
if com.IsFile(archivePath) {
|
||||
ctx.ServeFile(archivePath, ctx.Repo.Repository.Name+".zip")
|
||||
return
|
||||
}
|
||||
|
||||
if err := ctx.Repo.Commit.CreateArchive(archivePath, git.AT_ZIP); err != nil {
|
||||
ctx.Handle(500, "ZipDownload -> CreateArchive "+archivePath, err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.ServeFile(archivePath, ctx.Repo.Repository.Name+".zip")
|
||||
}
|
||||
|
||||
func TarGzDownload(ctx *middleware.Context, params martini.Params) {
|
||||
commitId := ctx.Repo.CommitId
|
||||
archivesPath := filepath.Join(ctx.Repo.GitRepo.Path, "archives/targz")
|
||||
if !com.IsDir(archivesPath) {
|
||||
if err := os.MkdirAll(archivesPath, 0755); err != nil {
|
||||
ctx.Handle(500, "TarGzDownload -> os.Mkdir(archivesPath)", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
archivePath := filepath.Join(archivesPath, commitId+".tar.gz")
|
||||
|
||||
if com.IsFile(archivePath) {
|
||||
ctx.ServeFile(archivePath, ctx.Repo.Repository.Name+".tar.gz")
|
||||
return
|
||||
}
|
||||
|
||||
if err := ctx.Repo.Commit.CreateArchive(archivePath, git.AT_TARGZ); err != nil {
|
||||
ctx.Handle(500, "TarGzDownload -> CreateArchive "+archivePath, err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.ServeFile(archivePath, ctx.Repo.Repository.Name+".tar.gz")
|
||||
// contentType, isTextFile := base.IsTextFile(buf)
|
||||
// _, isImageFile := base.IsImageFile(buf)
|
||||
// ctx.Res.Header().Set("Content-Type", contentType)
|
||||
// if !isTextFile && !isImageFile {
|
||||
// ctx.Res.Header().Set("Content-Disposition", "attachment; filename="+filepath.Base(treename))
|
||||
// ctx.Res.Header().Set("Content-Transfer-Encoding", "binary")
|
||||
// }
|
||||
// ctx.Res.Write(buf)
|
||||
// io.Copy(ctx.Res, dataRc)
|
||||
}
|
||||
|
@@ -6,6 +6,8 @@ package repo
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
@@ -19,16 +21,43 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-martini/martini"
|
||||
"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/setting"
|
||||
)
|
||||
|
||||
func Http(ctx *middleware.Context, params martini.Params) {
|
||||
username := params["username"]
|
||||
reponame := params["reponame"]
|
||||
func basicEncode(username, password string) string {
|
||||
auth := username + ":" + password
|
||||
return base64.StdEncoding.EncodeToString([]byte(auth))
|
||||
}
|
||||
|
||||
func basicDecode(encoded string) (user string, name string, err error) {
|
||||
var s []byte
|
||||
s, err = base64.StdEncoding.DecodeString(encoded)
|
||||
if err != nil {
|
||||
return user, name, err
|
||||
}
|
||||
|
||||
a := strings.Split(string(s), ":")
|
||||
if len(a) == 2 {
|
||||
user, name = a[0], a[1]
|
||||
} else {
|
||||
err = errors.New("decode failed")
|
||||
}
|
||||
return user, name, err
|
||||
}
|
||||
|
||||
func authRequired(ctx *middleware.Context) {
|
||||
ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=\".\"")
|
||||
ctx.Data["ErrorMsg"] = "no basic auth and digit auth"
|
||||
ctx.HTML(401, base.TplName("status/401"))
|
||||
}
|
||||
|
||||
func Http(ctx *middleware.Context) {
|
||||
username := ctx.Params(":username")
|
||||
reponame := ctx.Params(":reponame")
|
||||
if strings.HasSuffix(reponame, ".git") {
|
||||
reponame = reponame[:len(reponame)-4]
|
||||
}
|
||||
@@ -50,7 +79,7 @@ func Http(ctx *middleware.Context, params martini.Params) {
|
||||
if err == models.ErrUserNotExist {
|
||||
ctx.Handle(404, "repo.Http(GetUserByName)", nil)
|
||||
} else {
|
||||
ctx.Handle(500, "repo.Http(GetUserByName)", nil)
|
||||
ctx.Handle(500, "repo.Http(GetUserByName)", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -60,7 +89,7 @@ func Http(ctx *middleware.Context, params martini.Params) {
|
||||
if err == models.ErrRepoNotExist {
|
||||
ctx.Handle(404, "repo.Http(GetRepositoryByName)", nil)
|
||||
} else {
|
||||
ctx.Handle(500, "repo.Http(GetRepositoryByName)", nil)
|
||||
ctx.Handle(500, "repo.Http(GetRepositoryByName)", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -75,7 +104,6 @@ func Http(ctx *middleware.Context, params martini.Params) {
|
||||
if askAuth {
|
||||
baHead := ctx.Req.Header.Get("Authorization")
|
||||
if baHead == "" {
|
||||
// ask auth
|
||||
authRequired(ctx)
|
||||
return
|
||||
}
|
||||
@@ -142,7 +170,7 @@ func Http(ctx *middleware.Context, params martini.Params) {
|
||||
if head[0] == '0' && head[1] == '0' {
|
||||
size, err := strconv.ParseInt(string(input[lastLine+2:lastLine+4]), 16, 32)
|
||||
if err != nil {
|
||||
log.Error("%v", err)
|
||||
log.Error(4, "%v", err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -166,7 +194,6 @@ func Http(ctx *middleware.Context, params martini.Params) {
|
||||
}
|
||||
lastLine = lastLine + size
|
||||
} else {
|
||||
//fmt.Println("ddddddddddd")
|
||||
break
|
||||
}
|
||||
}
|
||||
@@ -176,7 +203,7 @@ func Http(ctx *middleware.Context, params martini.Params) {
|
||||
config := Config{setting.RepoRootPath, "git", true, true, f}
|
||||
|
||||
handler := HttpBackend(&config)
|
||||
handler(ctx.ResponseWriter, ctx.Req)
|
||||
handler(ctx.Resp, ctx.Req)
|
||||
}
|
||||
|
||||
type route struct {
|
||||
@@ -229,7 +256,7 @@ func HttpBackend(config *Config) http.HandlerFunc {
|
||||
dir, err := getGitDir(config, m[1])
|
||||
|
||||
if err != nil {
|
||||
log.GitLogger.Error(err.Error())
|
||||
log.GitLogger.Error(4, err.Error())
|
||||
renderNotFound(w)
|
||||
return
|
||||
}
|
||||
@@ -283,7 +310,7 @@ func serviceRpc(rpc string, hr handler) {
|
||||
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
log.GitLogger.Error(err.Error())
|
||||
log.GitLogger.Error(4, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
@@ -366,7 +393,7 @@ func getGitDir(config *Config, fPath string) (string, error) {
|
||||
cwd, err := os.Getwd()
|
||||
|
||||
if err != nil {
|
||||
log.GitLogger.Error(err.Error())
|
||||
log.GitLogger.Error(4, err.Error())
|
||||
return "", err
|
||||
}
|
||||
|
||||
@@ -443,7 +470,7 @@ func gitCommand(gitBinPath, dir string, args ...string) []byte {
|
||||
out, err := command.Output()
|
||||
|
||||
if err != nil {
|
||||
log.GitLogger.Error(err.Error())
|
||||
log.GitLogger.Error(4, err.Error())
|
||||
}
|
||||
|
||||
return out
|
||||
|
File diff suppressed because it is too large
Load Diff
@@ -5,8 +5,6 @@
|
||||
package repo
|
||||
|
||||
import (
|
||||
"github.com/go-martini/martini"
|
||||
|
||||
"github.com/gogits/gogs/modules/base"
|
||||
"github.com/gogits/gogs/modules/middleware"
|
||||
)
|
||||
@@ -15,7 +13,7 @@ const (
|
||||
PULLS base.TplName = "repo/pulls"
|
||||
)
|
||||
|
||||
func Pulls(ctx *middleware.Context, params martini.Params) {
|
||||
func Pulls(ctx *middleware.Context) {
|
||||
ctx.Data["IsRepoToolbarPulls"] = true
|
||||
ctx.HTML(200, PULLS)
|
||||
}
|
||||
|
@@ -5,13 +5,13 @@
|
||||
package repo
|
||||
|
||||
import (
|
||||
"github.com/go-martini/martini"
|
||||
// "github.com/go-martini/martini"
|
||||
|
||||
"github.com/gogits/gogs/models"
|
||||
"github.com/gogits/gogs/modules/auth"
|
||||
// "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/log"
|
||||
// "github.com/gogits/gogs/modules/middleware"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -20,215 +20,215 @@ const (
|
||||
RELEASE_EDIT base.TplName = "repo/release/edit"
|
||||
)
|
||||
|
||||
func Releases(ctx *middleware.Context) {
|
||||
ctx.Data["Title"] = "Releases"
|
||||
ctx.Data["IsRepoToolbarReleases"] = true
|
||||
ctx.Data["IsRepoReleaseNew"] = false
|
||||
rawTags, err := ctx.Repo.GitRepo.GetTags()
|
||||
if err != nil {
|
||||
ctx.Handle(500, "release.Releases(GetTags)", err)
|
||||
return
|
||||
}
|
||||
// func Releases(ctx *middleware.Context) {
|
||||
// ctx.Data["Title"] = "Releases"
|
||||
// ctx.Data["IsRepoToolbarReleases"] = true
|
||||
// ctx.Data["IsRepoReleaseNew"] = false
|
||||
// rawTags, err := ctx.Repo.GitRepo.GetTags()
|
||||
// if err != nil {
|
||||
// ctx.Handle(500, "release.Releases(GetTags)", err)
|
||||
// return
|
||||
// }
|
||||
|
||||
rels, err := models.GetReleasesByRepoId(ctx.Repo.Repository.Id)
|
||||
if err != nil {
|
||||
ctx.Handle(500, "release.Releases(GetReleasesByRepoId)", err)
|
||||
return
|
||||
}
|
||||
// rels, err := models.GetReleasesByRepoId(ctx.Repo.Repository.Id)
|
||||
// if err != nil {
|
||||
// ctx.Handle(500, "release.Releases(GetReleasesByRepoId)", err)
|
||||
// return
|
||||
// }
|
||||
|
||||
commitsCount, err := ctx.Repo.Commit.CommitsCount()
|
||||
if err != nil {
|
||||
ctx.Handle(500, "release.Releases(CommitsCount)", err)
|
||||
return
|
||||
}
|
||||
// commitsCount, err := ctx.Repo.Commit.CommitsCount()
|
||||
// if err != nil {
|
||||
// ctx.Handle(500, "release.Releases(CommitsCount)", err)
|
||||
// return
|
||||
// }
|
||||
|
||||
// Temproray cache commits count of used branches to speed up.
|
||||
countCache := make(map[string]int)
|
||||
// // Temproray cache commits count of used branches to speed up.
|
||||
// countCache := make(map[string]int)
|
||||
|
||||
tags := make([]*models.Release, len(rawTags))
|
||||
for i, rawTag := range rawTags {
|
||||
for _, rel := range rels {
|
||||
if rel.IsDraft && !ctx.Repo.IsOwner {
|
||||
continue
|
||||
}
|
||||
if rel.TagName == rawTag {
|
||||
rel.Publisher, err = models.GetUserById(rel.PublisherId)
|
||||
if err != nil {
|
||||
ctx.Handle(500, "release.Releases(GetUserById)", err)
|
||||
return
|
||||
}
|
||||
// Get corresponding target if it's not the current branch.
|
||||
if ctx.Repo.BranchName != rel.Target {
|
||||
// Get count if not exists.
|
||||
if _, ok := countCache[rel.Target]; !ok {
|
||||
commit, err := ctx.Repo.GitRepo.GetCommitOfTag(rel.TagName)
|
||||
if err != nil {
|
||||
ctx.Handle(500, "release.Releases(GetCommitOfTag)", err)
|
||||
return
|
||||
}
|
||||
countCache[rel.Target], err = commit.CommitsCount()
|
||||
if err != nil {
|
||||
ctx.Handle(500, "release.Releases(CommitsCount2)", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
rel.NumCommitsBehind = countCache[rel.Target] - rel.NumCommits
|
||||
} else {
|
||||
rel.NumCommitsBehind = commitsCount - rel.NumCommits
|
||||
}
|
||||
// tags := make([]*models.Release, len(rawTags))
|
||||
// for i, rawTag := range rawTags {
|
||||
// for _, rel := range rels {
|
||||
// if rel.IsDraft && !ctx.Repo.IsOwner {
|
||||
// continue
|
||||
// }
|
||||
// if rel.TagName == rawTag {
|
||||
// rel.Publisher, err = models.GetUserById(rel.PublisherId)
|
||||
// if err != nil {
|
||||
// ctx.Handle(500, "release.Releases(GetUserById)", err)
|
||||
// return
|
||||
// }
|
||||
// // Get corresponding target if it's not the current branch.
|
||||
// if ctx.Repo.BranchName != rel.Target {
|
||||
// // Get count if not exists.
|
||||
// if _, ok := countCache[rel.Target]; !ok {
|
||||
// commit, err := ctx.Repo.GitRepo.GetCommitOfTag(rel.TagName)
|
||||
// if err != nil {
|
||||
// ctx.Handle(500, "release.Releases(GetCommitOfTag)", err)
|
||||
// return
|
||||
// }
|
||||
// countCache[rel.Target], err = commit.CommitsCount()
|
||||
// if err != nil {
|
||||
// ctx.Handle(500, "release.Releases(CommitsCount2)", err)
|
||||
// return
|
||||
// }
|
||||
// }
|
||||
// rel.NumCommitsBehind = countCache[rel.Target] - rel.NumCommits
|
||||
// } else {
|
||||
// rel.NumCommitsBehind = commitsCount - rel.NumCommits
|
||||
// }
|
||||
|
||||
rel.Note = base.RenderMarkdownString(rel.Note, ctx.Repo.RepoLink)
|
||||
tags[i] = rel
|
||||
break
|
||||
}
|
||||
}
|
||||
// rel.Note = base.RenderMarkdownString(rel.Note, ctx.Repo.RepoLink)
|
||||
// tags[i] = rel
|
||||
// break
|
||||
// }
|
||||
// }
|
||||
|
||||
if tags[i] == nil {
|
||||
commit, err := ctx.Repo.GitRepo.GetCommitOfTag(rawTag)
|
||||
if err != nil {
|
||||
ctx.Handle(500, "release.Releases(GetCommitOfTag2)", err)
|
||||
return
|
||||
}
|
||||
// if tags[i] == nil {
|
||||
// commit, err := ctx.Repo.GitRepo.GetCommitOfTag(rawTag)
|
||||
// if err != nil {
|
||||
// ctx.Handle(500, "release.Releases(GetCommitOfTag2)", err)
|
||||
// return
|
||||
// }
|
||||
|
||||
tags[i] = &models.Release{
|
||||
Title: rawTag,
|
||||
TagName: rawTag,
|
||||
Sha1: commit.Id.String(),
|
||||
}
|
||||
// tags[i] = &models.Release{
|
||||
// Title: rawTag,
|
||||
// TagName: rawTag,
|
||||
// Sha1: commit.Id.String(),
|
||||
// }
|
||||
|
||||
tags[i].NumCommits, err = ctx.Repo.GitRepo.CommitsCount(commit.Id.String())
|
||||
if err != nil {
|
||||
ctx.Handle(500, "release.Releases(CommitsCount)", err)
|
||||
return
|
||||
}
|
||||
tags[i].NumCommitsBehind = commitsCount - tags[i].NumCommits
|
||||
}
|
||||
}
|
||||
models.SortReleases(tags)
|
||||
ctx.Data["Releases"] = tags
|
||||
ctx.HTML(200, RELEASES)
|
||||
}
|
||||
// tags[i].NumCommits, err = ctx.Repo.GitRepo.CommitsCount(commit.Id.String())
|
||||
// if err != nil {
|
||||
// ctx.Handle(500, "release.Releases(CommitsCount)", err)
|
||||
// return
|
||||
// }
|
||||
// tags[i].NumCommitsBehind = commitsCount - tags[i].NumCommits
|
||||
// }
|
||||
// }
|
||||
// models.SortReleases(tags)
|
||||
// ctx.Data["Releases"] = tags
|
||||
// ctx.HTML(200, RELEASES)
|
||||
// }
|
||||
|
||||
func NewRelease(ctx *middleware.Context) {
|
||||
if !ctx.Repo.IsOwner {
|
||||
ctx.Handle(403, "release.ReleasesNew", nil)
|
||||
return
|
||||
}
|
||||
// func NewRelease(ctx *middleware.Context) {
|
||||
// if !ctx.Repo.IsOwner {
|
||||
// ctx.Handle(403, "release.ReleasesNew", nil)
|
||||
// return
|
||||
// }
|
||||
|
||||
ctx.Data["Title"] = "New Release"
|
||||
ctx.Data["IsRepoToolbarReleases"] = true
|
||||
ctx.Data["IsRepoReleaseNew"] = true
|
||||
ctx.HTML(200, RELEASE_NEW)
|
||||
}
|
||||
// ctx.Data["Title"] = "New Release"
|
||||
// ctx.Data["IsRepoToolbarReleases"] = true
|
||||
// ctx.Data["IsRepoReleaseNew"] = true
|
||||
// ctx.HTML(200, RELEASE_NEW)
|
||||
// }
|
||||
|
||||
func NewReleasePost(ctx *middleware.Context, form auth.NewReleaseForm) {
|
||||
if !ctx.Repo.IsOwner {
|
||||
ctx.Handle(403, "release.ReleasesNew", nil)
|
||||
return
|
||||
}
|
||||
// func NewReleasePost(ctx *middleware.Context, form auth.NewReleaseForm) {
|
||||
// if !ctx.Repo.IsOwner {
|
||||
// ctx.Handle(403, "release.ReleasesNew", nil)
|
||||
// return
|
||||
// }
|
||||
|
||||
ctx.Data["Title"] = "New Release"
|
||||
ctx.Data["IsRepoToolbarReleases"] = true
|
||||
ctx.Data["IsRepoReleaseNew"] = true
|
||||
// ctx.Data["Title"] = "New Release"
|
||||
// ctx.Data["IsRepoToolbarReleases"] = true
|
||||
// ctx.Data["IsRepoReleaseNew"] = true
|
||||
|
||||
if ctx.HasError() {
|
||||
ctx.HTML(200, RELEASE_NEW)
|
||||
return
|
||||
}
|
||||
// if ctx.HasError() {
|
||||
// ctx.HTML(200, RELEASE_NEW)
|
||||
// return
|
||||
// }
|
||||
|
||||
commitsCount, err := ctx.Repo.Commit.CommitsCount()
|
||||
if err != nil {
|
||||
ctx.Handle(500, "release.ReleasesNewPost(CommitsCount)", err)
|
||||
return
|
||||
}
|
||||
// commitsCount, err := ctx.Repo.Commit.CommitsCount()
|
||||
// if err != nil {
|
||||
// ctx.Handle(500, "release.ReleasesNewPost(CommitsCount)", err)
|
||||
// return
|
||||
// }
|
||||
|
||||
if !ctx.Repo.GitRepo.IsBranchExist(form.Target) {
|
||||
ctx.RenderWithErr("Target branch does not exist", "release/new", &form)
|
||||
return
|
||||
}
|
||||
// if !ctx.Repo.GitRepo.IsBranchExist(form.Target) {
|
||||
// ctx.RenderWithErr("Target branch does not exist", "release/new", &form)
|
||||
// return
|
||||
// }
|
||||
|
||||
rel := &models.Release{
|
||||
RepoId: ctx.Repo.Repository.Id,
|
||||
PublisherId: ctx.User.Id,
|
||||
Title: form.Title,
|
||||
TagName: form.TagName,
|
||||
Target: form.Target,
|
||||
Sha1: ctx.Repo.Commit.Id.String(),
|
||||
NumCommits: commitsCount,
|
||||
Note: form.Content,
|
||||
IsDraft: len(form.Draft) > 0,
|
||||
IsPrerelease: form.Prerelease,
|
||||
}
|
||||
// rel := &models.Release{
|
||||
// RepoId: ctx.Repo.Repository.Id,
|
||||
// PublisherId: ctx.User.Id,
|
||||
// Title: form.Title,
|
||||
// TagName: form.TagName,
|
||||
// Target: form.Target,
|
||||
// Sha1: ctx.Repo.Commit.Id.String(),
|
||||
// NumCommits: commitsCount,
|
||||
// Note: form.Content,
|
||||
// IsDraft: len(form.Draft) > 0,
|
||||
// IsPrerelease: form.Prerelease,
|
||||
// }
|
||||
|
||||
if err = models.CreateRelease(ctx.Repo.GitRepo, rel); err != nil {
|
||||
if err == models.ErrReleaseAlreadyExist {
|
||||
ctx.RenderWithErr("Release with this tag name has already existed", "release/new", &form)
|
||||
} else {
|
||||
ctx.Handle(500, "release.ReleasesNewPost(IsReleaseExist)", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
log.Trace("%s Release created: %s/%s:%s", ctx.Req.RequestURI, ctx.User.LowerName, ctx.Repo.Repository.Name, form.TagName)
|
||||
// if err = models.CreateRelease(ctx.Repo.GitRepo, rel); err != nil {
|
||||
// if err == models.ErrReleaseAlreadyExist {
|
||||
// ctx.RenderWithErr("Release with this tag name has already existed", "release/new", &form)
|
||||
// } else {
|
||||
// ctx.Handle(500, "release.ReleasesNewPost(IsReleaseExist)", err)
|
||||
// }
|
||||
// return
|
||||
// }
|
||||
// log.Trace("%s Release created: %s/%s:%s", ctx.Req.RequestURI, ctx.User.LowerName, ctx.Repo.Repository.Name, form.TagName)
|
||||
|
||||
ctx.Redirect(ctx.Repo.RepoLink + "/releases")
|
||||
}
|
||||
// ctx.Redirect(ctx.Repo.RepoLink + "/releases")
|
||||
// }
|
||||
|
||||
func EditRelease(ctx *middleware.Context, params martini.Params) {
|
||||
if !ctx.Repo.IsOwner {
|
||||
ctx.Handle(403, "release.ReleasesEdit", nil)
|
||||
return
|
||||
}
|
||||
// func EditRelease(ctx *middleware.Context, params martini.Params) {
|
||||
// if !ctx.Repo.IsOwner {
|
||||
// ctx.Handle(403, "release.ReleasesEdit", nil)
|
||||
// return
|
||||
// }
|
||||
|
||||
tagName := params["tagname"]
|
||||
rel, err := models.GetRelease(ctx.Repo.Repository.Id, tagName)
|
||||
if err != nil {
|
||||
if err == models.ErrReleaseNotExist {
|
||||
ctx.Handle(404, "release.ReleasesEdit(GetRelease)", err)
|
||||
} else {
|
||||
ctx.Handle(500, "release.ReleasesEdit(GetRelease)", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
ctx.Data["Release"] = rel
|
||||
// tagName := params["tagname"]
|
||||
// rel, err := models.GetRelease(ctx.Repo.Repository.Id, tagName)
|
||||
// if err != nil {
|
||||
// if err == models.ErrReleaseNotExist {
|
||||
// ctx.Handle(404, "release.ReleasesEdit(GetRelease)", err)
|
||||
// } else {
|
||||
// ctx.Handle(500, "release.ReleasesEdit(GetRelease)", err)
|
||||
// }
|
||||
// return
|
||||
// }
|
||||
// ctx.Data["Release"] = rel
|
||||
|
||||
ctx.Data["Title"] = "Edit Release"
|
||||
ctx.Data["IsRepoToolbarReleases"] = true
|
||||
ctx.HTML(200, RELEASE_EDIT)
|
||||
}
|
||||
// ctx.Data["Title"] = "Edit Release"
|
||||
// ctx.Data["IsRepoToolbarReleases"] = true
|
||||
// ctx.HTML(200, RELEASE_EDIT)
|
||||
// }
|
||||
|
||||
func EditReleasePost(ctx *middleware.Context, params martini.Params, form auth.EditReleaseForm) {
|
||||
if !ctx.Repo.IsOwner {
|
||||
ctx.Handle(403, "release.EditReleasePost", nil)
|
||||
return
|
||||
}
|
||||
// func EditReleasePost(ctx *middleware.Context, params martini.Params, form auth.EditReleaseForm) {
|
||||
// if !ctx.Repo.IsOwner {
|
||||
// ctx.Handle(403, "release.EditReleasePost", nil)
|
||||
// return
|
||||
// }
|
||||
|
||||
tagName := params["tagname"]
|
||||
rel, err := models.GetRelease(ctx.Repo.Repository.Id, tagName)
|
||||
if err != nil {
|
||||
if err == models.ErrReleaseNotExist {
|
||||
ctx.Handle(404, "release.EditReleasePost(GetRelease)", err)
|
||||
} else {
|
||||
ctx.Handle(500, "release.EditReleasePost(GetRelease)", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
ctx.Data["Release"] = rel
|
||||
// tagName := params["tagname"]
|
||||
// rel, err := models.GetRelease(ctx.Repo.Repository.Id, tagName)
|
||||
// if err != nil {
|
||||
// if err == models.ErrReleaseNotExist {
|
||||
// ctx.Handle(404, "release.EditReleasePost(GetRelease)", err)
|
||||
// } else {
|
||||
// ctx.Handle(500, "release.EditReleasePost(GetRelease)", err)
|
||||
// }
|
||||
// return
|
||||
// }
|
||||
// ctx.Data["Release"] = rel
|
||||
|
||||
if ctx.HasError() {
|
||||
ctx.HTML(200, RELEASE_EDIT)
|
||||
return
|
||||
}
|
||||
// if ctx.HasError() {
|
||||
// ctx.HTML(200, RELEASE_EDIT)
|
||||
// return
|
||||
// }
|
||||
|
||||
ctx.Data["Title"] = "Edit Release"
|
||||
ctx.Data["IsRepoToolbarReleases"] = true
|
||||
// ctx.Data["Title"] = "Edit Release"
|
||||
// ctx.Data["IsRepoToolbarReleases"] = true
|
||||
|
||||
rel.Title = form.Title
|
||||
rel.Note = form.Content
|
||||
rel.IsDraft = len(form.Draft) > 0
|
||||
rel.IsPrerelease = form.Prerelease
|
||||
if err = models.UpdateRelease(ctx.Repo.GitRepo, rel); err != nil {
|
||||
ctx.Handle(500, "release.EditReleasePost(UpdateRelease)", err)
|
||||
return
|
||||
}
|
||||
ctx.Redirect(ctx.Repo.RepoLink + "/releases")
|
||||
}
|
||||
// rel.Title = form.Title
|
||||
// rel.Note = form.Content
|
||||
// rel.IsDraft = len(form.Draft) > 0
|
||||
// rel.IsPrerelease = form.Prerelease
|
||||
// if err = models.UpdateRelease(ctx.Repo.GitRepo, rel); err != nil {
|
||||
// ctx.Handle(500, "release.EditReleasePost(UpdateRelease)", err)
|
||||
// return
|
||||
// }
|
||||
// ctx.Redirect(ctx.Repo.RepoLink + "/releases")
|
||||
// }
|
||||
|
@@ -5,22 +5,15 @@
|
||||
package repo
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/go-martini/martini"
|
||||
|
||||
"github.com/gogits/git"
|
||||
"github.com/Unknwon/com"
|
||||
|
||||
"github.com/gogits/gogs/models"
|
||||
"github.com/gogits/gogs/modules/auth"
|
||||
"github.com/gogits/gogs/modules/base"
|
||||
"github.com/gogits/gogs/modules/git"
|
||||
"github.com/gogits/gogs/modules/log"
|
||||
"github.com/gogits/gogs/modules/middleware"
|
||||
)
|
||||
@@ -28,47 +21,63 @@ import (
|
||||
const (
|
||||
CREATE base.TplName = "repo/create"
|
||||
MIGRATE base.TplName = "repo/migrate"
|
||||
SINGLE base.TplName = "repo/single"
|
||||
)
|
||||
|
||||
func Create(ctx *middleware.Context) {
|
||||
ctx.Data["Title"] = "Create repository"
|
||||
ctx.Data["PageIsNewRepo"] = true
|
||||
ctx.Data["LanguageIgns"] = models.LanguageIgns
|
||||
ctx.Data["Title"] = ctx.Tr("new_repo")
|
||||
ctx.Data["PageIsRepoCreate"] = true
|
||||
|
||||
// Give default value for template to render.
|
||||
ctx.Data["gitignore"] = "0"
|
||||
ctx.Data["license"] = "0"
|
||||
ctx.Data["Gitignores"] = models.Gitignores
|
||||
ctx.Data["Licenses"] = models.Licenses
|
||||
|
||||
ctxUser := ctx.User
|
||||
orgId, _ := base.StrTo(ctx.Query("org")).Int64()
|
||||
if orgId > 0 {
|
||||
org, err := models.GetUserById(orgId)
|
||||
if err != nil && err != models.ErrUserNotExist {
|
||||
ctx.Handle(500, "home.Dashboard(GetUserById)", err)
|
||||
return
|
||||
}
|
||||
ctxUser = org
|
||||
}
|
||||
// orgId := com.StrTo(ctx.Query("org")).MustInt64()
|
||||
// if orgId > 0 {
|
||||
// org, err := models.GetUserById(orgId)
|
||||
// if err != nil && err != models.ErrUserNotExist {
|
||||
// ctx.Handle(500, "home.Dashboard(GetUserById)", err)
|
||||
// return
|
||||
// }
|
||||
// ctxUser = org
|
||||
// }
|
||||
ctx.Data["ContextUser"] = ctxUser
|
||||
|
||||
if err := ctx.User.GetOrganizations(); err != nil {
|
||||
ctx.Handle(500, "home.Dashboard(GetOrganizations)", err)
|
||||
return
|
||||
}
|
||||
ctx.Data["AllUsers"] = append([]*models.User{ctx.User}, ctx.User.Orgs...)
|
||||
// if err := ctx.User.GetOrganizations(); err != nil {
|
||||
// ctx.Handle(500, "home.Dashboard(GetOrganizations)", err)
|
||||
// return
|
||||
// }
|
||||
// ctx.Data["AllUsers"] = append([]*models.User{ctx.User}, ctx.User.Orgs...)
|
||||
|
||||
ctx.HTML(200, CREATE)
|
||||
}
|
||||
|
||||
func CreatePost(ctx *middleware.Context, form auth.CreateRepoForm) {
|
||||
ctx.Data["Title"] = "Create repository"
|
||||
ctx.Data["PageIsNewRepo"] = true
|
||||
ctx.Data["LanguageIgns"] = models.LanguageIgns
|
||||
ctx.Data["Title"] = ctx.Tr("new_repo")
|
||||
ctx.Data["PageIsRepoCreate"] = true
|
||||
|
||||
ctx.Data["Gitignores"] = models.Gitignores
|
||||
ctx.Data["Licenses"] = models.Licenses
|
||||
|
||||
if err := ctx.User.GetOrganizations(); err != nil {
|
||||
ctx.Handle(500, "home.CreatePost(GetOrganizations)", err)
|
||||
return
|
||||
}
|
||||
ctx.Data["Orgs"] = ctx.User.Orgs
|
||||
ctxUser := ctx.User
|
||||
// orgId := com.StrTo(ctx.Query("org")).MustInt64()
|
||||
// if orgId > 0 {
|
||||
// org, err := models.GetUserById(orgId)
|
||||
// if err != nil && err != models.ErrUserNotExist {
|
||||
// ctx.Handle(500, "home.Dashboard(GetUserById)", err)
|
||||
// return
|
||||
// }
|
||||
// ctxUser = org
|
||||
// }
|
||||
ctx.Data["ContextUser"] = ctxUser
|
||||
|
||||
// if err := ctx.User.GetOrganizations(); err != nil {
|
||||
// ctx.Handle(500, "home.CreatePost(GetOrganizations)", err)
|
||||
// return
|
||||
// }
|
||||
// ctx.Data["Orgs"] = ctx.User.Orgs
|
||||
|
||||
if ctx.HasError() {
|
||||
ctx.HTML(200, CREATE)
|
||||
@@ -97,337 +106,159 @@ func CreatePost(ctx *middleware.Context, form auth.CreateRepoForm) {
|
||||
}
|
||||
|
||||
repo, err := models.CreateRepository(u, form.RepoName, form.Description,
|
||||
form.Language, form.License, form.Private, false, form.InitReadme)
|
||||
form.Gitignore, form.License, form.Private, false, form.InitReadme)
|
||||
if err == nil {
|
||||
log.Trace("%s Repository created: %s/%s", ctx.Req.RequestURI, u.LowerName, form.RepoName)
|
||||
log.Trace("Repository created: %s/%s", u.Name, form.RepoName)
|
||||
ctx.Redirect("/" + u.Name + "/" + form.RepoName)
|
||||
return
|
||||
} else if err == models.ErrRepoAlreadyExist {
|
||||
ctx.RenderWithErr("Repository name has already been used", CREATE, &form)
|
||||
ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), CREATE, &form)
|
||||
return
|
||||
} else if err == models.ErrRepoNameIllegal {
|
||||
ctx.RenderWithErr(models.ErrRepoNameIllegal.Error(), CREATE, &form)
|
||||
ctx.RenderWithErr(ctx.Tr("form.illegal_repo_name"), CREATE, &form)
|
||||
return
|
||||
}
|
||||
|
||||
if repo != nil {
|
||||
if errDelete := models.DeleteRepository(u.Id, repo.Id, u.Name); errDelete != nil {
|
||||
log.Error("repo.CreatePost(DeleteRepository): %v", errDelete)
|
||||
log.Error(4, "DeleteRepository: %v", errDelete)
|
||||
}
|
||||
}
|
||||
ctx.Handle(500, "repo.CreatePost(CreateRepository)", err)
|
||||
ctx.Handle(500, "CreateRepository", err)
|
||||
}
|
||||
|
||||
func Migrate(ctx *middleware.Context) {
|
||||
ctx.Data["Title"] = "Migrate repository"
|
||||
ctx.Data["PageIsNewRepo"] = true
|
||||
// func Migrate(ctx *middleware.Context) {
|
||||
// ctx.Data["Title"] = "Migrate repository"
|
||||
// ctx.Data["PageIsNewRepo"] = true
|
||||
|
||||
if err := ctx.User.GetOrganizations(); err != nil {
|
||||
ctx.Handle(500, "home.Migrate(GetOrganizations)", err)
|
||||
return
|
||||
}
|
||||
ctx.Data["Orgs"] = ctx.User.Orgs
|
||||
// if err := ctx.User.GetOrganizations(); err != nil {
|
||||
// ctx.Handle(500, "home.Migrate(GetOrganizations)", err)
|
||||
// return
|
||||
// }
|
||||
// ctx.Data["Orgs"] = ctx.User.Orgs
|
||||
|
||||
ctx.HTML(200, MIGRATE)
|
||||
}
|
||||
// ctx.HTML(200, MIGRATE)
|
||||
// }
|
||||
|
||||
func MigratePost(ctx *middleware.Context, form auth.MigrateRepoForm) {
|
||||
ctx.Data["Title"] = "Migrate repository"
|
||||
ctx.Data["PageIsNewRepo"] = true
|
||||
// func MigratePost(ctx *middleware.Context, form auth.MigrateRepoForm) {
|
||||
// ctx.Data["Title"] = "Migrate repository"
|
||||
// ctx.Data["PageIsNewRepo"] = true
|
||||
|
||||
if err := ctx.User.GetOrganizations(); err != nil {
|
||||
ctx.Handle(500, "home.MigratePost(GetOrganizations)", err)
|
||||
return
|
||||
}
|
||||
ctx.Data["Orgs"] = ctx.User.Orgs
|
||||
// if err := ctx.User.GetOrganizations(); err != nil {
|
||||
// ctx.Handle(500, "home.MigratePost(GetOrganizations)", err)
|
||||
// return
|
||||
// }
|
||||
// ctx.Data["Orgs"] = ctx.User.Orgs
|
||||
|
||||
if ctx.HasError() {
|
||||
ctx.HTML(200, MIGRATE)
|
||||
// if ctx.HasError() {
|
||||
// ctx.HTML(200, MIGRATE)
|
||||
// return
|
||||
// }
|
||||
|
||||
// u := ctx.User
|
||||
// // Not equal means current user is an organization.
|
||||
// if u.Id != form.Uid {
|
||||
// var err error
|
||||
// u, err = models.GetUserById(form.Uid)
|
||||
// if err != nil {
|
||||
// if err == models.ErrUserNotExist {
|
||||
// ctx.Handle(404, "home.MigratePost(GetUserById)", err)
|
||||
// } else {
|
||||
// ctx.Handle(500, "home.MigratePost(GetUserById)", err)
|
||||
// }
|
||||
// return
|
||||
// }
|
||||
// }
|
||||
|
||||
// authStr := strings.Replace(fmt.Sprintf("://%s:%s",
|
||||
// form.AuthUserName, form.AuthPasswd), "@", "%40", -1)
|
||||
// url := strings.Replace(form.Url, "://", authStr+"@", 1)
|
||||
// repo, err := models.MigrateRepository(u, form.RepoName, form.Description, form.Private,
|
||||
// form.Mirror, url)
|
||||
// if err == nil {
|
||||
// log.Trace("%s Repository migrated: %s/%s", ctx.Req.RequestURI, u.LowerName, form.RepoName)
|
||||
// ctx.Redirect("/" + u.Name + "/" + form.RepoName)
|
||||
// return
|
||||
// } else if err == models.ErrRepoAlreadyExist {
|
||||
// ctx.RenderWithErr("Repository name has already been used", MIGRATE, &form)
|
||||
// return
|
||||
// } else if err == models.ErrRepoNameIllegal {
|
||||
// ctx.RenderWithErr(models.ErrRepoNameIllegal.Error(), MIGRATE, &form)
|
||||
// return
|
||||
// }
|
||||
|
||||
// if repo != nil {
|
||||
// if errDelete := models.DeleteRepository(u.Id, repo.Id, u.Name); errDelete != nil {
|
||||
// log.Error("repo.MigratePost(DeleteRepository): %v", errDelete)
|
||||
// }
|
||||
// }
|
||||
|
||||
// if strings.Contains(err.Error(), "Authentication failed") {
|
||||
// ctx.RenderWithErr(err.Error(), MIGRATE, &form)
|
||||
// return
|
||||
// }
|
||||
// ctx.Handle(500, "repo.Migrate(MigrateRepository)", err)
|
||||
// }
|
||||
|
||||
// func Action(ctx *middleware.Context, params martini.Params) {
|
||||
// var err error
|
||||
// switch params["action"] {
|
||||
// case "watch":
|
||||
// err = models.WatchRepo(ctx.User.Id, ctx.Repo.Repository.Id, true)
|
||||
// case "unwatch":
|
||||
// err = models.WatchRepo(ctx.User.Id, ctx.Repo.Repository.Id, false)
|
||||
// case "desc":
|
||||
// if !ctx.Repo.IsOwner {
|
||||
// ctx.Error(404)
|
||||
// return
|
||||
// }
|
||||
|
||||
// ctx.Repo.Repository.Description = ctx.Query("desc")
|
||||
// ctx.Repo.Repository.Website = ctx.Query("site")
|
||||
// err = models.UpdateRepository(ctx.Repo.Repository)
|
||||
// }
|
||||
|
||||
// if err != nil {
|
||||
// log.Error("repo.Action(%s): %v", params["action"], err)
|
||||
// ctx.JSON(200, map[string]interface{}{
|
||||
// "ok": false,
|
||||
// "err": err.Error(),
|
||||
// })
|
||||
// return
|
||||
// }
|
||||
// ctx.JSON(200, map[string]interface{}{
|
||||
// "ok": true,
|
||||
// })
|
||||
// }
|
||||
|
||||
func Download(ctx *middleware.Context) {
|
||||
ext := "." + ctx.Params(":ext")
|
||||
|
||||
var archivePath string
|
||||
switch ext {
|
||||
case ".zip":
|
||||
archivePath = path.Join(ctx.Repo.GitRepo.Path, "archives/zip")
|
||||
case ".tar.gz":
|
||||
archivePath = path.Join(ctx.Repo.GitRepo.Path, "archives/targz")
|
||||
default:
|
||||
ctx.Error(404)
|
||||
return
|
||||
}
|
||||
|
||||
u := ctx.User
|
||||
// Not equal means current user is an organization.
|
||||
if u.Id != form.Uid {
|
||||
var err error
|
||||
u, err = models.GetUserById(form.Uid)
|
||||
if err != nil {
|
||||
if err == models.ErrUserNotExist {
|
||||
ctx.Handle(404, "home.MigratePost(GetUserById)", err)
|
||||
} else {
|
||||
ctx.Handle(500, "home.MigratePost(GetUserById)", err)
|
||||
}
|
||||
if !com.IsDir(archivePath) {
|
||||
if err := os.MkdirAll(archivePath, os.ModePerm); err != nil {
|
||||
ctx.Handle(500, "Download -> os.MkdirAll(archivePath)", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
authStr := strings.Replace(fmt.Sprintf("://%s:%s",
|
||||
form.AuthUserName, form.AuthPasswd), "@", "%40", -1)
|
||||
url := strings.Replace(form.Url, "://", authStr+"@", 1)
|
||||
repo, err := models.MigrateRepository(u, form.RepoName, form.Description, form.Private,
|
||||
form.Mirror, url)
|
||||
if err == nil {
|
||||
log.Trace("%s Repository migrated: %s/%s", ctx.Req.RequestURI, u.LowerName, form.RepoName)
|
||||
ctx.Redirect("/" + u.Name + "/" + form.RepoName)
|
||||
return
|
||||
} else if err == models.ErrRepoAlreadyExist {
|
||||
ctx.RenderWithErr("Repository name has already been used", MIGRATE, &form)
|
||||
return
|
||||
} else if err == models.ErrRepoNameIllegal {
|
||||
ctx.RenderWithErr(models.ErrRepoNameIllegal.Error(), MIGRATE, &form)
|
||||
return
|
||||
}
|
||||
|
||||
if repo != nil {
|
||||
if errDelete := models.DeleteRepository(u.Id, repo.Id, u.Name); errDelete != nil {
|
||||
log.Error("repo.MigratePost(DeleteRepository): %v", errDelete)
|
||||
}
|
||||
}
|
||||
|
||||
if strings.Contains(err.Error(), "Authentication failed") {
|
||||
ctx.RenderWithErr(err.Error(), MIGRATE, &form)
|
||||
return
|
||||
}
|
||||
ctx.Handle(500, "repo.Migrate(MigrateRepository)", err)
|
||||
}
|
||||
|
||||
func Single(ctx *middleware.Context, params martini.Params) {
|
||||
branchName := ctx.Repo.BranchName
|
||||
userName := ctx.Repo.Owner.Name
|
||||
repoName := ctx.Repo.Repository.Name
|
||||
|
||||
repoLink := ctx.Repo.RepoLink
|
||||
branchLink := ctx.Repo.RepoLink + "/src/" + branchName
|
||||
rawLink := ctx.Repo.RepoLink + "/raw/" + branchName
|
||||
|
||||
// Get tree path
|
||||
treename := params["_1"]
|
||||
|
||||
if len(treename) > 0 && treename[len(treename)-1] == '/' {
|
||||
ctx.Redirect(repoLink + "/src/" + branchName + "/" + treename[:len(treename)-1])
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Data["IsRepoToolbarSource"] = true
|
||||
|
||||
isViewBranch := ctx.Repo.IsBranch
|
||||
ctx.Data["IsViewBranch"] = isViewBranch
|
||||
|
||||
treePath := treename
|
||||
if len(treePath) != 0 {
|
||||
treePath = treePath + "/"
|
||||
}
|
||||
|
||||
entry, err := ctx.Repo.Commit.GetTreeEntryByPath(treename)
|
||||
if err != nil && err != git.ErrNotExist {
|
||||
ctx.Handle(404, "repo.Single(GetTreeEntryByPath)", err)
|
||||
return
|
||||
}
|
||||
|
||||
if len(treename) != 0 && entry == nil {
|
||||
ctx.Handle(404, "repo.Single", nil)
|
||||
return
|
||||
}
|
||||
|
||||
if entry != nil && !entry.IsDir() {
|
||||
blob := entry.Blob()
|
||||
|
||||
if dataRc, err := blob.Data(); err != nil {
|
||||
ctx.Handle(404, "repo.Single(blob.Data)", err)
|
||||
} else {
|
||||
ctx.Data["FileSize"] = blob.Size()
|
||||
ctx.Data["IsFile"] = true
|
||||
ctx.Data["FileName"] = blob.Name()
|
||||
ext := path.Ext(blob.Name())
|
||||
if len(ext) > 0 {
|
||||
ext = ext[1:]
|
||||
}
|
||||
ctx.Data["FileExt"] = ext
|
||||
ctx.Data["FileLink"] = rawLink + "/" + treename
|
||||
|
||||
buf := make([]byte, 1024)
|
||||
n, _ := dataRc.Read(buf)
|
||||
if n > 0 {
|
||||
buf = buf[:n]
|
||||
}
|
||||
|
||||
defer func() {
|
||||
dataRc.Close()
|
||||
}()
|
||||
|
||||
_, isTextFile := base.IsTextFile(buf)
|
||||
_, isImageFile := base.IsImageFile(buf)
|
||||
ctx.Data["FileIsText"] = isTextFile
|
||||
|
||||
switch {
|
||||
case isImageFile:
|
||||
ctx.Data["IsImageFile"] = true
|
||||
case isTextFile:
|
||||
d, _ := ioutil.ReadAll(dataRc)
|
||||
buf = append(buf, d...)
|
||||
readmeExist := base.IsMarkdownFile(blob.Name()) || base.IsReadmeFile(blob.Name())
|
||||
ctx.Data["ReadmeExist"] = readmeExist
|
||||
if readmeExist {
|
||||
ctx.Data["FileContent"] = string(base.RenderMarkdown(buf, ""))
|
||||
} else {
|
||||
ctx.Data["FileContent"] = string(buf)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
// Directory and file list.
|
||||
tree, err := ctx.Repo.Commit.SubTree(treename)
|
||||
if err != nil {
|
||||
ctx.Handle(404, "repo.Single(SubTree)", err)
|
||||
archivePath = path.Join(archivePath, ctx.Repo.CommitId+ext)
|
||||
if !com.IsFile(archivePath) {
|
||||
if err := ctx.Repo.Commit.CreateArchive(archivePath, git.ZIP); err != nil {
|
||||
ctx.Handle(500, "Download -> CreateArchive "+archivePath, err)
|
||||
return
|
||||
}
|
||||
entries := tree.ListEntries()
|
||||
entries.Sort()
|
||||
|
||||
files := make([][]interface{}, 0, len(entries))
|
||||
|
||||
for _, te := range entries {
|
||||
c, err := ctx.Repo.Commit.GetCommitOfRelPath(filepath.Join(treePath, te.Name()))
|
||||
if err != nil {
|
||||
ctx.Handle(404, "repo.Single(SubTree)", err)
|
||||
return
|
||||
}
|
||||
|
||||
files = append(files, []interface{}{te, c})
|
||||
}
|
||||
|
||||
ctx.Data["Files"] = files
|
||||
|
||||
var readmeFile *git.Blob
|
||||
|
||||
for _, f := range entries {
|
||||
if f.IsDir() || !base.IsReadmeFile(f.Name()) {
|
||||
continue
|
||||
} else {
|
||||
readmeFile = f.Blob()
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if readmeFile != nil {
|
||||
ctx.Data["ReadmeInSingle"] = true
|
||||
ctx.Data["ReadmeExist"] = true
|
||||
if dataRc, err := readmeFile.Data(); err != nil {
|
||||
ctx.Handle(404, "repo.Single(readmeFile.LookupBlob)", err)
|
||||
return
|
||||
} else {
|
||||
|
||||
buf := make([]byte, 1024)
|
||||
n, _ := dataRc.Read(buf)
|
||||
if n > 0 {
|
||||
buf = buf[:n]
|
||||
}
|
||||
defer func() {
|
||||
dataRc.Close()
|
||||
}()
|
||||
|
||||
ctx.Data["FileSize"] = readmeFile.Size
|
||||
ctx.Data["FileLink"] = rawLink + "/" + treename
|
||||
_, isTextFile := base.IsTextFile(buf)
|
||||
ctx.Data["FileIsText"] = isTextFile
|
||||
ctx.Data["FileName"] = readmeFile.Name()
|
||||
if isTextFile {
|
||||
d, _ := ioutil.ReadAll(dataRc)
|
||||
buf = append(buf, d...)
|
||||
switch {
|
||||
case base.IsMarkdownFile(readmeFile.Name()):
|
||||
buf = base.RenderMarkdown(buf, branchLink)
|
||||
default:
|
||||
buf = bytes.Replace(buf, []byte("\n"), []byte(`<br>`), -1)
|
||||
}
|
||||
ctx.Data["FileContent"] = string(buf)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ctx.Data["Username"] = userName
|
||||
ctx.Data["Reponame"] = repoName
|
||||
|
||||
var treenames []string
|
||||
Paths := make([]string, 0)
|
||||
|
||||
if len(treename) > 0 {
|
||||
treenames = strings.Split(treename, "/")
|
||||
for i, _ := range treenames {
|
||||
Paths = append(Paths, strings.Join(treenames[0:i+1], "/"))
|
||||
}
|
||||
|
||||
ctx.Data["HasParentPath"] = true
|
||||
if len(Paths)-2 >= 0 {
|
||||
ctx.Data["ParentPath"] = "/" + Paths[len(Paths)-2]
|
||||
}
|
||||
}
|
||||
|
||||
ctx.Data["LastCommit"] = ctx.Repo.Commit
|
||||
ctx.Data["Paths"] = Paths
|
||||
ctx.Data["TreeName"] = treename
|
||||
ctx.Data["Treenames"] = treenames
|
||||
ctx.Data["TreePath"] = treePath
|
||||
ctx.Data["BranchLink"] = branchLink
|
||||
ctx.HTML(200, SINGLE)
|
||||
}
|
||||
|
||||
func basicEncode(username, password string) string {
|
||||
auth := username + ":" + password
|
||||
return base64.StdEncoding.EncodeToString([]byte(auth))
|
||||
}
|
||||
|
||||
func basicDecode(encoded string) (user string, name string, err error) {
|
||||
var s []byte
|
||||
s, err = base64.StdEncoding.DecodeString(encoded)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
a := strings.Split(string(s), ":")
|
||||
if len(a) == 2 {
|
||||
user, name = a[0], a[1]
|
||||
} else {
|
||||
err = errors.New("decode failed")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func authRequired(ctx *middleware.Context) {
|
||||
ctx.ResponseWriter.Header().Set("WWW-Authenticate", "Basic realm=\".\"")
|
||||
ctx.Data["ErrorMsg"] = "no basic auth and digit auth"
|
||||
ctx.HTML(401, base.TplName("status/401"))
|
||||
}
|
||||
|
||||
func Action(ctx *middleware.Context, params martini.Params) {
|
||||
var err error
|
||||
switch params["action"] {
|
||||
case "watch":
|
||||
err = models.WatchRepo(ctx.User.Id, ctx.Repo.Repository.Id, true)
|
||||
case "unwatch":
|
||||
err = models.WatchRepo(ctx.User.Id, ctx.Repo.Repository.Id, false)
|
||||
case "desc":
|
||||
if !ctx.Repo.IsOwner {
|
||||
ctx.Error(404)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Repo.Repository.Description = ctx.Query("desc")
|
||||
ctx.Repo.Repository.Website = ctx.Query("site")
|
||||
err = models.UpdateRepository(ctx.Repo.Repository)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Error("repo.Action(%s): %v", params["action"], err)
|
||||
ctx.JSON(200, map[string]interface{}{
|
||||
"ok": false,
|
||||
"err": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
ctx.JSON(200, map[string]interface{}{
|
||||
"ok": true,
|
||||
})
|
||||
ctx.ServeFile(archivePath, ctx.Repo.Repository.Name+"-"+base.ShortSha(ctx.Repo.CommitId)+ext)
|
||||
}
|
||||
|
@@ -4,362 +4,362 @@
|
||||
|
||||
package repo
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
// import (
|
||||
// "fmt"
|
||||
// "strings"
|
||||
// "time"
|
||||
|
||||
"github.com/go-martini/martini"
|
||||
// "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"
|
||||
"github.com/gogits/gogs/modules/setting"
|
||||
)
|
||||
// "github.com/gogits/gogs-ng/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"
|
||||
// "github.com/gogits/gogs/modules/setting"
|
||||
// )
|
||||
|
||||
const (
|
||||
SETTING base.TplName = "repo/setting"
|
||||
COLLABORATION base.TplName = "repo/collaboration"
|
||||
// const (
|
||||
// SETTING base.TplName = "repo/setting"
|
||||
// COLLABORATION base.TplName = "repo/collaboration"
|
||||
|
||||
HOOKS base.TplName = "repo/hooks"
|
||||
HOOK_ADD base.TplName = "repo/hook_add"
|
||||
HOOK_EDIT base.TplName = "repo/hook_edit"
|
||||
)
|
||||
// HOOKS base.TplName = "repo/hooks"
|
||||
// HOOK_ADD base.TplName = "repo/hook_add"
|
||||
// HOOK_EDIT base.TplName = "repo/hook_edit"
|
||||
// )
|
||||
|
||||
func Setting(ctx *middleware.Context) {
|
||||
ctx.Data["IsRepoToolbarSetting"] = true
|
||||
ctx.Data["Title"] = strings.TrimPrefix(ctx.Repo.RepoLink, "/") + " - settings"
|
||||
ctx.HTML(200, SETTING)
|
||||
}
|
||||
// func Setting(ctx *middleware.Context) {
|
||||
// ctx.Data["IsRepoToolbarSetting"] = true
|
||||
// ctx.Data["Title"] = strings.TrimPrefix(ctx.Repo.RepoLink, "/") + " - settings"
|
||||
// ctx.HTML(200, SETTING)
|
||||
// }
|
||||
|
||||
func SettingPost(ctx *middleware.Context, form auth.RepoSettingForm) {
|
||||
ctx.Data["IsRepoToolbarSetting"] = true
|
||||
// func SettingPost(ctx *middleware.Context, form auth.RepoSettingForm) {
|
||||
// ctx.Data["IsRepoToolbarSetting"] = true
|
||||
|
||||
switch ctx.Query("action") {
|
||||
case "update":
|
||||
if ctx.HasError() {
|
||||
ctx.HTML(200, SETTING)
|
||||
return
|
||||
}
|
||||
// switch ctx.Query("action") {
|
||||
// case "update":
|
||||
// if ctx.HasError() {
|
||||
// ctx.HTML(200, SETTING)
|
||||
// return
|
||||
// }
|
||||
|
||||
newRepoName := form.RepoName
|
||||
// Check if repository name has been changed.
|
||||
if ctx.Repo.Repository.Name != newRepoName {
|
||||
isExist, err := models.IsRepositoryExist(ctx.Repo.Owner, newRepoName)
|
||||
if err != nil {
|
||||
ctx.Handle(500, "setting.SettingPost(update: check existence)", err)
|
||||
return
|
||||
} else if isExist {
|
||||
ctx.RenderWithErr("Repository name has been taken in your repositories.", SETTING, nil)
|
||||
return
|
||||
} else if err = models.ChangeRepositoryName(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name, newRepoName); err != nil {
|
||||
ctx.Handle(500, "setting.SettingPost(change repository name)", err)
|
||||
return
|
||||
}
|
||||
log.Trace("%s Repository name changed: %s/%s -> %s", ctx.Req.RequestURI, ctx.User.Name, ctx.Repo.Repository.Name, newRepoName)
|
||||
// newRepoName := form.RepoName
|
||||
// // Check if repository name has been changed.
|
||||
// if ctx.Repo.Repository.Name != newRepoName {
|
||||
// isExist, err := models.IsRepositoryExist(ctx.Repo.Owner, newRepoName)
|
||||
// if err != nil {
|
||||
// ctx.Handle(500, "setting.SettingPost(update: check existence)", err)
|
||||
// return
|
||||
// } else if isExist {
|
||||
// ctx.RenderWithErr("Repository name has been taken in your repositories.", SETTING, nil)
|
||||
// return
|
||||
// } else if err = models.ChangeRepositoryName(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name, newRepoName); err != nil {
|
||||
// ctx.Handle(500, "setting.SettingPost(change repository name)", err)
|
||||
// return
|
||||
// }
|
||||
// log.Trace("%s Repository name changed: %s/%s -> %s", ctx.Req.RequestURI, ctx.User.Name, ctx.Repo.Repository.Name, newRepoName)
|
||||
|
||||
ctx.Repo.Repository.Name = newRepoName
|
||||
}
|
||||
// ctx.Repo.Repository.Name = newRepoName
|
||||
// }
|
||||
|
||||
br := form.Branch
|
||||
// br := form.Branch
|
||||
|
||||
if ctx.Repo.GitRepo.IsBranchExist(br) {
|
||||
ctx.Repo.Repository.DefaultBranch = br
|
||||
}
|
||||
ctx.Repo.Repository.Description = form.Description
|
||||
ctx.Repo.Repository.Website = form.Website
|
||||
ctx.Repo.Repository.IsPrivate = form.Private
|
||||
ctx.Repo.Repository.IsGoget = form.GoGet
|
||||
if err := models.UpdateRepository(ctx.Repo.Repository); err != nil {
|
||||
ctx.Handle(404, "setting.SettingPost(update)", err)
|
||||
return
|
||||
}
|
||||
log.Trace("%s Repository updated: %s/%s", ctx.Req.RequestURI, ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
|
||||
// if ctx.Repo.GitRepo.IsBranchExist(br) {
|
||||
// ctx.Repo.Repository.DefaultBranch = br
|
||||
// }
|
||||
// ctx.Repo.Repository.Description = form.Description
|
||||
// ctx.Repo.Repository.Website = form.Website
|
||||
// ctx.Repo.Repository.IsPrivate = form.Private
|
||||
// ctx.Repo.Repository.IsGoget = form.GoGet
|
||||
// if err := models.UpdateRepository(ctx.Repo.Repository); err != nil {
|
||||
// ctx.Handle(404, "setting.SettingPost(update)", err)
|
||||
// return
|
||||
// }
|
||||
// log.Trace("%s Repository updated: %s/%s", ctx.Req.RequestURI, ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
|
||||
|
||||
if ctx.Repo.Repository.IsMirror {
|
||||
if form.Interval > 0 {
|
||||
ctx.Repo.Mirror.Interval = form.Interval
|
||||
ctx.Repo.Mirror.NextUpdate = time.Now().Add(time.Duration(form.Interval) * time.Hour)
|
||||
if err := models.UpdateMirror(ctx.Repo.Mirror); err != nil {
|
||||
log.Error("setting.SettingPost(UpdateMirror): %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
// if ctx.Repo.Repository.IsMirror {
|
||||
// if form.Interval > 0 {
|
||||
// ctx.Repo.Mirror.Interval = form.Interval
|
||||
// ctx.Repo.Mirror.NextUpdate = time.Now().Add(time.Duration(form.Interval) * time.Hour)
|
||||
// if err := models.UpdateMirror(ctx.Repo.Mirror); err != nil {
|
||||
// log.Error("setting.SettingPost(UpdateMirror): %v", err)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
ctx.Flash.Success("Repository options has been successfully updated.")
|
||||
ctx.Redirect(fmt.Sprintf("/%s/%s/settings", ctx.Repo.Owner.Name, ctx.Repo.Repository.Name))
|
||||
case "transfer":
|
||||
if len(ctx.Repo.Repository.Name) == 0 || ctx.Repo.Repository.Name != ctx.Query("repository") {
|
||||
ctx.RenderWithErr("Please make sure you entered repository name is correct.", SETTING, nil)
|
||||
return
|
||||
} else if ctx.Repo.Repository.IsMirror {
|
||||
ctx.Error(404)
|
||||
return
|
||||
}
|
||||
// ctx.Flash.Success("Repository options has been successfully updated.")
|
||||
// ctx.Redirect(fmt.Sprintf("/%s/%s/settings", ctx.Repo.Owner.Name, ctx.Repo.Repository.Name))
|
||||
// case "transfer":
|
||||
// if len(ctx.Repo.Repository.Name) == 0 || ctx.Repo.Repository.Name != ctx.Query("repository") {
|
||||
// ctx.RenderWithErr("Please make sure you entered repository name is correct.", SETTING, nil)
|
||||
// return
|
||||
// } else if ctx.Repo.Repository.IsMirror {
|
||||
// ctx.Error(404)
|
||||
// return
|
||||
// }
|
||||
|
||||
newOwner := ctx.Query("owner")
|
||||
// Check if new owner exists.
|
||||
isExist, err := models.IsUserExist(newOwner)
|
||||
if err != nil {
|
||||
ctx.Handle(500, "setting.SettingPost(transfer: check existence)", err)
|
||||
return
|
||||
} else if !isExist {
|
||||
ctx.RenderWithErr("Please make sure you entered owner name is correct.", SETTING, nil)
|
||||
return
|
||||
} else if err = models.TransferOwnership(ctx.Repo.Owner, newOwner, ctx.Repo.Repository); err != nil {
|
||||
ctx.Handle(500, "setting.SettingPost(transfer repository)", err)
|
||||
return
|
||||
}
|
||||
log.Trace("%s Repository transfered: %s/%s -> %s", ctx.Req.RequestURI, ctx.User.Name, ctx.Repo.Repository.Name, newOwner)
|
||||
// newOwner := ctx.Query("owner")
|
||||
// // Check if new owner exists.
|
||||
// isExist, err := models.IsUserExist(newOwner)
|
||||
// if err != nil {
|
||||
// ctx.Handle(500, "setting.SettingPost(transfer: check existence)", err)
|
||||
// return
|
||||
// } else if !isExist {
|
||||
// ctx.RenderWithErr("Please make sure you entered owner name is correct.", SETTING, nil)
|
||||
// return
|
||||
// } else if err = models.TransferOwnership(ctx.Repo.Owner, newOwner, ctx.Repo.Repository); err != nil {
|
||||
// ctx.Handle(500, "setting.SettingPost(transfer repository)", err)
|
||||
// return
|
||||
// }
|
||||
// log.Trace("%s Repository transfered: %s/%s -> %s", ctx.Req.RequestURI, ctx.User.Name, ctx.Repo.Repository.Name, newOwner)
|
||||
|
||||
ctx.Redirect("/")
|
||||
case "delete":
|
||||
if len(ctx.Repo.Repository.Name) == 0 || ctx.Repo.Repository.Name != ctx.Query("repository") {
|
||||
ctx.RenderWithErr("Please make sure you entered repository name is correct.", SETTING, nil)
|
||||
return
|
||||
}
|
||||
// ctx.Redirect("/")
|
||||
// case "delete":
|
||||
// if len(ctx.Repo.Repository.Name) == 0 || ctx.Repo.Repository.Name != ctx.Query("repository") {
|
||||
// ctx.RenderWithErr("Please make sure you entered repository name is correct.", SETTING, nil)
|
||||
// return
|
||||
// }
|
||||
|
||||
if ctx.Repo.Owner.IsOrganization() &&
|
||||
!ctx.Repo.Owner.IsOrgOwner(ctx.User.Id) {
|
||||
ctx.Error(403)
|
||||
return
|
||||
}
|
||||
// if ctx.Repo.Owner.IsOrganization() &&
|
||||
// !ctx.Repo.Owner.IsOrgOwner(ctx.User.Id) {
|
||||
// ctx.Error(403)
|
||||
// return
|
||||
// }
|
||||
|
||||
if err := models.DeleteRepository(ctx.Repo.Owner.Id, ctx.Repo.Repository.Id, ctx.Repo.Owner.Name); err != nil {
|
||||
ctx.Handle(500, "setting.Delete(DeleteRepository)", err)
|
||||
return
|
||||
}
|
||||
log.Trace("%s Repository deleted: %s/%s", ctx.Req.RequestURI, ctx.Repo.Owner.LowerName, ctx.Repo.Repository.LowerName)
|
||||
// if err := models.DeleteRepository(ctx.Repo.Owner.Id, ctx.Repo.Repository.Id, ctx.Repo.Owner.Name); err != nil {
|
||||
// ctx.Handle(500, "setting.Delete(DeleteRepository)", err)
|
||||
// return
|
||||
// }
|
||||
// log.Trace("%s Repository deleted: %s/%s", ctx.Req.RequestURI, ctx.Repo.Owner.LowerName, ctx.Repo.Repository.LowerName)
|
||||
|
||||
if ctx.Repo.Owner.IsOrganization() {
|
||||
ctx.Redirect("/org/" + ctx.Repo.Owner.Name + "/dashboard")
|
||||
} else {
|
||||
ctx.Redirect("/")
|
||||
}
|
||||
}
|
||||
}
|
||||
// if ctx.Repo.Owner.IsOrganization() {
|
||||
// ctx.Redirect("/org/" + ctx.Repo.Owner.Name + "/dashboard")
|
||||
// } else {
|
||||
// ctx.Redirect("/")
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
func Collaboration(ctx *middleware.Context) {
|
||||
repoLink := strings.TrimPrefix(ctx.Repo.RepoLink, "/")
|
||||
ctx.Data["IsRepoToolbarCollaboration"] = true
|
||||
ctx.Data["Title"] = repoLink + " - collaboration"
|
||||
// func Collaboration(ctx *middleware.Context) {
|
||||
// repoLink := strings.TrimPrefix(ctx.Repo.RepoLink, "/")
|
||||
// ctx.Data["IsRepoToolbarCollaboration"] = true
|
||||
// ctx.Data["Title"] = repoLink + " - collaboration"
|
||||
|
||||
// Delete collaborator.
|
||||
remove := strings.ToLower(ctx.Query("remove"))
|
||||
if len(remove) > 0 && remove != ctx.Repo.Owner.LowerName {
|
||||
if err := models.DeleteAccess(&models.Access{UserName: remove, RepoName: repoLink}); err != nil {
|
||||
ctx.Handle(500, "setting.Collaboration(DeleteAccess)", err)
|
||||
return
|
||||
}
|
||||
ctx.Flash.Success("Collaborator has been removed.")
|
||||
ctx.Redirect(ctx.Repo.RepoLink + "/settings/collaboration")
|
||||
return
|
||||
}
|
||||
// // Delete collaborator.
|
||||
// remove := strings.ToLower(ctx.Query("remove"))
|
||||
// if len(remove) > 0 && remove != ctx.Repo.Owner.LowerName {
|
||||
// if err := models.DeleteAccess(&models.Access{UserName: remove, RepoName: repoLink}); err != nil {
|
||||
// ctx.Handle(500, "setting.Collaboration(DeleteAccess)", err)
|
||||
// return
|
||||
// }
|
||||
// ctx.Flash.Success("Collaborator has been removed.")
|
||||
// ctx.Redirect(ctx.Repo.RepoLink + "/settings/collaboration")
|
||||
// return
|
||||
// }
|
||||
|
||||
names, err := models.GetCollaboratorNames(repoLink)
|
||||
if err != nil {
|
||||
ctx.Handle(500, "setting.Collaboration(GetCollaborators)", err)
|
||||
return
|
||||
}
|
||||
// names, err := models.GetCollaboratorNames(repoLink)
|
||||
// if err != nil {
|
||||
// ctx.Handle(500, "setting.Collaboration(GetCollaborators)", err)
|
||||
// return
|
||||
// }
|
||||
|
||||
us := make([]*models.User, len(names))
|
||||
for i, name := range names {
|
||||
us[i], err = models.GetUserByName(name)
|
||||
if err != nil {
|
||||
ctx.Handle(500, "setting.Collaboration(GetUserByName)", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
// us := make([]*models.User, len(names))
|
||||
// for i, name := range names {
|
||||
// us[i], err = models.GetUserByName(name)
|
||||
// if err != nil {
|
||||
// ctx.Handle(500, "setting.Collaboration(GetUserByName)", err)
|
||||
// return
|
||||
// }
|
||||
// }
|
||||
|
||||
ctx.Data["Collaborators"] = us
|
||||
ctx.HTML(200, COLLABORATION)
|
||||
}
|
||||
// ctx.Data["Collaborators"] = us
|
||||
// ctx.HTML(200, COLLABORATION)
|
||||
// }
|
||||
|
||||
func CollaborationPost(ctx *middleware.Context) {
|
||||
repoLink := strings.TrimPrefix(ctx.Repo.RepoLink, "/")
|
||||
name := strings.ToLower(ctx.Query("collaborator"))
|
||||
if len(name) == 0 || ctx.Repo.Owner.LowerName == name {
|
||||
ctx.Redirect(ctx.Req.RequestURI)
|
||||
return
|
||||
}
|
||||
has, err := models.HasAccess(name, repoLink, models.WRITABLE)
|
||||
if err != nil {
|
||||
ctx.Handle(500, "setting.CollaborationPost(HasAccess)", err)
|
||||
return
|
||||
} else if has {
|
||||
ctx.Redirect(ctx.Req.RequestURI)
|
||||
return
|
||||
}
|
||||
// func CollaborationPost(ctx *middleware.Context) {
|
||||
// repoLink := strings.TrimPrefix(ctx.Repo.RepoLink, "/")
|
||||
// name := strings.ToLower(ctx.Query("collaborator"))
|
||||
// if len(name) == 0 || ctx.Repo.Owner.LowerName == name {
|
||||
// ctx.Redirect(ctx.Req.RequestURI)
|
||||
// return
|
||||
// }
|
||||
// has, err := models.HasAccess(name, repoLink, models.WRITABLE)
|
||||
// if err != nil {
|
||||
// ctx.Handle(500, "setting.CollaborationPost(HasAccess)", err)
|
||||
// return
|
||||
// } else if has {
|
||||
// ctx.Redirect(ctx.Req.RequestURI)
|
||||
// return
|
||||
// }
|
||||
|
||||
u, err := models.GetUserByName(name)
|
||||
if err != nil {
|
||||
if err == models.ErrUserNotExist {
|
||||
ctx.Flash.Error("Given user does not exist.")
|
||||
ctx.Redirect(ctx.Req.RequestURI)
|
||||
} else {
|
||||
ctx.Handle(500, "setting.CollaborationPost(GetUserByName)", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
// u, err := models.GetUserByName(name)
|
||||
// if err != nil {
|
||||
// if err == models.ErrUserNotExist {
|
||||
// ctx.Flash.Error("Given user does not exist.")
|
||||
// ctx.Redirect(ctx.Req.RequestURI)
|
||||
// } else {
|
||||
// ctx.Handle(500, "setting.CollaborationPost(GetUserByName)", err)
|
||||
// }
|
||||
// return
|
||||
// }
|
||||
|
||||
if err = models.AddAccess(&models.Access{UserName: name, RepoName: repoLink,
|
||||
Mode: models.WRITABLE}); err != nil {
|
||||
ctx.Handle(500, "setting.CollaborationPost(AddAccess)", err)
|
||||
return
|
||||
}
|
||||
// if err = models.AddAccess(&models.Access{UserName: name, RepoName: repoLink,
|
||||
// Mode: models.WRITABLE}); err != nil {
|
||||
// ctx.Handle(500, "setting.CollaborationPost(AddAccess)", err)
|
||||
// return
|
||||
// }
|
||||
|
||||
if setting.Service.EnableNotifyMail {
|
||||
if err = mailer.SendCollaboratorMail(ctx.Render, u, ctx.User, ctx.Repo.Repository); err != nil {
|
||||
ctx.Handle(500, "setting.CollaborationPost(SendCollaboratorMail)", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
// if setting.Service.EnableNotifyMail {
|
||||
// if err = mailer.SendCollaboratorMail(ctx.Render, u, ctx.User, ctx.Repo.Repository); err != nil {
|
||||
// ctx.Handle(500, "setting.CollaborationPost(SendCollaboratorMail)", err)
|
||||
// return
|
||||
// }
|
||||
// }
|
||||
|
||||
ctx.Flash.Success("New collaborator has been added.")
|
||||
ctx.Redirect(ctx.Req.RequestURI)
|
||||
}
|
||||
// ctx.Flash.Success("New collaborator has been added.")
|
||||
// ctx.Redirect(ctx.Req.RequestURI)
|
||||
// }
|
||||
|
||||
func WebHooks(ctx *middleware.Context) {
|
||||
ctx.Data["IsRepoToolbarWebHooks"] = true
|
||||
ctx.Data["Title"] = strings.TrimPrefix(ctx.Repo.RepoLink, "/") + " - Webhooks"
|
||||
// func WebHooks(ctx *middleware.Context) {
|
||||
// ctx.Data["IsRepoToolbarWebHooks"] = true
|
||||
// ctx.Data["Title"] = strings.TrimPrefix(ctx.Repo.RepoLink, "/") + " - Webhooks"
|
||||
|
||||
// Delete webhook.
|
||||
remove, _ := base.StrTo(ctx.Query("remove")).Int64()
|
||||
if remove > 0 {
|
||||
if err := models.DeleteWebhook(remove); err != nil {
|
||||
ctx.Handle(500, "setting.WebHooks(DeleteWebhook)", err)
|
||||
return
|
||||
}
|
||||
ctx.Flash.Success("Webhook has been removed.")
|
||||
ctx.Redirect(ctx.Repo.RepoLink + "/settings/hooks")
|
||||
return
|
||||
}
|
||||
// // Delete webhook.
|
||||
// remove, _ := base.StrTo(ctx.Query("remove")).Int64()
|
||||
// if remove > 0 {
|
||||
// if err := models.DeleteWebhook(remove); err != nil {
|
||||
// ctx.Handle(500, "setting.WebHooks(DeleteWebhook)", err)
|
||||
// return
|
||||
// }
|
||||
// ctx.Flash.Success("Webhook has been removed.")
|
||||
// ctx.Redirect(ctx.Repo.RepoLink + "/settings/hooks")
|
||||
// return
|
||||
// }
|
||||
|
||||
ws, err := models.GetWebhooksByRepoId(ctx.Repo.Repository.Id)
|
||||
if err != nil {
|
||||
ctx.Handle(500, "setting.WebHooks(GetWebhooksByRepoId)", err)
|
||||
return
|
||||
}
|
||||
// ws, err := models.GetWebhooksByRepoId(ctx.Repo.Repository.Id)
|
||||
// if err != nil {
|
||||
// ctx.Handle(500, "setting.WebHooks(GetWebhooksByRepoId)", err)
|
||||
// return
|
||||
// }
|
||||
|
||||
ctx.Data["Webhooks"] = ws
|
||||
ctx.HTML(200, HOOKS)
|
||||
}
|
||||
// ctx.Data["Webhooks"] = ws
|
||||
// ctx.HTML(200, HOOKS)
|
||||
// }
|
||||
|
||||
func WebHooksAdd(ctx *middleware.Context) {
|
||||
ctx.Data["IsRepoToolbarWebHooks"] = true
|
||||
ctx.Data["Title"] = strings.TrimPrefix(ctx.Repo.RepoLink, "/") + " - Add Webhook"
|
||||
ctx.HTML(200, HOOK_ADD)
|
||||
}
|
||||
// func WebHooksAdd(ctx *middleware.Context) {
|
||||
// ctx.Data["IsRepoToolbarWebHooks"] = true
|
||||
// ctx.Data["Title"] = strings.TrimPrefix(ctx.Repo.RepoLink, "/") + " - Add Webhook"
|
||||
// ctx.HTML(200, HOOK_ADD)
|
||||
// }
|
||||
|
||||
func WebHooksAddPost(ctx *middleware.Context, form auth.NewWebhookForm) {
|
||||
ctx.Data["IsRepoToolbarWebHooks"] = true
|
||||
ctx.Data["Title"] = strings.TrimPrefix(ctx.Repo.RepoLink, "/") + " - Add Webhook"
|
||||
// func WebHooksAddPost(ctx *middleware.Context, form auth.NewWebhookForm) {
|
||||
// ctx.Data["IsRepoToolbarWebHooks"] = true
|
||||
// ctx.Data["Title"] = strings.TrimPrefix(ctx.Repo.RepoLink, "/") + " - Add Webhook"
|
||||
|
||||
if ctx.HasError() {
|
||||
ctx.HTML(200, HOOK_ADD)
|
||||
return
|
||||
}
|
||||
// if ctx.HasError() {
|
||||
// ctx.HTML(200, HOOK_ADD)
|
||||
// return
|
||||
// }
|
||||
|
||||
ct := models.JSON
|
||||
if form.ContentType == "2" {
|
||||
ct = models.FORM
|
||||
}
|
||||
// ct := models.JSON
|
||||
// if form.ContentType == "2" {
|
||||
// ct = models.FORM
|
||||
// }
|
||||
|
||||
w := &models.Webhook{
|
||||
RepoId: ctx.Repo.Repository.Id,
|
||||
Url: form.Url,
|
||||
ContentType: ct,
|
||||
Secret: form.Secret,
|
||||
HookEvent: &models.HookEvent{
|
||||
PushOnly: form.PushOnly,
|
||||
},
|
||||
IsActive: form.Active,
|
||||
}
|
||||
if err := w.UpdateEvent(); err != nil {
|
||||
ctx.Handle(500, "setting.WebHooksAddPost(UpdateEvent)", err)
|
||||
return
|
||||
} else if err := models.CreateWebhook(w); err != nil {
|
||||
ctx.Handle(500, "setting.WebHooksAddPost(CreateWebhook)", err)
|
||||
return
|
||||
}
|
||||
// w := &models.Webhook{
|
||||
// RepoId: ctx.Repo.Repository.Id,
|
||||
// Url: form.Url,
|
||||
// ContentType: ct,
|
||||
// Secret: form.Secret,
|
||||
// HookEvent: &models.HookEvent{
|
||||
// PushOnly: form.PushOnly,
|
||||
// },
|
||||
// IsActive: form.Active,
|
||||
// }
|
||||
// if err := w.UpdateEvent(); err != nil {
|
||||
// ctx.Handle(500, "setting.WebHooksAddPost(UpdateEvent)", err)
|
||||
// return
|
||||
// } else if err := models.CreateWebhook(w); err != nil {
|
||||
// ctx.Handle(500, "setting.WebHooksAddPost(CreateWebhook)", err)
|
||||
// return
|
||||
// }
|
||||
|
||||
ctx.Flash.Success("New webhook has been added.")
|
||||
ctx.Redirect(ctx.Repo.RepoLink + "/settings/hooks")
|
||||
}
|
||||
// ctx.Flash.Success("New webhook has been added.")
|
||||
// ctx.Redirect(ctx.Repo.RepoLink + "/settings/hooks")
|
||||
// }
|
||||
|
||||
func WebHooksEdit(ctx *middleware.Context, params martini.Params) {
|
||||
ctx.Data["IsRepoToolbarWebHooks"] = true
|
||||
ctx.Data["Title"] = strings.TrimPrefix(ctx.Repo.RepoLink, "/") + " - Webhook"
|
||||
// func WebHooksEdit(ctx *middleware.Context, params martini.Params) {
|
||||
// ctx.Data["IsRepoToolbarWebHooks"] = true
|
||||
// ctx.Data["Title"] = strings.TrimPrefix(ctx.Repo.RepoLink, "/") + " - Webhook"
|
||||
|
||||
hookId, _ := base.StrTo(params["id"]).Int64()
|
||||
if hookId == 0 {
|
||||
ctx.Handle(404, "setting.WebHooksEdit", nil)
|
||||
return
|
||||
}
|
||||
// hookId, _ := base.StrTo(params["id"]).Int64()
|
||||
// if hookId == 0 {
|
||||
// ctx.Handle(404, "setting.WebHooksEdit", nil)
|
||||
// return
|
||||
// }
|
||||
|
||||
w, err := models.GetWebhookById(hookId)
|
||||
if err != nil {
|
||||
if err == models.ErrWebhookNotExist {
|
||||
ctx.Handle(404, "setting.WebHooksEdit(GetWebhookById)", nil)
|
||||
} else {
|
||||
ctx.Handle(500, "setting.WebHooksEdit(GetWebhookById)", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
// w, err := models.GetWebhookById(hookId)
|
||||
// if err != nil {
|
||||
// if err == models.ErrWebhookNotExist {
|
||||
// ctx.Handle(404, "setting.WebHooksEdit(GetWebhookById)", nil)
|
||||
// } else {
|
||||
// ctx.Handle(500, "setting.WebHooksEdit(GetWebhookById)", err)
|
||||
// }
|
||||
// return
|
||||
// }
|
||||
|
||||
w.GetEvent()
|
||||
ctx.Data["Webhook"] = w
|
||||
ctx.HTML(200, HOOK_EDIT)
|
||||
}
|
||||
// w.GetEvent()
|
||||
// ctx.Data["Webhook"] = w
|
||||
// ctx.HTML(200, HOOK_EDIT)
|
||||
// }
|
||||
|
||||
func WebHooksEditPost(ctx *middleware.Context, params martini.Params, form auth.NewWebhookForm) {
|
||||
ctx.Data["IsRepoToolbarWebHooks"] = true
|
||||
ctx.Data["Title"] = strings.TrimPrefix(ctx.Repo.RepoLink, "/") + " - Webhook"
|
||||
// func WebHooksEditPost(ctx *middleware.Context, params martini.Params, form auth.NewWebhookForm) {
|
||||
// ctx.Data["IsRepoToolbarWebHooks"] = true
|
||||
// ctx.Data["Title"] = strings.TrimPrefix(ctx.Repo.RepoLink, "/") + " - Webhook"
|
||||
|
||||
hookId, _ := base.StrTo(params["id"]).Int64()
|
||||
if hookId == 0 {
|
||||
ctx.Handle(404, "setting.WebHooksEditPost", nil)
|
||||
return
|
||||
}
|
||||
// hookId, _ := base.StrTo(params["id"]).Int64()
|
||||
// if hookId == 0 {
|
||||
// ctx.Handle(404, "setting.WebHooksEditPost", nil)
|
||||
// return
|
||||
// }
|
||||
|
||||
w, err := models.GetWebhookById(hookId)
|
||||
if err != nil {
|
||||
if err == models.ErrWebhookNotExist {
|
||||
ctx.Handle(404, "setting.WebHooksEditPost(GetWebhookById)", nil)
|
||||
} else {
|
||||
ctx.Handle(500, "setting.WebHooksEditPost(GetWebhookById)", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
// w, err := models.GetWebhookById(hookId)
|
||||
// if err != nil {
|
||||
// if err == models.ErrWebhookNotExist {
|
||||
// ctx.Handle(404, "setting.WebHooksEditPost(GetWebhookById)", nil)
|
||||
// } else {
|
||||
// ctx.Handle(500, "setting.WebHooksEditPost(GetWebhookById)", err)
|
||||
// }
|
||||
// return
|
||||
// }
|
||||
|
||||
if ctx.HasError() {
|
||||
ctx.HTML(200, HOOK_EDIT)
|
||||
return
|
||||
}
|
||||
// if ctx.HasError() {
|
||||
// ctx.HTML(200, HOOK_EDIT)
|
||||
// return
|
||||
// }
|
||||
|
||||
ct := models.JSON
|
||||
if form.ContentType == "2" {
|
||||
ct = models.FORM
|
||||
}
|
||||
// ct := models.JSON
|
||||
// if form.ContentType == "2" {
|
||||
// ct = models.FORM
|
||||
// }
|
||||
|
||||
w.Url = form.Url
|
||||
w.ContentType = ct
|
||||
w.Secret = form.Secret
|
||||
w.HookEvent = &models.HookEvent{
|
||||
PushOnly: form.PushOnly,
|
||||
}
|
||||
w.IsActive = form.Active
|
||||
if err := w.UpdateEvent(); err != nil {
|
||||
ctx.Handle(500, "setting.WebHooksEditPost(UpdateEvent)", err)
|
||||
return
|
||||
} else if err := models.UpdateWebhook(w); err != nil {
|
||||
ctx.Handle(500, "setting.WebHooksEditPost(WebHooksEditPost)", err)
|
||||
return
|
||||
}
|
||||
// w.Url = form.Url
|
||||
// w.ContentType = ct
|
||||
// w.Secret = form.Secret
|
||||
// w.HookEvent = &models.HookEvent{
|
||||
// PushOnly: form.PushOnly,
|
||||
// }
|
||||
// w.IsActive = form.Active
|
||||
// if err := w.UpdateEvent(); err != nil {
|
||||
// ctx.Handle(500, "setting.WebHooksEditPost(UpdateEvent)", err)
|
||||
// return
|
||||
// } else if err := models.UpdateWebhook(w); err != nil {
|
||||
// ctx.Handle(500, "setting.WebHooksEditPost(WebHooksEditPost)", err)
|
||||
// return
|
||||
// }
|
||||
|
||||
ctx.Flash.Success("Webhook has been updated.")
|
||||
ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", ctx.Repo.RepoLink, hookId))
|
||||
}
|
||||
// ctx.Flash.Success("Webhook has been updated.")
|
||||
// ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", ctx.Repo.RepoLink, hookId))
|
||||
// }
|
||||
|
203
routers/repo/view.go
Normal file
203
routers/repo/view.go
Normal file
@@ -0,0 +1,203 @@
|
||||
// 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 repo
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/gogits/gogs/modules/base"
|
||||
"github.com/gogits/gogs/modules/git"
|
||||
"github.com/gogits/gogs/modules/middleware"
|
||||
)
|
||||
|
||||
const (
|
||||
HOME base.TplName = "repo/home"
|
||||
)
|
||||
|
||||
func Home(ctx *middleware.Context) {
|
||||
ctx.Data["Title"] = ctx.Repo.Repository.Name
|
||||
|
||||
branchName := ctx.Repo.BranchName
|
||||
userName := ctx.Repo.Owner.Name
|
||||
repoName := ctx.Repo.Repository.Name
|
||||
|
||||
repoLink := ctx.Repo.RepoLink
|
||||
branchLink := ctx.Repo.RepoLink + "/src/" + branchName
|
||||
rawLink := ctx.Repo.RepoLink + "/raw/" + branchName
|
||||
|
||||
// Get tree path
|
||||
treename := ctx.Params("*")
|
||||
|
||||
if len(treename) > 0 && treename[len(treename)-1] == '/' {
|
||||
ctx.Redirect(repoLink + "/src/" + branchName + "/" + treename[:len(treename)-1])
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Data["IsRepoToolbarSource"] = true
|
||||
|
||||
isViewBranch := ctx.Repo.IsBranch
|
||||
ctx.Data["IsViewBranch"] = isViewBranch
|
||||
|
||||
treePath := treename
|
||||
if len(treePath) != 0 {
|
||||
treePath = treePath + "/"
|
||||
}
|
||||
|
||||
entry, err := ctx.Repo.Commit.GetTreeEntryByPath(treename)
|
||||
if err != nil && err != git.ErrNotExist {
|
||||
ctx.Handle(404, "GetTreeEntryByPath", err)
|
||||
return
|
||||
}
|
||||
|
||||
if len(treename) != 0 && entry == nil {
|
||||
ctx.Handle(404, "repo.Home", nil)
|
||||
return
|
||||
}
|
||||
|
||||
if entry != nil && !entry.IsDir() {
|
||||
blob := entry.Blob()
|
||||
|
||||
if dataRc, err := blob.Data(); err != nil {
|
||||
ctx.Handle(404, "blob.Data", err)
|
||||
return
|
||||
} else {
|
||||
ctx.Data["FileSize"] = blob.Size()
|
||||
ctx.Data["IsFile"] = true
|
||||
ctx.Data["FileName"] = blob.Name()
|
||||
ext := path.Ext(blob.Name())
|
||||
if len(ext) > 0 {
|
||||
ext = ext[1:]
|
||||
}
|
||||
ctx.Data["FileExt"] = ext
|
||||
ctx.Data["FileLink"] = rawLink + "/" + treename
|
||||
|
||||
buf := make([]byte, 1024)
|
||||
n, _ := dataRc.Read(buf)
|
||||
if n > 0 {
|
||||
buf = buf[:n]
|
||||
}
|
||||
|
||||
_, isTextFile := base.IsTextFile(buf)
|
||||
_, isImageFile := base.IsImageFile(buf)
|
||||
ctx.Data["IsFileText"] = isTextFile
|
||||
|
||||
switch {
|
||||
case isImageFile:
|
||||
ctx.Data["IsImageFile"] = true
|
||||
case isTextFile:
|
||||
d, _ := ioutil.ReadAll(dataRc)
|
||||
buf = append(buf, d...)
|
||||
readmeExist := base.IsMarkdownFile(blob.Name()) || base.IsReadmeFile(blob.Name())
|
||||
ctx.Data["ReadmeExist"] = readmeExist
|
||||
if readmeExist {
|
||||
ctx.Data["FileContent"] = string(base.RenderMarkdown(buf, ""))
|
||||
} else {
|
||||
ctx.Data["FileContent"] = string(buf)
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Directory and file list.
|
||||
tree, err := ctx.Repo.Commit.SubTree(treename)
|
||||
if err != nil {
|
||||
ctx.Handle(404, "SubTree", err)
|
||||
return
|
||||
}
|
||||
entries, err := tree.ListEntries(treename)
|
||||
if err != nil {
|
||||
ctx.Handle(500, "ListEntries", err)
|
||||
return
|
||||
}
|
||||
entries.Sort()
|
||||
|
||||
files := make([][]interface{}, 0, len(entries))
|
||||
|
||||
for _, te := range entries {
|
||||
c, err := ctx.Repo.Commit.GetCommitOfRelPath(filepath.Join(treePath, te.Name()))
|
||||
if err != nil {
|
||||
ctx.Handle(404, "GetCommitOfRelPath", err)
|
||||
return
|
||||
}
|
||||
|
||||
files = append(files, []interface{}{te, c})
|
||||
}
|
||||
|
||||
ctx.Data["Files"] = files
|
||||
|
||||
var readmeFile *git.Blob
|
||||
|
||||
for _, f := range entries {
|
||||
if f.IsDir() || !base.IsReadmeFile(f.Name()) {
|
||||
continue
|
||||
} else {
|
||||
readmeFile = f.Blob()
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if readmeFile != nil {
|
||||
ctx.Data["ReadmeInHome"] = true
|
||||
ctx.Data["ReadmeExist"] = true
|
||||
if dataRc, err := readmeFile.Data(); err != nil {
|
||||
ctx.Handle(404, "repo.SinglereadmeFile.LookupBlob", err)
|
||||
return
|
||||
} else {
|
||||
|
||||
buf := make([]byte, 1024)
|
||||
n, _ := dataRc.Read(buf)
|
||||
if n > 0 {
|
||||
buf = buf[:n]
|
||||
}
|
||||
|
||||
ctx.Data["FileSize"] = readmeFile.Size()
|
||||
ctx.Data["FileLink"] = rawLink + "/" + treename
|
||||
_, isTextFile := base.IsTextFile(buf)
|
||||
ctx.Data["FileIsText"] = isTextFile
|
||||
ctx.Data["FileName"] = readmeFile.Name()
|
||||
if isTextFile {
|
||||
d, _ := ioutil.ReadAll(dataRc)
|
||||
buf = append(buf, d...)
|
||||
switch {
|
||||
case base.IsMarkdownFile(readmeFile.Name()):
|
||||
buf = base.RenderMarkdown(buf, branchLink)
|
||||
default:
|
||||
buf = bytes.Replace(buf, []byte("\n"), []byte(`<br>`), -1)
|
||||
}
|
||||
ctx.Data["FileContent"] = string(buf)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ctx.Data["Username"] = userName
|
||||
ctx.Data["Reponame"] = repoName
|
||||
|
||||
var treenames []string
|
||||
Paths := make([]string, 0)
|
||||
|
||||
if len(treename) > 0 {
|
||||
treenames = strings.Split(treename, "/")
|
||||
for i, _ := range treenames {
|
||||
Paths = append(Paths, strings.Join(treenames[0:i+1], "/"))
|
||||
}
|
||||
|
||||
ctx.Data["HasParentPath"] = true
|
||||
if len(Paths)-2 >= 0 {
|
||||
ctx.Data["ParentPath"] = "/" + Paths[len(Paths)-2]
|
||||
}
|
||||
}
|
||||
|
||||
ctx.Data["LastCommit"] = ctx.Repo.Commit
|
||||
ctx.Data["Paths"] = Paths
|
||||
ctx.Data["TreeName"] = treename
|
||||
ctx.Data["Treenames"] = treenames
|
||||
ctx.Data["TreePath"] = treePath
|
||||
ctx.Data["BranchLink"] = branchLink
|
||||
ctx.HTML(200, HOME)
|
||||
}
|
442
routers/user/auth.go
Normal file
442
routers/user/auth.go
Normal file
@@ -0,0 +1,442 @@
|
||||
// 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 (
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/gogits/gogs/models"
|
||||
"github.com/gogits/gogs/modules/auth"
|
||||
"github.com/gogits/gogs/modules/base"
|
||||
"github.com/gogits/gogs/modules/captcha"
|
||||
"github.com/gogits/gogs/modules/log"
|
||||
// "github.com/gogits/gogs/modules/mailer"
|
||||
"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")
|
||||
|
||||
// if _, ok := ctx.Session.Get("socialId").(int64); ok {
|
||||
// ctx.Data["IsSocialLogin"] = true
|
||||
// ctx.HTML(200, SIGNIN)
|
||||
// return
|
||||
// }
|
||||
|
||||
// if setting.OauthService != nil {
|
||||
// ctx.Data["OauthEnabled"] = true
|
||||
// ctx.Data["OauthService"] = setting.OauthService
|
||||
// }
|
||||
|
||||
// Check auto-login.
|
||||
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")
|
||||
|
||||
// sid, isOauth := ctx.Session.Get("socialId").(int64)
|
||||
// if isOauth {
|
||||
// ctx.Data["IsSocialLogin"] = true
|
||||
// } else if setting.OauthService != nil {
|
||||
// ctx.Data["OauthEnabled"] = true
|
||||
// ctx.Data["OauthService"] = setting.OauthService
|
||||
// }
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
// Bind with social account.
|
||||
// if isOauth {
|
||||
// if err = models.BindUserOauth2(user.Id, sid); err != nil {
|
||||
// if err == models.ErrOauth2RecordNotExist {
|
||||
// ctx.Handle(404, "user.SignInPost(GetOauth2ById)", err)
|
||||
// } else {
|
||||
// ctx.Handle(500, "user.SignInPost(GetOauth2ById)", err)
|
||||
// }
|
||||
// return
|
||||
// }
|
||||
// ctx.Session.Delete("socialId")
|
||||
// log.Trace("%s OAuth binded: %s -> %d", ctx.Req.RequestURI, form.UserName, sid)
|
||||
// }
|
||||
|
||||
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.Session.Delete("socialId")
|
||||
// ctx.Session.Delete("socialName")
|
||||
// ctx.Session.Delete("socialEmail")
|
||||
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
|
||||
}
|
||||
|
||||
// if sid, ok := ctx.Session.Get("socialId").(int64); ok {
|
||||
// oauthSignUp(ctx, sid)
|
||||
// return
|
||||
// }
|
||||
|
||||
ctx.HTML(200, SIGNUP)
|
||||
}
|
||||
|
||||
// func oauthSignUp(ctx *middleware.Context, sid int64) {
|
||||
// ctx.Data["Title"] = "OAuth Sign Up"
|
||||
// ctx.Data["PageIsSignUp"] = true
|
||||
|
||||
// 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
|
||||
// }
|
||||
|
||||
// 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)
|
||||
// }
|
||||
|
||||
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
|
||||
// sid, isOauth := ctx.Session.Get("socialId").(int64)
|
||||
// if isOauth {
|
||||
// ctx.Data["IsSocialLogin"] = true
|
||||
// }
|
||||
|
||||
// May redirect from home page.
|
||||
if ctx.Query("from") == "home" {
|
||||
// Clear input error box.
|
||||
ctx.Data["Err_UserName"] = false
|
||||
ctx.Data["Err_Email"] = false
|
||||
|
||||
// Make the best guess.
|
||||
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)
|
||||
|
||||
// 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)
|
||||
// }
|
||||
|
||||
// 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 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 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 setting.Service.RegisterEmailConfirm {
|
||||
// if ctx.Cache.IsExist("MailResendLimit_" + ctx.User.LowerName) {
|
||||
// ctx.Data["ResendLimited"] = true
|
||||
// } else {
|
||||
// ctx.Data["Hours"] = setting.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, ACTIVATE)
|
||||
// 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, ACTIVATE)
|
||||
}
|
||||
|
||||
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) {
|
||||
// ctx.Data["Title"] = "Forgot Password"
|
||||
|
||||
// if setting.MailService == nil {
|
||||
// ctx.Handle(403, "user.ForgotPasswdPost", nil)
|
||||
// return
|
||||
// }
|
||||
// ctx.Data["IsResetRequest"] = true
|
||||
|
||||
// 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(500, "user.ResetPasswd(check existence)", err)
|
||||
// }
|
||||
// return
|
||||
// }
|
||||
|
||||
// if ctx.Cache.IsExist("MailResendLimit_" + u.LowerName) {
|
||||
// ctx.Data["ResendLimited"] = true
|
||||
// ctx.HTML(200, FORGOT_PASSWORD)
|
||||
// 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"] = setting.Service.ActiveCodeLives / 60
|
||||
// ctx.Data["IsResetSent"] = true
|
||||
// ctx.HTML(200, FORGOT_PASSWORD)
|
||||
}
|
||||
|
||||
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) {
|
||||
// ctx.Data["Title"] = "Reset Password"
|
||||
|
||||
// code := ctx.Query("code")
|
||||
// if len(code) == 0 {
|
||||
// ctx.Error(404)
|
||||
// return
|
||||
// }
|
||||
// ctx.Data["Code"] = code
|
||||
|
||||
// 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(500, "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, RESET_PASSWORD)
|
||||
}
|
@@ -8,50 +8,49 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/Unknwon/com"
|
||||
"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/middleware"
|
||||
)
|
||||
|
||||
const (
|
||||
DASHBOARD base.TplName = "user/dashboard"
|
||||
PROFILE base.TplName = "user/profile"
|
||||
DASHBOARD base.TplName = "user/dashboard/dashboard"
|
||||
ISSUES base.TplName = "user/issues"
|
||||
PULLS base.TplName = "user/pulls"
|
||||
STARS base.TplName = "user/stars"
|
||||
PROFILE base.TplName = "user/profile"
|
||||
)
|
||||
|
||||
func Dashboard(ctx *middleware.Context) {
|
||||
ctx.Data["Title"] = "Dashboard"
|
||||
ctx.Data["PageIsUserDashboard"] = true
|
||||
ctx.Data["Title"] = ctx.Tr("dashboard")
|
||||
ctx.Data["PageIsDashboard"] = true
|
||||
ctx.Data["PageIsNews"] = true
|
||||
|
||||
if err := ctx.User.GetOrganizations(); err != nil {
|
||||
ctx.Handle(500, "home.Dashboard(GetOrganizations)", err)
|
||||
return
|
||||
}
|
||||
ctx.Data["Orgs"] = ctx.User.Orgs
|
||||
// if err := ctx.User.GetOrganizations(); err != nil {
|
||||
// ctx.Handle(500, "home.Dashboard(GetOrganizations)", err)
|
||||
// return
|
||||
// }
|
||||
// ctx.Data["Orgs"] = ctx.User.Orgs
|
||||
ctx.Data["ContextUser"] = ctx.User
|
||||
|
||||
var err error
|
||||
ctx.Data["MyRepos"], err = models.GetRepositories(ctx.User.Id, true)
|
||||
repos, err := models.GetRepositories(ctx.User.Id, true)
|
||||
if err != nil {
|
||||
ctx.Handle(500, "home.Dashboard(GetRepositories)", err)
|
||||
ctx.Handle(500, "GetRepositories", err)
|
||||
return
|
||||
}
|
||||
ctx.Data["Repos"] = repos
|
||||
|
||||
ctx.Data["CollaborativeRepos"], err = models.GetCollaborativeRepos(ctx.User.Name)
|
||||
if err != nil {
|
||||
ctx.Handle(500, "home.Dashboard(GetCollaborativeRepos)", err)
|
||||
return
|
||||
}
|
||||
// ctx.Data["CollaborativeRepos"], err = models.GetCollaborativeRepos(ctx.User.Name)
|
||||
// if err != nil {
|
||||
// ctx.Handle(500, "home.Dashboard(GetCollaborativeRepos)", err)
|
||||
// return
|
||||
// }
|
||||
|
||||
actions, err := models.GetFeeds(ctx.User.Id, 0, false)
|
||||
actions, err := models.GetFeeds(ctx.User.Id, 0, true)
|
||||
if err != nil {
|
||||
ctx.Handle(500, "home.Dashboard(GetFeeds)", err)
|
||||
ctx.Handle(500, "GetFeeds", err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -70,11 +69,11 @@ func Dashboard(ctx *middleware.Context) {
|
||||
ctx.HTML(200, DASHBOARD)
|
||||
}
|
||||
|
||||
func Profile(ctx *middleware.Context, params martini.Params) {
|
||||
func Profile(ctx *middleware.Context) {
|
||||
ctx.Data["Title"] = "Profile"
|
||||
ctx.Data["PageIsUserProfile"] = true
|
||||
|
||||
u, err := models.GetUserByName(params["username"])
|
||||
u, err := models.GetUserByName(ctx.Params(":username"))
|
||||
if err != nil {
|
||||
if err == models.ErrUserNotExist {
|
||||
ctx.Handle(404, "user.Profile(GetUserByName)", err)
|
||||
@@ -133,26 +132,26 @@ const (
|
||||
<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)
|
||||
return
|
||||
}
|
||||
// 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)
|
||||
// return
|
||||
// }
|
||||
|
||||
feeds := make([]string, 0, len(actions))
|
||||
for _, act := range actions {
|
||||
if act.IsPrivate {
|
||||
if has, _ := models.HasAccess(ctx.User.Name, act.RepoUserName+"/"+act.RepoName,
|
||||
models.READABLE); !has {
|
||||
continue
|
||||
}
|
||||
}
|
||||
feeds = append(feeds, fmt.Sprintf(TPL_FEED, base.ActionIcon(act.OpType),
|
||||
base.TimeSince(act.Created), base.ActionDesc(act)))
|
||||
}
|
||||
ctx.JSON(200, &feeds)
|
||||
}
|
||||
// feeds := make([]string, 0, len(actions))
|
||||
// for _, act := range actions {
|
||||
// if act.IsPrivate {
|
||||
// if has, _ := models.HasAccess(ctx.User.Name, act.RepoUserName+"/"+act.RepoName,
|
||||
// models.READABLE); !has {
|
||||
// continue
|
||||
// }
|
||||
// }
|
||||
// feeds = append(feeds, fmt.Sprintf(TPL_FEED, base.ActionIcon(act.OpType),
|
||||
// base.TimeSince(act.Created), base.ActionDesc(act)))
|
||||
// }
|
||||
// ctx.JSON(200, &feeds)
|
||||
// }
|
||||
|
||||
func Issues(ctx *middleware.Context) {
|
||||
ctx.Data["Title"] = "Your Issues"
|
||||
@@ -173,7 +172,7 @@ func Issues(ctx *middleware.Context) {
|
||||
filterMode = models.FM_CREATE
|
||||
}
|
||||
|
||||
repoId, _ := base.StrTo(ctx.Query("repoid")).Int64()
|
||||
repoId, _ := com.StrTo(ctx.Query("repoid")).Int64()
|
||||
issueStats := models.GetUserIssueStats(ctx.User.Id, filterMode)
|
||||
|
||||
// Get all repositories.
|
||||
@@ -215,7 +214,7 @@ func Issues(ctx *middleware.Context) {
|
||||
repoIds = []int64{repoId}
|
||||
}
|
||||
|
||||
page, _ := base.StrTo(ctx.Query("page")).Int()
|
||||
page, _ := com.StrTo(ctx.Query("page")).Int()
|
||||
|
||||
// Get all issues.
|
||||
var ius []*models.IssueUser
|
||||
|
@@ -5,76 +5,62 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"github.com/Unknwon/com"
|
||||
|
||||
"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/process"
|
||||
)
|
||||
|
||||
const (
|
||||
SETTING base.TplName = "user/setting"
|
||||
SOCIAL base.TplName = "user/social"
|
||||
PASSWORD base.TplName = "user/password"
|
||||
PUBLICKEY base.TplName = "user/publickey"
|
||||
NOTIFICATION base.TplName = "user/notification"
|
||||
SECURITY base.TplName = "user/security"
|
||||
SETTINGS_PROFILE base.TplName = "user/settings/profile"
|
||||
SETTINGS_PASSWORD base.TplName = "user/settings/password"
|
||||
SETTINGS_SSH_KEYS base.TplName = "user/settings/sshkeys"
|
||||
SETTINGS_SOCIAL base.TplName = "user/settings/social"
|
||||
SETTINGS_DELETE base.TplName = "user/settings/delete"
|
||||
NOTIFICATION base.TplName = "user/notification"
|
||||
SECURITY base.TplName = "user/security"
|
||||
)
|
||||
|
||||
var (
|
||||
MinimumKeySize = map[string]int{
|
||||
"(ED25519)": 256,
|
||||
"(ECDSA)": 256,
|
||||
"(NTRU)": 1087,
|
||||
"(MCE)": 1702,
|
||||
"(McE)": 1702,
|
||||
"(RSA)": 2048,
|
||||
}
|
||||
)
|
||||
|
||||
func Setting(ctx *middleware.Context) {
|
||||
ctx.Data["Title"] = "Setting"
|
||||
ctx.Data["PageIsUserSetting"] = true
|
||||
ctx.Data["IsUserPageSetting"] = true
|
||||
ctx.Data["Owner"] = ctx.User
|
||||
ctx.HTML(200, SETTING)
|
||||
func Settings(ctx *middleware.Context) {
|
||||
ctx.Data["Title"] = ctx.Tr("settings")
|
||||
ctx.Data["PageIsUserSettings"] = true
|
||||
ctx.Data["PageIsSettingsProfile"] = true
|
||||
ctx.HTML(200, SETTINGS_PROFILE)
|
||||
}
|
||||
|
||||
func SettingPost(ctx *middleware.Context, form auth.UpdateProfileForm) {
|
||||
ctx.Data["Title"] = "Setting"
|
||||
ctx.Data["PageIsUserSetting"] = true
|
||||
ctx.Data["IsUserPageSetting"] = true
|
||||
func SettingsPost(ctx *middleware.Context, form auth.UpdateProfileForm) {
|
||||
ctx.Data["Title"] = ctx.Tr("settings")
|
||||
ctx.Data["PageIsUserSettings"] = true
|
||||
ctx.Data["PageIsSettingsProfile"] = true
|
||||
|
||||
if ctx.HasError() {
|
||||
ctx.HTML(200, SETTING)
|
||||
ctx.HTML(200, SETTINGS_PROFILE)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Data["Owner"] = ctx.User
|
||||
|
||||
// Check if user name has been changed.
|
||||
if ctx.User.Name != form.UserName {
|
||||
isExist, err := models.IsUserExist(form.UserName)
|
||||
if err != nil {
|
||||
ctx.Handle(500, "user.SettingPost(update: check existence)", err)
|
||||
ctx.Handle(500, "IsUserExist", err)
|
||||
return
|
||||
} else if isExist {
|
||||
ctx.RenderWithErr("User name has been taken.", SETTING, &form)
|
||||
ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), SETTINGS_PROFILE, &form)
|
||||
return
|
||||
} else if err = models.ChangeUserName(ctx.User, form.UserName); err != nil {
|
||||
ctx.Handle(500, "user.SettingPost(change user name)", err)
|
||||
if err == models.ErrUserNameIllegal {
|
||||
ctx.Flash.Error(ctx.Tr("form.illegal_username"))
|
||||
ctx.Redirect("/user/settings")
|
||||
return
|
||||
} else {
|
||||
ctx.Handle(500, "ChangeUserName", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
log.Trace("%s User name changed: %s -> %s", ctx.Req.RequestURI, ctx.User.Name, form.UserName)
|
||||
|
||||
log.Trace("User name changed: %s -> %s", ctx.User.Name, form.UserName)
|
||||
ctx.User.Name = form.UserName
|
||||
}
|
||||
|
||||
@@ -85,213 +71,197 @@ func SettingPost(ctx *middleware.Context, form auth.UpdateProfileForm) {
|
||||
ctx.User.Avatar = base.EncodeMd5(form.Avatar)
|
||||
ctx.User.AvatarEmail = form.Avatar
|
||||
if err := models.UpdateUser(ctx.User); err != nil {
|
||||
ctx.Handle(500, "setting.SettingPost(UpdateUser)", err)
|
||||
ctx.Handle(500, "UpdateUser", err)
|
||||
return
|
||||
}
|
||||
log.Trace("%s User setting updated: %s", ctx.Req.RequestURI, ctx.User.LowerName)
|
||||
ctx.Flash.Success("Your profile has been successfully updated.")
|
||||
log.Trace("User setting updated: %s", ctx.User.Name)
|
||||
ctx.Flash.Success(ctx.Tr("settings.update_profile_success"))
|
||||
ctx.Redirect("/user/settings")
|
||||
}
|
||||
|
||||
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 SettingsPassword(ctx *middleware.Context) {
|
||||
ctx.Data["Title"] = ctx.Tr("settings")
|
||||
ctx.Data["PageIsUserSettings"] = true
|
||||
ctx.Data["PageIsSettingsPassword"] = true
|
||||
ctx.HTML(200, SETTINGS_PASSWORD)
|
||||
}
|
||||
|
||||
func SettingPassword(ctx *middleware.Context) {
|
||||
ctx.Data["Title"] = "Password"
|
||||
ctx.Data["PageIsUserSetting"] = true
|
||||
ctx.Data["IsUserPageSettingPasswd"] = true
|
||||
ctx.HTML(200, PASSWORD)
|
||||
}
|
||||
|
||||
func SettingPasswordPost(ctx *middleware.Context, form auth.UpdatePasswdForm) {
|
||||
ctx.Data["Title"] = "Password"
|
||||
ctx.Data["PageIsUserSetting"] = true
|
||||
ctx.Data["IsUserPageSettingPasswd"] = true
|
||||
func SettingsPasswordPost(ctx *middleware.Context, form auth.ChangePasswordForm) {
|
||||
ctx.Data["Title"] = ctx.Tr("settings")
|
||||
ctx.Data["PageIsUserSettings"] = true
|
||||
ctx.Data["PageIsSettingsPassword"] = true
|
||||
|
||||
if ctx.HasError() {
|
||||
ctx.HTML(200, PASSWORD)
|
||||
ctx.HTML(200, SETTINGS_PASSWORD)
|
||||
return
|
||||
}
|
||||
|
||||
tmpUser := &models.User{
|
||||
Passwd: form.OldPasswd,
|
||||
Passwd: form.OldPassword,
|
||||
Salt: ctx.User.Salt,
|
||||
}
|
||||
tmpUser.EncodePasswd()
|
||||
if ctx.User.Passwd != tmpUser.Passwd {
|
||||
ctx.Flash.Error("Old password is not correct.")
|
||||
} else if form.NewPasswd != form.RetypePasswd {
|
||||
ctx.Flash.Error("New password and re-type password are not same.")
|
||||
ctx.Flash.Error(ctx.Tr("settings.password_incorrect"))
|
||||
} else if form.Password != form.Retype {
|
||||
ctx.Flash.Error(ctx.Tr("form.password_not_match"))
|
||||
} else {
|
||||
ctx.User.Passwd = form.NewPasswd
|
||||
ctx.User.Passwd = form.Password
|
||||
ctx.User.Salt = models.GetUserSalt()
|
||||
ctx.User.EncodePasswd()
|
||||
if err := models.UpdateUser(ctx.User); err != nil {
|
||||
ctx.Handle(200, "setting.SettingPassword", err)
|
||||
ctx.Handle(500, "UpdateUser", err)
|
||||
return
|
||||
}
|
||||
log.Trace("%s User password updated: %s", ctx.Req.RequestURI, ctx.User.LowerName)
|
||||
ctx.Flash.Success("Password is changed successfully. You can now sign in via new password.")
|
||||
log.Trace("User password updated: %s", ctx.User.Name)
|
||||
ctx.Flash.Success(ctx.Tr("settings.change_password_success"))
|
||||
}
|
||||
|
||||
ctx.Redirect("/user/settings/password")
|
||||
}
|
||||
|
||||
// Checks if the given public key string is recognized by SSH.
|
||||
func CheckPublicKeyString(keyContent string) (ok bool, err error) {
|
||||
if strings.ContainsAny(keyContent, "\n\r") {
|
||||
return false, errors.New("Only a single line with a single key please")
|
||||
}
|
||||
|
||||
// write the key to a file…
|
||||
tmpFile, err := ioutil.TempFile(os.TempDir(), "keytest")
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
tmpPath := tmpFile.Name()
|
||||
defer os.Remove(tmpPath)
|
||||
tmpFile.WriteString(keyContent)
|
||||
tmpFile.Close()
|
||||
|
||||
// … see if ssh-keygen recognizes its contents
|
||||
stdout, stderr, err := process.Exec("CheckPublicKeyString", "ssh-keygen", "-l", "-f", tmpPath)
|
||||
if err != nil {
|
||||
return false, errors.New("ssh-keygen -l -f: " + stderr)
|
||||
} else if len(stdout) < 2 {
|
||||
return false, errors.New("ssh-keygen returned not enough output to evaluate the key")
|
||||
}
|
||||
sshKeygenOutput := strings.Split(stdout, " ")
|
||||
if len(sshKeygenOutput) < 4 {
|
||||
return false, errors.New("Not enough fields returned by ssh-keygen -l -f")
|
||||
}
|
||||
keySize, err := strconv.Atoi(sshKeygenOutput[0])
|
||||
if err != nil {
|
||||
return false, errors.New("Cannot get key size of the given key")
|
||||
}
|
||||
keyType := strings.TrimSpace(sshKeygenOutput[len(sshKeygenOutput)-1])
|
||||
|
||||
if minimumKeySize := MinimumKeySize[keyType]; minimumKeySize == 0 {
|
||||
return false, errors.New("Sorry, unrecognized public key type")
|
||||
} else {
|
||||
if keySize < minimumKeySize {
|
||||
return false, fmt.Errorf("The minimum accepted size of a public key %s is %d", keyType, minimumKeySize)
|
||||
}
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func SettingSSHKeys(ctx *middleware.Context, form auth.AddSSHKeyForm) {
|
||||
ctx.Data["Title"] = "SSH Keys"
|
||||
ctx.Data["PageIsUserSetting"] = true
|
||||
ctx.Data["IsUserPageSettingSSH"] = true
|
||||
|
||||
// Delete SSH key.
|
||||
if ctx.Req.Method == "DELETE" || ctx.Query("_method") == "DELETE" {
|
||||
id, err := base.StrTo(ctx.Query("id")).Int64()
|
||||
if err != nil {
|
||||
log.Error("ssh.DelPublicKey: %v", err)
|
||||
ctx.JSON(200, map[string]interface{}{
|
||||
"ok": false,
|
||||
"err": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if err = models.DeletePublicKey(&models.PublicKey{Id: id}); 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
|
||||
}
|
||||
func SettingsSSHKeys(ctx *middleware.Context) {
|
||||
ctx.Data["Title"] = ctx.Tr("settings")
|
||||
ctx.Data["PageIsUserSettings"] = true
|
||||
ctx.Data["PageIsSettingsSSHKeys"] = true
|
||||
|
||||
var err error
|
||||
// List existed SSH keys.
|
||||
ctx.Data["Keys"], err = models.ListPublicKey(ctx.User.Id)
|
||||
if err != nil {
|
||||
ctx.Handle(500, "ssh.ListPublicKey", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Add new SSH key.
|
||||
if ctx.Req.Method == "POST" {
|
||||
if ctx.HasError() {
|
||||
ctx.HTML(200, "user/publickey")
|
||||
ctx.HTML(200, SETTINGS_SSH_KEYS)
|
||||
}
|
||||
|
||||
func SettingsSSHKeysPost(ctx *middleware.Context, form auth.AddSSHKeyForm) {
|
||||
ctx.Data["Title"] = ctx.Tr("settings")
|
||||
ctx.Data["PageIsUserSettings"] = true
|
||||
ctx.Data["PageIsSettingsSSHKeys"] = true
|
||||
|
||||
var err error
|
||||
ctx.Data["Keys"], err = models.ListPublicKey(ctx.User.Id)
|
||||
if err != nil {
|
||||
ctx.Handle(500, "ssh.ListPublicKey", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Delete SSH key.
|
||||
if ctx.Query("_method") == "DELETE" {
|
||||
id := com.StrTo(ctx.Query("id")).MustInt64()
|
||||
if id <= 0 {
|
||||
return
|
||||
}
|
||||
|
||||
if ok, err := CheckPublicKeyString(form.KeyContent); !ok {
|
||||
ctx.Flash.Error(err.Error())
|
||||
if err = models.DeletePublicKey(&models.PublicKey{Id: id}); err != nil {
|
||||
ctx.Handle(500, "DeletePublicKey", err)
|
||||
} else {
|
||||
log.Trace("SSH key deleted: %s", ctx.User.Name)
|
||||
ctx.Redirect("/user/settings/ssh")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Add new SSH key.
|
||||
if ctx.Req.Method == "POST" {
|
||||
if ctx.HasError() {
|
||||
ctx.HTML(200, SETTINGS_SSH_KEYS)
|
||||
return
|
||||
}
|
||||
|
||||
if ok, err := models.CheckPublicKeyString(form.Content); !ok {
|
||||
ctx.Flash.Error(ctx.Tr("form.invalid_ssh_key", err.Error()))
|
||||
ctx.Redirect("/user/settings/ssh")
|
||||
return
|
||||
}
|
||||
|
||||
k := &models.PublicKey{
|
||||
OwnerId: ctx.User.Id,
|
||||
Name: form.KeyName,
|
||||
Content: form.KeyContent,
|
||||
Name: form.SSHTitle,
|
||||
Content: form.Content,
|
||||
}
|
||||
|
||||
if err := models.AddPublicKey(k); err != nil {
|
||||
if err.Error() == models.ErrKeyAlreadyExist.Error() {
|
||||
ctx.RenderWithErr("Public key name has been used", "user/publickey", &form)
|
||||
if err == models.ErrKeyAlreadyExist {
|
||||
ctx.RenderWithErr(ctx.Tr("form.ssh_key_been_used"), SETTINGS_SSH_KEYS, &form)
|
||||
return
|
||||
}
|
||||
ctx.Handle(500, "ssh.AddPublicKey", err)
|
||||
return
|
||||
} else {
|
||||
log.Trace("%s User SSH key added: %s", ctx.Req.RequestURI, ctx.User.LowerName)
|
||||
ctx.Flash.Success("New SSH Key has been added!")
|
||||
log.Trace("SSH key added: %s", ctx.User.Name)
|
||||
ctx.Flash.Success(ctx.Tr("settings.add_key_success"))
|
||||
ctx.Redirect("/user/settings/ssh")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
ctx.HTML(200, PUBLICKEY)
|
||||
ctx.HTML(200, SETTINGS_SSH_KEYS)
|
||||
}
|
||||
|
||||
func SettingNotification(ctx *middleware.Context) {
|
||||
// TODO: user setting notification
|
||||
ctx.Data["Title"] = "Notification"
|
||||
ctx.Data["PageIsUserSetting"] = true
|
||||
ctx.Data["IsUserPageSettingNotify"] = true
|
||||
ctx.HTML(200, NOTIFICATION)
|
||||
// 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
|
||||
ctx.HTML(200, SETTINGS_SOCIAL)
|
||||
}
|
||||
|
||||
func SettingSecurity(ctx *middleware.Context) {
|
||||
// TODO: user setting security
|
||||
ctx.Data["Title"] = "Security"
|
||||
ctx.Data["PageIsUserSetting"] = true
|
||||
ctx.Data["IsUserPageSettingSecurity"] = true
|
||||
ctx.HTML(200, SECURITY)
|
||||
func SettingsDelete(ctx *middleware.Context) {
|
||||
ctx.Data["Title"] = ctx.Tr("settings")
|
||||
ctx.Data["PageIsUserSettings"] = true
|
||||
ctx.Data["PageIsSettingsDelete"] = true
|
||||
|
||||
if ctx.Req.Method == "POST" {
|
||||
// tmpUser := models.User{
|
||||
// Passwd: ctx.Query("password"),
|
||||
// Salt: ctx.User.Salt,
|
||||
// }
|
||||
// tmpUser.EncodePasswd()
|
||||
// if tmpUser.Passwd != ctx.User.Passwd {
|
||||
// ctx.Flash.Error("Password is not correct. Make sure you are owner of this account.")
|
||||
// } else {
|
||||
if err := models.DeleteUser(ctx.User); err != nil {
|
||||
switch err {
|
||||
case models.ErrUserOwnRepos:
|
||||
ctx.Flash.Error(ctx.Tr("form.still_own_repo"))
|
||||
ctx.Redirect("/user/settings/delete")
|
||||
return
|
||||
default:
|
||||
ctx.Handle(500, "DeleteUser", err)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
log.Trace("Account deleted: %s", ctx.User.Name)
|
||||
ctx.Redirect("/")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
ctx.HTML(200, SETTINGS_DELETE)
|
||||
}
|
||||
|
@@ -11,8 +11,6 @@ import (
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/go-martini/martini"
|
||||
|
||||
"github.com/gogits/gogs/models"
|
||||
"github.com/gogits/gogs/modules/log"
|
||||
"github.com/gogits/gogs/modules/middleware"
|
||||
@@ -28,14 +26,14 @@ func extractPath(next string) string {
|
||||
return n.Path
|
||||
}
|
||||
|
||||
func SocialSignIn(ctx *middleware.Context, params martini.Params) {
|
||||
func SocialSignIn(ctx *middleware.Context) {
|
||||
if setting.OauthService == nil {
|
||||
ctx.Handle(404, "social.SocialSignIn(oauth service not enabled)", nil)
|
||||
return
|
||||
}
|
||||
|
||||
next := extractPath(ctx.Query("next"))
|
||||
name := params["name"]
|
||||
name := ctx.Params(":name")
|
||||
connect, ok := social.SocialMap[name]
|
||||
if !ok {
|
||||
ctx.Handle(404, "social.SocialSignIn(social login not enabled)", errors.New(name))
|
||||
@@ -81,7 +79,7 @@ func SocialSignIn(ctx *middleware.Context, params martini.Params) {
|
||||
}
|
||||
log.Trace("social.SocialSignIn(oa): %v", oa)
|
||||
if err = models.AddOauth2(oa); err != nil {
|
||||
log.Error("social.SocialSignIn(add oauth2): %v", err) // 501
|
||||
log.Error(4, "social.SocialSignIn(add oauth2): %v", err) // 501
|
||||
return
|
||||
}
|
||||
case models.ErrOauth2NotAssociated:
|
||||
|
@@ -1,457 +0,0 @@
|
||||
// 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 (
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"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"
|
||||
"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"] = "Log In"
|
||||
|
||||
if _, ok := ctx.Session.Get("socialId").(int64); ok {
|
||||
ctx.Data["IsSocialLogin"] = true
|
||||
ctx.HTML(200, SIGNIN)
|
||||
return
|
||||
}
|
||||
|
||||
if setting.OauthService != nil {
|
||||
ctx.Data["OauthEnabled"] = true
|
||||
ctx.Data["OauthService"] = setting.OauthService
|
||||
}
|
||||
|
||||
// Check auto-login.
|
||||
uname := ctx.GetCookie(setting.CookieUserName)
|
||||
if len(uname) == 0 {
|
||||
ctx.HTML(200, SIGNIN)
|
||||
return
|
||||
}
|
||||
|
||||
isSucceed := false
|
||||
defer func() {
|
||||
if !isSucceed {
|
||||
log.Trace("user.SignIn(auto-login cookie cleared): %s", uname)
|
||||
ctx.SetCookie(setting.CookieUserName, "", -1)
|
||||
ctx.SetCookie(setting.CookieRememberName, "", -1)
|
||||
return
|
||||
}
|
||||
}()
|
||||
|
||||
user, err := models.GetUserByName(uname)
|
||||
if err != nil {
|
||||
ctx.Handle(500, "user.SignIn(GetUserByName)", err)
|
||||
return
|
||||
}
|
||||
|
||||
secret := base.EncodeMd5(user.Rands + user.Passwd)
|
||||
value, _ := ctx.GetSecureCookie(secret, setting.CookieRememberName)
|
||||
if value != user.Name {
|
||||
ctx.HTML(200, SIGNIN)
|
||||
return
|
||||
}
|
||||
|
||||
isSucceed = true
|
||||
|
||||
ctx.Session.Set("userId", user.Id)
|
||||
ctx.Session.Set("userName", user.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.LogInForm) {
|
||||
ctx.Data["Title"] = "Log In"
|
||||
|
||||
sid, isOauth := ctx.Session.Get("socialId").(int64)
|
||||
if isOauth {
|
||||
ctx.Data["IsSocialLogin"] = true
|
||||
} else if setting.OauthService != nil {
|
||||
ctx.Data["OauthEnabled"] = true
|
||||
ctx.Data["OauthService"] = setting.OauthService
|
||||
}
|
||||
|
||||
if ctx.HasError() {
|
||||
ctx.HTML(200, SIGNIN)
|
||||
return
|
||||
}
|
||||
|
||||
user, err := models.UserSignIn(form.UserName, form.Password)
|
||||
if err != nil {
|
||||
if err == models.ErrUserNotExist {
|
||||
log.Trace("%s Log in failed: %s", ctx.Req.RequestURI, form.UserName)
|
||||
ctx.RenderWithErr("Username or password is not correct", SIGNIN, &form)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Handle(500, "user.SignInPost(UserSignIn)", err)
|
||||
return
|
||||
}
|
||||
|
||||
if form.Remember {
|
||||
secret := base.EncodeMd5(user.Rands + user.Passwd)
|
||||
days := 86400 * setting.LogInRememberDays
|
||||
ctx.SetCookie(setting.CookieUserName, user.Name, days)
|
||||
ctx.SetSecureCookie(secret, setting.CookieRememberName, user.Name, days)
|
||||
}
|
||||
|
||||
// Bind with social account.
|
||||
if isOauth {
|
||||
if err = models.BindUserOauth2(user.Id, sid); err != nil {
|
||||
if err == models.ErrOauth2RecordNotExist {
|
||||
ctx.Handle(404, "user.SignInPost(GetOauth2ById)", err)
|
||||
} else {
|
||||
ctx.Handle(500, "user.SignInPost(GetOauth2ById)", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
ctx.Session.Delete("socialId")
|
||||
log.Trace("%s OAuth binded: %s -> %d", ctx.Req.RequestURI, form.UserName, sid)
|
||||
}
|
||||
|
||||
ctx.Session.Set("userId", user.Id)
|
||||
ctx.Session.Set("userName", user.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("userId")
|
||||
ctx.Session.Delete("userName")
|
||||
ctx.Session.Delete("socialId")
|
||||
ctx.Session.Delete("socialName")
|
||||
ctx.Session.Delete("socialEmail")
|
||||
ctx.SetCookie(setting.CookieUserName, "", -1)
|
||||
ctx.SetCookie(setting.CookieRememberName, "", -1)
|
||||
ctx.Redirect("/")
|
||||
}
|
||||
|
||||
func SignUp(ctx *middleware.Context) {
|
||||
ctx.Data["Title"] = "Sign Up"
|
||||
ctx.Data["PageIsSignUp"] = true
|
||||
|
||||
if setting.Service.DisableRegistration {
|
||||
ctx.Data["DisableRegistration"] = true
|
||||
ctx.HTML(200, SIGNUP)
|
||||
return
|
||||
}
|
||||
|
||||
if sid, ok := ctx.Session.Get("socialId").(int64); ok {
|
||||
oauthSignUp(ctx, sid)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.HTML(200, SIGNUP)
|
||||
}
|
||||
|
||||
func oauthSignUp(ctx *middleware.Context, sid int64) {
|
||||
ctx.Data["Title"] = "OAuth Sign Up"
|
||||
ctx.Data["PageIsSignUp"] = true
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
func SignUpPost(ctx *middleware.Context, form auth.RegisterForm) {
|
||||
ctx.Data["Title"] = "Sign Up"
|
||||
ctx.Data["PageIsSignUp"] = true
|
||||
|
||||
if setting.Service.DisableRegistration {
|
||||
ctx.Handle(403, "user.SignUpPost", nil)
|
||||
return
|
||||
}
|
||||
|
||||
sid, isOauth := ctx.Session.Get("socialId").(int64)
|
||||
if isOauth {
|
||||
ctx.Data["IsSocialLogin"] = true
|
||||
}
|
||||
|
||||
if ctx.HasError() {
|
||||
ctx.HTML(200, SIGNUP)
|
||||
return
|
||||
}
|
||||
|
||||
if form.Password != form.RetypePasswd {
|
||||
ctx.Data["Err_Password"] = true
|
||||
ctx.Data["Err_RetypePasswd"] = true
|
||||
ctx.RenderWithErr("Password and re-type password are not same.", SIGNUP, &form)
|
||||
return
|
||||
}
|
||||
|
||||
u := &models.User{
|
||||
Name: form.UserName,
|
||||
Email: form.Email,
|
||||
Passwd: form.Password,
|
||||
IsActive: !setting.Service.RegisterEmailConfirm || isOauth,
|
||||
}
|
||||
|
||||
var err error
|
||||
if u, err = models.CreateUser(u); err != nil {
|
||||
switch err {
|
||||
case models.ErrUserAlreadyExist:
|
||||
ctx.Data["Err_UserName"] = true
|
||||
ctx.RenderWithErr("Username has been already taken", SIGNUP, &form)
|
||||
case models.ErrEmailAlreadyUsed:
|
||||
ctx.Data["Err_Email"] = true
|
||||
ctx.RenderWithErr("E-mail address has been already used", SIGNUP, &form)
|
||||
case models.ErrUserNameIllegal:
|
||||
ctx.Data["Err_UserName"] = true
|
||||
ctx.RenderWithErr(models.ErrRepoNameIllegal.Error(), SIGNUP, &form)
|
||||
default:
|
||||
ctx.Handle(500, "user.SignUpPost(CreateUser)", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
log.Trace("%s User created: %s", ctx.Req.RequestURI, 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)
|
||||
}
|
||||
|
||||
// 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 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
|
||||
ctx.HTML(200, DELETE)
|
||||
}
|
||||
|
||||
func DeletePost(ctx *middleware.Context) {
|
||||
ctx.Data["Title"] = "Delete Account"
|
||||
ctx.Data["PageIsUserSetting"] = true
|
||||
ctx.Data["IsUserPageSettingDelete"] = true
|
||||
|
||||
tmpUser := models.User{
|
||||
Passwd: ctx.Query("password"),
|
||||
Salt: ctx.User.Salt,
|
||||
}
|
||||
tmpUser.EncodePasswd()
|
||||
if tmpUser.Passwd != ctx.User.Passwd {
|
||||
ctx.Flash.Error("Password is not correct. Make sure you are owner of this account.")
|
||||
} else {
|
||||
if err := models.DeleteUser(ctx.User); err != nil {
|
||||
switch err {
|
||||
case models.ErrUserOwnRepos:
|
||||
ctx.Flash.Error("Your account still have ownership of repository, you have to delete or transfer them first.")
|
||||
default:
|
||||
ctx.Handle(500, "user.DeletePost(DeleteUser)", err)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
ctx.Redirect("/")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
ctx.Redirect("/user/delete")
|
||||
}
|
||||
|
||||
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 setting.Service.RegisterEmailConfirm {
|
||||
if ctx.Cache.IsExist("MailResendLimit_" + ctx.User.LowerName) {
|
||||
ctx.Data["ResendLimited"] = true
|
||||
} else {
|
||||
ctx.Data["Hours"] = setting.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, ACTIVATE)
|
||||
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, ACTIVATE)
|
||||
}
|
||||
|
||||
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) {
|
||||
ctx.Data["Title"] = "Forgot Password"
|
||||
|
||||
if setting.MailService == nil {
|
||||
ctx.Handle(403, "user.ForgotPasswdPost", nil)
|
||||
return
|
||||
}
|
||||
ctx.Data["IsResetRequest"] = true
|
||||
|
||||
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(500, "user.ResetPasswd(check existence)", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if ctx.Cache.IsExist("MailResendLimit_" + u.LowerName) {
|
||||
ctx.Data["ResendLimited"] = true
|
||||
ctx.HTML(200, FORGOT_PASSWORD)
|
||||
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"] = setting.Service.ActiveCodeLives / 60
|
||||
ctx.Data["IsResetSent"] = true
|
||||
ctx.HTML(200, FORGOT_PASSWORD)
|
||||
}
|
||||
|
||||
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) {
|
||||
ctx.Data["Title"] = "Reset Password"
|
||||
|
||||
code := ctx.Query("code")
|
||||
if len(code) == 0 {
|
||||
ctx.Error(404)
|
||||
return
|
||||
}
|
||||
ctx.Data["Code"] = code
|
||||
|
||||
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(500, "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, RESET_PASSWORD)
|
||||
}
|
Reference in New Issue
Block a user