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
account := store.Account{
Username: username,
Handle: strings.ToLower(username),
Password: password,
Guid: fingerprint,
}

View File

@ -41,17 +41,27 @@ func GetAccountUsername(w http.ResponseWriter, r *http.Request) {
}
var accounts []accountUsername;
err := store.DB.Model(&store.Account{}).Where("username = ?", username).Find(&accounts).Error
if err != nil {
if err := store.DB.Model(&store.Account{}).Where("username = ?", username).Find(&accounts).Error; err != nil {
LogMsg("failed to query accounts")
w.WriteHeader(http.StatusInternalServerError)
return
}
if len(accounts) == 0 {
WriteResponse(w, true)
} else {
if len(accounts) != 0 {
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 (
"errors"
"strings"
"net/http"
"gorm.io/gorm"
"databag/internal/store"
@ -26,6 +27,7 @@ func SetAccountAuthentication(w http.ResponseWriter, r *http.Request) {
}
token.Account.Username = username;
token.Account.Handle = strings.ToLower(username);
token.Account.Password = password;
err := store.DB.Transaction(func(tx *gorm.DB) error {
if res := tx.Save(token.Account).Error; res != nil {

View File

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