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.

user_form.go 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package forms
  6. import (
  7. "mime/multipart"
  8. "net/http"
  9. "strings"
  10. "code.gitea.io/gitea/modules/context"
  11. "code.gitea.io/gitea/modules/setting"
  12. "code.gitea.io/gitea/modules/structs"
  13. "code.gitea.io/gitea/modules/web/middleware"
  14. "gitea.com/go-chi/binding"
  15. )
  16. // InstallForm form for installation page
  17. type InstallForm struct {
  18. DbType string `binding:"Required"`
  19. DbHost string
  20. DbUser string
  21. DbPasswd string
  22. DbName string
  23. SSLMode string
  24. Charset string `binding:"Required;In(utf8,utf8mb4)"`
  25. DbPath string
  26. DbSchema string
  27. AppName string `binding:"Required" locale:"install.app_name"`
  28. RepoRootPath string `binding:"Required"`
  29. LFSRootPath string
  30. RunUser string `binding:"Required"`
  31. Domain string `binding:"Required"`
  32. SSHPort int
  33. HTTPPort string `binding:"Required"`
  34. AppURL string `binding:"Required"`
  35. LogRootPath string `binding:"Required"`
  36. SMTPHost string
  37. SMTPFrom string
  38. SMTPUser string `binding:"OmitEmpty;MaxSize(254)" locale:"install.mailer_user"`
  39. SMTPPasswd string
  40. RegisterConfirm bool
  41. MailNotify bool
  42. OfflineMode bool
  43. DisableGravatar bool
  44. EnableFederatedAvatar bool
  45. EnableOpenIDSignIn bool
  46. EnableOpenIDSignUp bool
  47. DisableRegistration bool
  48. AllowOnlyExternalRegistration bool
  49. EnableCaptcha bool
  50. RequireSignInView bool
  51. DefaultKeepEmailPrivate bool
  52. DefaultAllowCreateOrganization bool
  53. DefaultEnableTimetracking bool
  54. NoReplyAddress string
  55. PasswordAlgorithm string
  56. AdminName string `binding:"OmitEmpty;AlphaDashDot;MaxSize(30)" locale:"install.admin_name"`
  57. AdminPasswd string `binding:"OmitEmpty;MaxSize(255)" locale:"install.admin_password"`
  58. AdminConfirmPasswd string
  59. AdminEmail string `binding:"OmitEmpty;MinSize(3);MaxSize(254);Include(@)" locale:"install.admin_email"`
  60. // ReinstallConfirmFirst we can not use 1/2/3 or A/B/C here, there is a framework bug, can not parse "reinstall_confirm_1" or "reinstall_confirm_a"
  61. ReinstallConfirmFirst bool
  62. ReinstallConfirmSecond bool
  63. ReinstallConfirmThird bool
  64. }
  65. // Validate validates the fields
  66. func (f *InstallForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  67. ctx := context.GetContext(req)
  68. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  69. }
  70. // _____ ____ _________________ ___
  71. // / _ \ | | \__ ___/ | \
  72. // / /_\ \| | / | | / ~ \
  73. // / | \ | / | | \ Y /
  74. // \____|__ /______/ |____| \___|_ /
  75. // \/ \/
  76. // RegisterForm form for registering
  77. type RegisterForm struct {
  78. UserName string `binding:"Required;AlphaDashDot;MaxSize(40)"`
  79. Email string `binding:"Required;MaxSize(254)"`
  80. Password string `binding:"MaxSize(255)"`
  81. Retype string
  82. GRecaptchaResponse string `form:"g-recaptcha-response"`
  83. HcaptchaResponse string `form:"h-captcha-response"`
  84. }
  85. // Validate validates the fields
  86. func (f *RegisterForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  87. ctx := context.GetContext(req)
  88. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  89. }
  90. // IsEmailDomainListed checks whether the domain of an email address
  91. // matches a list of domains
  92. func IsEmailDomainListed(list []string, email string) bool {
  93. if len(list) == 0 {
  94. return false
  95. }
  96. n := strings.LastIndex(email, "@")
  97. if n <= 0 {
  98. return false
  99. }
  100. domain := strings.ToLower(email[n+1:])
  101. for _, v := range list {
  102. if strings.ToLower(v) == domain {
  103. return true
  104. }
  105. }
  106. return false
  107. }
  108. // IsEmailDomainAllowed validates that the email address
  109. // provided by the user matches what has been configured .
  110. // The email is marked as allowed if it matches any of the
  111. // domains in the whitelist or if it doesn't match any of
  112. // domains in the blocklist, if any such list is not empty.
  113. func (f RegisterForm) IsEmailDomainAllowed() bool {
  114. if len(setting.Service.EmailDomainWhitelist) == 0 {
  115. return !IsEmailDomainListed(setting.Service.EmailDomainBlocklist, f.Email)
  116. }
  117. return IsEmailDomainListed(setting.Service.EmailDomainWhitelist, f.Email)
  118. }
  119. // MustChangePasswordForm form for updating your password after account creation
  120. // by an admin
  121. type MustChangePasswordForm struct {
  122. Password string `binding:"Required;MaxSize(255)"`
  123. Retype string
  124. }
  125. // Validate validates the fields
  126. func (f *MustChangePasswordForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  127. ctx := context.GetContext(req)
  128. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  129. }
  130. // SignInForm form for signing in with user/password
  131. type SignInForm struct {
  132. UserName string `binding:"Required;MaxSize(254)"`
  133. // TODO remove required from password for SecondFactorAuthentication
  134. Password string `binding:"Required;MaxSize(255)"`
  135. Remember bool
  136. }
  137. // Validate validates the fields
  138. func (f *SignInForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  139. ctx := context.GetContext(req)
  140. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  141. }
  142. // AuthorizationForm form for authorizing oauth2 clients
  143. type AuthorizationForm struct {
  144. ResponseType string `binding:"Required;In(code)"`
  145. ClientID string `binding:"Required"`
  146. RedirectURI string
  147. State string
  148. Scope string
  149. Nonce string
  150. // PKCE support
  151. CodeChallengeMethod string // S256, plain
  152. CodeChallenge string
  153. }
  154. // Validate validates the fields
  155. func (f *AuthorizationForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  156. ctx := context.GetContext(req)
  157. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  158. }
  159. // GrantApplicationForm form for authorizing oauth2 clients
  160. type GrantApplicationForm struct {
  161. ClientID string `binding:"Required"`
  162. RedirectURI string
  163. State string
  164. Scope string
  165. Nonce string
  166. }
  167. // Validate validates the fields
  168. func (f *GrantApplicationForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  169. ctx := context.GetContext(req)
  170. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  171. }
  172. // AccessTokenForm for issuing access tokens from authorization codes or refresh tokens
  173. type AccessTokenForm struct {
  174. GrantType string `json:"grant_type"`
  175. ClientID string `json:"client_id"`
  176. ClientSecret string `json:"client_secret"`
  177. RedirectURI string `json:"redirect_uri"`
  178. Code string `json:"code"`
  179. RefreshToken string `json:"refresh_token"`
  180. // PKCE support
  181. CodeVerifier string `json:"code_verifier"`
  182. }
  183. // Validate validates the fields
  184. func (f *AccessTokenForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  185. ctx := context.GetContext(req)
  186. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  187. }
  188. // IntrospectTokenForm for introspecting tokens
  189. type IntrospectTokenForm struct {
  190. Token string `json:"token"`
  191. }
  192. // Validate validates the fields
  193. func (f *IntrospectTokenForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  194. ctx := context.GetContext(req)
  195. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  196. }
  197. // __________________________________________.___ _______ ________ _________
  198. // / _____/\_ _____/\__ ___/\__ ___/| |\ \ / _____/ / _____/
  199. // \_____ \ | __)_ | | | | | |/ | \/ \ ___ \_____ \
  200. // / \ | \ | | | | | / | \ \_\ \/ \
  201. // /_______ //_______ / |____| |____| |___\____|__ /\______ /_______ /
  202. // \/ \/ \/ \/ \/
  203. // UpdateProfileForm form for updating profile
  204. type UpdateProfileForm struct {
  205. Name string `binding:"AlphaDashDot;MaxSize(40)"`
  206. FullName string `binding:"MaxSize(100)"`
  207. KeepEmailPrivate bool
  208. Website string `binding:"ValidSiteUrl;MaxSize(255)"`
  209. Location string `binding:"MaxSize(50)"`
  210. Description string `binding:"MaxSize(255)"`
  211. Visibility structs.VisibleType
  212. KeepActivityPrivate bool
  213. }
  214. // Validate validates the fields
  215. func (f *UpdateProfileForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  216. ctx := context.GetContext(req)
  217. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  218. }
  219. // UpdateLanguageForm form for updating profile
  220. type UpdateLanguageForm struct {
  221. Language string
  222. }
  223. // Validate validates the fields
  224. func (f *UpdateLanguageForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  225. ctx := context.GetContext(req)
  226. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  227. }
  228. // Avatar types
  229. const (
  230. AvatarLocal string = "local"
  231. AvatarByMail string = "bymail"
  232. )
  233. // AvatarForm form for changing avatar
  234. type AvatarForm struct {
  235. Source string
  236. Avatar *multipart.FileHeader
  237. Gravatar string `binding:"OmitEmpty;Email;MaxSize(254)"`
  238. Federavatar bool
  239. }
  240. // Validate validates the fields
  241. func (f *AvatarForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  242. ctx := context.GetContext(req)
  243. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  244. }
  245. // AddEmailForm form for adding new email
  246. type AddEmailForm struct {
  247. Email string `binding:"Required;Email;MaxSize(254)"`
  248. }
  249. // Validate validates the fields
  250. func (f *AddEmailForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  251. ctx := context.GetContext(req)
  252. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  253. }
  254. // UpdateThemeForm form for updating a users' theme
  255. type UpdateThemeForm struct {
  256. Theme string `binding:"Required;MaxSize(30)"`
  257. }
  258. // Validate validates the field
  259. func (f *UpdateThemeForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  260. ctx := context.GetContext(req)
  261. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  262. }
  263. // IsThemeExists checks if the theme is a theme available in the config.
  264. func (f UpdateThemeForm) IsThemeExists() bool {
  265. var exists bool
  266. for _, v := range setting.UI.Themes {
  267. if strings.EqualFold(v, f.Theme) {
  268. exists = true
  269. break
  270. }
  271. }
  272. return exists
  273. }
  274. // ChangePasswordForm form for changing password
  275. type ChangePasswordForm struct {
  276. OldPassword string `form:"old_password" binding:"MaxSize(255)"`
  277. Password string `form:"password" binding:"Required;MaxSize(255)"`
  278. Retype string `form:"retype"`
  279. }
  280. // Validate validates the fields
  281. func (f *ChangePasswordForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  282. ctx := context.GetContext(req)
  283. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  284. }
  285. // AddOpenIDForm is for changing openid uri
  286. type AddOpenIDForm struct {
  287. Openid string `binding:"Required;MaxSize(256)"`
  288. }
  289. // Validate validates the fields
  290. func (f *AddOpenIDForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  291. ctx := context.GetContext(req)
  292. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  293. }
  294. // AddKeyForm form for adding SSH/GPG key
  295. type AddKeyForm struct {
  296. Type string `binding:"OmitEmpty"`
  297. Title string `binding:"Required;MaxSize(50)"`
  298. Content string `binding:"Required"`
  299. Signature string `binding:"OmitEmpty"`
  300. KeyID string `binding:"OmitEmpty"`
  301. IsWritable bool
  302. }
  303. // Validate validates the fields
  304. func (f *AddKeyForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  305. ctx := context.GetContext(req)
  306. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  307. }
  308. // NewAccessTokenForm form for creating access token
  309. type NewAccessTokenForm struct {
  310. Name string `binding:"Required;MaxSize(255)"`
  311. }
  312. // Validate validates the fields
  313. func (f *NewAccessTokenForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  314. ctx := context.GetContext(req)
  315. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  316. }
  317. // EditOAuth2ApplicationForm form for editing oauth2 applications
  318. type EditOAuth2ApplicationForm struct {
  319. Name string `binding:"Required;MaxSize(255)" form:"application_name"`
  320. RedirectURI string `binding:"Required" form:"redirect_uri"`
  321. }
  322. // Validate validates the fields
  323. func (f *EditOAuth2ApplicationForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  324. ctx := context.GetContext(req)
  325. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  326. }
  327. // TwoFactorAuthForm for logging in with 2FA token.
  328. type TwoFactorAuthForm struct {
  329. Passcode string `binding:"Required"`
  330. }
  331. // Validate validates the fields
  332. func (f *TwoFactorAuthForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  333. ctx := context.GetContext(req)
  334. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  335. }
  336. // TwoFactorScratchAuthForm for logging in with 2FA scratch token.
  337. type TwoFactorScratchAuthForm struct {
  338. Token string `binding:"Required"`
  339. }
  340. // Validate validates the fields
  341. func (f *TwoFactorScratchAuthForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  342. ctx := context.GetContext(req)
  343. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  344. }
  345. // U2FRegistrationForm for reserving an U2F name
  346. type U2FRegistrationForm struct {
  347. Name string `binding:"Required"`
  348. }
  349. // Validate validates the fields
  350. func (f *U2FRegistrationForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  351. ctx := context.GetContext(req)
  352. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  353. }
  354. // U2FDeleteForm for deleting U2F keys
  355. type U2FDeleteForm struct {
  356. ID int64 `binding:"Required"`
  357. }
  358. // Validate validates the fields
  359. func (f *U2FDeleteForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  360. ctx := context.GetContext(req)
  361. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  362. }