added handle column to provide case insensitive constraint

This commit is contained in:
Roland Osborne 2022-07-21 14:59:18 -07:00
parent 3c617c2829
commit fae657d356
4 changed files with 20 additions and 6 deletions

View File

@ -80,6 +80,7 @@ func AddAccount(w http.ResponseWriter, r *http.Request) {
// create new account // create new account
account := store.Account{ account := store.Account{
Username: username, Username: username,
Handle: strings.ToLower(username),
Password: password, Password: password,
Guid: fingerprint, Guid: fingerprint,
} }

View File

@ -41,17 +41,27 @@ func GetAccountUsername(w http.ResponseWriter, r *http.Request) {
} }
var accounts []accountUsername; var accounts []accountUsername;
err := store.DB.Model(&store.Account{}).Where("username = ?", username).Find(&accounts).Error if err := store.DB.Model(&store.Account{}).Where("username = ?", username).Find(&accounts).Error; err != nil {
if err != nil {
LogMsg("failed to query accounts") LogMsg("failed to query accounts")
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
return return
} }
if len(accounts) != 0 {
if len(accounts) == 0 {
WriteResponse(w, true)
} else {
WriteResponse(w, false) WriteResponse(w, false)
} return
}
handle := strings.ToLower(username);
if err := store.DB.Model(&store.Account{}).Where("handle = ?", handle).Find(&accounts).Error; err != nil {
LogMsg("failed to query accounts")
w.WriteHeader(http.StatusInternalServerError)
return
}
if len(accounts) != 0 {
WriteResponse(w, false)
return
}
WriteResponse(w, true)
} }

View File

@ -2,6 +2,7 @@ package databag
import ( import (
"errors" "errors"
"strings"
"net/http" "net/http"
"gorm.io/gorm" "gorm.io/gorm"
"databag/internal/store" "databag/internal/store"
@ -26,6 +27,7 @@ func SetAccountAuthentication(w http.ResponseWriter, r *http.Request) {
} }
token.Account.Username = username; token.Account.Username = username;
token.Account.Handle = strings.ToLower(username);
token.Account.Password = password; token.Account.Password = password;
err := store.DB.Transaction(func(tx *gorm.DB) error { err := store.DB.Transaction(func(tx *gorm.DB) error {
if res := tx.Save(token.Account).Error; res != nil { if res := tx.Save(token.Account).Error; res != nil {

View File

@ -57,6 +57,7 @@ type Account struct {
AccountDetailID uint `gorm:"not null"` AccountDetailID uint `gorm:"not null"`
Guid string `gorm:"not null;uniqueIndex"` Guid string `gorm:"not null;uniqueIndex"`
Username string `gorm:"not null;uniqueIndex"` Username string `gorm:"not null;uniqueIndex"`
Handle string `gorm:"uniqueIndex"`
Password []byte `gorm:"not null"` Password []byte `gorm:"not null"`
AccountRevision int64 `gorm:"not null;default:1"` AccountRevision int64 `gorm:"not null;default:1"`
ProfileRevision int64 `gorm:"not null;default:1"` ProfileRevision int64 `gorm:"not null;default:1"`