You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

366 lines
12 KiB

10 years ago
10 years ago
10 years ago
10 years ago
Git LFS support v2 (#122) * Import github.com/git-lfs/lfs-test-server as lfs module base Imported commit is 3968aac269a77b73924649b9412ae03f7ccd3198 Removed: Dockerfile CONTRIBUTING.md mgmt* script/ vendor/ kvlogger.go .dockerignore .gitignore README.md * Remove config, add JWT support from github.com/mgit-at/lfs-test-server Imported commit f0cdcc5a01599c5a955dc1bbf683bb4acecdba83 * Add LFS settings * Add LFS meta object model * Add LFS routes and initialization * Import github.com/dgrijalva/jwt-go into vendor/ * Adapt LFS module: handlers, routing, meta store * Move LFS routes to /user/repo/info/lfs/* * Add request header checks to LFS BatchHandler / PostHandler * Implement LFS basic authentication * Rework JWT secret generation / load * Implement LFS SSH token authentication with JWT Specification: https://github.com/github/git-lfs/tree/master/docs/api * Integrate LFS settings into install process * Remove LFS objects when repository is deleted Only removes objects from content store when deleted repo is the only referencing repository * Make LFS module stateless Fixes bug where LFS would not work after installation without restarting Gitea * Change 500 'Internal Server Error' to 400 'Bad Request' * Change sql query to xorm call * Remove unneeded type from LFS module * Change internal imports to code.gitea.io/gitea/ * Add Gitea authors copyright * Change basic auth realm to "gitea-lfs" * Add unique indexes to LFS model * Use xorm count function in LFS check on repository delete * Return io.ReadCloser from content store and close after usage * Add LFS info to runWeb() * Export LFS content store base path * LFS file download from UI * Work around git-lfs client issue with unauthenticated requests Returning a dummy Authorization header for unauthenticated requests lets git-lfs client skip asking for auth credentials See: https://github.com/github/git-lfs/issues/1088 * Fix unauthenticated UI downloads from public repositories * Authentication check order, Finish LFS file view logic * Ignore LFS hooks if installed for current OS user Fixes Gitea UI actions for repositories tracking LFS files. Checks for minimum needed git version by parsing the semantic version string. * Hide LFS metafile diff from commit view, marking as binary * Show LFS notice if file in commit view is tracked * Add notbefore/nbf JWT claim * Correct lint suggestions - comments for structs and functions - Add comments to LFS model - Function comment for GetRandomBytesAsBase64 - LFS server function comments and lint variable suggestion * Move secret generation code out of conditional Ensures no LFS code may run with an empty secret * Do not hand out JWT tokens if LFS server support is disabled
8 years ago
10 years ago
Add support for federated avatars (#3320) * Add support for federated avatars Fixes #3105 Removes avatar fetching duplication code Adds an "Enable Federated Avatar" checkbox in user settings (defaults to unchecked) Moves avatar settings all in the same form, making local and remote avatars mutually exclusive Renames UploadAvatarForm to AvatarForm as it's not anymore only for uploading * Run gofmt on all modified files * Move Avatar form in its own page * Add go-libravatar dependency to vendor/ dir Hopefully helps with accepting the contribution. See also #3214 * Revert "Add go-libravatar dependency to vendor/ dir" This reverts commit a8cb93ae640bbb90f7d25012fc257bda9fae9b82. * Make federated avatar setting a global configuration Removes the per-user setting * Move avatar handling back to base tool, disable federated avatar in offline mode * Format, handle error * Properly set fallback host * Use unsupported github.com mirror for importing go-libravatar * Remove comment showing life exists outside of github.com ... pity, but contribution would not be accepted otherwise * Use Combo for Get and Post methods over /avatar * FEDERATED_AVATAR -> ENABLE_FEDERATED_AVATAR * Fix persistance of federated avatar lookup checkbox at install time * Federated Avatars -> Enable Federated Avatars * Use len(string) == 0 instead of string == "" * Move import line where it belong See https://github.com/Unknwon/go-code-convention/blob/master/en-US/import_packages.md Pity the import url is still the unofficial one, but oh well... * Save a line (and waste much more expensive time) * Remove redundant parens * Remove an empty line * Remove empty lines * Reorder lines to make diff smaller * Remove another newline Unknwon review got me start a fight against newlines * Move DISABLE_GRAVATAR and ENABLE_FEDERATED_AVATAR after OFFLINE_MODE On re-reading the diff I figured what Unknwon meant here: https://github.com/gogits/gogs/pull/3320/files#r73741106 * Remove newlines that weren't there before my intervention
8 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
Git LFS support v2 (#122) * Import github.com/git-lfs/lfs-test-server as lfs module base Imported commit is 3968aac269a77b73924649b9412ae03f7ccd3198 Removed: Dockerfile CONTRIBUTING.md mgmt* script/ vendor/ kvlogger.go .dockerignore .gitignore README.md * Remove config, add JWT support from github.com/mgit-at/lfs-test-server Imported commit f0cdcc5a01599c5a955dc1bbf683bb4acecdba83 * Add LFS settings * Add LFS meta object model * Add LFS routes and initialization * Import github.com/dgrijalva/jwt-go into vendor/ * Adapt LFS module: handlers, routing, meta store * Move LFS routes to /user/repo/info/lfs/* * Add request header checks to LFS BatchHandler / PostHandler * Implement LFS basic authentication * Rework JWT secret generation / load * Implement LFS SSH token authentication with JWT Specification: https://github.com/github/git-lfs/tree/master/docs/api * Integrate LFS settings into install process * Remove LFS objects when repository is deleted Only removes objects from content store when deleted repo is the only referencing repository * Make LFS module stateless Fixes bug where LFS would not work after installation without restarting Gitea * Change 500 'Internal Server Error' to 400 'Bad Request' * Change sql query to xorm call * Remove unneeded type from LFS module * Change internal imports to code.gitea.io/gitea/ * Add Gitea authors copyright * Change basic auth realm to "gitea-lfs" * Add unique indexes to LFS model * Use xorm count function in LFS check on repository delete * Return io.ReadCloser from content store and close after usage * Add LFS info to runWeb() * Export LFS content store base path * LFS file download from UI * Work around git-lfs client issue with unauthenticated requests Returning a dummy Authorization header for unauthenticated requests lets git-lfs client skip asking for auth credentials See: https://github.com/github/git-lfs/issues/1088 * Fix unauthenticated UI downloads from public repositories * Authentication check order, Finish LFS file view logic * Ignore LFS hooks if installed for current OS user Fixes Gitea UI actions for repositories tracking LFS files. Checks for minimum needed git version by parsing the semantic version string. * Hide LFS metafile diff from commit view, marking as binary * Show LFS notice if file in commit view is tracked * Add notbefore/nbf JWT claim * Correct lint suggestions - comments for structs and functions - Add comments to LFS model - Function comment for GetRandomBytesAsBase64 - LFS server function comments and lint variable suggestion * Move secret generation code out of conditional Ensures no LFS code may run with an empty secret * Do not hand out JWT tokens if LFS server support is disabled
8 years ago
10 years ago
9 years ago
10 years ago
10 years ago
Git LFS support v2 (#122) * Import github.com/git-lfs/lfs-test-server as lfs module base Imported commit is 3968aac269a77b73924649b9412ae03f7ccd3198 Removed: Dockerfile CONTRIBUTING.md mgmt* script/ vendor/ kvlogger.go .dockerignore .gitignore README.md * Remove config, add JWT support from github.com/mgit-at/lfs-test-server Imported commit f0cdcc5a01599c5a955dc1bbf683bb4acecdba83 * Add LFS settings * Add LFS meta object model * Add LFS routes and initialization * Import github.com/dgrijalva/jwt-go into vendor/ * Adapt LFS module: handlers, routing, meta store * Move LFS routes to /user/repo/info/lfs/* * Add request header checks to LFS BatchHandler / PostHandler * Implement LFS basic authentication * Rework JWT secret generation / load * Implement LFS SSH token authentication with JWT Specification: https://github.com/github/git-lfs/tree/master/docs/api * Integrate LFS settings into install process * Remove LFS objects when repository is deleted Only removes objects from content store when deleted repo is the only referencing repository * Make LFS module stateless Fixes bug where LFS would not work after installation without restarting Gitea * Change 500 'Internal Server Error' to 400 'Bad Request' * Change sql query to xorm call * Remove unneeded type from LFS module * Change internal imports to code.gitea.io/gitea/ * Add Gitea authors copyright * Change basic auth realm to "gitea-lfs" * Add unique indexes to LFS model * Use xorm count function in LFS check on repository delete * Return io.ReadCloser from content store and close after usage * Add LFS info to runWeb() * Export LFS content store base path * LFS file download from UI * Work around git-lfs client issue with unauthenticated requests Returning a dummy Authorization header for unauthenticated requests lets git-lfs client skip asking for auth credentials See: https://github.com/github/git-lfs/issues/1088 * Fix unauthenticated UI downloads from public repositories * Authentication check order, Finish LFS file view logic * Ignore LFS hooks if installed for current OS user Fixes Gitea UI actions for repositories tracking LFS files. Checks for minimum needed git version by parsing the semantic version string. * Hide LFS metafile diff from commit view, marking as binary * Show LFS notice if file in commit view is tracked * Add notbefore/nbf JWT claim * Correct lint suggestions - comments for structs and functions - Add comments to LFS model - Function comment for GetRandomBytesAsBase64 - LFS server function comments and lint variable suggestion * Move secret generation code out of conditional Ensures no LFS code may run with an empty secret * Do not hand out JWT tokens if LFS server support is disabled
8 years ago
Add support for federated avatars (#3320) * Add support for federated avatars Fixes #3105 Removes avatar fetching duplication code Adds an "Enable Federated Avatar" checkbox in user settings (defaults to unchecked) Moves avatar settings all in the same form, making local and remote avatars mutually exclusive Renames UploadAvatarForm to AvatarForm as it's not anymore only for uploading * Run gofmt on all modified files * Move Avatar form in its own page * Add go-libravatar dependency to vendor/ dir Hopefully helps with accepting the contribution. See also #3214 * Revert "Add go-libravatar dependency to vendor/ dir" This reverts commit a8cb93ae640bbb90f7d25012fc257bda9fae9b82. * Make federated avatar setting a global configuration Removes the per-user setting * Move avatar handling back to base tool, disable federated avatar in offline mode * Format, handle error * Properly set fallback host * Use unsupported github.com mirror for importing go-libravatar * Remove comment showing life exists outside of github.com ... pity, but contribution would not be accepted otherwise * Use Combo for Get and Post methods over /avatar * FEDERATED_AVATAR -> ENABLE_FEDERATED_AVATAR * Fix persistance of federated avatar lookup checkbox at install time * Federated Avatars -> Enable Federated Avatars * Use len(string) == 0 instead of string == "" * Move import line where it belong See https://github.com/Unknwon/go-code-convention/blob/master/en-US/import_packages.md Pity the import url is still the unofficial one, but oh well... * Save a line (and waste much more expensive time) * Remove redundant parens * Remove an empty line * Remove empty lines * Reorder lines to make diff smaller * Remove another newline Unknwon review got me start a fight against newlines * Move DISABLE_GRAVATAR and ENABLE_FEDERATED_AVATAR after OFFLINE_MODE On re-reading the diff I figured what Unknwon meant here: https://github.com/gogits/gogs/pull/3320/files#r73741106 * Remove newlines that weren't there before my intervention
8 years ago
10 years ago
10 years ago
10 years ago
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package routers
  5. import (
  6. "errors"
  7. "os"
  8. "os/exec"
  9. "path"
  10. "path/filepath"
  11. "strings"
  12. "github.com/Unknwon/com"
  13. "github.com/go-xorm/xorm"
  14. "gopkg.in/ini.v1"
  15. "code.gitea.io/gitea/models"
  16. "code.gitea.io/gitea/modules/auth"
  17. "code.gitea.io/gitea/modules/base"
  18. "code.gitea.io/gitea/modules/context"
  19. "code.gitea.io/gitea/modules/log"
  20. "code.gitea.io/gitea/modules/setting"
  21. "code.gitea.io/gitea/modules/user"
  22. )
  23. const (
  24. // tplInstall template for installation page
  25. tplInstall base.TplName = "install"
  26. )
  27. // InstallInit prepare for rendering installation page
  28. func InstallInit(ctx *context.Context) {
  29. if setting.InstallLock {
  30. ctx.Handle(404, "Install", errors.New("Installation is prohibited"))
  31. return
  32. }
  33. ctx.Data["Title"] = ctx.Tr("install.install")
  34. ctx.Data["PageIsInstall"] = true
  35. dbOpts := []string{"MySQL", "PostgreSQL", "MSSQL"}
  36. if models.EnableSQLite3 {
  37. dbOpts = append(dbOpts, "SQLite3")
  38. }
  39. if models.EnableTiDB {
  40. dbOpts = append(dbOpts, "TiDB")
  41. }
  42. ctx.Data["DbOptions"] = dbOpts
  43. }
  44. // Install render installation page
  45. func Install(ctx *context.Context) {
  46. form := auth.InstallForm{}
  47. // Database settings
  48. form.DbHost = models.DbCfg.Host
  49. form.DbUser = models.DbCfg.User
  50. form.DbName = models.DbCfg.Name
  51. form.DbPath = models.DbCfg.Path
  52. ctx.Data["CurDbOption"] = "MySQL"
  53. switch models.DbCfg.Type {
  54. case "postgres":
  55. ctx.Data["CurDbOption"] = "PostgreSQL"
  56. case "mssql":
  57. ctx.Data["CurDbOption"] = "MSSQL"
  58. case "sqlite3":
  59. if models.EnableSQLite3 {
  60. ctx.Data["CurDbOption"] = "SQLite3"
  61. }
  62. case "tidb":
  63. if models.EnableTiDB {
  64. ctx.Data["CurDbOption"] = "TiDB"
  65. }
  66. }
  67. // Application general settings
  68. form.AppName = setting.AppName
  69. form.RepoRootPath = setting.RepoRootPath
  70. form.LFSRootPath = setting.LFS.ContentPath
  71. // Note(unknown): it's hard for Windows users change a running user,
  72. // so just use current one if config says default.
  73. if setting.IsWindows && setting.RunUser == "git" {
  74. form.RunUser = user.CurrentUsername()
  75. } else {
  76. form.RunUser = setting.RunUser
  77. }
  78. form.Domain = setting.Domain
  79. form.SSHPort = setting.SSH.Port
  80. form.HTTPPort = setting.HTTPPort
  81. form.AppURL = setting.AppURL
  82. form.LogRootPath = setting.LogRootPath
  83. // E-mail service settings
  84. if setting.MailService != nil {
  85. form.SMTPHost = setting.MailService.Host
  86. form.SMTPFrom = setting.MailService.From
  87. form.SMTPUser = setting.MailService.User
  88. }
  89. form.RegisterConfirm = setting.Service.RegisterEmailConfirm
  90. form.MailNotify = setting.Service.EnableNotifyMail
  91. // Server and other services settings
  92. form.OfflineMode = setting.OfflineMode
  93. form.DisableGravatar = setting.DisableGravatar
  94. form.EnableFederatedAvatar = setting.EnableFederatedAvatar
  95. form.DisableRegistration = setting.Service.DisableRegistration
  96. form.EnableCaptcha = setting.Service.EnableCaptcha
  97. form.RequireSignInView = setting.Service.RequireSignInView
  98. form.DefaultKeepEmailPrivate = setting.Service.DefaultKeepEmailPrivate
  99. form.DefaultAllowCreateOrganization = setting.Service.DefaultAllowCreateOrganization
  100. form.NoReplyAddress = setting.Service.NoReplyAddress
  101. auth.AssignForm(form, ctx.Data)
  102. ctx.HTML(200, tplInstall)
  103. }
  104. // InstallPost response for submit install items
  105. func InstallPost(ctx *context.Context, form auth.InstallForm) {
  106. var err error
  107. ctx.Data["CurDbOption"] = form.DbType
  108. if ctx.HasError() {
  109. if ctx.HasValue("Err_SMTPUser") {
  110. ctx.Data["Err_SMTP"] = true
  111. }
  112. if ctx.HasValue("Err_AdminName") ||
  113. ctx.HasValue("Err_AdminPasswd") ||
  114. ctx.HasValue("Err_AdminEmail") {
  115. ctx.Data["Err_Admin"] = true
  116. }
  117. ctx.HTML(200, tplInstall)
  118. return
  119. }
  120. if _, err = exec.LookPath("git"); err != nil {
  121. ctx.RenderWithErr(ctx.Tr("install.test_git_failed", err), tplInstall, &form)
  122. return
  123. }
  124. // Pass basic check, now test configuration.
  125. // Test database setting.
  126. dbTypes := map[string]string{"MySQL": "mysql", "PostgreSQL": "postgres", "MSSQL": "mssql", "SQLite3": "sqlite3", "TiDB": "tidb"}
  127. models.DbCfg.Type = dbTypes[form.DbType]
  128. models.DbCfg.Host = form.DbHost
  129. models.DbCfg.User = form.DbUser
  130. models.DbCfg.Passwd = form.DbPasswd
  131. models.DbCfg.Name = form.DbName
  132. models.DbCfg.SSLMode = form.SSLMode
  133. models.DbCfg.Path = form.DbPath
  134. if (models.DbCfg.Type == "sqlite3" || models.DbCfg.Type == "tidb") &&
  135. len(models.DbCfg.Path) == 0 {
  136. ctx.Data["Err_DbPath"] = true
  137. ctx.RenderWithErr(ctx.Tr("install.err_empty_db_path"), tplInstall, &form)
  138. return
  139. } else if models.DbCfg.Type == "tidb" &&
  140. strings.ContainsAny(path.Base(models.DbCfg.Path), ".-") {
  141. ctx.Data["Err_DbPath"] = true
  142. ctx.RenderWithErr(ctx.Tr("install.err_invalid_tidb_name"), tplInstall, &form)
  143. return
  144. }
  145. // Set test engine.
  146. var x *xorm.Engine
  147. if err = models.NewTestEngine(x); err != nil {
  148. if strings.Contains(err.Error(), `Unknown database type: sqlite3`) {
  149. ctx.Data["Err_DbType"] = true
  150. ctx.RenderWithErr(ctx.Tr("install.sqlite3_not_available", "https://docs.gitea.io/installation/install_from_binary.html"), tplInstall, &form)
  151. } else {
  152. ctx.Data["Err_DbSetting"] = true
  153. ctx.RenderWithErr(ctx.Tr("install.invalid_db_setting", err), tplInstall, &form)
  154. }
  155. return
  156. }
  157. // Test repository root path.
  158. form.RepoRootPath = strings.Replace(form.RepoRootPath, "\\", "/", -1)
  159. if err = os.MkdirAll(form.RepoRootPath, os.ModePerm); err != nil {
  160. ctx.Data["Err_RepoRootPath"] = true
  161. ctx.RenderWithErr(ctx.Tr("install.invalid_repo_path", err), tplInstall, &form)
  162. return
  163. }
  164. // Test LFS root path if not empty, empty meaning disable LFS
  165. if form.LFSRootPath != "" {
  166. form.LFSRootPath = strings.Replace(form.LFSRootPath, "\\", "/", -1)
  167. if err := os.MkdirAll(form.LFSRootPath, os.ModePerm); err != nil {
  168. ctx.Data["Err_LFSRootPath"] = true
  169. ctx.RenderWithErr(ctx.Tr("install.invalid_lfs_path", err), tplInstall, &form)
  170. return
  171. }
  172. }
  173. // Test log root path.
  174. form.LogRootPath = strings.Replace(form.LogRootPath, "\\", "/", -1)
  175. if err = os.MkdirAll(form.LogRootPath, os.ModePerm); err != nil {
  176. ctx.Data["Err_LogRootPath"] = true
  177. ctx.RenderWithErr(ctx.Tr("install.invalid_log_root_path", err), tplInstall, &form)
  178. return
  179. }
  180. currentUser, match := setting.IsRunUserMatchCurrentUser(form.RunUser)
  181. if !match {
  182. ctx.Data["Err_RunUser"] = true
  183. ctx.RenderWithErr(ctx.Tr("install.run_user_not_match", form.RunUser, currentUser), tplInstall, &form)
  184. return
  185. }
  186. // Check logic loophole between disable self-registration and no admin account.
  187. if form.DisableRegistration && len(form.AdminName) == 0 {
  188. ctx.Data["Err_Services"] = true
  189. ctx.Data["Err_Admin"] = true
  190. ctx.RenderWithErr(ctx.Tr("install.no_admin_and_disable_registration"), tplInstall, form)
  191. return
  192. }
  193. // Check admin password.
  194. if len(form.AdminName) > 0 && len(form.AdminPasswd) == 0 {
  195. ctx.Data["Err_Admin"] = true
  196. ctx.Data["Err_AdminPasswd"] = true
  197. ctx.RenderWithErr(ctx.Tr("install.err_empty_admin_password"), tplInstall, form)
  198. return
  199. }
  200. if form.AdminPasswd != form.AdminConfirmPasswd {
  201. ctx.Data["Err_Admin"] = true
  202. ctx.Data["Err_AdminPasswd"] = true
  203. ctx.RenderWithErr(ctx.Tr("form.password_not_match"), tplInstall, form)
  204. return
  205. }
  206. if form.AppURL[len(form.AppURL)-1] != '/' {
  207. form.AppURL += "/"
  208. }
  209. // Save settings.
  210. cfg := ini.Empty()
  211. if com.IsFile(setting.CustomConf) {
  212. // Keeps custom settings if there is already something.
  213. if err = cfg.Append(setting.CustomConf); err != nil {
  214. log.Error(4, "Failed to load custom conf '%s': %v", setting.CustomConf, err)
  215. }
  216. }
  217. cfg.Section("database").Key("DB_TYPE").SetValue(models.DbCfg.Type)
  218. cfg.Section("database").Key("HOST").SetValue(models.DbCfg.Host)
  219. cfg.Section("database").Key("NAME").SetValue(models.DbCfg.Name)
  220. cfg.Section("database").Key("USER").SetValue(models.DbCfg.User)
  221. cfg.Section("database").Key("PASSWD").SetValue(models.DbCfg.Passwd)
  222. cfg.Section("database").Key("SSL_MODE").SetValue(models.DbCfg.SSLMode)
  223. cfg.Section("database").Key("PATH").SetValue(models.DbCfg.Path)
  224. cfg.Section("").Key("APP_NAME").SetValue(form.AppName)
  225. cfg.Section("repository").Key("ROOT").SetValue(form.RepoRootPath)
  226. cfg.Section("").Key("RUN_USER").SetValue(form.RunUser)
  227. cfg.Section("server").Key("SSH_DOMAIN").SetValue(form.Domain)
  228. cfg.Section("server").Key("DOMAIN").SetValue(form.Domain)
  229. cfg.Section("server").Key("HTTP_PORT").SetValue(form.HTTPPort)
  230. cfg.Section("server").Key("ROOT_URL").SetValue(form.AppURL)
  231. if form.SSHPort == 0 {
  232. cfg.Section("server").Key("DISABLE_SSH").SetValue("true")
  233. } else {
  234. cfg.Section("server").Key("DISABLE_SSH").SetValue("false")
  235. cfg.Section("server").Key("SSH_PORT").SetValue(com.ToStr(form.SSHPort))
  236. }
  237. if form.LFSRootPath != "" {
  238. cfg.Section("server").Key("LFS_START_SERVER").SetValue("true")
  239. cfg.Section("server").Key("LFS_CONTENT_PATH").SetValue(form.LFSRootPath)
  240. cfg.Section("server").Key("LFS_JWT_SECRET").SetValue(base.GetRandomBytesAsBase64(32))
  241. } else {
  242. cfg.Section("server").Key("LFS_START_SERVER").SetValue("false")
  243. }
  244. if len(strings.TrimSpace(form.SMTPHost)) > 0 {
  245. cfg.Section("mailer").Key("ENABLED").SetValue("true")
  246. cfg.Section("mailer").Key("HOST").SetValue(form.SMTPHost)
  247. cfg.Section("mailer").Key("FROM").SetValue(form.SMTPFrom)
  248. cfg.Section("mailer").Key("USER").SetValue(form.SMTPUser)
  249. cfg.Section("mailer").Key("PASSWD").SetValue(form.SMTPPasswd)
  250. } else {
  251. cfg.Section("mailer").Key("ENABLED").SetValue("false")
  252. }
  253. cfg.Section("service").Key("REGISTER_EMAIL_CONFIRM").SetValue(com.ToStr(form.RegisterConfirm))
  254. cfg.Section("service").Key("ENABLE_NOTIFY_MAIL").SetValue(com.ToStr(form.MailNotify))
  255. cfg.Section("server").Key("OFFLINE_MODE").SetValue(com.ToStr(form.OfflineMode))
  256. cfg.Section("picture").Key("DISABLE_GRAVATAR").SetValue(com.ToStr(form.DisableGravatar))
  257. cfg.Section("picture").Key("ENABLE_FEDERATED_AVATAR").SetValue(com.ToStr(form.EnableFederatedAvatar))
  258. cfg.Section("service").Key("DISABLE_REGISTRATION").SetValue(com.ToStr(form.DisableRegistration))
  259. cfg.Section("service").Key("ENABLE_CAPTCHA").SetValue(com.ToStr(form.EnableCaptcha))
  260. cfg.Section("service").Key("REQUIRE_SIGNIN_VIEW").SetValue(com.ToStr(form.RequireSignInView))
  261. cfg.Section("service").Key("DEFAULT_KEEP_EMAIL_PRIVATE").SetValue(com.ToStr(form.DefaultKeepEmailPrivate))
  262. cfg.Section("service").Key("DEFAULT_ALLOW_CREATE_ORGANIZATION").SetValue(com.ToStr(form.DefaultAllowCreateOrganization))
  263. cfg.Section("service").Key("NO_REPLY_ADDRESS").SetValue(com.ToStr(form.NoReplyAddress))
  264. cfg.Section("").Key("RUN_MODE").SetValue("prod")
  265. cfg.Section("session").Key("PROVIDER").SetValue("file")
  266. cfg.Section("log").Key("MODE").SetValue("file")
  267. cfg.Section("log").Key("LEVEL").SetValue("Info")
  268. cfg.Section("log").Key("ROOT_PATH").SetValue(form.LogRootPath)
  269. cfg.Section("security").Key("INSTALL_LOCK").SetValue("true")
  270. var secretKey string
  271. if secretKey, err = base.GetRandomString(10); err != nil {
  272. ctx.RenderWithErr(ctx.Tr("install.secret_key_failed", err), tplInstall, &form)
  273. return
  274. }
  275. cfg.Section("security").Key("SECRET_KEY").SetValue(secretKey)
  276. err = os.MkdirAll(filepath.Dir(setting.CustomConf), os.ModePerm)
  277. if err != nil {
  278. ctx.RenderWithErr(ctx.Tr("install.save_config_failed", err), tplInstall, &form)
  279. return
  280. }
  281. if err = cfg.SaveTo(setting.CustomConf); err != nil {
  282. ctx.RenderWithErr(ctx.Tr("install.save_config_failed", err), tplInstall, &form)
  283. return
  284. }
  285. GlobalInit()
  286. // Create admin account
  287. if len(form.AdminName) > 0 {
  288. u := &models.User{
  289. Name: form.AdminName,
  290. Email: form.AdminEmail,
  291. Passwd: form.AdminPasswd,
  292. IsAdmin: true,
  293. IsActive: true,
  294. }
  295. if err = models.CreateUser(u); err != nil {
  296. if !models.IsErrUserAlreadyExist(err) {
  297. setting.InstallLock = false
  298. ctx.Data["Err_AdminName"] = true
  299. ctx.Data["Err_AdminEmail"] = true
  300. ctx.RenderWithErr(ctx.Tr("install.invalid_admin_setting", err), tplInstall, &form)
  301. return
  302. }
  303. log.Info("Admin account already exist")
  304. u, _ = models.GetUserByName(u.Name)
  305. }
  306. // Auto-login for admin
  307. if err = ctx.Session.Set("uid", u.ID); err != nil {
  308. ctx.RenderWithErr(ctx.Tr("install.save_config_failed", err), tplInstall, &form)
  309. return
  310. }
  311. if err = ctx.Session.Set("uname", u.Name); err != nil {
  312. ctx.RenderWithErr(ctx.Tr("install.save_config_failed", err), tplInstall, &form)
  313. return
  314. }
  315. }
  316. log.Info("First-time run install finished!")
  317. ctx.Flash.Success(ctx.Tr("install.install_success"))
  318. ctx.Redirect(form.AppURL + "user/login")
  319. }