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 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 auth
  6. import (
  7. "mime/multipart"
  8. "strings"
  9. "code.gitea.io/gitea/modules/setting"
  10. "github.com/go-macaron/binding"
  11. macaron "gopkg.in/macaron.v1"
  12. )
  13. // InstallForm form for installation page
  14. type InstallForm struct {
  15. DbType string `binding:"Required"`
  16. DbHost string
  17. DbUser string
  18. DbPasswd string
  19. DbName string
  20. SSLMode string
  21. DbPath string
  22. AppName string `binding:"Required" locale:"install.app_name"`
  23. RepoRootPath string `binding:"Required"`
  24. LFSRootPath string
  25. RunUser string `binding:"Required"`
  26. Domain string `binding:"Required"`
  27. SSHPort int
  28. HTTPPort string `binding:"Required"`
  29. AppURL string `binding:"Required"`
  30. LogRootPath string `binding:"Required"`
  31. SMTPHost string
  32. SMTPFrom string
  33. SMTPUser string `binding:"OmitEmpty;MaxSize(254)" locale:"install.mailer_user"`
  34. SMTPPasswd string
  35. RegisterConfirm bool
  36. MailNotify bool
  37. OfflineMode bool
  38. DisableGravatar bool
  39. EnableFederatedAvatar bool
  40. EnableOpenIDSignIn bool
  41. EnableOpenIDSignUp bool
  42. DisableRegistration bool
  43. AllowOnlyExternalRegistration bool
  44. EnableCaptcha bool
  45. RequireSignInView bool
  46. DefaultKeepEmailPrivate bool
  47. DefaultAllowCreateOrganization bool
  48. DefaultEnableTimetracking bool
  49. NoReplyAddress string
  50. AdminName string `binding:"OmitEmpty;AlphaDashDot;MaxSize(30)" locale:"install.admin_name"`
  51. AdminPasswd string `binding:"OmitEmpty;MaxSize(255)" locale:"install.admin_password"`
  52. AdminConfirmPasswd string
  53. AdminEmail string `binding:"OmitEmpty;MinSize(3);MaxSize(254);Include(@)" locale:"install.admin_email"`
  54. }
  55. // Validate validates the fields
  56. func (f *InstallForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  57. return validate(errs, ctx.Data, f, ctx.Locale)
  58. }
  59. // _____ ____ _________________ ___
  60. // / _ \ | | \__ ___/ | \
  61. // / /_\ \| | / | | / ~ \
  62. // / | \ | / | | \ Y /
  63. // \____|__ /______/ |____| \___|_ /
  64. // \/ \/
  65. // RegisterForm form for registering
  66. type RegisterForm struct {
  67. UserName string `binding:"Required;AlphaDashDot;MaxSize(35)"`
  68. Email string `binding:"Required;Email;MaxSize(254)"`
  69. Password string `binding:"Required;MaxSize(255)"`
  70. Retype string
  71. GRecaptchaResponse string `form:"g-recaptcha-response"`
  72. }
  73. // Validate valideates the fields
  74. func (f *RegisterForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  75. return validate(errs, ctx.Data, f, ctx.Locale)
  76. }
  77. // IsEmailDomainWhitelisted validates that the email address
  78. // provided by the user matches what has been configured .
  79. // If the domain whitelist from the config is empty, it marks the
  80. // email as whitelisted
  81. func (f RegisterForm) IsEmailDomainWhitelisted() bool {
  82. if len(setting.Service.EmailDomainWhitelist) == 0 {
  83. return true
  84. }
  85. n := strings.LastIndex(f.Email, "@")
  86. if n <= 0 {
  87. return false
  88. }
  89. domain := strings.ToLower(f.Email[n+1:])
  90. for _, v := range setting.Service.EmailDomainWhitelist {
  91. if strings.ToLower(v) == domain {
  92. return true
  93. }
  94. }
  95. return false
  96. }
  97. // MustChangePasswordForm form for updating your password after account creation
  98. // by an admin
  99. type MustChangePasswordForm struct {
  100. Password string `binding:"Required;MaxSize(255)"`
  101. Retype string
  102. }
  103. // Validate valideates the fields
  104. func (f *MustChangePasswordForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  105. return validate(errs, ctx.Data, f, ctx.Locale)
  106. }
  107. // SignInForm form for signing in with user/password
  108. type SignInForm struct {
  109. UserName string `binding:"Required;MaxSize(254)"`
  110. Password string `binding:"Required;MaxSize(255)"`
  111. Remember bool
  112. }
  113. // Validate valideates the fields
  114. func (f *SignInForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  115. return validate(errs, ctx.Data, f, ctx.Locale)
  116. }
  117. // __________________________________________.___ _______ ________ _________
  118. // / _____/\_ _____/\__ ___/\__ ___/| |\ \ / _____/ / _____/
  119. // \_____ \ | __)_ | | | | | |/ | \/ \ ___ \_____ \
  120. // / \ | \ | | | | | / | \ \_\ \/ \
  121. // /_______ //_______ / |____| |____| |___\____|__ /\______ /_______ /
  122. // \/ \/ \/ \/ \/
  123. // UpdateProfileForm form for updating profile
  124. type UpdateProfileForm struct {
  125. Name string `binding:"AlphaDashDot;MaxSize(35)"`
  126. FullName string `binding:"MaxSize(100)"`
  127. Email string `binding:"Required;Email;MaxSize(254)"`
  128. KeepEmailPrivate bool
  129. Website string `binding:"ValidUrl;MaxSize(255)"`
  130. Location string `binding:"MaxSize(50)"`
  131. Language string `binding:"Size(5)"`
  132. }
  133. // Validate validates the fields
  134. func (f *UpdateProfileForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  135. return validate(errs, ctx.Data, f, ctx.Locale)
  136. }
  137. // Avatar types
  138. const (
  139. AvatarLocal string = "local"
  140. AvatarByMail string = "bymail"
  141. )
  142. // AvatarForm form for changing avatar
  143. type AvatarForm struct {
  144. Source string
  145. Avatar *multipart.FileHeader
  146. Gravatar string `binding:"OmitEmpty;Email;MaxSize(254)"`
  147. Federavatar bool
  148. }
  149. // Validate validates the fields
  150. func (f *AvatarForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  151. return validate(errs, ctx.Data, f, ctx.Locale)
  152. }
  153. // AddEmailForm form for adding new email
  154. type AddEmailForm struct {
  155. Email string `binding:"Required;Email;MaxSize(254)"`
  156. }
  157. // Validate validates the fields
  158. func (f *AddEmailForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  159. return validate(errs, ctx.Data, f, ctx.Locale)
  160. }
  161. // UpdateThemeForm form for updating a users' theme
  162. type UpdateThemeForm struct {
  163. Theme string `binding:"Required;MaxSize(30)"`
  164. }
  165. // Validate validates the field
  166. func (f *UpdateThemeForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  167. return validate(errs, ctx.Data, f, ctx.Locale)
  168. }
  169. // IsThemeExists checks if the theme is a theme available in the config.
  170. func (f UpdateThemeForm) IsThemeExists() bool {
  171. var exists bool
  172. for _, v := range setting.UI.Themes {
  173. if strings.ToLower(v) == strings.ToLower(f.Theme) {
  174. exists = true
  175. break
  176. }
  177. }
  178. return exists
  179. }
  180. // ChangePasswordForm form for changing password
  181. type ChangePasswordForm struct {
  182. OldPassword string `form:"old_password" binding:"MaxSize(255)"`
  183. Password string `form:"password" binding:"Required;MaxSize(255)"`
  184. Retype string `form:"retype"`
  185. }
  186. // Validate validates the fields
  187. func (f *ChangePasswordForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  188. return validate(errs, ctx.Data, f, ctx.Locale)
  189. }
  190. // AddOpenIDForm is for changing openid uri
  191. type AddOpenIDForm struct {
  192. Openid string `binding:"Required;MaxSize(256)"`
  193. }
  194. // Validate validates the fields
  195. func (f *AddOpenIDForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  196. return validate(errs, ctx.Data, f, ctx.Locale)
  197. }
  198. // AddKeyForm form for adding SSH/GPG key
  199. type AddKeyForm struct {
  200. Type string `binding:"OmitEmpty"`
  201. Title string `binding:"Required;MaxSize(50)"`
  202. Content string `binding:"Required"`
  203. IsWritable bool
  204. }
  205. // Validate validates the fields
  206. func (f *AddKeyForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  207. return validate(errs, ctx.Data, f, ctx.Locale)
  208. }
  209. // NewAccessTokenForm form for creating access token
  210. type NewAccessTokenForm struct {
  211. Name string `binding:"Required;MaxSize(255)"`
  212. }
  213. // Validate valideates the fields
  214. func (f *NewAccessTokenForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  215. return validate(errs, ctx.Data, f, ctx.Locale)
  216. }
  217. // TwoFactorAuthForm for logging in with 2FA token.
  218. type TwoFactorAuthForm struct {
  219. Passcode string `binding:"Required"`
  220. }
  221. // Validate validates the fields
  222. func (f *TwoFactorAuthForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  223. return validate(errs, ctx.Data, f, ctx.Locale)
  224. }
  225. // TwoFactorScratchAuthForm for logging in with 2FA scratch token.
  226. type TwoFactorScratchAuthForm struct {
  227. Token string `binding:"Required"`
  228. }
  229. // Validate valideates the fields
  230. func (f *TwoFactorScratchAuthForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  231. return validate(errs, ctx.Data, f, ctx.Locale)
  232. }
  233. // U2FRegistrationForm for reserving an U2F name
  234. type U2FRegistrationForm struct {
  235. Name string `binding:"Required"`
  236. }
  237. // Validate valideates the fields
  238. func (f *U2FRegistrationForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  239. return validate(errs, ctx.Data, f, ctx.Locale)
  240. }
  241. // U2FDeleteForm for deleting U2F keys
  242. type U2FDeleteForm struct {
  243. ID int64 `binding:"Required"`
  244. }
  245. // Validate valideates the fields
  246. func (f *U2FDeleteForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  247. return validate(errs, ctx.Data, f, ctx.Locale)
  248. }