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.

332 lines
11 KiB

10 years ago
10 years ago
10 years ago
10 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
10 years ago
10 years ago
9 years ago
10 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
  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"}
  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 "sqlite3":
  57. if models.EnableSQLite3 {
  58. ctx.Data["CurDbOption"] = "SQLite3"
  59. }
  60. case "tidb":
  61. if models.EnableTiDB {
  62. ctx.Data["CurDbOption"] = "TiDB"
  63. }
  64. }
  65. // Application general settings
  66. form.AppName = setting.AppName
  67. form.RepoRootPath = setting.RepoRootPath
  68. // Note(unknwon): it's hard for Windows users change a running user,
  69. // so just use current one if config says default.
  70. if setting.IsWindows && setting.RunUser == "git" {
  71. form.RunUser = user.CurrentUsername()
  72. } else {
  73. form.RunUser = setting.RunUser
  74. }
  75. form.Domain = setting.Domain
  76. form.SSHPort = setting.SSH.Port
  77. form.HTTPPort = setting.HTTPPort
  78. form.AppUrl = setting.AppUrl
  79. form.LogRootPath = setting.LogRootPath
  80. // E-mail service settings
  81. if setting.MailService != nil {
  82. form.SMTPHost = setting.MailService.Host
  83. form.SMTPFrom = setting.MailService.From
  84. form.SMTPEmail = setting.MailService.User
  85. }
  86. form.RegisterConfirm = setting.Service.RegisterEmailConfirm
  87. form.MailNotify = setting.Service.EnableNotifyMail
  88. // Server and other services settings
  89. form.OfflineMode = setting.OfflineMode
  90. form.DisableGravatar = setting.DisableGravatar
  91. form.EnableFederatedAvatar = setting.EnableFederatedAvatar
  92. form.DisableRegistration = setting.Service.DisableRegistration
  93. form.EnableCaptcha = setting.Service.EnableCaptcha
  94. form.RequireSignInView = setting.Service.RequireSignInView
  95. auth.AssignForm(form, ctx.Data)
  96. ctx.HTML(200, tplInstall)
  97. }
  98. // InstallPost response for submit install items
  99. func InstallPost(ctx *context.Context, form auth.InstallForm) {
  100. ctx.Data["CurDbOption"] = form.DbType
  101. if ctx.HasError() {
  102. if ctx.HasValue("Err_SMTPEmail") {
  103. ctx.Data["Err_SMTP"] = true
  104. }
  105. if ctx.HasValue("Err_AdminName") ||
  106. ctx.HasValue("Err_AdminPasswd") ||
  107. ctx.HasValue("Err_AdminEmail") {
  108. ctx.Data["Err_Admin"] = true
  109. }
  110. ctx.HTML(200, tplInstall)
  111. return
  112. }
  113. if _, err := exec.LookPath("git"); err != nil {
  114. ctx.RenderWithErr(ctx.Tr("install.test_git_failed", err), tplInstall, &form)
  115. return
  116. }
  117. // Pass basic check, now test configuration.
  118. // Test database setting.
  119. dbTypes := map[string]string{"MySQL": "mysql", "PostgreSQL": "postgres", "SQLite3": "sqlite3", "TiDB": "tidb"}
  120. models.DbCfg.Type = dbTypes[form.DbType]
  121. models.DbCfg.Host = form.DbHost
  122. models.DbCfg.User = form.DbUser
  123. models.DbCfg.Passwd = form.DbPasswd
  124. models.DbCfg.Name = form.DbName
  125. models.DbCfg.SSLMode = form.SSLMode
  126. models.DbCfg.Path = form.DbPath
  127. if (models.DbCfg.Type == "sqlite3" || models.DbCfg.Type == "tidb") &&
  128. len(models.DbCfg.Path) == 0 {
  129. ctx.Data["Err_DbPath"] = true
  130. ctx.RenderWithErr(ctx.Tr("install.err_empty_db_path"), tplInstall, &form)
  131. return
  132. } else if models.DbCfg.Type == "tidb" &&
  133. strings.ContainsAny(path.Base(models.DbCfg.Path), ".-") {
  134. ctx.Data["Err_DbPath"] = true
  135. ctx.RenderWithErr(ctx.Tr("install.err_invalid_tidb_name"), tplInstall, &form)
  136. return
  137. }
  138. // Set test engine.
  139. var x *xorm.Engine
  140. if err := models.NewTestEngine(x); err != nil {
  141. if strings.Contains(err.Error(), `Unknown database type: sqlite3`) {
  142. ctx.Data["Err_DbType"] = true
  143. ctx.RenderWithErr(ctx.Tr("install.sqlite3_not_available", "https://gogs.io/docs/installation/install_from_binary.html"), tplInstall, &form)
  144. } else {
  145. ctx.Data["Err_DbSetting"] = true
  146. ctx.RenderWithErr(ctx.Tr("install.invalid_db_setting", err), tplInstall, &form)
  147. }
  148. return
  149. }
  150. // Test repository root path.
  151. form.RepoRootPath = strings.Replace(form.RepoRootPath, "\\", "/", -1)
  152. if err := os.MkdirAll(form.RepoRootPath, os.ModePerm); err != nil {
  153. ctx.Data["Err_RepoRootPath"] = true
  154. ctx.RenderWithErr(ctx.Tr("install.invalid_repo_path", err), tplInstall, &form)
  155. return
  156. }
  157. // Test log root path.
  158. form.LogRootPath = strings.Replace(form.LogRootPath, "\\", "/", -1)
  159. if err := os.MkdirAll(form.LogRootPath, os.ModePerm); err != nil {
  160. ctx.Data["Err_LogRootPath"] = true
  161. ctx.RenderWithErr(ctx.Tr("install.invalid_log_root_path", err), tplInstall, &form)
  162. return
  163. }
  164. currentUser, match := setting.IsRunUserMatchCurrentUser(form.RunUser)
  165. if !match {
  166. ctx.Data["Err_RunUser"] = true
  167. ctx.RenderWithErr(ctx.Tr("install.run_user_not_match", form.RunUser, currentUser), tplInstall, &form)
  168. return
  169. }
  170. // Check logic loophole between disable self-registration and no admin account.
  171. if form.DisableRegistration && len(form.AdminName) == 0 {
  172. ctx.Data["Err_Services"] = true
  173. ctx.Data["Err_Admin"] = true
  174. ctx.RenderWithErr(ctx.Tr("install.no_admin_and_disable_registration"), tplInstall, form)
  175. return
  176. }
  177. // Check admin password.
  178. if len(form.AdminName) > 0 && len(form.AdminPasswd) == 0 {
  179. ctx.Data["Err_Admin"] = true
  180. ctx.Data["Err_AdminPasswd"] = true
  181. ctx.RenderWithErr(ctx.Tr("install.err_empty_admin_password"), tplInstall, form)
  182. return
  183. }
  184. if form.AdminPasswd != form.AdminConfirmPasswd {
  185. ctx.Data["Err_Admin"] = true
  186. ctx.Data["Err_AdminPasswd"] = true
  187. ctx.RenderWithErr(ctx.Tr("form.password_not_match"), tplInstall, form)
  188. return
  189. }
  190. if form.AppUrl[len(form.AppUrl)-1] != '/' {
  191. form.AppUrl += "/"
  192. }
  193. // Save settings.
  194. cfg := ini.Empty()
  195. if com.IsFile(setting.CustomConf) {
  196. // Keeps custom settings if there is already something.
  197. if err := cfg.Append(setting.CustomConf); err != nil {
  198. log.Error(4, "Fail to load custom conf '%s': %v", setting.CustomConf, err)
  199. }
  200. }
  201. cfg.Section("database").Key("DB_TYPE").SetValue(models.DbCfg.Type)
  202. cfg.Section("database").Key("HOST").SetValue(models.DbCfg.Host)
  203. cfg.Section("database").Key("NAME").SetValue(models.DbCfg.Name)
  204. cfg.Section("database").Key("USER").SetValue(models.DbCfg.User)
  205. cfg.Section("database").Key("PASSWD").SetValue(models.DbCfg.Passwd)
  206. cfg.Section("database").Key("SSL_MODE").SetValue(models.DbCfg.SSLMode)
  207. cfg.Section("database").Key("PATH").SetValue(models.DbCfg.Path)
  208. cfg.Section("").Key("APP_NAME").SetValue(form.AppName)
  209. cfg.Section("repository").Key("ROOT").SetValue(form.RepoRootPath)
  210. cfg.Section("").Key("RUN_USER").SetValue(form.RunUser)
  211. cfg.Section("server").Key("DOMAIN").SetValue(form.Domain)
  212. cfg.Section("server").Key("HTTP_PORT").SetValue(form.HTTPPort)
  213. cfg.Section("server").Key("ROOT_URL").SetValue(form.AppUrl)
  214. if form.SSHPort == 0 {
  215. cfg.Section("server").Key("DISABLE_SSH").SetValue("true")
  216. } else {
  217. cfg.Section("server").Key("DISABLE_SSH").SetValue("false")
  218. cfg.Section("server").Key("SSH_PORT").SetValue(com.ToStr(form.SSHPort))
  219. }
  220. if len(strings.TrimSpace(form.SMTPHost)) > 0 {
  221. cfg.Section("mailer").Key("ENABLED").SetValue("true")
  222. cfg.Section("mailer").Key("HOST").SetValue(form.SMTPHost)
  223. cfg.Section("mailer").Key("FROM").SetValue(form.SMTPFrom)
  224. cfg.Section("mailer").Key("USER").SetValue(form.SMTPEmail)
  225. cfg.Section("mailer").Key("PASSWD").SetValue(form.SMTPPasswd)
  226. } else {
  227. cfg.Section("mailer").Key("ENABLED").SetValue("false")
  228. }
  229. cfg.Section("service").Key("REGISTER_EMAIL_CONFIRM").SetValue(com.ToStr(form.RegisterConfirm))
  230. cfg.Section("service").Key("ENABLE_NOTIFY_MAIL").SetValue(com.ToStr(form.MailNotify))
  231. cfg.Section("server").Key("OFFLINE_MODE").SetValue(com.ToStr(form.OfflineMode))
  232. cfg.Section("picture").Key("DISABLE_GRAVATAR").SetValue(com.ToStr(form.DisableGravatar))
  233. cfg.Section("picture").Key("ENABLE_FEDERATED_AVATAR").SetValue(com.ToStr(form.EnableFederatedAvatar))
  234. cfg.Section("service").Key("DISABLE_REGISTRATION").SetValue(com.ToStr(form.DisableRegistration))
  235. cfg.Section("service").Key("ENABLE_CAPTCHA").SetValue(com.ToStr(form.EnableCaptcha))
  236. cfg.Section("service").Key("REQUIRE_SIGNIN_VIEW").SetValue(com.ToStr(form.RequireSignInView))
  237. cfg.Section("").Key("RUN_MODE").SetValue("prod")
  238. cfg.Section("session").Key("PROVIDER").SetValue("file")
  239. cfg.Section("log").Key("MODE").SetValue("file")
  240. cfg.Section("log").Key("LEVEL").SetValue("Info")
  241. cfg.Section("log").Key("ROOT_PATH").SetValue(form.LogRootPath)
  242. cfg.Section("security").Key("INSTALL_LOCK").SetValue("true")
  243. cfg.Section("security").Key("SECRET_KEY").SetValue(base.GetRandomString(15))
  244. err := os.MkdirAll(filepath.Dir(setting.CustomConf), os.ModePerm)
  245. if err != nil {
  246. ctx.RenderWithErr(ctx.Tr("install.save_config_failed", err), tplInstall, &form)
  247. return
  248. }
  249. if err := cfg.SaveTo(setting.CustomConf); err != nil {
  250. ctx.RenderWithErr(ctx.Tr("install.save_config_failed", err), tplInstall, &form)
  251. return
  252. }
  253. GlobalInit()
  254. // Create admin account
  255. if len(form.AdminName) > 0 {
  256. u := &models.User{
  257. Name: form.AdminName,
  258. Email: form.AdminEmail,
  259. Passwd: form.AdminPasswd,
  260. IsAdmin: true,
  261. IsActive: true,
  262. }
  263. if err := models.CreateUser(u); err != nil {
  264. if !models.IsErrUserAlreadyExist(err) {
  265. setting.InstallLock = false
  266. ctx.Data["Err_AdminName"] = true
  267. ctx.Data["Err_AdminEmail"] = true
  268. ctx.RenderWithErr(ctx.Tr("install.invalid_admin_setting", err), tplInstall, &form)
  269. return
  270. }
  271. log.Info("Admin account already exist")
  272. u, _ = models.GetUserByName(u.Name)
  273. }
  274. // Auto-login for admin
  275. if err := ctx.Session.Set("uid", u.ID); err != nil {
  276. ctx.RenderWithErr(ctx.Tr("install.save_config_failed", err), tplInstall, &form)
  277. return
  278. }
  279. if err := ctx.Session.Set("uname", u.Name); err != nil {
  280. ctx.RenderWithErr(ctx.Tr("install.save_config_failed", err), tplInstall, &form)
  281. return
  282. }
  283. }
  284. log.Info("First-time run install finished!")
  285. ctx.Flash.Success(ctx.Tr("install.install_success"))
  286. ctx.Redirect(form.AppUrl + "user/login")
  287. }