Make sure username are always lowercase

This commit is contained in:
Frédéric Guillot 2017-12-29 14:55:16 -08:00
parent 038ea790f7
commit 747e3edab3
2 changed files with 9 additions and 7 deletions

View file

@ -36,7 +36,7 @@ func (s *Storage) UserExists(username string) bool {
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:UserExists] username=%s", username)) defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:UserExists] username=%s", username))
var result int var result int
s.db.QueryRow(`SELECT count(*) as c FROM users WHERE username=$1`, username).Scan(&result) s.db.QueryRow(`SELECT count(*) as c FROM users WHERE username=LOWER($1)`, username).Scan(&result)
return result >= 1 return result >= 1
} }
@ -45,7 +45,7 @@ func (s *Storage) AnotherUserExists(userID int64, username string) bool {
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:AnotherUserExists] userID=%d, username=%s", userID, username)) defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:AnotherUserExists] userID=%d, username=%s", userID, username))
var result int var result int
s.db.QueryRow(`SELECT count(*) as c FROM users WHERE id != $1 AND username=$2`, userID, username).Scan(&result) s.db.QueryRow(`SELECT count(*) as c FROM users WHERE id != $1 AND username=LOWER($2)`, userID, username).Scan(&result)
return result >= 1 return result >= 1
} }
@ -71,11 +71,13 @@ func (s *Storage) CreateUser(user *model.User) (err error) {
query := `INSERT INTO users query := `INSERT INTO users
(username, password, is_admin, extra) (username, password, is_admin, extra)
VALUES VALUES
($1, $2, $3, $4) (LOWER($1), $2, $3, $4)
RETURNING id, language, theme, timezone, entry_direction` RETURNING id, username, is_admin, language, theme, timezone, entry_direction`
err = s.db.QueryRow(query, strings.ToLower(user.Username), password, user.IsAdmin, extra).Scan( err = s.db.QueryRow(query, user.Username, password, user.IsAdmin, extra).Scan(
&user.ID, &user.ID,
&user.Username,
&user.IsAdmin,
&user.Language, &user.Language,
&user.Theme, &user.Theme,
&user.Timezone, &user.Timezone,
@ -146,7 +148,7 @@ func (s *Storage) UpdateUser(user *model.User) error {
} }
} else { } else {
query := `UPDATE users SET query := `UPDATE users SET
username=$1, username=LOWER($1),
is_admin=$2, is_admin=$2,
theme=$3, theme=$3,
language=$4, language=$4,

View file

@ -50,7 +50,7 @@ func (s *Storage) UserSessions(userID int64) (model.UserSessions, error) {
func (s *Storage) CreateUserSession(username, userAgent, ip string) (sessionID string, err error) { func (s *Storage) CreateUserSession(username, userAgent, ip string) (sessionID string, err error) {
var userID int64 var userID int64
err = s.db.QueryRow("SELECT id FROM users WHERE username = $1", username).Scan(&userID) err = s.db.QueryRow("SELECT id FROM users WHERE username = LOWER($1)", username).Scan(&userID)
if err != nil { if err != nil {
return "", fmt.Errorf("unable to fetch UserID: %v", err) return "", fmt.Errorf("unable to fetch UserID: %v", err)
} }