From 69e2ab161109eef667cd22a96711e4a8ee9d01d2 Mon Sep 17 00:00:00 2001
From: Lauris BH <lauris@nix.lv>
Date: Thu, 5 Jul 2018 02:51:02 +0300
Subject: [PATCH] Allow administrator to create repository for any organization
 (#4368)

---
 integrations/api_repo_test.go | 23 +++++++++++++++++++++++
 routers/api/v1/repo/repo.go   | 16 +++++++++-------
 2 files changed, 32 insertions(+), 7 deletions(-)

diff --git a/integrations/api_repo_test.go b/integrations/api_repo_test.go
index c789cc9ee4..aec8c8f81b 100644
--- a/integrations/api_repo_test.go
+++ b/integrations/api_repo_test.go
@@ -262,3 +262,26 @@ func TestAPIRepoMigrate(t *testing.T) {
 		session.MakeRequest(t, req, testCase.expectedStatus)
 	}
 }
+
+func TestAPIOrgRepoCreate(t *testing.T) {
+	testCases := []struct {
+		ctxUserID         int64
+		orgName, repoName string
+		expectedStatus    int
+	}{
+		{ctxUserID: 1, orgName: "user3", repoName: "repo-admin", expectedStatus: http.StatusCreated},
+		{ctxUserID: 2, orgName: "user3", repoName: "repo-own", expectedStatus: http.StatusCreated},
+		{ctxUserID: 2, orgName: "user6", repoName: "repo-bad-org", expectedStatus: http.StatusForbidden},
+	}
+
+	prepareTestEnv(t)
+	for _, testCase := range testCases {
+		user := models.AssertExistsAndLoadBean(t, &models.User{ID: testCase.ctxUserID}).(*models.User)
+		session := loginUser(t, user.Name)
+
+		req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/org/%s/repos", testCase.orgName), &api.CreateRepoOption{
+			Name: testCase.repoName,
+		})
+		session.MakeRequest(t, req, testCase.expectedStatus)
+	}
+}
diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go
index c6c5d4aec3..044b1e9c18 100644
--- a/routers/api/v1/repo/repo.go
+++ b/routers/api/v1/repo/repo.go
@@ -257,13 +257,15 @@ func CreateOrgRepo(ctx *context.APIContext, opt api.CreateRepoOption) {
 		return
 	}
 
-	isOwner, err := org.IsOwnedBy(ctx.User.ID)
-	if err != nil {
-		ctx.ServerError("IsOwnedBy", err)
-		return
-	} else if !isOwner {
-		ctx.Error(403, "", "Given user is not owner of organization.")
-		return
+	if !ctx.User.IsAdmin {
+		isOwner, err := org.IsOwnedBy(ctx.User.ID)
+		if err != nil {
+			ctx.ServerError("IsOwnedBy", err)
+			return
+		} else if !isOwner {
+			ctx.Error(403, "", "Given user is not owner of organization.")
+			return
+		}
 	}
 	CreateUserRepo(ctx, org, opt)
 }