Retroactively fix some SQL schema problems
I originally wasn't going to do passwords, but after struggling with webauthn I decided I'd just go for it. It simplifies the code a lot if I assert that I always have a password, displayname, etc.
This commit is contained in:
parent
981f043b7f
commit
4d55a391c9
12 changed files with 179 additions and 243 deletions
22
auth.go
22
auth.go
|
|
@ -9,7 +9,6 @@ import (
|
|||
"strconv"
|
||||
|
||||
"github.com/aarondl/opt/omit"
|
||||
"github.com/aarondl/opt/omitnull"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/enums"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/models"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/sql"
|
||||
|
|
@ -37,6 +36,7 @@ func addUserSession(r *http.Request, user *models.User) {
|
|||
func getAuthenticatedUser(r *http.Request) (*models.User, error) {
|
||||
//user_id := sessionManager.GetInt(r.Context(), "user_id")
|
||||
user_id_str := sessionManager.GetString(r.Context(), "user_id")
|
||||
if user_id_str != "" {
|
||||
user_id, err := strconv.Atoi(user_id_str)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to convert user_id to int: %v", err)
|
||||
|
|
@ -48,6 +48,7 @@ func getAuthenticatedUser(r *http.Request) (*models.User, error) {
|
|||
if user_id > 0 && username != "" {
|
||||
return models.FindUser(r.Context(), PGInstance.BobDB, int32(user_id))
|
||||
}
|
||||
}
|
||||
// If we can't get the user from the session try to get from auth headers
|
||||
username, password, ok := r.BasicAuth()
|
||||
if !ok {
|
||||
|
|
@ -84,9 +85,9 @@ func signupUser(username string, name string, password string) (*models.User, er
|
|||
return nil, fmt.Errorf("Cannot signup user: %v", err)
|
||||
}
|
||||
setter := models.UserSetter{
|
||||
DisplayName: omitnull.From(name),
|
||||
PasswordHash: omitnull.From(passwordHash),
|
||||
PasswordHashType: omitnull.From(enums.HashtypeBcrypt14),
|
||||
DisplayName: omit.From(name),
|
||||
PasswordHash: omit.From(passwordHash),
|
||||
PasswordHashType: omit.From(enums.HashtypeBcrypt14),
|
||||
Username: omit.From(username),
|
||||
}
|
||||
u, err := models.Users.Insert(&setter).One(context.TODO(), PGInstance.BobDB)
|
||||
|
|
@ -123,18 +124,7 @@ func validateUser(ctx context.Context, username string, password string) (*model
|
|||
return nil, InvalidUsername{}
|
||||
case 1:
|
||||
row := result[0]
|
||||
hash, err := row.PasswordHash.Value()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if hash == nil {
|
||||
return nil, errors.New("Hash is nil")
|
||||
}
|
||||
hashStr, ok := hash.(string);
|
||||
if !ok {
|
||||
return nil, errors.New("Hash isn't a string")
|
||||
}
|
||||
if !validatePassword(password, hashStr) {
|
||||
if !validatePassword(password, row.PasswordHash) {
|
||||
return nil, InvalidCredentials{}
|
||||
}
|
||||
user := models.User{
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ var Users = Table[
|
|||
},
|
||||
ArcgisLicense: column{
|
||||
Name: "arcgis_license",
|
||||
DBType: "public.arcgis_license_type",
|
||||
DBType: "public.arcgislicensetype",
|
||||
Default: "NULL",
|
||||
Comment: "",
|
||||
Nullable: true,
|
||||
|
|
@ -72,9 +72,9 @@ var Users = Table[
|
|||
DisplayName: column{
|
||||
Name: "display_name",
|
||||
DBType: "character varying",
|
||||
Default: "NULL",
|
||||
Default: "",
|
||||
Comment: "",
|
||||
Nullable: true,
|
||||
Nullable: false,
|
||||
Generated: false,
|
||||
AutoIncr: false,
|
||||
},
|
||||
|
|
@ -108,18 +108,18 @@ var Users = Table[
|
|||
PasswordHashType: column{
|
||||
Name: "password_hash_type",
|
||||
DBType: "public.hashtype",
|
||||
Default: "NULL",
|
||||
Default: "",
|
||||
Comment: "",
|
||||
Nullable: true,
|
||||
Nullable: false,
|
||||
Generated: false,
|
||||
AutoIncr: false,
|
||||
},
|
||||
PasswordHash: column{
|
||||
Name: "password_hash",
|
||||
DBType: "text",
|
||||
Default: "NULL",
|
||||
Default: "",
|
||||
Comment: "",
|
||||
Nullable: true,
|
||||
Nullable: false,
|
||||
Generated: false,
|
||||
AutoIncr: false,
|
||||
},
|
||||
|
|
|
|||
|
|
@ -8,65 +8,65 @@ import (
|
|||
"fmt"
|
||||
)
|
||||
|
||||
// Enum values for ArcgisLicenseType
|
||||
// Enum values for Arcgislicensetype
|
||||
const (
|
||||
ArcgisLicenseTypeAdvancedut ArcgisLicenseType = "advancedUT"
|
||||
ArcgisLicenseTypeBasicut ArcgisLicenseType = "basicUT"
|
||||
ArcgisLicenseTypeCreatorut ArcgisLicenseType = "creatorUT"
|
||||
ArcgisLicenseTypeEditorut ArcgisLicenseType = "editorUT"
|
||||
ArcgisLicenseTypeFieldworkerut ArcgisLicenseType = "fieldWorkerUT"
|
||||
ArcgisLicenseTypeGisprofessionaladvut ArcgisLicenseType = "GISProfessionalAdvUT"
|
||||
ArcgisLicenseTypeGisprofessionalbasicut ArcgisLicenseType = "GISProfessionalBasicUT"
|
||||
ArcgisLicenseTypeGisprofessionalstdut ArcgisLicenseType = "GISProfessionalStdUT"
|
||||
ArcgisLicenseTypeIndoorsuserut ArcgisLicenseType = "IndoorsUserUT"
|
||||
ArcgisLicenseTypeInsightsanalystut ArcgisLicenseType = "insightsAnalystUT"
|
||||
ArcgisLicenseTypeLiteut ArcgisLicenseType = "liteUT"
|
||||
ArcgisLicenseTypeStandardut ArcgisLicenseType = "standardUT"
|
||||
ArcgisLicenseTypeStorytellerut ArcgisLicenseType = "storytellerUT"
|
||||
ArcgisLicenseTypeViewerut ArcgisLicenseType = "viewerUT"
|
||||
ArcgislicensetypeAdvancedut Arcgislicensetype = "advancedUT"
|
||||
ArcgislicensetypeBasicut Arcgislicensetype = "basicUT"
|
||||
ArcgislicensetypeCreatorut Arcgislicensetype = "creatorUT"
|
||||
ArcgislicensetypeEditorut Arcgislicensetype = "editorUT"
|
||||
ArcgislicensetypeFieldworkerut Arcgislicensetype = "fieldWorkerUT"
|
||||
ArcgislicensetypeGisprofessionaladvut Arcgislicensetype = "GISProfessionalAdvUT"
|
||||
ArcgislicensetypeGisprofessionalbasicut Arcgislicensetype = "GISProfessionalBasicUT"
|
||||
ArcgislicensetypeGisprofessionalstdut Arcgislicensetype = "GISProfessionalStdUT"
|
||||
ArcgislicensetypeIndoorsuserut Arcgislicensetype = "IndoorsUserUT"
|
||||
ArcgislicensetypeInsightsanalystut Arcgislicensetype = "insightsAnalystUT"
|
||||
ArcgislicensetypeLiteut Arcgislicensetype = "liteUT"
|
||||
ArcgislicensetypeStandardut Arcgislicensetype = "standardUT"
|
||||
ArcgislicensetypeStorytellerut Arcgislicensetype = "storytellerUT"
|
||||
ArcgislicensetypeViewerut Arcgislicensetype = "viewerUT"
|
||||
)
|
||||
|
||||
func AllArcgisLicenseType() []ArcgisLicenseType {
|
||||
return []ArcgisLicenseType{
|
||||
ArcgisLicenseTypeAdvancedut,
|
||||
ArcgisLicenseTypeBasicut,
|
||||
ArcgisLicenseTypeCreatorut,
|
||||
ArcgisLicenseTypeEditorut,
|
||||
ArcgisLicenseTypeFieldworkerut,
|
||||
ArcgisLicenseTypeGisprofessionaladvut,
|
||||
ArcgisLicenseTypeGisprofessionalbasicut,
|
||||
ArcgisLicenseTypeGisprofessionalstdut,
|
||||
ArcgisLicenseTypeIndoorsuserut,
|
||||
ArcgisLicenseTypeInsightsanalystut,
|
||||
ArcgisLicenseTypeLiteut,
|
||||
ArcgisLicenseTypeStandardut,
|
||||
ArcgisLicenseTypeStorytellerut,
|
||||
ArcgisLicenseTypeViewerut,
|
||||
func AllArcgislicensetype() []Arcgislicensetype {
|
||||
return []Arcgislicensetype{
|
||||
ArcgislicensetypeAdvancedut,
|
||||
ArcgislicensetypeBasicut,
|
||||
ArcgislicensetypeCreatorut,
|
||||
ArcgislicensetypeEditorut,
|
||||
ArcgislicensetypeFieldworkerut,
|
||||
ArcgislicensetypeGisprofessionaladvut,
|
||||
ArcgislicensetypeGisprofessionalbasicut,
|
||||
ArcgislicensetypeGisprofessionalstdut,
|
||||
ArcgislicensetypeIndoorsuserut,
|
||||
ArcgislicensetypeInsightsanalystut,
|
||||
ArcgislicensetypeLiteut,
|
||||
ArcgislicensetypeStandardut,
|
||||
ArcgislicensetypeStorytellerut,
|
||||
ArcgislicensetypeViewerut,
|
||||
}
|
||||
}
|
||||
|
||||
type ArcgisLicenseType string
|
||||
type Arcgislicensetype string
|
||||
|
||||
func (e ArcgisLicenseType) String() string {
|
||||
func (e Arcgislicensetype) String() string {
|
||||
return string(e)
|
||||
}
|
||||
|
||||
func (e ArcgisLicenseType) Valid() bool {
|
||||
func (e Arcgislicensetype) Valid() bool {
|
||||
switch e {
|
||||
case ArcgisLicenseTypeAdvancedut,
|
||||
ArcgisLicenseTypeBasicut,
|
||||
ArcgisLicenseTypeCreatorut,
|
||||
ArcgisLicenseTypeEditorut,
|
||||
ArcgisLicenseTypeFieldworkerut,
|
||||
ArcgisLicenseTypeGisprofessionaladvut,
|
||||
ArcgisLicenseTypeGisprofessionalbasicut,
|
||||
ArcgisLicenseTypeGisprofessionalstdut,
|
||||
ArcgisLicenseTypeIndoorsuserut,
|
||||
ArcgisLicenseTypeInsightsanalystut,
|
||||
ArcgisLicenseTypeLiteut,
|
||||
ArcgisLicenseTypeStandardut,
|
||||
ArcgisLicenseTypeStorytellerut,
|
||||
ArcgisLicenseTypeViewerut:
|
||||
case ArcgislicensetypeAdvancedut,
|
||||
ArcgislicensetypeBasicut,
|
||||
ArcgislicensetypeCreatorut,
|
||||
ArcgislicensetypeEditorut,
|
||||
ArcgislicensetypeFieldworkerut,
|
||||
ArcgislicensetypeGisprofessionaladvut,
|
||||
ArcgislicensetypeGisprofessionalbasicut,
|
||||
ArcgislicensetypeGisprofessionalstdut,
|
||||
ArcgislicensetypeIndoorsuserut,
|
||||
ArcgislicensetypeInsightsanalystut,
|
||||
ArcgislicensetypeLiteut,
|
||||
ArcgislicensetypeStandardut,
|
||||
ArcgislicensetypeStorytellerut,
|
||||
ArcgislicensetypeViewerut:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
|
|
@ -74,44 +74,44 @@ func (e ArcgisLicenseType) Valid() bool {
|
|||
}
|
||||
|
||||
// useful when testing in other packages
|
||||
func (e ArcgisLicenseType) All() []ArcgisLicenseType {
|
||||
return AllArcgisLicenseType()
|
||||
func (e Arcgislicensetype) All() []Arcgislicensetype {
|
||||
return AllArcgislicensetype()
|
||||
}
|
||||
|
||||
func (e ArcgisLicenseType) MarshalText() ([]byte, error) {
|
||||
func (e Arcgislicensetype) MarshalText() ([]byte, error) {
|
||||
return []byte(e), nil
|
||||
}
|
||||
|
||||
func (e *ArcgisLicenseType) UnmarshalText(text []byte) error {
|
||||
func (e *Arcgislicensetype) UnmarshalText(text []byte) error {
|
||||
return e.Scan(text)
|
||||
}
|
||||
|
||||
func (e ArcgisLicenseType) MarshalBinary() ([]byte, error) {
|
||||
func (e Arcgislicensetype) MarshalBinary() ([]byte, error) {
|
||||
return []byte(e), nil
|
||||
}
|
||||
|
||||
func (e *ArcgisLicenseType) UnmarshalBinary(data []byte) error {
|
||||
func (e *Arcgislicensetype) UnmarshalBinary(data []byte) error {
|
||||
return e.Scan(data)
|
||||
}
|
||||
|
||||
func (e ArcgisLicenseType) Value() (driver.Value, error) {
|
||||
func (e Arcgislicensetype) Value() (driver.Value, error) {
|
||||
return string(e), nil
|
||||
}
|
||||
|
||||
func (e *ArcgisLicenseType) Scan(value any) error {
|
||||
func (e *Arcgislicensetype) Scan(value any) error {
|
||||
switch x := value.(type) {
|
||||
case string:
|
||||
*e = ArcgisLicenseType(x)
|
||||
*e = Arcgislicensetype(x)
|
||||
case []byte:
|
||||
*e = ArcgisLicenseType(x)
|
||||
*e = Arcgislicensetype(x)
|
||||
case nil:
|
||||
return fmt.Errorf("cannot nil into ArcgisLicenseType")
|
||||
return fmt.Errorf("cannot nil into Arcgislicensetype")
|
||||
default:
|
||||
return fmt.Errorf("cannot scan type %T: %v", value, value)
|
||||
}
|
||||
|
||||
if !e.Valid() {
|
||||
return fmt.Errorf("invalid ArcgisLicenseType value: %s", *e)
|
||||
return fmt.Errorf("invalid Arcgislicensetype value: %s", *e)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
|||
|
|
@ -127,16 +127,16 @@ func (f *Factory) FromExistingUser(m *models.User) *UserTemplate {
|
|||
|
||||
o.ID = func() int32 { return m.ID }
|
||||
o.ArcgisAccessToken = func() null.Val[string] { return m.ArcgisAccessToken }
|
||||
o.ArcgisLicense = func() null.Val[enums.ArcgisLicenseType] { return m.ArcgisLicense }
|
||||
o.ArcgisLicense = func() null.Val[enums.Arcgislicensetype] { return m.ArcgisLicense }
|
||||
o.ArcgisRefreshToken = func() null.Val[string] { return m.ArcgisRefreshToken }
|
||||
o.ArcgisRefreshTokenExpires = func() null.Val[time.Time] { return m.ArcgisRefreshTokenExpires }
|
||||
o.ArcgisRole = func() null.Val[string] { return m.ArcgisRole }
|
||||
o.DisplayName = func() null.Val[string] { return m.DisplayName }
|
||||
o.DisplayName = func() string { return m.DisplayName }
|
||||
o.Email = func() null.Val[string] { return m.Email }
|
||||
o.OrganizationID = func() null.Val[int32] { return m.OrganizationID }
|
||||
o.Username = func() string { return m.Username }
|
||||
o.PasswordHashType = func() null.Val[enums.Hashtype] { return m.PasswordHashType }
|
||||
o.PasswordHash = func() null.Val[string] { return m.PasswordHash }
|
||||
o.PasswordHashType = func() enums.Hashtype { return m.PasswordHashType }
|
||||
o.PasswordHash = func() string { return m.PasswordHash }
|
||||
|
||||
ctx := context.Background()
|
||||
if m.R.Organization != nil {
|
||||
|
|
|
|||
|
|
@ -30,12 +30,12 @@ func random_bool(f *faker.Faker, limits ...string) bool {
|
|||
return f.Bool()
|
||||
}
|
||||
|
||||
func random_enums_ArcgisLicenseType(f *faker.Faker, limits ...string) enums.ArcgisLicenseType {
|
||||
func random_enums_Arcgislicensetype(f *faker.Faker, limits ...string) enums.Arcgislicensetype {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
var e enums.ArcgisLicenseType
|
||||
var e enums.Arcgislicensetype
|
||||
all := e.All()
|
||||
return all[f.IntBetween(0, len(all)-1)]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -40,16 +40,16 @@ func (mods UserModSlice) Apply(ctx context.Context, n *UserTemplate) {
|
|||
type UserTemplate struct {
|
||||
ID func() int32
|
||||
ArcgisAccessToken func() null.Val[string]
|
||||
ArcgisLicense func() null.Val[enums.ArcgisLicenseType]
|
||||
ArcgisLicense func() null.Val[enums.Arcgislicensetype]
|
||||
ArcgisRefreshToken func() null.Val[string]
|
||||
ArcgisRefreshTokenExpires func() null.Val[time.Time]
|
||||
ArcgisRole func() null.Val[string]
|
||||
DisplayName func() null.Val[string]
|
||||
DisplayName func() string
|
||||
Email func() null.Val[string]
|
||||
OrganizationID func() null.Val[int32]
|
||||
Username func() string
|
||||
PasswordHashType func() null.Val[enums.Hashtype]
|
||||
PasswordHash func() null.Val[string]
|
||||
PasswordHashType func() enums.Hashtype
|
||||
PasswordHash func() string
|
||||
|
||||
r userR
|
||||
f *Factory
|
||||
|
|
@ -114,7 +114,7 @@ func (o UserTemplate) BuildSetter() *models.UserSetter {
|
|||
}
|
||||
if o.DisplayName != nil {
|
||||
val := o.DisplayName()
|
||||
m.DisplayName = omitnull.FromNull(val)
|
||||
m.DisplayName = omit.From(val)
|
||||
}
|
||||
if o.Email != nil {
|
||||
val := o.Email()
|
||||
|
|
@ -130,11 +130,11 @@ func (o UserTemplate) BuildSetter() *models.UserSetter {
|
|||
}
|
||||
if o.PasswordHashType != nil {
|
||||
val := o.PasswordHashType()
|
||||
m.PasswordHashType = omitnull.FromNull(val)
|
||||
m.PasswordHashType = omit.From(val)
|
||||
}
|
||||
if o.PasswordHash != nil {
|
||||
val := o.PasswordHash()
|
||||
m.PasswordHash = omitnull.FromNull(val)
|
||||
m.PasswordHash = omit.From(val)
|
||||
}
|
||||
|
||||
return m
|
||||
|
|
@ -214,10 +214,22 @@ func (o UserTemplate) BuildMany(number int) models.UserSlice {
|
|||
}
|
||||
|
||||
func ensureCreatableUser(m *models.UserSetter) {
|
||||
if !(m.DisplayName.IsValue()) {
|
||||
val := random_string(nil, "200")
|
||||
m.DisplayName = omit.From(val)
|
||||
}
|
||||
if !(m.Username.IsValue()) {
|
||||
val := random_string(nil)
|
||||
m.Username = omit.From(val)
|
||||
}
|
||||
if !(m.PasswordHashType.IsValue()) {
|
||||
val := random_enums_Hashtype(nil)
|
||||
m.PasswordHashType = omit.From(val)
|
||||
}
|
||||
if !(m.PasswordHash.IsValue()) {
|
||||
val := random_string(nil)
|
||||
m.PasswordHash = omit.From(val)
|
||||
}
|
||||
}
|
||||
|
||||
// insertOptRels creates and inserts any optional the relationships on *models.User
|
||||
|
|
@ -437,14 +449,14 @@ func (m userMods) RandomArcgisAccessTokenNotNull(f *faker.Faker) UserMod {
|
|||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m userMods) ArcgisLicense(val null.Val[enums.ArcgisLicenseType]) UserMod {
|
||||
func (m userMods) ArcgisLicense(val null.Val[enums.Arcgislicensetype]) UserMod {
|
||||
return UserModFunc(func(_ context.Context, o *UserTemplate) {
|
||||
o.ArcgisLicense = func() null.Val[enums.ArcgisLicenseType] { return val }
|
||||
o.ArcgisLicense = func() null.Val[enums.Arcgislicensetype] { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m userMods) ArcgisLicenseFunc(f func() null.Val[enums.ArcgisLicenseType]) UserMod {
|
||||
func (m userMods) ArcgisLicenseFunc(f func() null.Val[enums.Arcgislicensetype]) UserMod {
|
||||
return UserModFunc(func(_ context.Context, o *UserTemplate) {
|
||||
o.ArcgisLicense = f
|
||||
})
|
||||
|
|
@ -462,12 +474,12 @@ func (m userMods) UnsetArcgisLicense() UserMod {
|
|||
// The generated value is sometimes null
|
||||
func (m userMods) RandomArcgisLicense(f *faker.Faker) UserMod {
|
||||
return UserModFunc(func(_ context.Context, o *UserTemplate) {
|
||||
o.ArcgisLicense = func() null.Val[enums.ArcgisLicenseType] {
|
||||
o.ArcgisLicense = func() null.Val[enums.Arcgislicensetype] {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
val := random_enums_ArcgisLicenseType(f)
|
||||
val := random_enums_Arcgislicensetype(f)
|
||||
return null.From(val)
|
||||
}
|
||||
})
|
||||
|
|
@ -478,12 +490,12 @@ func (m userMods) RandomArcgisLicense(f *faker.Faker) UserMod {
|
|||
// The generated value is never null
|
||||
func (m userMods) RandomArcgisLicenseNotNull(f *faker.Faker) UserMod {
|
||||
return UserModFunc(func(_ context.Context, o *UserTemplate) {
|
||||
o.ArcgisLicense = func() null.Val[enums.ArcgisLicenseType] {
|
||||
o.ArcgisLicense = func() null.Val[enums.Arcgislicensetype] {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
val := random_enums_ArcgisLicenseType(f)
|
||||
val := random_enums_Arcgislicensetype(f)
|
||||
return null.From(val)
|
||||
}
|
||||
})
|
||||
|
|
@ -649,14 +661,14 @@ func (m userMods) RandomArcgisRoleNotNull(f *faker.Faker) UserMod {
|
|||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m userMods) DisplayName(val null.Val[string]) UserMod {
|
||||
func (m userMods) DisplayName(val string) UserMod {
|
||||
return UserModFunc(func(_ context.Context, o *UserTemplate) {
|
||||
o.DisplayName = func() null.Val[string] { return val }
|
||||
o.DisplayName = func() string { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m userMods) DisplayNameFunc(f func() null.Val[string]) UserMod {
|
||||
func (m userMods) DisplayNameFunc(f func() string) UserMod {
|
||||
return UserModFunc(func(_ context.Context, o *UserTemplate) {
|
||||
o.DisplayName = f
|
||||
})
|
||||
|
|
@ -671,32 +683,10 @@ func (m userMods) UnsetDisplayName() UserMod {
|
|||
|
||||
// Generates a random value for the column using the given faker
|
||||
// if faker is nil, a default faker is used
|
||||
// The generated value is sometimes null
|
||||
func (m userMods) RandomDisplayName(f *faker.Faker) UserMod {
|
||||
return UserModFunc(func(_ context.Context, o *UserTemplate) {
|
||||
o.DisplayName = func() null.Val[string] {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
val := random_string(f, "200")
|
||||
return null.From(val)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Generates a random value for the column using the given faker
|
||||
// if faker is nil, a default faker is used
|
||||
// The generated value is never null
|
||||
func (m userMods) RandomDisplayNameNotNull(f *faker.Faker) UserMod {
|
||||
return UserModFunc(func(_ context.Context, o *UserTemplate) {
|
||||
o.DisplayName = func() null.Val[string] {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
val := random_string(f, "200")
|
||||
return null.From(val)
|
||||
o.DisplayName = func() string {
|
||||
return random_string(f, "200")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
@ -839,14 +829,14 @@ func (m userMods) RandomUsername(f *faker.Faker) UserMod {
|
|||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m userMods) PasswordHashType(val null.Val[enums.Hashtype]) UserMod {
|
||||
func (m userMods) PasswordHashType(val enums.Hashtype) UserMod {
|
||||
return UserModFunc(func(_ context.Context, o *UserTemplate) {
|
||||
o.PasswordHashType = func() null.Val[enums.Hashtype] { return val }
|
||||
o.PasswordHashType = func() enums.Hashtype { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m userMods) PasswordHashTypeFunc(f func() null.Val[enums.Hashtype]) UserMod {
|
||||
func (m userMods) PasswordHashTypeFunc(f func() enums.Hashtype) UserMod {
|
||||
return UserModFunc(func(_ context.Context, o *UserTemplate) {
|
||||
o.PasswordHashType = f
|
||||
})
|
||||
|
|
@ -861,45 +851,23 @@ func (m userMods) UnsetPasswordHashType() UserMod {
|
|||
|
||||
// Generates a random value for the column using the given faker
|
||||
// if faker is nil, a default faker is used
|
||||
// The generated value is sometimes null
|
||||
func (m userMods) RandomPasswordHashType(f *faker.Faker) UserMod {
|
||||
return UserModFunc(func(_ context.Context, o *UserTemplate) {
|
||||
o.PasswordHashType = func() null.Val[enums.Hashtype] {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
val := random_enums_Hashtype(f)
|
||||
return null.From(val)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Generates a random value for the column using the given faker
|
||||
// if faker is nil, a default faker is used
|
||||
// The generated value is never null
|
||||
func (m userMods) RandomPasswordHashTypeNotNull(f *faker.Faker) UserMod {
|
||||
return UserModFunc(func(_ context.Context, o *UserTemplate) {
|
||||
o.PasswordHashType = func() null.Val[enums.Hashtype] {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
val := random_enums_Hashtype(f)
|
||||
return null.From(val)
|
||||
o.PasswordHashType = func() enums.Hashtype {
|
||||
return random_enums_Hashtype(f)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m userMods) PasswordHash(val null.Val[string]) UserMod {
|
||||
func (m userMods) PasswordHash(val string) UserMod {
|
||||
return UserModFunc(func(_ context.Context, o *UserTemplate) {
|
||||
o.PasswordHash = func() null.Val[string] { return val }
|
||||
o.PasswordHash = func() string { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m userMods) PasswordHashFunc(f func() null.Val[string]) UserMod {
|
||||
func (m userMods) PasswordHashFunc(f func() string) UserMod {
|
||||
return UserModFunc(func(_ context.Context, o *UserTemplate) {
|
||||
o.PasswordHash = f
|
||||
})
|
||||
|
|
@ -914,32 +882,10 @@ func (m userMods) UnsetPasswordHash() UserMod {
|
|||
|
||||
// Generates a random value for the column using the given faker
|
||||
// if faker is nil, a default faker is used
|
||||
// The generated value is sometimes null
|
||||
func (m userMods) RandomPasswordHash(f *faker.Faker) UserMod {
|
||||
return UserModFunc(func(_ context.Context, o *UserTemplate) {
|
||||
o.PasswordHash = func() null.Val[string] {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
val := random_string(f)
|
||||
return null.From(val)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Generates a random value for the column using the given faker
|
||||
// if faker is nil, a default faker is used
|
||||
// The generated value is never null
|
||||
func (m userMods) RandomPasswordHashNotNull(f *faker.Faker) UserMod {
|
||||
return UserModFunc(func(_ context.Context, o *UserTemplate) {
|
||||
o.PasswordHash = func() null.Val[string] {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
val := random_string(f)
|
||||
return null.From(val)
|
||||
o.PasswordHash = func() string {
|
||||
return random_string(f)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
-- +goose Up
|
||||
CREATE TYPE arcgis_license_type AS ENUM (
|
||||
CREATE TYPE ArcgisLicenseType AS ENUM (
|
||||
'advancedUT',
|
||||
'basicUT',
|
||||
'creatorUT',
|
||||
|
|
@ -23,11 +23,11 @@ CREATE TABLE organization (
|
|||
CREATE TABLE user_ (
|
||||
id SERIAL PRIMARY KEY,
|
||||
arcgis_access_token TEXT,
|
||||
arcgis_license arcgis_license_type,
|
||||
arcgis_license ArcgisLicenseType,
|
||||
arcgis_refresh_token TEXT,
|
||||
arcgis_refresh_token_expires TIMESTAMP,
|
||||
arcgis_role TEXT,
|
||||
display_name VARCHAR(200),
|
||||
display_name VARCHAR(200) NOT NULL,
|
||||
email TEXT,
|
||||
organization_id INTEGER REFERENCES organization (id),
|
||||
username TEXT NOT NULL
|
||||
|
|
|
|||
|
|
@ -2,8 +2,8 @@
|
|||
CREATE TYPE HashType AS ENUM (
|
||||
'bcrypt-14');
|
||||
|
||||
ALTER TABLE user_ ADD COLUMN password_hash_type HashType;
|
||||
ALTER TABLE user_ ADD COLUMN password_hash TEXT;
|
||||
ALTER TABLE user_ ADD COLUMN password_hash_type HashType NOT NULL;
|
||||
ALTER TABLE user_ ADD COLUMN password_hash TEXT NOT NULL;
|
||||
-- +goose Down
|
||||
ALTER TABLE user_ DROP COLUMN password_hash;
|
||||
ALTER TABLE user_ DROP COLUMN password_hash_type;
|
||||
|
|
|
|||
|
|
@ -26,11 +26,11 @@ var _ bob.HookableType = &Session{}
|
|||
// Make sure the type User runs hooks after queries
|
||||
var _ bob.HookableType = &User{}
|
||||
|
||||
// Make sure the type enums.ArcgisLicenseType satisfies database/sql.Scanner
|
||||
var _ sql.Scanner = (*enums.ArcgisLicenseType)(nil)
|
||||
// Make sure the type enums.Arcgislicensetype satisfies database/sql.Scanner
|
||||
var _ sql.Scanner = (*enums.Arcgislicensetype)(nil)
|
||||
|
||||
// Make sure the type enums.ArcgisLicenseType satisfies database/sql/driver.Valuer
|
||||
var _ driver.Valuer = *new(enums.ArcgisLicenseType)
|
||||
// Make sure the type enums.Arcgislicensetype satisfies database/sql/driver.Valuer
|
||||
var _ driver.Valuer = *new(enums.Arcgislicensetype)
|
||||
|
||||
// Make sure the type enums.Hashtype satisfies database/sql.Scanner
|
||||
var _ sql.Scanner = (*enums.Hashtype)(nil)
|
||||
|
|
|
|||
|
|
@ -29,16 +29,16 @@ import (
|
|||
type User struct {
|
||||
ID int32 `db:"id,pk" `
|
||||
ArcgisAccessToken null.Val[string] `db:"arcgis_access_token" `
|
||||
ArcgisLicense null.Val[enums.ArcgisLicenseType] `db:"arcgis_license" `
|
||||
ArcgisLicense null.Val[enums.Arcgislicensetype] `db:"arcgis_license" `
|
||||
ArcgisRefreshToken null.Val[string] `db:"arcgis_refresh_token" `
|
||||
ArcgisRefreshTokenExpires null.Val[time.Time] `db:"arcgis_refresh_token_expires" `
|
||||
ArcgisRole null.Val[string] `db:"arcgis_role" `
|
||||
DisplayName null.Val[string] `db:"display_name" `
|
||||
DisplayName string `db:"display_name" `
|
||||
Email null.Val[string] `db:"email" `
|
||||
OrganizationID null.Val[int32] `db:"organization_id" `
|
||||
Username string `db:"username" `
|
||||
PasswordHashType null.Val[enums.Hashtype] `db:"password_hash_type" `
|
||||
PasswordHash null.Val[string] `db:"password_hash" `
|
||||
PasswordHashType enums.Hashtype `db:"password_hash_type" `
|
||||
PasswordHash string `db:"password_hash" `
|
||||
|
||||
R userR `db:"-" `
|
||||
}
|
||||
|
|
@ -110,16 +110,16 @@ func (userColumns) AliasedAs(alias string) userColumns {
|
|||
type UserSetter struct {
|
||||
ID omit.Val[int32] `db:"id,pk" `
|
||||
ArcgisAccessToken omitnull.Val[string] `db:"arcgis_access_token" `
|
||||
ArcgisLicense omitnull.Val[enums.ArcgisLicenseType] `db:"arcgis_license" `
|
||||
ArcgisLicense omitnull.Val[enums.Arcgislicensetype] `db:"arcgis_license" `
|
||||
ArcgisRefreshToken omitnull.Val[string] `db:"arcgis_refresh_token" `
|
||||
ArcgisRefreshTokenExpires omitnull.Val[time.Time] `db:"arcgis_refresh_token_expires" `
|
||||
ArcgisRole omitnull.Val[string] `db:"arcgis_role" `
|
||||
DisplayName omitnull.Val[string] `db:"display_name" `
|
||||
DisplayName omit.Val[string] `db:"display_name" `
|
||||
Email omitnull.Val[string] `db:"email" `
|
||||
OrganizationID omitnull.Val[int32] `db:"organization_id" `
|
||||
Username omit.Val[string] `db:"username" `
|
||||
PasswordHashType omitnull.Val[enums.Hashtype] `db:"password_hash_type" `
|
||||
PasswordHash omitnull.Val[string] `db:"password_hash" `
|
||||
PasswordHashType omit.Val[enums.Hashtype] `db:"password_hash_type" `
|
||||
PasswordHash omit.Val[string] `db:"password_hash" `
|
||||
}
|
||||
|
||||
func (s UserSetter) SetColumns() []string {
|
||||
|
|
@ -142,7 +142,7 @@ func (s UserSetter) SetColumns() []string {
|
|||
if !s.ArcgisRole.IsUnset() {
|
||||
vals = append(vals, "arcgis_role")
|
||||
}
|
||||
if !s.DisplayName.IsUnset() {
|
||||
if s.DisplayName.IsValue() {
|
||||
vals = append(vals, "display_name")
|
||||
}
|
||||
if !s.Email.IsUnset() {
|
||||
|
|
@ -154,10 +154,10 @@ func (s UserSetter) SetColumns() []string {
|
|||
if s.Username.IsValue() {
|
||||
vals = append(vals, "username")
|
||||
}
|
||||
if !s.PasswordHashType.IsUnset() {
|
||||
if s.PasswordHashType.IsValue() {
|
||||
vals = append(vals, "password_hash_type")
|
||||
}
|
||||
if !s.PasswordHash.IsUnset() {
|
||||
if s.PasswordHash.IsValue() {
|
||||
vals = append(vals, "password_hash")
|
||||
}
|
||||
return vals
|
||||
|
|
@ -182,8 +182,8 @@ func (s UserSetter) Overwrite(t *User) {
|
|||
if !s.ArcgisRole.IsUnset() {
|
||||
t.ArcgisRole = s.ArcgisRole.MustGetNull()
|
||||
}
|
||||
if !s.DisplayName.IsUnset() {
|
||||
t.DisplayName = s.DisplayName.MustGetNull()
|
||||
if s.DisplayName.IsValue() {
|
||||
t.DisplayName = s.DisplayName.MustGet()
|
||||
}
|
||||
if !s.Email.IsUnset() {
|
||||
t.Email = s.Email.MustGetNull()
|
||||
|
|
@ -194,11 +194,11 @@ func (s UserSetter) Overwrite(t *User) {
|
|||
if s.Username.IsValue() {
|
||||
t.Username = s.Username.MustGet()
|
||||
}
|
||||
if !s.PasswordHashType.IsUnset() {
|
||||
t.PasswordHashType = s.PasswordHashType.MustGetNull()
|
||||
if s.PasswordHashType.IsValue() {
|
||||
t.PasswordHashType = s.PasswordHashType.MustGet()
|
||||
}
|
||||
if !s.PasswordHash.IsUnset() {
|
||||
t.PasswordHash = s.PasswordHash.MustGetNull()
|
||||
if s.PasswordHash.IsValue() {
|
||||
t.PasswordHash = s.PasswordHash.MustGet()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -245,8 +245,8 @@ func (s *UserSetter) Apply(q *dialect.InsertQuery) {
|
|||
vals[5] = psql.Raw("DEFAULT")
|
||||
}
|
||||
|
||||
if !s.DisplayName.IsUnset() {
|
||||
vals[6] = psql.Arg(s.DisplayName.MustGetNull())
|
||||
if s.DisplayName.IsValue() {
|
||||
vals[6] = psql.Arg(s.DisplayName.MustGet())
|
||||
} else {
|
||||
vals[6] = psql.Raw("DEFAULT")
|
||||
}
|
||||
|
|
@ -269,14 +269,14 @@ func (s *UserSetter) Apply(q *dialect.InsertQuery) {
|
|||
vals[9] = psql.Raw("DEFAULT")
|
||||
}
|
||||
|
||||
if !s.PasswordHashType.IsUnset() {
|
||||
vals[10] = psql.Arg(s.PasswordHashType.MustGetNull())
|
||||
if s.PasswordHashType.IsValue() {
|
||||
vals[10] = psql.Arg(s.PasswordHashType.MustGet())
|
||||
} else {
|
||||
vals[10] = psql.Raw("DEFAULT")
|
||||
}
|
||||
|
||||
if !s.PasswordHash.IsUnset() {
|
||||
vals[11] = psql.Arg(s.PasswordHash.MustGetNull())
|
||||
if s.PasswordHash.IsValue() {
|
||||
vals[11] = psql.Arg(s.PasswordHash.MustGet())
|
||||
} else {
|
||||
vals[11] = psql.Raw("DEFAULT")
|
||||
}
|
||||
|
|
@ -334,7 +334,7 @@ func (s UserSetter) Expressions(prefix ...string) []bob.Expression {
|
|||
}})
|
||||
}
|
||||
|
||||
if !s.DisplayName.IsUnset() {
|
||||
if s.DisplayName.IsValue() {
|
||||
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
||||
psql.Quote(append(prefix, "display_name")...),
|
||||
psql.Arg(s.DisplayName),
|
||||
|
|
@ -362,14 +362,14 @@ func (s UserSetter) Expressions(prefix ...string) []bob.Expression {
|
|||
}})
|
||||
}
|
||||
|
||||
if !s.PasswordHashType.IsUnset() {
|
||||
if s.PasswordHashType.IsValue() {
|
||||
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
||||
psql.Quote(append(prefix, "password_hash_type")...),
|
||||
psql.Arg(s.PasswordHashType),
|
||||
}})
|
||||
}
|
||||
|
||||
if !s.PasswordHash.IsUnset() {
|
||||
if s.PasswordHash.IsValue() {
|
||||
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
||||
psql.Quote(append(prefix, "password_hash")...),
|
||||
psql.Arg(s.PasswordHash),
|
||||
|
|
@ -677,16 +677,16 @@ func (user0 *User) AttachOrganization(ctx context.Context, exec bob.Executor, or
|
|||
type userWhere[Q psql.Filterable] struct {
|
||||
ID psql.WhereMod[Q, int32]
|
||||
ArcgisAccessToken psql.WhereNullMod[Q, string]
|
||||
ArcgisLicense psql.WhereNullMod[Q, enums.ArcgisLicenseType]
|
||||
ArcgisLicense psql.WhereNullMod[Q, enums.Arcgislicensetype]
|
||||
ArcgisRefreshToken psql.WhereNullMod[Q, string]
|
||||
ArcgisRefreshTokenExpires psql.WhereNullMod[Q, time.Time]
|
||||
ArcgisRole psql.WhereNullMod[Q, string]
|
||||
DisplayName psql.WhereNullMod[Q, string]
|
||||
DisplayName psql.WhereMod[Q, string]
|
||||
Email psql.WhereNullMod[Q, string]
|
||||
OrganizationID psql.WhereNullMod[Q, int32]
|
||||
Username psql.WhereMod[Q, string]
|
||||
PasswordHashType psql.WhereNullMod[Q, enums.Hashtype]
|
||||
PasswordHash psql.WhereNullMod[Q, string]
|
||||
PasswordHashType psql.WhereMod[Q, enums.Hashtype]
|
||||
PasswordHash psql.WhereMod[Q, string]
|
||||
}
|
||||
|
||||
func (userWhere[Q]) AliasedAs(alias string) userWhere[Q] {
|
||||
|
|
@ -697,16 +697,16 @@ func buildUserWhere[Q psql.Filterable](cols userColumns) userWhere[Q] {
|
|||
return userWhere[Q]{
|
||||
ID: psql.Where[Q, int32](cols.ID),
|
||||
ArcgisAccessToken: psql.WhereNull[Q, string](cols.ArcgisAccessToken),
|
||||
ArcgisLicense: psql.WhereNull[Q, enums.ArcgisLicenseType](cols.ArcgisLicense),
|
||||
ArcgisLicense: psql.WhereNull[Q, enums.Arcgislicensetype](cols.ArcgisLicense),
|
||||
ArcgisRefreshToken: psql.WhereNull[Q, string](cols.ArcgisRefreshToken),
|
||||
ArcgisRefreshTokenExpires: psql.WhereNull[Q, time.Time](cols.ArcgisRefreshTokenExpires),
|
||||
ArcgisRole: psql.WhereNull[Q, string](cols.ArcgisRole),
|
||||
DisplayName: psql.WhereNull[Q, string](cols.DisplayName),
|
||||
DisplayName: psql.Where[Q, string](cols.DisplayName),
|
||||
Email: psql.WhereNull[Q, string](cols.Email),
|
||||
OrganizationID: psql.WhereNull[Q, int32](cols.OrganizationID),
|
||||
Username: psql.Where[Q, string](cols.Username),
|
||||
PasswordHashType: psql.WhereNull[Q, enums.Hashtype](cols.PasswordHashType),
|
||||
PasswordHash: psql.WhereNull[Q, string](cols.PasswordHash),
|
||||
PasswordHashType: psql.Where[Q, enums.Hashtype](cols.PasswordHashType),
|
||||
PasswordHash: psql.Where[Q, string](cols.PasswordHash),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -28,12 +28,12 @@ func formatQuery(s string) (string, error) {
|
|||
|
||||
var defaultFaker = faker.New()
|
||||
|
||||
func random_enums_ArcgisLicenseType(f *faker.Faker, limits ...string) enums.ArcgisLicenseType {
|
||||
func random_enums_Arcgislicensetype(f *faker.Faker, limits ...string) enums.Arcgislicensetype {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
var e enums.ArcgisLicenseType
|
||||
var e enums.Arcgislicensetype
|
||||
all := e.All()
|
||||
return all[f.IntBetween(0, len(all)-1)]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -72,16 +72,16 @@ func UserByUsername(Username string) *UserByUsernameQuery {
|
|||
type UserByUsernameRow = struct {
|
||||
ID int32 `db:"id"`
|
||||
ArcgisAccessToken null.Val[string] `db:"arcgis_access_token"`
|
||||
ArcgisLicense null.Val[enums.ArcgisLicenseType] `db:"arcgis_license"`
|
||||
ArcgisLicense null.Val[enums.Arcgislicensetype] `db:"arcgis_license"`
|
||||
ArcgisRefreshToken null.Val[string] `db:"arcgis_refresh_token"`
|
||||
ArcgisRefreshTokenExpires null.Val[time.Time] `db:"arcgis_refresh_token_expires"`
|
||||
ArcgisRole null.Val[string] `db:"arcgis_role"`
|
||||
DisplayName null.Val[string] `db:"display_name"`
|
||||
DisplayName string `db:"display_name"`
|
||||
Email null.Val[string] `db:"email"`
|
||||
OrganizationID null.Val[int32] `db:"organization_id"`
|
||||
Username string `db:"username"`
|
||||
PasswordHashType null.Val[enums.Hashtype] `db:"password_hash_type"`
|
||||
PasswordHash null.Val[string] `db:"password_hash"`
|
||||
PasswordHashType enums.Hashtype `db:"password_hash_type"`
|
||||
PasswordHash string `db:"password_hash"`
|
||||
}
|
||||
|
||||
type userByUsernameTransformer = bob.SliceTransformer[UserByUsernameRow, []UserByUsernameRow]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue