Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0

Fixing registration + Added a log on request errors

Cette révision appartient à :
akuma06 2017-07-13 18:05:53 +02:00
Parent dc6fda3333
révision 8122015a2e
8 fichiers modifiés avec 52 ajouts et 38 suppressions

Voir le fichier

@ -3,18 +3,26 @@ package controllers
import (
"net/http"
"github.com/NyaaPantsu/nyaa/utils/messages"
"github.com/NyaaPantsu/nyaa/config"
"github.com/NyaaPantsu/nyaa/utils/log"
msg "github.com/NyaaPantsu/nyaa/utils/messages"
"github.com/gin-gonic/gin"
)
func errorMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Next()
if config.Get().Environment == "DEVELOPMENT" {
messages := msg.GetMessages(c)
if messages.HasErrors() {
log.Errorf("Request has errors: %v", messages.GetAllErrors())
}
}
if c.Writer.Status() != http.StatusOK && c.Writer.Size() <= 0 {
if c.ContentType() == "application/json" {
msg := messages.GetMessages(c)
msg.AddErrorT("errors", "404_not_found")
c.JSON(c.Writer.Status(), msg.GetAllErrors())
messages := msg.GetMessages(c)
messages.AddErrorT("errors", "404_not_found")
c.JSON(c.Writer.Status(), messages.GetAllErrors())
return
}
httpError(c, c.Writer.Status())

Voir le fichier

@ -13,7 +13,7 @@
<a class="form-input up-input" href="/user/{{ UserProfile.ID}}/{{UserProfile.Username }}/apireset">Reset API key</a> <br> <br>
<label class="input-label">{{ T("email_address") }}:</label> <br>
<input class="form-input up-input" placeholder="{{ T("email_placeholder")}}" type="text" name="email" id="email" value="{{UserProfile.Email}}"> <br>
{{ yield errors(name="email")}}
{{ yield errors(name="Email")}}
<label class="input-label">{{ T("language")}}:</label> <br>
<select id="language" name="language" class="form-input up-input">
{{ userLanguage := UserProfile.Language }}
@ -21,18 +21,18 @@
<option value="{{ language.Code }}" {{ if userLanguage == language.Code || ((userLanguage == "") && (language.Code == getDefaultLanguage())) }}selected{{end}}>{{ language.Name }} {{if language.Code == getDefaultLanguage()}}({{ T("default") }}){{end}}</option>
{{ end }}
</select> <br>
{{ yield errors(name="language")}}
{{ yield errors(name="Language")}}
{{ if !User.HasAdmin()}}
<label class="input-label">{{ T("current_password")}}:</label> <br>
<input class="form-input up-input up-input" name="current_password" id="current_password" type="password"> <br>
{{ yield errors(name="current_password")}}
{{ yield errors(name="CurrentPassword")}}
{{end}}
<label class="input-label">{{ T("password")}}:</label> <br>
<input class="form-input up-input up-input" name="password" id="password" type="password"> <br>
{{ yield errors(name="password")}}
{{ yield errors(name="Password")}}
<label class="input-label">{{ T("confirm_password")}}:</label> <br>
<input class="form-input up-input up-input" name="password_confirmation" id="password_confirmation" type="password"> <br>
{{ yield errors(name="password_confirmation")}}
{{ yield errors(name="ConfirmPassword")}}
<h3>{{ T("preferences")}}</h3>
<label class="input-label" for="theme">{{ T("theme")}}</label> <br>
@ -42,14 +42,14 @@
<option value="tomorrow"{{ if Theme == "tomorrow" }} selected{{end}}>Tomorrow</option>
<option value=""{{ if Theme == "" }} selected{{end}}>{{ T("theme_none")}}</option>
</select><br>
{{ yield errors(name="theme")}}
{{ yield errors(name="Theme")}}
{{ if DefaultUserSettings("new_torrent")}}
<label class="input-label">{{ T("new_torrent_settings") }}:</label> <br>
<select id="new_torrent" name="new_torrent" class="form-input up-input">
<option value="0" {{ if !UserProfile.Settings.Get("new_torrent") }}selected{{end}}>{{ T("no")}}</option>
<option value="1" {{ if UserProfile.Settings.Get("new_torrent") }}selected{{end}}>{{ T("yes")}}</option>
</select> <br>
{{ yield errors(name="new_torrent")}}
{{ yield errors(name="NewTorrent")}}
{{end}}
{{ if DefaultUserSettings("new_torrent_email")}}
<label class="input-label">{{ T("new_torrent_email_settings") }}:</label> <br>
@ -57,7 +57,7 @@
<option value="0" {{ if !UserProfile.Settings.Get("new_torrent_email") }}selected{{end}}>{{ T("no")}}</option>
<option value="1" {{ if UserProfile.Settings.Get("new_torrent_email")}}selected{{end}}>{{ T("yes")}}</option>
</select> <br>
{{ yield errors(name="new_torrent_email")}}
{{ yield errors(name="NewTorrentEmail")}}
{{end}}
{{ if DefaultUserSettings("new_comment")}}
<label class="input-label">{{ T("new_comment_settings") }}:</label> <br>
@ -65,7 +65,7 @@
<option value="0" {{ if !UserProfile.Settings.Get("new_comment") }}selected{{end}}>{{ T("no")}}</option>
<option value="1" {{ if UserProfile.Settings.Get("new_comment") }}selected{{end}}>{{ T("yes")}}</option>
</select> <br>
{{ yield errors(name="new_comment")}}
{{ yield errors(name="NewComment")}}
{{end}}
{{ if DefaultUserSettings("new_comment_email")}}
<label class="input-label">{{ T("new_comment_email_settings") }}:</label> <br>
@ -73,7 +73,7 @@
<option value="0" {{ if !UserProfile.Settings.Get("new_comment_email") }}selected{{end}}>{{ T("no")}}</option>
<option value="1" {{ if UserProfile.Settings.Get("new_comment_email") }}selected{{end}}>{{ T("yes")}}</option>
</select> <br>
{{ yield errors(name="new_comment_email")}}
{{ yield errors(name="NewCommentEmail")}}
{{end}}
{{ if DefaultUserSettings("new_responses")}}
<label class="input-label">{{ T("new_responses_settings") }}:</label> <br>
@ -81,7 +81,7 @@
<option value="0" {{ if !UserProfile.Settings.Get("new_responses") }}selected{{end}}>{{ T("no")}}</option>
<option value="1" {{ if UserProfile.Settings.Get("new_responses")}}selected{{end}}>{{ T("yes")}}</option>
</select> <br>
{{ yield errors(name="new_responses")}}
{{ yield errors(name="NewResponses")}}
{{end}}
{{ if DefaultUserSettings("new_responses_email")}}
<label class="input-label">{{ T("new_responses_email_settings") }}:</label> <br>
@ -89,7 +89,7 @@
<option value="0" {{ if UserProfile.Settings.Get("new_responses_email") }}selected{{end}}>{{ T("no")}}</option>
<option value="1" {{ if UserProfile.Settings.Get("new_responses_email")}}selected{{end}}>{{ T("yes")}}</option>
</select> <br>
{{ yield errors(name="new_responses_email")}}
{{ yield errors(name="NewResponsesEmail")}}
{{end}}
{{ if DefaultUserSettings("new_follower")}}
<label class="input-label">{{ T("new_follower_settings") }}:</label> <br>
@ -97,7 +97,7 @@
<option value="0" {{ if UserProfile.Settings.Get("new_follower") }}selected{{end}}>{{ T("no")}}</option>
<option value="1" {{ if UserProfile.Settings.Get("new_follower")}}selected{{end}}>{{ T("yes")}}</option>
</select> <br>
{{ yield errors(name="new_follower")}}
{{ yield errors(name="NewFollower")}}
{{end}}
{{ if DefaultUserSettings("new_follower_email")}}
<label class="input-label">{{ T("new_follower_email_settings") }}:</label> <br>
@ -105,7 +105,7 @@
<option value="0" {{ if UserProfile.Settings.Get("new_follower_email") }}selected{{end}}>{{ T("no")}}</option>
<option value="1" {{ if UserProfile.Settings.Get("new_follower_email")}}selected{{end}}>{{ T("yes")}}</option>
</select> <br>
{{ yield errors(name="new_follower_email")}}
{{ yield errors(name="NewFollowerEmail")}}
{{end}}
{{ if DefaultUserSettings("followed")}}
<label class="input-label">{{ T("followed_settings") }}:</label> <br>
@ -113,7 +113,7 @@
<option value="0" {{ if UserProfile.Settings.Get("followed") }}selected{{end}}>{{ T("no")}}</option>
<option value="1" {{ if UserProfile.Settings.Get("followed")}}selected{{end}}>{{ T("yes")}}</option>
</select><br>
{{ yield errors(name="followed")}}
{{ yield errors(name="Followed")}}
{{end}}
{{ if DefaultUserSettings("followed_email")}}
<label class="input-label">{{ T("followed_email_settings") }}:</label> <br>
@ -121,13 +121,13 @@
<option value="0" {{ if UserProfile.Settings.Get("followed_email") }}selected{{end}}>{{ T("no")}}</option>
<option value="1" {{ if UserProfile.Settings.Get("followed_email")}}selected{{end}}>{{ T("yes")}}</option>
</select> <br>
{{ yield errors(name="followed_email")}}
{{ yield errors(name="FollowedEmail")}}
{{end}}
{{ if User.HasAdmin()}}
<h3>{{ T("moderation")}}</h3>
<label class="input-label">{{ T("username")}}:</label> <br>
<input class="form-input up-input" name="username" id="username" type="text" value="{{UserProfile.Username}}">
{{ yield errors(name="username")}}
{{ yield errors(name="Username")}}
<label class="input-label">{{ T("role") }}:</label>
<select id="status" name="status" class="form-input up-input">
@ -139,7 +139,7 @@
{{end}}
<option value="3" {{ if UserProfile.Status == 3 }}selected{{end}}>{{ T("scraped_user")}}</option>
</select>
{{ yield errors(name="status")}}
{{ yield errors(name="Status")}}
{{end}}
<label class="input-label"></label>
<input type="submit" class="form-input btn-green" name="save" value="{{ T("save_changes")}}">

Voir le fichier

@ -9,10 +9,10 @@
{{ yield csrf_field()}}
<h2>{{ T("sign_in_box_title")}}</h2>
<input type="text" name="username" id="username" class="form-input up-input" autofocus placeholder="{{ T("email_address_or_username")}}"><br>
{{ yield errors(name="username")}}
{{ yield errors(name="Username")}}
<input type="password" name="password" id="password" class="form-input up-input" placeholder="{{ T("password")}}"><br>
<input type="hidden" name="redirectTo" value="{{ Form.RedirectTo }}">
{{ yield errors(name="password")}}
{{ yield errors(name="Password")}}
<!-- <span class="button-checkbox">
<button type="button" class="btn hidden" data-color="info">{{ T("remember_me")}}</button>
<input type="checkbox" name="remember_me" id="remember_me" checked="checked">

Voir le fichier

@ -14,14 +14,14 @@
<input type="email" name="email" id="email" class="form-input up-input" placeholder="{{ T("email_address") }} ({{ T("optional")}})" value="{{ Form.Email }}"> <br>
{{ yield errors(name="email") }}
<input type="password" name="password" id="password" class="form-input up-input" placeholder="{{ T("password") }}" value="{{ Form.Password }}"> <br>
{{ yield errors(name="password") }}
{{ yield errors(name="Password") }}
<input type="password" name="password_confirmation" id="password_confirmation" class="form-input up-input" placeholder="{{ T("confirm_password") }}"> <br>
{{ yield errors(name="password_confirmation") }}
{{ yield errors(name="ConfirmPassword") }}
<p>
<span class="button-checkbox"><input type="checkbox" name="t_and_c" id="t_and_c" value="1"></span>
{{ T("terms_conditions_confirm")|raw }}
</p>
{{ yield errors(name="t_and_c") }}
{{ yield errors(name="TermsAndConditions") }}
<!-- -->
{{ yield captcha(captchaid=Form.CaptchaID)}}

Voir le fichier

@ -7,7 +7,7 @@ type RegistrationForm struct {
Password string `validate:"required,min=6,max=72,eqfield=ConfirmPassword" form:"password" json:"password"`
ConfirmPassword string `validate:"required" omit:"true" form:"password_confirmation" json:"password_confirmation"` // Omit when binding to user model since user model doesn't have those field
CaptchaID string `validate:"required" omit:"true" form:"captchaID" json:"captchaID"`
TermsAndConditions string `validate:"eq=true" omit:"true" form:"t_and_c" json:"t_and_c"`
TermsAndConditions string `validate:"eq=1" omit:"true" form:"t_and_c" json:"t_and_c"`
}
// LoginForm is used when a user logs in.

Voir le fichier

@ -25,7 +25,7 @@ func EmailValidation(email string) bool {
// ValidateUsername : Check if a username is valid
func ValidateUsername(username string) bool {
return validator.IsUTFLetterNumeric(username)
return validator.IsUTFLetterNumeric(username) && len(username) > 0
}
// IsAgreed : Check if terms and conditions are valid

Voir le fichier

@ -2,12 +2,13 @@ package userValidator
import (
"testing"
"github.com/NyaaPantsu/nyaa/utils/validator"
)
func TestValideUsername(t *testing.T) {
tests := [][]interface{}{
tests := []struct {
Username string
Expected bool
}{
{
"lol",
true,
@ -36,12 +37,16 @@ func TestValideUsername(t *testing.T) {
"れんちょん",
true,
},
{
"",
false,
},
}
for _, val := range tests {
testVal := validator.IsUTFLetterNumeric(val[0].(string))
if testVal != val[1] {
t.Errorf("The test returned a result %t instead of %t for %s", testVal, val[1].(bool), val[0].(string))
testVal := ValidateUsername(val.Username)
if testVal != val.Expected {
t.Errorf("The test returned a result %t instead of %t for '%s'", testVal, val.Expected, val.Username)
}
}
}

Voir le fichier

@ -6,10 +6,11 @@ import (
"time"
"unicode"
"strconv"
"github.com/NyaaPantsu/nyaa/utils/log"
msg "github.com/NyaaPantsu/nyaa/utils/messages"
"github.com/go-playground/validator"
"strconv"
)
var validate *validator.Validate
@ -27,7 +28,7 @@ func ValidateForm(form interface{}, mes *msg.Messages) {
for _, fieldError := range err.(validator.ValidationErrors) {
switch fieldError.Tag() {
case "required":
mes.AddErrorTf(fieldError.Field(), "error_field_needed", fieldError.Field(), fieldError.Param())
mes.AddErrorTf(fieldError.Field(), "error_field_needed", fieldError.Field())
case "eq":
mes.AddErrorTf(fieldError.Field(), "error_equal", fieldError.Field(), fieldError.Param())
case "ne", "nefield", "necsfield":
@ -164,4 +165,4 @@ func DefaultValidator(fl validator.FieldLevel) bool {
}
}
return true
}
}