summaryrefslogtreecommitdiffstats
path: root/routers
diff options
context:
space:
mode:
Diffstat (limited to 'routers')
-rw-r--r--routers/api/v1/repo.go75
-rw-r--r--routers/api/v1/repo_file.go2
-rw-r--r--routers/install.go6
-rw-r--r--routers/org/setting.go15
-rw-r--r--routers/org/teams.go2
-rw-r--r--routers/repo/http.go5
-rw-r--r--routers/repo/issue.go14
-rw-r--r--routers/repo/release.go10
-rw-r--r--routers/repo/repo.go30
-rw-r--r--routers/repo/setting.go3
-rw-r--r--routers/user/auth.go5
-rw-r--r--routers/user/setting.go91
12 files changed, 137 insertions, 121 deletions
diff --git a/routers/api/v1/repo.go b/routers/api/v1/repo.go
index f5128e4746..eb9908911f 100644
--- a/routers/api/v1/repo.go
+++ b/routers/api/v1/repo.go
@@ -5,7 +5,7 @@
package v1
import (
- "fmt"
+ "net/url"
"path"
"strings"
@@ -156,17 +156,15 @@ func CreateOrgRepo(ctx *middleware.Context, opt api.CreateRepoOption) {
func MigrateRepo(ctx *middleware.Context, form auth.MigrateRepoForm) {
u, err := models.GetUserByName(ctx.Query("username"))
if err != nil {
- ctx.JSON(500, map[string]interface{}{
- "ok": false,
- "error": err.Error(),
- })
+ if err == models.ErrUserNotExist {
+ ctx.HandleAPI(422, err)
+ } else {
+ ctx.HandleAPI(500, err)
+ }
return
}
if !u.ValidtePassword(ctx.Query("password")) {
- ctx.JSON(500, map[string]interface{}{
- "ok": false,
- "error": "username or password is not correct",
- })
+ ctx.HandleAPI(422, "Username or password is not correct.")
return
}
@@ -175,56 +173,59 @@ func MigrateRepo(ctx *middleware.Context, form auth.MigrateRepoForm) {
if form.Uid != u.Id {
org, err := models.GetUserById(form.Uid)
if err != nil {
- log.Error(4, "GetUserById: %v", err)
- ctx.Error(500)
+ if err == models.ErrUserNotExist {
+ ctx.HandleAPI(422, err)
+ } else {
+ ctx.HandleAPI(500, err)
+ }
return
}
ctxUser = org
}
if ctx.HasError() {
- ctx.JSON(422, map[string]interface{}{
- "ok": false,
- "error": ctx.GetErrMsg(),
- })
+ ctx.HandleAPI(422, ctx.GetErrMsg())
return
}
if ctxUser.IsOrganization() {
// Check ownership of organization.
if !ctxUser.IsOwnedBy(u.Id) {
- ctx.JSON(403, map[string]interface{}{
- "ok": false,
- "error": "given user is not owner of organization",
- })
+ ctx.HandleAPI(403, "Given user is not owner of organization.")
return
}
}
- authStr := strings.Replace(fmt.Sprintf("://%s:%s",
- form.AuthUserName, form.AuthPasswd), "@", "%40", -1)
- url := strings.Replace(form.HttpsUrl, "://", authStr+"@", 1)
- repo, err := models.MigrateRepository(ctxUser, form.RepoName, form.Description, form.Private,
- form.Mirror, url)
- if err == nil {
- log.Trace("Repository migrated: %s/%s", ctxUser.Name, form.RepoName)
- ctx.JSON(200, map[string]interface{}{
- "ok": true,
- "data": "/" + ctxUser.Name + "/" + form.RepoName,
- })
+ // Remote address can be HTTPS URL or local path.
+ remoteAddr := form.CloneAddr
+ if strings.HasPrefix(form.CloneAddr, "http") {
+ u, err := url.Parse(form.CloneAddr)
+ if err != nil {
+ ctx.HandleAPI(422, err)
+ return
+ }
+ if len(form.AuthUserName) > 0 || len(form.AuthPasswd) > 0 {
+ u.User = url.UserPassword(form.AuthUserName, form.AuthPasswd)
+ }
+ remoteAddr = u.String()
+ } else if !com.IsDir(remoteAddr) {
+ ctx.HandleAPI(422, "Invalid local path, it does not exist or not a directory.")
return
}
- if repo != nil {
- if errDelete := models.DeleteRepository(ctxUser.Id, repo.Id, ctxUser.Name); errDelete != nil {
- log.Error(4, "DeleteRepository: %v", errDelete)
+ repo, err := models.MigrateRepository(ctxUser, form.RepoName, form.Description, form.Private, form.Mirror, remoteAddr)
+ if err != nil {
+ if repo != nil {
+ if errDelete := models.DeleteRepository(ctxUser.Id, repo.Id, ctxUser.Name); errDelete != nil {
+ log.Error(4, "DeleteRepository: %v", errDelete)
+ }
}
+ ctx.HandleAPI(500, err)
+ return
}
- ctx.JSON(500, map[string]interface{}{
- "ok": false,
- "error": err.Error(),
- })
+ log.Trace("Repository migrated: %s/%s", ctxUser.Name, form.RepoName)
+ ctx.WriteHeader(200)
}
// GET /user/repos
diff --git a/routers/api/v1/repo_file.go b/routers/api/v1/repo_file.go
index a049904f95..73f97b2cae 100644
--- a/routers/api/v1/repo_file.go
+++ b/routers/api/v1/repo_file.go
@@ -12,7 +12,7 @@ import (
)
func GetRepoRawFile(ctx *middleware.Context) {
- if ctx.Repo.Repository.IsPrivate && !ctx.Repo.HasAccess {
+ if !ctx.Repo.HasAccess() {
ctx.Error(404)
return
}
diff --git a/routers/install.go b/routers/install.go
index a3583a1a98..a7828e351c 100644
--- a/routers/install.go
+++ b/routers/install.go
@@ -189,6 +189,12 @@ func InstallPost(ctx *middleware.Context, form auth.InstallForm) {
// Save settings.
cfg := ini.Empty()
+ if com.IsFile(setting.CustomConf) {
+ // Keeps custom settings if there is already something.
+ if err := cfg.Append(setting.CustomConf); err != nil {
+ log.Error(4, "Fail to load custom conf '%s': %v", setting.CustomConf, err)
+ }
+ }
cfg.Section("database").Key("DB_TYPE").SetValue(models.DbCfg.Type)
cfg.Section("database").Key("HOST").SetValue(models.DbCfg.Host)
cfg.Section("database").Key("NAME").SetValue(models.DbCfg.Name)
diff --git a/routers/org/setting.go b/routers/org/setting.go
index 41ec4a2141..c638a032e7 100644
--- a/routers/org/setting.go
+++ b/routers/org/setting.go
@@ -39,18 +39,18 @@ func SettingsPost(ctx *middleware.Context, form auth.UpdateOrgSettingForm) {
// Check if organization name has been changed.
if org.Name != form.OrgUserName {
- isExist, err := models.IsUserExist(form.OrgUserName)
+ isExist, err := models.IsUserExist(org.Id, form.OrgUserName)
if err != nil {
ctx.Handle(500, "IsUserExist", err)
return
} else if isExist {
+ ctx.Data["Err_UserName"] = true
ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), SETTINGS_OPTIONS, &form)
return
} else if err = models.ChangeUserName(org, form.OrgUserName); err != nil {
if err == models.ErrUserNameIllegal {
- ctx.Flash.Error(ctx.Tr("form.illegal_username"))
- ctx.Redirect(setting.AppSubUrl + "/org/" + org.LowerName + "/settings")
- return
+ ctx.Data["Err_UserName"] = true
+ ctx.RenderWithErr(ctx.Tr("form.illegal_username"), SETTINGS_OPTIONS, &form)
} else {
ctx.Handle(500, "ChangeUserName", err)
}
@@ -68,7 +68,12 @@ func SettingsPost(ctx *middleware.Context, form auth.UpdateOrgSettingForm) {
org.Avatar = base.EncodeMd5(form.Avatar)
org.AvatarEmail = form.Avatar
if err := models.UpdateUser(org); err != nil {
- ctx.Handle(500, "UpdateUser", err)
+ if err == models.ErrEmailAlreadyUsed {
+ ctx.Data["Err_Email"] = true
+ ctx.RenderWithErr(ctx.Tr("form.email_been_used"), SETTINGS_OPTIONS, &form)
+ } else {
+ ctx.Handle(500, "UpdateUser", err)
+ }
return
}
log.Trace("Organization setting updated: %s", org.Name)
diff --git a/routers/org/teams.go b/routers/org/teams.go
index 2fbb1480d1..69f2734c64 100644
--- a/routers/org/teams.go
+++ b/routers/org/teams.go
@@ -181,7 +181,7 @@ func NewTeamPost(ctx *middleware.Context, form auth.CreateTeamForm) {
org := ctx.Org.Organization
t := &models.Team{
- OrgId: org.Id,
+ OrgID: org.Id,
Name: form.TeamName,
Description: form.Description,
Authorize: auth,
diff --git a/routers/repo/http.go b/routers/repo/http.go
index 034b5a7b5e..d47d73ef05 100644
--- a/routers/repo/http.go
+++ b/routers/repo/http.go
@@ -158,6 +158,11 @@ func Http(ctx *middleware.Context) {
return
}
}
+
+ if !isPull && repo.IsMirror {
+ ctx.Handle(401, "can't push to mirror", nil)
+ return
+ }
}
}
diff --git a/routers/repo/issue.go b/routers/repo/issue.go
index bf39d9aba6..40e9338970 100644
--- a/routers/repo/issue.go
+++ b/routers/repo/issue.go
@@ -230,7 +230,7 @@ func CreateIssuePost(ctx *middleware.Context, form auth.CreateIssueForm) {
}
// Only collaborators can assign.
- if !ctx.Repo.IsOwner {
+ if !ctx.Repo.IsOwner() {
form.AssigneeId = 0
}
issue := &models.Issue{
@@ -434,7 +434,7 @@ func ViewIssue(ctx *middleware.Context) {
ctx.Data["Title"] = issue.Name
ctx.Data["Issue"] = issue
ctx.Data["Comments"] = comments
- ctx.Data["IsIssueOwner"] = ctx.Repo.IsOwner || (ctx.IsSigned && issue.PosterId == ctx.User.Id)
+ ctx.Data["IsIssueOwner"] = ctx.Repo.IsOwner() || (ctx.IsSigned && issue.PosterId == ctx.User.Id)
ctx.Data["IsRepoToolbarIssues"] = true
ctx.Data["IsRepoToolbarIssuesList"] = false
ctx.HTML(200, ISSUE_VIEW)
@@ -457,7 +457,7 @@ func UpdateIssue(ctx *middleware.Context, form auth.CreateIssueForm) {
return
}
- if ctx.User.Id != issue.PosterId && !ctx.Repo.IsOwner {
+ if ctx.User.Id != issue.PosterId && !ctx.Repo.IsOwner() {
ctx.Error(403)
return
}
@@ -484,7 +484,7 @@ func UpdateIssue(ctx *middleware.Context, form auth.CreateIssueForm) {
}
func UpdateIssueLabel(ctx *middleware.Context) {
- if !ctx.Repo.IsOwner {
+ if !ctx.Repo.IsOwner() {
ctx.Error(403)
return
}
@@ -560,7 +560,7 @@ func UpdateIssueLabel(ctx *middleware.Context) {
}
func UpdateIssueMilestone(ctx *middleware.Context) {
- if !ctx.Repo.IsOwner {
+ if !ctx.Repo.IsOwner() {
ctx.Error(403)
return
}
@@ -606,7 +606,7 @@ func UpdateIssueMilestone(ctx *middleware.Context) {
}
func UpdateAssignee(ctx *middleware.Context) {
- if !ctx.Repo.IsOwner {
+ if !ctx.Repo.IsOwner() {
ctx.Error(403)
return
}
@@ -752,7 +752,7 @@ func Comment(ctx *middleware.Context) {
// Check if issue owner changes the status of issue.
var newStatus string
- if ctx.Repo.IsOwner || issue.PosterId == ctx.User.Id {
+ if ctx.Repo.IsOwner() || issue.PosterId == ctx.User.Id {
newStatus = ctx.Query("change_status")
}
if len(newStatus) > 0 {
diff --git a/routers/repo/release.go b/routers/repo/release.go
index 591810cc5f..52d78b1967 100644
--- a/routers/repo/release.go
+++ b/routers/repo/release.go
@@ -41,7 +41,7 @@ func Releases(ctx *middleware.Context) {
tags := make([]*models.Release, len(rawTags))
for i, rawTag := range rawTags {
for j, rel := range rels {
- if rel == nil || (rel.IsDraft && !ctx.Repo.IsOwner) {
+ if rel == nil || (rel.IsDraft && !ctx.Repo.IsOwner()) {
continue
}
if rel.TagName == rawTag {
@@ -140,7 +140,7 @@ func Releases(ctx *middleware.Context) {
}
func NewRelease(ctx *middleware.Context) {
- if !ctx.Repo.IsOwner {
+ if !ctx.Repo.IsOwner() {
ctx.Handle(403, "release.ReleasesNew", nil)
return
}
@@ -153,7 +153,7 @@ func NewRelease(ctx *middleware.Context) {
}
func NewReleasePost(ctx *middleware.Context, form auth.NewReleaseForm) {
- if !ctx.Repo.IsOwner {
+ if !ctx.Repo.IsOwner() {
ctx.Handle(403, "release.ReleasesNew", nil)
return
}
@@ -211,7 +211,7 @@ func NewReleasePost(ctx *middleware.Context, form auth.NewReleaseForm) {
}
func EditRelease(ctx *middleware.Context) {
- if !ctx.Repo.IsOwner {
+ if !ctx.Repo.IsOwner() {
ctx.Handle(403, "release.ReleasesEdit", nil)
return
}
@@ -234,7 +234,7 @@ func EditRelease(ctx *middleware.Context) {
}
func EditReleasePost(ctx *middleware.Context, form auth.EditReleaseForm) {
- if !ctx.Repo.IsOwner {
+ if !ctx.Repo.IsOwner() {
ctx.Handle(403, "release.EditReleasePost", nil)
return
}
diff --git a/routers/repo/repo.go b/routers/repo/repo.go
index 48f7b09bc0..6b84a389d5 100644
--- a/routers/repo/repo.go
+++ b/routers/repo/repo.go
@@ -181,20 +181,26 @@ func MigratePost(ctx *middleware.Context, form auth.MigrateRepoForm) {
}
}
- u, err := url.Parse(form.HttpsUrl)
-
- if err != nil || u.Scheme != "https" {
- ctx.Data["Err_HttpsUrl"] = true
- ctx.RenderWithErr(ctx.Tr("form.url_error"), MIGRATE, &form)
+ // Remote address can be HTTPS URL or local path.
+ remoteAddr := form.CloneAddr
+ if strings.HasPrefix(form.CloneAddr, "http") {
+ u, err := url.Parse(form.CloneAddr)
+ if err != nil {
+ ctx.Data["Err_CloneAddr"] = true
+ ctx.RenderWithErr(ctx.Tr("form.url_error"), MIGRATE, &form)
+ return
+ }
+ if len(form.AuthUserName) > 0 || len(form.AuthPasswd) > 0 {
+ u.User = url.UserPassword(form.AuthUserName, form.AuthPasswd)
+ }
+ remoteAddr = u.String()
+ } else if !com.IsDir(remoteAddr) {
+ ctx.Data["Err_CloneAddr"] = true
+ ctx.RenderWithErr(ctx.Tr("repo.migrate.invalid_local_path"), MIGRATE, &form)
return
}
- if len(form.AuthUserName) > 0 || len(form.AuthPasswd) > 0 {
- u.User = url.UserPassword(form.AuthUserName, form.AuthPasswd)
- }
-
- repo, err := models.MigrateRepository(ctxUser, form.RepoName, form.Description, form.Private,
- form.Mirror, u.String())
+ repo, err := models.MigrateRepository(ctxUser, form.RepoName, form.Description, form.Private, form.Mirror, remoteAddr)
if err == nil {
log.Trace("Repository migrated: %s/%s", ctxUser.Name, form.RepoName)
ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + form.RepoName)
@@ -343,7 +349,7 @@ func Action(ctx *middleware.Context) {
case "unstar":
err = models.StarRepo(ctx.User.Id, ctx.Repo.Repository.Id, false)
case "desc":
- if !ctx.Repo.IsOwner {
+ if !ctx.Repo.IsOwner() {
ctx.Error(404)
return
}
diff --git a/routers/repo/setting.go b/routers/repo/setting.go
index a6f50d3068..e2b8968375 100644
--- a/routers/repo/setting.go
+++ b/routers/repo/setting.go
@@ -83,7 +83,6 @@ func SettingsPost(ctx *middleware.Context, form auth.RepoSettingForm) {
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, "UpdateRepository", err)
return
@@ -109,7 +108,7 @@ func SettingsPost(ctx *middleware.Context, form auth.RepoSettingForm) {
}
newOwner := ctx.Query("new_owner_name")
- isExist, err := models.IsUserExist(newOwner)
+ isExist, err := models.IsUserExist(0, newOwner)
if err != nil {
ctx.Handle(500, "IsUserExist", err)
return
diff --git a/routers/user/auth.go b/routers/user/auth.go
index 9ed44e353c..5dacaf8c79 100644
--- a/routers/user/auth.go
+++ b/routers/user/auth.go
@@ -351,15 +351,12 @@ func ActivateEmail(ctx *middleware.Context) {
// Verify code.
if email := models.VerifyActiveEmailCode(code, email_string); email != nil {
- err := email.Activate()
- if err != nil {
+ if err := email.Activate(); err != nil {
ctx.Handle(500, "ActivateEmail", err)
}
log.Trace("Email activated: %s", email.Email)
-
ctx.Flash.Success(ctx.Tr("settings.activate_email_success"))
-
}
ctx.Redirect(setting.AppSubUrl + "/user/settings/email")
diff --git a/routers/user/setting.go b/routers/user/setting.go
index 953e61138f..a44d3b7e5d 100644
--- a/routers/user/setting.go
+++ b/routers/user/setting.go
@@ -50,7 +50,7 @@ func SettingsPost(ctx *middleware.Context, form auth.UpdateProfileForm) {
// Check if user name has been changed.
if ctx.User.Name != form.UserName {
- isExist, err := models.IsUserExist(form.UserName)
+ isExist, err := models.IsUserExist(ctx.User.Id, form.UserName)
if err != nil {
ctx.Handle(500, "IsUserExist", err)
return
@@ -58,11 +58,14 @@ func SettingsPost(ctx *middleware.Context, form auth.UpdateProfileForm) {
ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), SETTINGS_PROFILE, &form)
return
} else if err = models.ChangeUserName(ctx.User, form.UserName); err != nil {
- if err == models.ErrUserNameIllegal {
+ switch err {
+ case models.ErrUserNameIllegal:
ctx.Flash.Error(ctx.Tr("form.illegal_username"))
ctx.Redirect(setting.AppSubUrl + "/user/settings")
- return
- } else {
+ case models.ErrEmailAlreadyUsed:
+ ctx.Flash.Error(ctx.Tr("form.email_been_used"))
+ ctx.Redirect(setting.AppSubUrl + "/user/settings")
+ default:
ctx.Handle(500, "ChangeUserName", err)
}
return
@@ -133,13 +136,12 @@ func SettingsEmails(ctx *middleware.Context) {
ctx.Data["PageIsUserSettings"] = true
ctx.Data["PageIsSettingsEmails"] = true
- var err error
- ctx.Data["Emails"], err = models.GetEmailAddresses(ctx.User.Id)
-
+ emails, err := models.GetEmailAddresses(ctx.User.Id)
if err != nil {
- ctx.Handle(500, "email.GetEmailAddresses", err)
+ ctx.Handle(500, "GetEmailAddresses", err)
return
}
+ ctx.Data["Emails"] = emails
ctx.HTML(200, SETTINGS_EMAILS)
}
@@ -149,16 +151,16 @@ func SettingsEmailPost(ctx *middleware.Context, form auth.AddEmailForm) {
ctx.Data["PageIsUserSettings"] = true
ctx.Data["PageIsSettingsEmails"] = true
- var err error
- ctx.Data["Emails"], err = models.GetEmailAddresses(ctx.User.Id)
+ emails, err := models.GetEmailAddresses(ctx.User.Id)
if err != nil {
- ctx.Handle(500, "email.GetEmailAddresses", err)
+ ctx.Handle(500, "GetEmailAddresses", err)
return
}
+ ctx.Data["Emails"] = emails
- // Delete Email address.
+ // Delete E-mail address.
if ctx.Query("_method") == "DELETE" {
- id := com.StrTo(ctx.Query("id")).MustInt64()
+ id := ctx.QueryInt64("id")
if id <= 0 {
return
}
@@ -174,7 +176,7 @@ func SettingsEmailPost(ctx *middleware.Context, form auth.AddEmailForm) {
// Make emailaddress primary.
if ctx.Query("_method") == "PRIMARY" {
- id := com.StrTo(ctx.Query("id")).MustInt64()
+ id := ctx.QueryInt64("id")
if id <= 0 {
return
}
@@ -189,46 +191,41 @@ func SettingsEmailPost(ctx *middleware.Context, form auth.AddEmailForm) {
}
// Add Email address.
- if ctx.Req.Method == "POST" {
- if ctx.HasError() {
- ctx.HTML(200, SETTINGS_EMAILS)
- return
- }
+ if ctx.HasError() {
+ ctx.HTML(200, SETTINGS_EMAILS)
+ return
+ }
- cleanEmail := strings.Replace(form.Email, "\n", "", -1)
- e := &models.EmailAddress{
- Uid: ctx.User.Id,
- Email: cleanEmail,
- IsActivated: !setting.Service.RegisterEmailConfirm,
- }
+ cleanEmail := strings.Replace(form.Email, "\n", "", -1)
+ e := &models.EmailAddress{
+ Uid: ctx.User.Id,
+ Email: cleanEmail,
+ IsActivated: !setting.Service.RegisterEmailConfirm,
+ }
- if err := models.AddEmailAddress(e); err != nil {
- if err == models.ErrEmailAlreadyUsed {
- ctx.RenderWithErr(ctx.Tr("form.email_has_been_used"), SETTINGS_EMAILS, &form)
- return
- }
- ctx.Handle(500, "email.AddEmailAddress", err)
+ if err := models.AddEmailAddress(e); err != nil {
+ if err == models.ErrEmailAlreadyUsed {
+ ctx.RenderWithErr(ctx.Tr("form.email_been_used"), SETTINGS_EMAILS, &form)
return
- } else {
-
- // Send confirmation e-mail
- if setting.Service.RegisterEmailConfirm {
- mailer.SendActivateEmail(ctx.Render, ctx.User, e)
+ }
+ ctx.Handle(500, "AddEmailAddress", err)
+ return
+ } else {
+ // Send confirmation e-mail
+ if setting.Service.RegisterEmailConfirm {
+ mailer.SendActivateEmail(ctx.Render, ctx.User, e)
- if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil {
- log.Error(4, "Set cache(MailResendLimit) fail: %v", err)
- }
- ctx.Flash.Success(ctx.Tr("settings.add_email_success_confirmation_email_sent"))
- } else {
- ctx.Flash.Success(ctx.Tr("settings.add_email_success"))
+ if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil {
+ log.Error(4, "Set cache(MailResendLimit) fail: %v", err)
}
-
- log.Trace("Email address added: %s", e.Email)
-
- ctx.Redirect(setting.AppSubUrl + "/user/settings/email")
- return
+ ctx.Flash.Success(ctx.Tr("settings.add_email_success_confirmation_email_sent"))
+ } else {
+ ctx.Flash.Success(ctx.Tr("settings.add_email_success"))
}
+ log.Trace("Email address added: %s", e.Email)
+ ctx.Redirect(setting.AppSubUrl + "/user/settings/email")
+ return
}
ctx.HTML(200, SETTINGS_EMAILS)