func createUserInContext(ctx *context.Context, tpl base.TplName, form interface{}, u *models.User, gothUser *goth.User, allowLink bool) (ok bool) {
	if err := models.CreateUser(u); err != nil {
		if allowLink && (models.IsErrUserAlreadyExist(err) || models.IsErrEmailAlreadyUsed(err)) {
			if setting.OAuth2Client.AccountLinking == setting.OAuth2AccountLinkingAuto {
				var user *models.User
				user = &models.User{Name: u.Name}
				hasUser, err := models.GetUser(user)
				if !hasUser || err != nil {
					user = &models.User{Email: u.Email}
					hasUser, err = models.GetUser(user)
					if !hasUser || err != nil {
						ctx.ServerError("UserLinkAccount", err)
						return
					}
				}

				// TODO: probably we should respect 'remember' user's choice...
				linkAccount(ctx, user, *gothUser, true)
				return // user is already created here, all redirects are handled
			} else if setting.OAuth2Client.AccountLinking == setting.OAuth2AccountLinkingLogin {
				showLinkingLogin(ctx, *gothUser)
				return // user will be created only after linking login
			}
		}

		// handle error without template
		if len(tpl) == 0 {
			ctx.ServerError("CreateUser", err)
			return
		}

		// handle error with template
		switch {
		case models.IsErrUserAlreadyExist(err):
			ctx.Data["Err_UserName"] = true
			ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), tpl, form)
		case models.IsErrEmailAlreadyUsed(err):
			ctx.Data["Err_Email"] = true
			ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tpl, form)
		case models.IsErrEmailInvalid(err):
			ctx.Data["Err_Email"] = true
			ctx.RenderWithErr(ctx.Tr("form.email_invalid"), tpl, form)
		case models.IsErrNameReserved(err):
			ctx.Data["Err_UserName"] = true
			ctx.RenderWithErr(ctx.Tr("user.form.name_reserved", err.(models.ErrNameReserved).Name), tpl, form)
		case models.IsErrNamePatternNotAllowed(err):
			ctx.Data["Err_UserName"] = true
			ctx.RenderWithErr(ctx.Tr("user.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), tpl, form)
		case models.IsErrNameCharsNotAllowed(err):
			ctx.Data["Err_UserName"] = true
			ctx.RenderWithErr(ctx.Tr("user.form.name_chars_not_allowed", err.(models.ErrNameCharsNotAllowed).Name), tpl, form)
		default:
			ctx.ServerError("CreateUser", err)
		}
		return
	}
	log.Trace("Account created: %s", u.Name)
	return true
}
// createUserInContext creates a user and handles errors within a given context.
// Optionally a template can be specified.
createUserInContext creates a user and handles errors within a given context.
Optionally a template can be specified.
// createUserInContext creates a user and handles errors within a given context . // Optionally a template can be specified .
func createUserInContext(ctx *context.Context, tpl base.TplName, form interface{}, u *models.User, gothUser *goth.User, allowLink bool) (ok bool) {
	if err := models.CreateUser(u); err != nil {
		if allowLink && (models.IsErrUserAlreadyExist(err) || models.IsErrEmailAlreadyUsed(err)) {
			if setting.OAuth2Client.AccountLinking == setting.OAuth2AccountLinkingAuto {
				var user *models.User
				user = &models.User{Name: u.Name}
				hasUser, err := models.GetUser(user)
				if !hasUser || err != nil {
					user = &models.User{Email: u.Email}
					hasUser, err = models.GetUser(user)
					if !hasUser || err != nil {
						ctx.ServerError("UserLinkAccount", err)
						return
					}
				}
				linkAccount(ctx, user, *gothUser, true)
				return 
			} else if setting.OAuth2Client.AccountLinking == setting.OAuth2AccountLinkingLogin {
				showLinkingLogin(ctx, *gothUser)
				return 
			}
		}
		if len(tpl) == 0 {
			ctx.ServerError("CreateUser", err)
			return
		}
		switch {
		case models.IsErrUserAlreadyExist(err):
			ctx.Data["Err_UserName"] = true
			ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), tpl, form)
		case models.IsErrEmailAlreadyUsed(err):
			ctx.Data["Err_Email"] = true
			ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tpl, form)
		case models.IsErrEmailInvalid(err):
			ctx.Data["Err_Email"] = true
			ctx.RenderWithErr(ctx.Tr("form.email_invalid"), tpl, form)
		case models.IsErrNameReserved(err):
			ctx.Data["Err_UserName"] = true
			ctx.RenderWithErr(ctx.Tr("user.form.name_reserved", err.(models.ErrNameReserved).Name), tpl, form)
		case models.IsErrNamePatternNotAllowed(err):
			ctx.Data["Err_UserName"] = true
			ctx.RenderWithErr(ctx.Tr("user.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), tpl, form)
		case models.IsErrNameCharsNotAllowed(err):
			ctx.Data["Err_UserName"] = true
			ctx.RenderWithErr(ctx.Tr("user.form.name_chars_not_allowed", err.(models.ErrNameCharsNotAllowed).Name), tpl, form)
		default:
			ctx.ServerError("CreateUser", err)
		}
		return
	}
	log.Trace("Account created: %s", u.Name)
	return true
}
func createUserInContext ( ctx * context . Context , tpl base . TplName , form interface { } , u * models . User , gothUser * goth . User , allowLink bool ) ( ok bool ) { if err := models . CreateUser ( u ) ; err != nil { if allowLink && ( models . IsErrUserAlreadyExist ( err ) || models . IsErrEmailAlreadyUsed ( err ) ) { if setting . OAuth2Client . AccountLinking == setting . OAuth2AccountLinkingAuto { var user * models . User user = & models . User { Name : u . Name } hasUser , err := models . GetUser ( user ) if ! hasUser || err != nil { user = & models . User { Email : u . Email } hasUser , err = models . GetUser ( user ) if ! hasUser || err != nil { ctx . ServerError ( " " , err ) return } } linkAccount ( ctx , user , * gothUser , true ) return } else if setting . OAuth2Client . AccountLinking == setting . OAuth2AccountLinkingLogin { showLinkingLogin ( ctx , * gothUser ) return } } if len ( tpl ) == 0 { ctx . ServerError ( " " , err ) return } switch { case models . IsErrUserAlreadyExist ( err ) : ctx . Data [ " " ] = true ctx . RenderWithErr ( ctx . Tr ( " " ) , tpl , form ) case models . IsErrEmailAlreadyUsed ( err ) : ctx . Data [ " " ] = true ctx . RenderWithErr ( ctx . Tr ( " " ) , tpl , form ) case models . IsErrEmailInvalid ( err ) : ctx . Data [ " " ] = true ctx . RenderWithErr ( ctx . Tr ( " " ) , tpl , form ) case models . IsErrNameReserved ( err ) : ctx . Data [ " " ] = true ctx . RenderWithErr ( ctx . Tr ( " " , err . ( models . ErrNameReserved ) . Name ) , tpl , form ) case models . IsErrNamePatternNotAllowed ( err ) : ctx . Data [ " " ] = true ctx . RenderWithErr ( ctx . Tr ( " " , err . ( models . ErrNamePatternNotAllowed ) . Pattern ) , tpl , form ) case models . IsErrNameCharsNotAllowed ( err ) : ctx . Data [ " " ] = true ctx . RenderWithErr ( ctx . Tr ( " " , err . ( models . ErrNameCharsNotAllowed ) . Name ) , tpl , form ) default : ctx . ServerError ( " " , err ) } return } log . Trace ( " " , u . Name ) return true }
createUserInContext creates a user and handles errors within a given context.
createUserInContext creates a user and handles errors within a given context .
// TODO: probably we should respect 'remember' user's choice... // user is already created here, all redirects are handled // user will be created only after linking login // handle error without template // handle error with template