Merge branch 'master' of https://github.com/ewhal/nyaa into ip-neutral-captchas
Cette révision appartient à :
révision
0d6688ad3f
5 fichiers modifiés avec 49 ajouts et 35 suppressions
|
@ -7,7 +7,7 @@ import (
|
|||
|
||||
var TemplateDir = "templates"
|
||||
|
||||
var homeTemplate, searchTemplate, faqTemplate, uploadTemplate, viewTemplate *template.Template
|
||||
var homeTemplate, searchTemplate, faqTemplate, uploadTemplate, viewTemplate, viewRegisterTemplate, viewLoginTemplate, viewRegisterSuccessTemplate *template.Template
|
||||
|
||||
type templateLoader struct {
|
||||
templ **template.Template
|
||||
|
@ -43,6 +43,21 @@ func ReloadTemplates() {
|
|||
name: "view",
|
||||
file: "view.html",
|
||||
},
|
||||
templateLoader{
|
||||
templ: &viewRegisterTemplate,
|
||||
name: "user_register",
|
||||
file: "user/register.html",
|
||||
},
|
||||
templateLoader{
|
||||
templ: &viewRegisterSuccessTemplate,
|
||||
name: "user_register_success",
|
||||
file: "user/signup_success.html",
|
||||
},
|
||||
templateLoader{
|
||||
templ: &viewLoginTemplate,
|
||||
name: "user_login",
|
||||
file: "user/login.html",
|
||||
},
|
||||
}
|
||||
for _, templ := range templs {
|
||||
t := template.Must(template.New(templ.name).Funcs(FuncMap).ParseFiles(filepath.Join(TemplateDir, "index.html"), filepath.Join(TemplateDir, templ.file)))
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
package router
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"net/http"
|
||||
|
||||
"github.com/ewhal/nyaa/service/captcha"
|
||||
"github.com/ewhal/nyaa/service/user"
|
||||
"github.com/ewhal/nyaa/service/user/form"
|
||||
"github.com/ewhal/nyaa/util/languages"
|
||||
|
@ -11,23 +11,14 @@ import (
|
|||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
var viewRegisterTemplate = template.Must(template.New("userRegister").Funcs(FuncMap).ParseFiles("templates/index.html", "templates/user/register.html"))
|
||||
var viewLoginTemplate = template.Must(template.New("userLogin").Funcs(FuncMap).ParseFiles("templates/index.html", "templates/user/login.html"))
|
||||
var viewRegisterSuccessTemplate = template.Must(template.New("userRegisterSuccess").Funcs(FuncMap).ParseFiles("templates/index.html", "templates/user/signup_success.html"))
|
||||
|
||||
//var viewTemplate = template.Must(template.New("view").Funcs(FuncMap).ParseFiles("templates/index.html", "templates/view.html"))
|
||||
//var viewTemplate = template.Must(template.New("view").Funcs(FuncMap).ParseFiles("templates/index.html", "templates/view.html"))
|
||||
|
||||
func init() {
|
||||
template.Must(viewRegisterTemplate.ParseGlob("templates/_*.html"))
|
||||
template.Must(viewLoginTemplate.ParseGlob("templates/_*.html"))
|
||||
template.Must(viewRegisterSuccessTemplate.ParseGlob("templates/_*.html"))
|
||||
}
|
||||
|
||||
// Getting View User Registration
|
||||
func UserRegisterFormHandler(w http.ResponseWriter, r *http.Request) {
|
||||
b := form.RegistrationForm{}
|
||||
modelHelper.BindValueForm(&b, r)
|
||||
b.CaptchaID = captcha.GetID(r.RemoteAddr)
|
||||
languages.SetTranslation("en-us", viewRegisterTemplate)
|
||||
htv := UserRegisterTemplateVariables{b, NewSearchForm(), Navigation{}, r.URL, mux.CurrentRoute(r)}
|
||||
err := viewRegisterTemplate.ExecuteTemplate(w, "index.html", htv)
|
||||
|
@ -58,9 +49,14 @@ func UserProfileFormHandler(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
}
|
||||
|
||||
// Post Registration controller
|
||||
// Post Registration controller, we do some check on the form here, the rest on user service
|
||||
func UserRegisterPostHandler(w http.ResponseWriter, r *http.Request) {
|
||||
// Check same Password
|
||||
if !captcha.Authenticate(captcha.Extract(r)) {
|
||||
// TODO: Prettier passing of mistyoed captcha errors
|
||||
http.Error(w, captcha.ErrInvalidCaptcha.Error(), 403)
|
||||
return
|
||||
}
|
||||
if (r.PostFormValue("password") == r.PostFormValue("password_confirm")) && (r.PostFormValue("password") != "") {
|
||||
if (form.EmailValidation(r.PostFormValue("email"))) && (form.ValidateUsername(r.PostFormValue("username"))) {
|
||||
_, err := userService.CreateUser(w, r)
|
||||
|
|
|
@ -41,52 +41,52 @@ func ValidateUsername(username string) bool {
|
|||
|
||||
// RegistrationForm is used when creating a user.
|
||||
type RegistrationForm struct {
|
||||
Username string `form:"registrationUsername" binding:"required"`
|
||||
Email string `form:"registrationEmail" binding:"required"`
|
||||
Password string `form:"registrationPassword" binding:"required"`
|
||||
CaptchaID string `form:"captchaID" binding:"required"`
|
||||
Username string `form:"registrationUsername"`
|
||||
Email string `form:"registrationEmail"`
|
||||
Password string `form:"registrationPassword"`
|
||||
CaptchaID string `form:"captchaID" inmodel:"false"`
|
||||
}
|
||||
|
||||
// RegistrationForm is used when creating a user authentication.
|
||||
type LoginForm struct {
|
||||
Email string `form:"email" binding:"required"`
|
||||
Password string `form:"password" binding:"required"`
|
||||
Email string `form:"email"`
|
||||
Password string `form:"password"`
|
||||
}
|
||||
|
||||
// UserForm is used when updating a user.
|
||||
type UserForm struct {
|
||||
Email string `form:"email" binding:"required"`
|
||||
Email string `form:"email"`
|
||||
}
|
||||
|
||||
// PasswordForm is used when updating a user password.
|
||||
type PasswordForm struct {
|
||||
CurrentPassword string `form:"currentPassword" binding:"required"`
|
||||
Password string `form:"newPassword" binding:"required"`
|
||||
CurrentPassword string `form:"currentPassword"`
|
||||
Password string `form:"newPassword"`
|
||||
}
|
||||
|
||||
// SendPasswordResetForm is used when sending a password reset token.
|
||||
type SendPasswordResetForm struct {
|
||||
Email string `form:"email" binding:"required"`
|
||||
Email string `form:"email"`
|
||||
}
|
||||
|
||||
// PasswordResetForm is used when reseting a password.
|
||||
type PasswordResetForm struct {
|
||||
PasswordResetToken string `form:"token" binding:"required"`
|
||||
Password string `form:"newPassword" binding:"required"`
|
||||
PasswordResetToken string `form:"token"`
|
||||
Password string `form:"newPassword"`
|
||||
}
|
||||
|
||||
// VerifyEmailForm is used when verifying an email.
|
||||
type VerifyEmailForm struct {
|
||||
ActivationToken string `form:"token" binding:"required"`
|
||||
ActivationToken string `form:"token"`
|
||||
}
|
||||
|
||||
// ActivateForm is used when activating user.
|
||||
type ActivateForm struct {
|
||||
Activation bool `form:"activation" binding:"required"`
|
||||
Activation bool `form:"activation"`
|
||||
}
|
||||
|
||||
// UserRoleForm is used when adding or removing a role from a user.
|
||||
type UserRoleForm struct {
|
||||
UserId int `form:"userId" binding:"required"`
|
||||
RoleId int `form:"roleId" binding:"required"`
|
||||
UserId int `form:"userId"`
|
||||
RoleId int `form:"roleId"`
|
||||
}
|
||||
|
|
|
@ -17,12 +17,15 @@ func AssignValue(model interface{}, form interface{}) {
|
|||
formElem := reflect.ValueOf(form).Elem()
|
||||
typeOfTForm := formElem.Type()
|
||||
for i := 0; i < formElem.NumField(); i++ {
|
||||
modelField := modelIndirect.FieldByName(typeOfTForm.Field(i).Name)
|
||||
if modelField.IsValid() {
|
||||
formField := formElem.Field(i)
|
||||
modelField.Set(formField)
|
||||
} else {
|
||||
log.Warnf("modelField : %s - %s", typeOfTForm.Field(i).Name, modelField)
|
||||
tag := typeOfTForm.Field(i).Tag
|
||||
if tag.Get("omit") != "false" {
|
||||
modelField := modelIndirect.FieldByName(typeOfTForm.Field(i).Name)
|
||||
if modelField.IsValid() {
|
||||
formField := formElem.Field(i)
|
||||
modelField.Set(formField)
|
||||
} else {
|
||||
log.Warnf("modelField : %s - %s", typeOfTForm.Field(i).Name, modelField)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Référencer dans un nouveau ticket