summaryrefslogtreecommitdiffstats
path: root/routers
diff options
context:
space:
mode:
authorUnknwon <u@gogs.io>2015-02-22 22:51:25 -0500
committerUnknwon <u@gogs.io>2015-02-22 22:51:25 -0500
commit7ccab9cd09eca8fa60fdd519c97c259d4b521abd (patch)
tree914ea87da32fd27d889b2145f559c0fdad8c62af /routers
parent25f5a8d7986a710cef01d02725071d2de0a6b143 (diff)
parent2369881808a906f3072c935ab0575f08bd358821 (diff)
downloadgitea-7ccab9cd09eca8fa60fdd519c97c259d4b521abd.tar.gz
gitea-7ccab9cd09eca8fa60fdd519c97c259d4b521abd.zip
Merge branch 'dev' of github.com:gogits/gogs into access
Conflicts: gogs.go models/models.go models/user.go templates/.VERSION templates/org/home.tmpl
Diffstat (limited to 'routers')
-rw-r--r--routers/api/v1/repo.go75
-rw-r--r--routers/install.go6
-rw-r--r--routers/org/setting.go15
-rw-r--r--routers/repo/repo.go28
-rw-r--r--routers/repo/setting.go4
-rw-r--r--routers/user/auth.go5
-rw-r--r--routers/user/setting.go91
7 files changed, 118 insertions, 106 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/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/repo/repo.go b/routers/repo/repo.go
index 48f7b09bc0..dfd827bbb9 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)
diff --git a/routers/repo/setting.go b/routers/repo/setting.go
index a6f50d3068..0f8acfa5ba 100644
--- a/routers/repo/setting.go
+++ b/routers/repo/setting.go
@@ -8,6 +8,7 @@ import (
"encoding/json"
"errors"
"fmt"
+ "path"
"strings"
"time"
@@ -83,7 +84,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 +109,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)