From 7140dbac95a96849a7f82166439fd72a7b56f9bf Mon Sep 17 00:00:00 2001
From: Unknwon <u@gogs.io>
Date: Sat, 20 Feb 2016 15:10:34 -0500
Subject: Fix #857

---
 routers/repo/setting.go | 90 ++++++++++++++++++++++++++-----------------------
 1 file changed, 48 insertions(+), 42 deletions(-)

(limited to 'routers/repo/setting.go')

diff --git a/routers/repo/setting.go b/routers/repo/setting.go
index ac3f534b..5f268f3b 100644
--- a/routers/repo/setting.go
+++ b/routers/repo/setting.go
@@ -233,48 +233,6 @@ func Collaboration(ctx *middleware.Context) {
 	ctx.Data["Title"] = ctx.Tr("repo.settings")
 	ctx.Data["PageIsSettingsCollaboration"] = true
 
-	if ctx.Req.Method == "POST" {
-		name := strings.ToLower(ctx.Query("collaborator"))
-		if len(name) == 0 || ctx.Repo.Owner.LowerName == name {
-			ctx.Redirect(setting.AppSubUrl + ctx.Req.URL.Path)
-			return
-		}
-
-		u, err := models.GetUserByName(name)
-		if err != nil {
-			if models.IsErrUserNotExist(err) {
-				ctx.Flash.Error(ctx.Tr("form.user_not_exist"))
-				ctx.Redirect(setting.AppSubUrl + ctx.Req.URL.Path)
-			} else {
-				ctx.Handle(500, "GetUserByName", err)
-			}
-			return
-		}
-
-		// Check if user is organization member.
-		if ctx.Repo.Owner.IsOrganization() && ctx.Repo.Owner.IsOrgMember(u.Id) {
-			ctx.Flash.Info(ctx.Tr("repo.settings.user_is_org_member"))
-			ctx.Redirect(ctx.Repo.RepoLink + "/settings/collaboration")
-			return
-		}
-
-		if err = ctx.Repo.Repository.AddCollaborator(u); err != nil {
-			ctx.Handle(500, "AddCollaborator", err)
-			return
-		}
-
-		if setting.Service.EnableNotifyMail {
-			if err = mailer.SendCollaboratorMail(ctx.Render, u, ctx.User, ctx.Repo.Repository); err != nil {
-				ctx.Handle(500, "SendCollaboratorMail", err)
-				return
-			}
-		}
-
-		ctx.Flash.Success(ctx.Tr("repo.settings.add_collaborator_success"))
-		ctx.Redirect(setting.AppSubUrl + ctx.Req.URL.Path)
-		return
-	}
-
 	// Delete collaborator.
 	remove := strings.ToLower(ctx.Query("remove"))
 	if len(remove) > 0 && remove != ctx.Repo.Owner.LowerName {
@@ -302,6 +260,54 @@ func Collaboration(ctx *middleware.Context) {
 	ctx.HTML(200, COLLABORATION)
 }
 
+func CollaborationPost(ctx *middleware.Context) {
+	name := strings.ToLower(ctx.Query("collaborator"))
+	if len(name) == 0 || ctx.Repo.Owner.LowerName == name {
+		ctx.Redirect(setting.AppSubUrl + ctx.Req.URL.Path)
+		return
+	}
+
+	u, err := models.GetUserByName(name)
+	if err != nil {
+		if models.IsErrUserNotExist(err) {
+			ctx.Flash.Error(ctx.Tr("form.user_not_exist"))
+			ctx.Redirect(setting.AppSubUrl + ctx.Req.URL.Path)
+		} else {
+			ctx.Handle(500, "GetUserByName", err)
+		}
+		return
+	}
+
+	// Organization is not allowed to be added as a collaborator.
+	if u.IsOrganization() {
+		ctx.Flash.Error(ctx.Tr("repo.settings.org_not_allowed_to_be_collaborator"))
+		ctx.Redirect(setting.AppSubUrl + ctx.Req.URL.Path)
+		return
+	}
+
+	// Check if user is organization member.
+	if ctx.Repo.Owner.IsOrganization() && ctx.Repo.Owner.IsOrgMember(u.Id) {
+		ctx.Flash.Info(ctx.Tr("repo.settings.user_is_org_member"))
+		ctx.Redirect(ctx.Repo.RepoLink + "/settings/collaboration")
+		return
+	}
+
+	if err = ctx.Repo.Repository.AddCollaborator(u); err != nil {
+		ctx.Handle(500, "AddCollaborator", err)
+		return
+	}
+
+	if setting.Service.EnableNotifyMail {
+		if err = mailer.SendCollaboratorMail(ctx.Render, u, ctx.User, ctx.Repo.Repository); err != nil {
+			ctx.Handle(500, "SendCollaboratorMail", err)
+			return
+		}
+	}
+
+	ctx.Flash.Success(ctx.Tr("repo.settings.add_collaborator_success"))
+	ctx.Redirect(setting.AppSubUrl + ctx.Req.URL.Path)
+}
+
 func parseOwnerAndRepo(ctx *middleware.Context) (*models.User, *models.Repository) {
 	owner, err := models.GetUserByName(ctx.Params(":username"))
 	if err != nil {
-- 
cgit v1.2.3