Files
Atay-Makhzan/services/forms/user_form.go
T

433 lines
14 KiB
Go
Raw Normal View History

2014-07-26 00:24:27 -04:00
// Copyright 2014 The Gogs Authors. All rights reserved.
// Copyright 2018 The Gitea Authors. All rights reserved.
2014-07-26 00:24:27 -04:00
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
2021-01-26 23:36:53 +08:00
package forms
2014-07-26 00:24:27 -04:00
import (
2014-11-21 10:58:08 -05:00
"mime/multipart"
2021-01-26 23:36:53 +08:00
"net/http"
"strings"
2021-01-26 23:36:53 +08:00
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/structs"
2021-01-30 16:55:53 +08:00
"code.gitea.io/gitea/modules/web/middleware"
2014-11-21 10:58:08 -05:00
2021-01-26 23:36:53 +08:00
"gitea.com/go-chi/binding"
2014-07-26 00:24:27 -04:00
)
2016-11-27 14:03:59 +08:00
// InstallForm form for installation page
2014-07-26 00:24:27 -04:00
type InstallForm struct {
2015-07-09 13:17:48 +08:00
DbType string `binding:"Required"`
DbHost string
DbUser string
DbPasswd string
DbName string
2015-07-20 12:34:53 +08:00
SSLMode string
2019-05-24 12:15:26 +08:00
Charset string `binding:"Required;In(utf8,utf8mb4)"`
2015-07-09 13:17:48 +08:00
DbPath string
DbSchema string
2015-07-09 13:17:48 +08:00
AppName string `binding:"Required" locale:"install.app_name"`
RepoRootPath string `binding:"Required"`
2016-12-26 02:16:37 +01:00
LFSRootPath string
2015-07-09 13:17:48 +08:00
RunUser string `binding:"Required"`
Domain string `binding:"Required"`
SSHPort int
2015-07-20 12:34:53 +08:00
HTTPPort string `binding:"Required"`
2016-11-27 14:03:59 +08:00
AppURL string `binding:"Required"`
2016-02-12 10:10:02 -05:00
LogRootPath string `binding:"Required"`
2015-07-09 13:17:48 +08:00
2015-07-20 12:34:53 +08:00
SMTPHost string
SMTPFrom string
SMTPUser string `binding:"OmitEmpty;MaxSize(254)" locale:"install.mailer_user"`
2015-07-20 12:34:53 +08:00
SMTPPasswd string
2015-07-09 13:17:48 +08:00
RegisterConfirm bool
MailNotify bool
2017-05-12 04:09:53 -04:00
OfflineMode bool
DisableGravatar bool
EnableFederatedAvatar bool
EnableOpenIDSignIn bool
EnableOpenIDSignUp bool
2017-05-12 04:09:53 -04:00
DisableRegistration bool
AllowOnlyExternalRegistration bool
2017-05-12 04:09:53 -04:00
EnableCaptcha bool
RequireSignInView bool
DefaultKeepEmailPrivate bool
DefaultAllowCreateOrganization bool
2017-09-12 08:48:13 +02:00
DefaultEnableTimetracking bool
2017-05-12 04:09:53 -04:00
NoReplyAddress string
2015-07-09 13:17:48 +08:00
PasswordAlgorithm string
2015-07-08 19:47:56 +08:00
AdminName string `binding:"OmitEmpty;AlphaDashDot;MaxSize(30)" locale:"install.admin_name"`
2015-08-18 02:30:33 +08:00
AdminPasswd string `binding:"OmitEmpty;MaxSize(255)" locale:"install.admin_password"`
2015-07-08 19:47:56 +08:00
AdminConfirmPasswd string
2015-10-29 21:12:41 -04:00
AdminEmail string `binding:"OmitEmpty;MinSize(3);MaxSize(254);Include(@)" locale:"install.admin_email"`
// 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"
ReinstallConfirmFirst bool
ReinstallConfirmSecond bool
ReinstallConfirmThird bool
2014-07-26 00:24:27 -04:00
}
2017-03-14 20:52:01 -04:00
// Validate validates the fields
2021-01-26 23:36:53 +08:00
func (f *InstallForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetContext(req)
2021-01-30 16:55:53 +08:00
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
2014-07-26 00:24:27 -04:00
}
// _____ ____ _________________ ___
// / _ \ | | \__ ___/ | \
// / /_\ \| | / | | / ~ \
// / | \ | / | | \ Y /
// \____|__ /______/ |____| \___|_ /
// \/ \/
2016-11-27 14:03:59 +08:00
// RegisterForm form for registering
2014-07-26 00:24:27 -04:00
type RegisterForm struct {
UserName string `binding:"Required;AlphaDashDot;MaxSize(40)"`
2021-10-14 10:50:23 +08:00
Email string `binding:"Required;MaxSize(254)"`
Password string `binding:"MaxSize(255)"`
Retype string
GRecaptchaResponse string `form:"g-recaptcha-response"`
2020-10-02 22:37:53 -05:00
HcaptchaResponse string `form:"h-captcha-response"`
2014-07-26 00:24:27 -04:00
}
2020-06-05 22:34:23 +08:00
// Validate validates the fields
2021-01-26 23:36:53 +08:00
func (f *RegisterForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetContext(req)
2021-01-30 16:55:53 +08:00
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
2014-07-26 00:24:27 -04:00
}
// IsEmailDomainListed checks whether the domain of an email address
// matches a list of domains
func IsEmailDomainListed(list []string, email string) bool {
if len(list) == 0 {
return false
}
n := strings.LastIndex(email, "@")
if n <= 0 {
return false
}
domain := strings.ToLower(email[n+1:])
for _, v := range list {
if strings.ToLower(v) == domain {
return true
}
}
return false
}
// IsEmailDomainAllowed validates that the email address
// provided by the user matches what has been configured .
// The email is marked as allowed if it matches any of the
// domains in the whitelist or if it doesn't match any of
// domains in the blocklist, if any such list is not empty.
func (f RegisterForm) IsEmailDomainAllowed() bool {
if len(setting.Service.EmailDomainWhitelist) == 0 {
return !IsEmailDomainListed(setting.Service.EmailDomainBlocklist, f.Email)
}
return IsEmailDomainListed(setting.Service.EmailDomainWhitelist, f.Email)
}
2018-09-13 13:04:25 +01:00
// MustChangePasswordForm form for updating your password after account creation
// by an admin
type MustChangePasswordForm struct {
Password string `binding:"Required;MaxSize(255)"`
Retype string
}
2020-06-05 22:34:23 +08:00
// Validate validates the fields
2021-01-26 23:36:53 +08:00
func (f *MustChangePasswordForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetContext(req)
2021-01-30 16:55:53 +08:00
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
2018-09-13 13:04:25 +01:00
}
2017-03-17 15:16:08 +01:00
// SignInForm form for signing in with user/password
2014-07-26 00:24:27 -04:00
type SignInForm struct {
2015-09-14 22:50:44 -04:00
UserName string `binding:"Required;MaxSize(254)"`
// TODO remove required from password for SecondFactorAuthentication
2015-09-14 22:50:44 -04:00
Password string `binding:"Required;MaxSize(255)"`
Remember bool
2014-07-26 00:24:27 -04:00
}
2020-06-05 22:34:23 +08:00
// Validate validates the fields
2021-01-26 23:36:53 +08:00
func (f *SignInForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetContext(req)
2021-01-30 16:55:53 +08:00
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
2014-07-26 00:24:27 -04:00
}
2019-03-08 17:42:50 +01:00
// AuthorizationForm form for authorizing oauth2 clients
type AuthorizationForm struct {
ResponseType string `binding:"Required;In(code)"`
ClientID string `binding:"Required"`
RedirectURI string
State string
Scope string
Nonce string
2019-03-08 17:42:50 +01:00
// PKCE support
CodeChallengeMethod string // S256, plain
CodeChallenge string
}
2020-06-05 22:34:23 +08:00
// Validate validates the fields
2021-01-26 23:36:53 +08:00
func (f *AuthorizationForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetContext(req)
2021-01-30 16:55:53 +08:00
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
2019-03-08 17:42:50 +01:00
}
// GrantApplicationForm form for authorizing oauth2 clients
type GrantApplicationForm struct {
ClientID string `binding:"Required"`
RedirectURI string
State string
Scope string
Nonce string
2019-03-08 17:42:50 +01:00
}
2020-06-05 22:34:23 +08:00
// Validate validates the fields
2021-01-26 23:36:53 +08:00
func (f *GrantApplicationForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetContext(req)
2021-01-30 16:55:53 +08:00
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
2019-03-08 17:42:50 +01:00
}
// AccessTokenForm for issuing access tokens from authorization codes or refresh tokens
type AccessTokenForm struct {
2019-04-15 17:54:50 +02:00
GrantType string `json:"grant_type"`
ClientID string `json:"client_id"`
ClientSecret string `json:"client_secret"`
RedirectURI string `json:"redirect_uri"`
Code string `json:"code"`
RefreshToken string `json:"refresh_token"`
2019-03-08 17:42:50 +01:00
// PKCE support
2019-04-15 17:54:50 +02:00
CodeVerifier string `json:"code_verifier"`
2019-03-08 17:42:50 +01:00
}
2020-06-05 22:34:23 +08:00
// Validate validates the fields
2021-01-26 23:36:53 +08:00
func (f *AccessTokenForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetContext(req)
2021-01-30 16:55:53 +08:00
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
2019-03-08 17:42:50 +01:00
}
2021-08-21 04:16:45 +02:00
// IntrospectTokenForm for introspecting tokens
type IntrospectTokenForm struct {
Token string `json:"token"`
}
// Validate validates the fields
func (f *IntrospectTokenForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetContext(req)
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
}
2014-07-26 00:24:27 -04:00
// __________________________________________.___ _______ ________ _________
// / _____/\_ _____/\__ ___/\__ ___/| |\ \ / _____/ / _____/
// \_____ \ | __)_ | | | | | |/ | \/ \ ___ \_____ \
// / \ | \ | | | | | / | \ \_\ \/ \
// /_______ //_______ / |____| |____| |___\____|__ /\______ /_______ /
// \/ \/ \/ \/ \/
2016-11-27 14:03:59 +08:00
// UpdateProfileForm form for updating profile
2014-07-26 00:24:27 -04:00
type UpdateProfileForm struct {
2020-06-05 22:01:53 +02:00
Name string `binding:"AlphaDashDot;MaxSize(40)"`
FullName string `binding:"MaxSize(100)"`
KeepEmailPrivate bool
2021-06-26 00:38:27 +02:00
Website string `binding:"ValidSiteUrl;MaxSize(255)"`
2020-06-05 22:01:53 +02:00
Location string `binding:"MaxSize(50)"`
Description string `binding:"MaxSize(255)"`
Visibility structs.VisibleType
2020-06-05 22:01:53 +02:00
KeepActivityPrivate bool
2014-07-26 00:24:27 -04:00
}
2017-03-14 20:52:01 -04:00
// Validate validates the fields
2021-01-26 23:36:53 +08:00
func (f *UpdateProfileForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
2021-10-27 17:40:08 +02:00
ctx := context.GetContext(req)
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
}
// UpdateLanguageForm form for updating profile
type UpdateLanguageForm struct {
Language string
}
// Validate validates the fields
func (f *UpdateLanguageForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
2021-01-26 23:36:53 +08:00
ctx := context.GetContext(req)
2021-01-30 16:55:53 +08:00
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
2014-07-26 00:24:27 -04:00
}
2016-11-27 14:03:59 +08:00
// Avatar types
2016-08-07 19:27:38 +02:00
const (
2016-11-07 17:55:31 +01:00
AvatarLocal string = "local"
AvatarByMail string = "bymail"
2016-08-07 19:27:38 +02:00
)
2016-11-27 14:03:59 +08:00
// AvatarForm form for changing avatar
2016-08-07 19:27:38 +02:00
type AvatarForm struct {
Source string
Avatar *multipart.FileHeader
Gravatar string `binding:"OmitEmpty;Email;MaxSize(254)"`
Federavatar bool
2014-11-21 10:58:08 -05:00
}
2017-03-14 20:52:01 -04:00
// Validate validates the fields
2021-01-26 23:36:53 +08:00
func (f *AvatarForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetContext(req)
2021-01-30 16:55:53 +08:00
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
2014-11-21 10:58:08 -05:00
}
2016-11-27 14:03:59 +08:00
// AddEmailForm form for adding new email
type AddEmailForm struct {
2015-09-10 11:40:34 -04:00
Email string `binding:"Required;Email;MaxSize(254)"`
}
2017-03-14 20:52:01 -04:00
// Validate validates the fields
2021-01-26 23:36:53 +08:00
func (f *AddEmailForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetContext(req)
2021-01-30 16:55:53 +08:00
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
}
2019-01-09 18:22:57 +01:00
// UpdateThemeForm form for updating a users' theme
type UpdateThemeForm struct {
Theme string `binding:"Required;MaxSize(30)"`
}
// Validate validates the field
2021-01-26 23:36:53 +08:00
func (f *UpdateThemeForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetContext(req)
2021-01-30 16:55:53 +08:00
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
2019-01-09 18:22:57 +01:00
}
// IsThemeExists checks if the theme is a theme available in the config.
func (f UpdateThemeForm) IsThemeExists() bool {
var exists bool
for _, v := range setting.UI.Themes {
2019-06-12 21:41:28 +02:00
if strings.EqualFold(v, f.Theme) {
2019-01-09 18:22:57 +01:00
exists = true
break
}
}
return exists
}
2016-11-27 14:03:59 +08:00
// ChangePasswordForm form for changing password
2014-07-26 00:24:27 -04:00
type ChangePasswordForm struct {
2017-02-22 08:14:37 +01:00
OldPassword string `form:"old_password" binding:"MaxSize(255)"`
2015-08-18 02:30:33 +08:00
Password string `form:"password" binding:"Required;MaxSize(255)"`
2014-07-26 00:24:27 -04:00
Retype string `form:"retype"`
}
2017-03-14 20:52:01 -04:00
// Validate validates the fields
2021-01-26 23:36:53 +08:00
func (f *ChangePasswordForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetContext(req)
2021-01-30 16:55:53 +08:00
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
2014-07-26 00:24:27 -04:00
}
2014-11-12 06:48:50 -05:00
2017-03-17 15:16:08 +01:00
// AddOpenIDForm is for changing openid uri
type AddOpenIDForm struct {
2017-03-21 01:55:00 +01:00
Openid string `binding:"Required;MaxSize(256)"`
2017-03-17 15:16:08 +01:00
}
// Validate validates the fields
2021-01-26 23:36:53 +08:00
func (f *AddOpenIDForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetContext(req)
2021-01-30 16:55:53 +08:00
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
2017-03-17 15:16:08 +01:00
}
// AddKeyForm form for adding SSH/GPG key
type AddKeyForm struct {
2021-12-19 06:37:18 +01:00
Type string `binding:"OmitEmpty"`
Title string `binding:"Required;MaxSize(50)"`
Content string `binding:"Required"`
Signature string `binding:"OmitEmpty"`
KeyID string `binding:"OmitEmpty"`
Fingerprint string `binding:"OmitEmpty"`
IsWritable bool
2014-11-12 06:48:50 -05:00
}
2017-03-14 20:52:01 -04:00
// Validate validates the fields
2021-01-26 23:36:53 +08:00
func (f *AddKeyForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetContext(req)
2021-01-30 16:55:53 +08:00
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
2014-11-12 06:48:50 -05:00
}
2016-11-27 14:03:59 +08:00
// NewAccessTokenForm form for creating access token
2014-11-12 06:48:50 -05:00
type NewAccessTokenForm struct {
Name string `binding:"Required;MaxSize(255)"`
2014-11-12 06:48:50 -05:00
}
2020-06-05 22:34:23 +08:00
// Validate validates the fields
2021-01-26 23:36:53 +08:00
func (f *NewAccessTokenForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetContext(req)
2021-01-30 16:55:53 +08:00
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
2014-11-12 06:48:50 -05:00
}
2017-01-15 21:14:29 -05:00
2019-03-08 17:42:50 +01:00
// EditOAuth2ApplicationForm form for editing oauth2 applications
type EditOAuth2ApplicationForm struct {
Name string `binding:"Required;MaxSize(255)" form:"application_name"`
RedirectURI string `binding:"Required" form:"redirect_uri"`
}
2020-06-05 22:34:23 +08:00
// Validate validates the fields
2021-01-26 23:36:53 +08:00
func (f *EditOAuth2ApplicationForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetContext(req)
2021-01-30 16:55:53 +08:00
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
2019-03-08 17:42:50 +01:00
}
2017-01-15 21:14:29 -05:00
// TwoFactorAuthForm for logging in with 2FA token.
type TwoFactorAuthForm struct {
Passcode string `binding:"Required"`
}
2017-03-14 20:52:01 -04:00
// Validate validates the fields
2021-01-26 23:36:53 +08:00
func (f *TwoFactorAuthForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetContext(req)
2021-01-30 16:55:53 +08:00
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
2017-01-15 21:14:29 -05:00
}
// TwoFactorScratchAuthForm for logging in with 2FA scratch token.
type TwoFactorScratchAuthForm struct {
Token string `binding:"Required"`
}
2020-06-05 22:34:23 +08:00
// Validate validates the fields
2021-01-26 23:36:53 +08:00
func (f *TwoFactorScratchAuthForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetContext(req)
2021-01-30 16:55:53 +08:00
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
2017-01-15 21:14:29 -05:00
}
2018-05-19 16:12:37 +02:00
2022-01-14 23:03:31 +08:00
// WebauthnRegistrationForm for reserving an WebAuthn name
type WebauthnRegistrationForm struct {
2018-05-19 16:12:37 +02:00
Name string `binding:"Required"`
}
2020-06-05 22:34:23 +08:00
// Validate validates the fields
2022-01-14 23:03:31 +08:00
func (f *WebauthnRegistrationForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
2021-01-26 23:36:53 +08:00
ctx := context.GetContext(req)
2021-01-30 16:55:53 +08:00
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
2018-05-19 16:12:37 +02:00
}
2022-01-14 23:03:31 +08:00
// WebauthnDeleteForm for deleting WebAuthn keys
type WebauthnDeleteForm struct {
2018-05-19 16:12:37 +02:00
ID int64 `binding:"Required"`
}
2020-06-05 22:34:23 +08:00
// Validate validates the fields
2022-01-14 23:03:31 +08:00
func (f *WebauthnDeleteForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
2021-01-26 23:36:53 +08:00
ctx := context.GetContext(req)
2021-01-30 16:55:53 +08:00
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
2018-05-19 16:12:37 +02:00
}