From 529482135c8e9304dd7cdf08772eaba61d903894 Mon Sep 17 00:00:00 2001
From: Lunny Xiao <xiaolunwen@gmail.com>
Date: Wed, 20 Dec 2017 06:59:56 -0600
Subject: [PATCH] Support default private when creating or migrating repository
 (#3239)

* support default private when creating or migrating repository

* fix fmt

* use string constants on repository default private in app.ini

* fix fmt
---
 custom/conf/app.ini.sample                      |  2 ++
 .../doc/advanced/config-cheat-sheet.en-us.md    |  1 +
 .../doc/advanced/config-cheat-sheet.zh-cn.md    |  1 +
 modules/setting/setting.go                      |  9 +++++++++
 routers/repo/repo.go                            | 17 +++++++++++++++--
 5 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/custom/conf/app.ini.sample b/custom/conf/app.ini.sample
index 6a8ae48a9..abe004217 100644
--- a/custom/conf/app.ini.sample
+++ b/custom/conf/app.ini.sample
@@ -16,6 +16,8 @@ SCRIPT_TYPE = bash
 ANSI_CHARSET =
 ; Force every new repository to be private
 FORCE_PRIVATE = false
+; Default private when create a new repository, could be: last, private, public. Default is last which means last user repo visiblity.
+DEFAULT_PRIVATE = last
 ; Global maximum creation limit of repository per user, -1 means no limit
 MAX_CREATION_LIMIT = -1
 ; Mirror sync queue length, increase if mirror syncing starts hanging
diff --git a/docs/content/doc/advanced/config-cheat-sheet.en-us.md b/docs/content/doc/advanced/config-cheat-sheet.en-us.md
index ea01b07d1..b5517080d 100644
--- a/docs/content/doc/advanced/config-cheat-sheet.en-us.md
+++ b/docs/content/doc/advanced/config-cheat-sheet.en-us.md
@@ -39,6 +39,7 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`.
 - `SCRIPT_TYPE`: The script type your server supports, usually this is `bash`, but some customers report that they only have `sh`.
 - `ANSI_CHARSET`: The default charset for an unrecognized charset.
 - `FORCE_PRIVATE`: Force every new repository to be private.
+- `DEFAULT_PRIVATE`: Default private when create a new repository, could be: `last`, `private` and `public`. Default is last which means last user repo visiblity.
 - `MAX_CREATION_LIMIT`: Global maximum creation limit of repositories per user, `-1` means no limit.
 - `PULL_REQUEST_QUEUE_LENGTH`:exclamation:: Length of pull request patch test queue, make it as large as possible.
 - `MIRROR_QUEUE_LENGTH`: Patch test queue length, increase if pull request patch testing starts hanging. Defaults to 1000.
diff --git a/docs/content/doc/advanced/config-cheat-sheet.zh-cn.md b/docs/content/doc/advanced/config-cheat-sheet.zh-cn.md
index 7ec650ddb..9a11d960c 100644
--- a/docs/content/doc/advanced/config-cheat-sheet.zh-cn.md
+++ b/docs/content/doc/advanced/config-cheat-sheet.zh-cn.md
@@ -29,6 +29,7 @@ menu:
 - `SCRIPT_TYPE`: 服务器支持的Shell类型,通常是 `bash`,但有些服务器也有可能是 `sh`。
 - `ANSI_CHARSET`: 默认字符编码。
 - `FORCE_PRIVATE`: 强制所有git工程必须私有。
+- `DEFAULT_PRIVATE`: 默认创建的git工程为私有。 可以是`last`, `private` 或 `public`。默认值是 `last`表示用户最后创建的Repo的选择。
 - `MAX_CREATION_LIMIT`: 全局最大每个用户创建的git工程数目, `-1` 表示没限制。
 - `PULL_REQUEST_QUEUE_LENGTH`: 小心:合并请求测试队列的长度,尽量放大。
 
diff --git a/modules/setting/setting.go b/modules/setting/setting.go
index 7d86ef305..587666227 100644
--- a/modules/setting/setting.go
+++ b/modules/setting/setting.go
@@ -70,6 +70,13 @@ type MarkupParser struct {
 	IsInputFile    bool
 }
 
+// enumerates all the policy repository creating
+const (
+	RepoCreatingLastUserVisibility = "last"
+	RepoCreatingPrivate            = "private"
+	RepoCreatingPublic             = "public"
+)
+
 // settings
 var (
 	// AppVer settings
@@ -180,6 +187,7 @@ var (
 	Repository = struct {
 		AnsiCharset            string
 		ForcePrivate           bool
+		DefaultPrivate         string
 		MaxCreationLimit       int
 		MirrorQueueLength      int
 		PullRequestQueueLength int
@@ -209,6 +217,7 @@ var (
 	}{
 		AnsiCharset:            "",
 		ForcePrivate:           false,
+		DefaultPrivate:         RepoCreatingLastUserVisibility,
 		MaxCreationLimit:       -1,
 		MirrorQueueLength:      1000,
 		PullRequestQueueLength: 1000,
diff --git a/routers/repo/repo.go b/routers/repo/repo.go
index 36105bfe1..aedc4e547 100644
--- a/routers/repo/repo.go
+++ b/routers/repo/repo.go
@@ -81,6 +81,19 @@ func checkContextUser(ctx *context.Context, uid int64) *models.User {
 	return org
 }
 
+func getRepoPrivate(ctx *context.Context) bool {
+	switch strings.ToLower(setting.Repository.DefaultPrivate) {
+	case setting.RepoCreatingLastUserVisibility:
+		return ctx.User.LastRepoVisibility
+	case setting.RepoCreatingPrivate:
+		return true
+	case setting.RepoCreatingPublic:
+		return false
+	default:
+		return ctx.User.LastRepoVisibility
+	}
+}
+
 // Create render creating repository page
 func Create(ctx *context.Context) {
 	if !ctx.User.CanCreateRepo() {
@@ -94,7 +107,7 @@ func Create(ctx *context.Context) {
 	ctx.Data["Licenses"] = models.Licenses
 	ctx.Data["Readmes"] = models.Readmes
 	ctx.Data["readme"] = "Default"
-	ctx.Data["private"] = ctx.User.LastRepoVisibility
+	ctx.Data["private"] = getRepoPrivate(ctx)
 	ctx.Data["IsForcedPrivate"] = setting.Repository.ForcePrivate
 
 	ctxUser := checkContextUser(ctx, ctx.QueryInt64("org"))
@@ -170,7 +183,7 @@ func CreatePost(ctx *context.Context, form auth.CreateRepoForm) {
 // Migrate render migration of repository page
 func Migrate(ctx *context.Context) {
 	ctx.Data["Title"] = ctx.Tr("new_migrate")
-	ctx.Data["private"] = ctx.User.LastRepoVisibility
+	ctx.Data["private"] = getRepoPrivate(ctx)
 	ctx.Data["IsForcedPrivate"] = setting.Repository.ForcePrivate
 	ctx.Data["mirror"] = ctx.Query("mirror") == "1"
 	ctx.Data["LFSActive"] = setting.LFS.StartServer