Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

user_form.go 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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(40)"`
  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. // AuthorizationForm form for authorizing oauth2 clients
  118. type AuthorizationForm struct {
  119. ResponseType string `binding:"Required;In(code)"`
  120. ClientID string `binding:"Required"`
  121. RedirectURI string
  122. State string
  123. // PKCE support
  124. CodeChallengeMethod string // S256, plain
  125. CodeChallenge string
  126. }
  127. // Validate valideates the fields
  128. func (f *AuthorizationForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  129. return validate(errs, ctx.Data, f, ctx.Locale)
  130. }
  131. // GrantApplicationForm form for authorizing oauth2 clients
  132. type GrantApplicationForm struct {
  133. ClientID string `binding:"Required"`
  134. RedirectURI string
  135. State string
  136. }
  137. // Validate valideates the fields
  138. func (f *GrantApplicationForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  139. return validate(errs, ctx.Data, f, ctx.Locale)
  140. }
  141. // AccessTokenForm for issuing access tokens from authorization codes or refresh tokens
  142. type AccessTokenForm struct {
  143. GrantType string `json:"grant_type"`
  144. ClientID string `json:"client_id"`
  145. ClientSecret string `json:"client_secret"`
  146. RedirectURI string `json:"redirect_uri"`
  147. Code string `json:"code"`
  148. RefreshToken string `json:"refresh_token"`
  149. // PKCE support
  150. CodeVerifier string `json:"code_verifier"`
  151. }
  152. // Validate valideates the fields
  153. func (f *AccessTokenForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  154. return validate(errs, ctx.Data, f, ctx.Locale)
  155. }
  156. // __________________________________________.___ _______ ________ _________
  157. // / _____/\_ _____/\__ ___/\__ ___/| |\ \ / _____/ / _____/
  158. // \_____ \ | __)_ | | | | | |/ | \/ \ ___ \_____ \
  159. // / \ | \ | | | | | / | \ \_\ \/ \
  160. // /_______ //_______ / |____| |____| |___\____|__ /\______ /_______ /
  161. // \/ \/ \/ \/ \/
  162. // UpdateProfileForm form for updating profile
  163. type UpdateProfileForm struct {
  164. Name string `binding:"AlphaDashDot;MaxSize(40)"`
  165. FullName string `binding:"MaxSize(100)"`
  166. Email string `binding:"Required;Email;MaxSize(254)"`
  167. KeepEmailPrivate bool
  168. Website string `binding:"ValidUrl;MaxSize(255)"`
  169. Location string `binding:"MaxSize(50)"`
  170. Language string `binding:"Size(5)"`
  171. Description string `binding:"MaxSize(255)"`
  172. }
  173. // Validate validates the fields
  174. func (f *UpdateProfileForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  175. return validate(errs, ctx.Data, f, ctx.Locale)
  176. }
  177. // Avatar types
  178. const (
  179. AvatarLocal string = "local"
  180. AvatarByMail string = "bymail"
  181. )
  182. // AvatarForm form for changing avatar
  183. type AvatarForm struct {
  184. Source string
  185. Avatar *multipart.FileHeader
  186. Gravatar string `binding:"OmitEmpty;Email;MaxSize(254)"`
  187. Federavatar bool
  188. }
  189. // Validate validates the fields
  190. func (f *AvatarForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  191. return validate(errs, ctx.Data, f, ctx.Locale)
  192. }
  193. // AddEmailForm form for adding new email
  194. type AddEmailForm struct {
  195. Email string `binding:"Required;Email;MaxSize(254)"`
  196. }
  197. // Validate validates the fields
  198. func (f *AddEmailForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  199. return validate(errs, ctx.Data, f, ctx.Locale)
  200. }
  201. // UpdateThemeForm form for updating a users' theme
  202. type UpdateThemeForm struct {
  203. Theme string `binding:"Required;MaxSize(30)"`
  204. }
  205. // Validate validates the field
  206. func (f *UpdateThemeForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  207. return validate(errs, ctx.Data, f, ctx.Locale)
  208. }
  209. // IsThemeExists checks if the theme is a theme available in the config.
  210. func (f UpdateThemeForm) IsThemeExists() bool {
  211. var exists bool
  212. for _, v := range setting.UI.Themes {
  213. if strings.ToLower(v) == strings.ToLower(f.Theme) {
  214. exists = true
  215. break
  216. }
  217. }
  218. return exists
  219. }
  220. // ChangePasswordForm form for changing password
  221. type ChangePasswordForm struct {
  222. OldPassword string `form:"old_password" binding:"MaxSize(255)"`
  223. Password string `form:"password" binding:"Required;MaxSize(255)"`
  224. Retype string `form:"retype"`
  225. }
  226. // Validate validates the fields
  227. func (f *ChangePasswordForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  228. return validate(errs, ctx.Data, f, ctx.Locale)
  229. }
  230. // AddOpenIDForm is for changing openid uri
  231. type AddOpenIDForm struct {
  232. Openid string `binding:"Required;MaxSize(256)"`
  233. }
  234. // Validate validates the fields
  235. func (f *AddOpenIDForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  236. return validate(errs, ctx.Data, f, ctx.Locale)
  237. }
  238. // AddKeyForm form for adding SSH/GPG key
  239. type AddKeyForm struct {
  240. Type string `binding:"OmitEmpty"`
  241. Title string `binding:"Required;MaxSize(50)"`
  242. Content string `binding:"Required"`
  243. IsWritable bool
  244. }
  245. // Validate validates the fields
  246. func (f *AddKeyForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  247. return validate(errs, ctx.Data, f, ctx.Locale)
  248. }
  249. // NewAccessTokenForm form for creating access token
  250. type NewAccessTokenForm struct {
  251. Name string `binding:"Required;MaxSize(255)"`
  252. }
  253. // Validate valideates the fields
  254. func (f *NewAccessTokenForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  255. return validate(errs, ctx.Data, f, ctx.Locale)
  256. }
  257. // EditOAuth2ApplicationForm form for editing oauth2 applications
  258. type EditOAuth2ApplicationForm struct {
  259. Name string `binding:"Required;MaxSize(255)" form:"application_name"`
  260. RedirectURI string `binding:"Required" form:"redirect_uri"`
  261. }
  262. // Validate valideates the fields
  263. func (f *EditOAuth2ApplicationForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  264. return validate(errs, ctx.Data, f, ctx.Locale)
  265. }
  266. // TwoFactorAuthForm for logging in with 2FA token.
  267. type TwoFactorAuthForm struct {
  268. Passcode string `binding:"Required"`
  269. }
  270. // Validate validates the fields
  271. func (f *TwoFactorAuthForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  272. return validate(errs, ctx.Data, f, ctx.Locale)
  273. }
  274. // TwoFactorScratchAuthForm for logging in with 2FA scratch token.
  275. type TwoFactorScratchAuthForm struct {
  276. Token string `binding:"Required"`
  277. }
  278. // Validate valideates the fields
  279. func (f *TwoFactorScratchAuthForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  280. return validate(errs, ctx.Data, f, ctx.Locale)
  281. }
  282. // U2FRegistrationForm for reserving an U2F name
  283. type U2FRegistrationForm struct {
  284. Name string `binding:"Required"`
  285. }
  286. // Validate valideates the fields
  287. func (f *U2FRegistrationForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  288. return validate(errs, ctx.Data, f, ctx.Locale)
  289. }
  290. // U2FDeleteForm for deleting U2F keys
  291. type U2FDeleteForm struct {
  292. ID int64 `binding:"Required"`
  293. }
  294. // Validate valideates the fields
  295. func (f *U2FDeleteForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  296. return validate(errs, ctx.Data, f, ctx.Locale)
  297. }