updated the rules when linking OAuth2 by email

This commit is contained in:
Gani Georgiev
2024-06-18 16:15:53 +03:00
parent af9cf33553
commit 58ace5d5e7
53 changed files with 637 additions and 351 deletions
+21 -3
View File
@@ -309,7 +309,7 @@ func (m *Record) Set(key string, value any) {
switch key {
case schema.FieldNameEmailVisibility, schema.FieldNameVerified:
v = cast.ToBool(value)
case schema.FieldNameLastResetSentAt, schema.FieldNameLastVerificationSentAt:
case schema.FieldNameLastResetSentAt, schema.FieldNameLastVerificationSentAt, schema.FieldNameLastLoginAlertSentAt:
v, _ = types.ParseDateTime(value)
case schema.FieldNameUsername, schema.FieldNameEmail, schema.FieldNameTokenKey, schema.FieldNamePasswordHash:
v = cast.ToString(value)
@@ -347,7 +347,7 @@ func (m *Record) Get(key string) any {
switch key {
case schema.FieldNameEmailVisibility, schema.FieldNameVerified:
v = cast.ToBool(v)
case schema.FieldNameLastResetSentAt, schema.FieldNameLastVerificationSentAt:
case schema.FieldNameLastResetSentAt, schema.FieldNameLastVerificationSentAt, schema.FieldNameLastLoginAlertSentAt:
v, _ = types.ParseDateTime(v)
case schema.FieldNameUsername, schema.FieldNameEmail, schema.FieldNameTokenKey, schema.FieldNamePasswordHash:
v = cast.ToString(v)
@@ -685,7 +685,7 @@ func (m *Record) getNormalizeDataValueForDB(key string) any {
switch key {
case schema.FieldNameEmailVisibility, schema.FieldNameVerified:
return m.GetBool(key)
case schema.FieldNameLastResetSentAt, schema.FieldNameLastVerificationSentAt:
case schema.FieldNameLastResetSentAt, schema.FieldNameLastVerificationSentAt, schema.FieldNameLastLoginAlertSentAt:
return m.GetDateTime(key)
case schema.FieldNameUsername, schema.FieldNameEmail, schema.FieldNameTokenKey, schema.FieldNamePasswordHash:
return m.GetString(key)
@@ -898,6 +898,24 @@ func (m *Record) SetLastVerificationSentAt(dateTime types.DateTime) error {
return nil
}
// LastLoginAlertSentAt returns the "lastLoginAlertSentAt" auth record data value.
func (m *Record) LastLoginAlertSentAt() types.DateTime {
return m.GetDateTime(schema.FieldNameLastLoginAlertSentAt)
}
// SetLastLoginAlertSentAt sets an "lastLoginAlertSentAt" auth record data value.
//
// Returns an error if the record is not from an auth collection.
func (m *Record) SetLastLoginAlertSentAt(dateTime types.DateTime) error {
if !m.collection.IsAuth() {
return notAuthRecordErr
}
m.Set(schema.FieldNameLastLoginAlertSentAt, dateTime)
return nil
}
// PasswordHash returns the "passwordHash" auth record data value.
func (m *Record) PasswordHash() string {
return m.GetString(schema.FieldNamePasswordHash)
+47 -1
View File
@@ -1328,7 +1328,7 @@ func TestRecordColumnValueMap(t *testing.T) {
},
{
models.CollectionTypeAuth,
`{"created":"2022-01-01 10:00:30.123Z","email":"test_email","emailVisibility":true,"field1":"test","field2":"test.png","field3":["test1","test2"],"field4":["test11","test12"],"id":"test_id","lastResetSentAt":"2022-01-02 10:00:30.123Z","lastVerificationSentAt":"","passwordHash":"test_passwordHash","tokenKey":"test_tokenKey","updated":"","username":"test_username","verified":false}`,
`{"created":"2022-01-01 10:00:30.123Z","email":"test_email","emailVisibility":true,"field1":"test","field2":"test.png","field3":["test1","test2"],"field4":["test11","test12"],"id":"test_id","lastLoginAlertSentAt":"","lastResetSentAt":"2022-01-02 10:00:30.123Z","lastVerificationSentAt":"","passwordHash":"test_passwordHash","tokenKey":"test_tokenKey","updated":"","username":"test_username","verified":false}`,
},
}
@@ -1975,6 +1975,52 @@ func TestRecordRefreshTokenKey(t *testing.T) {
}
}
func TestRecordLastPasswordLoginAlertSentAt(t *testing.T) {
t.Parallel()
scenarios := []struct {
collectionType string
expectError bool
}{
{models.CollectionTypeBase, true},
{models.CollectionTypeAuth, false},
}
testValue, err := types.ParseDateTime("2022-01-01 00:00:00.123Z")
if err != nil {
t.Fatal(err)
}
for i, s := range scenarios {
collection := &models.Collection{Type: s.collectionType}
m := models.NewRecord(collection)
if s.expectError {
if err := m.SetLastLoginAlertSentAt(testValue); err == nil {
t.Errorf("(%d) Expected error, got nil", i)
}
if v := m.LastLoginAlertSentAt(); !v.IsZero() {
t.Fatalf("(%d) Expected empty value, got %v", i, v)
}
// verify that nothing is stored in the record data slice
if v := m.Get(schema.FieldNameLastLoginAlertSentAt); v != nil {
t.Fatalf("(%d) Didn't expect data field %q: %v", i, schema.FieldNameLastLoginAlertSentAt, v)
}
} else {
if err := m.SetLastLoginAlertSentAt(testValue); err != nil {
t.Fatalf("(%d) Expected nil, got error %v", i, err)
}
if v := m.LastLoginAlertSentAt(); v != testValue {
t.Fatalf("(%d) Expected %v, got %v", i, testValue, v)
}
// verify that the field is stored in the record data slice
if v := m.Get(schema.FieldNameLastLoginAlertSentAt); v != testValue {
t.Fatalf("(%d) Expected data field value %v, got %v", i, testValue, v)
}
}
}
}
func TestRecordLastResetSentAt(t *testing.T) {
t.Parallel()
+2
View File
@@ -47,6 +47,7 @@ const (
FieldNamePasswordHash string = "passwordHash"
FieldNameLastResetSentAt string = "lastResetSentAt"
FieldNameLastVerificationSentAt string = "lastVerificationSentAt"
FieldNameLastLoginAlertSentAt string = "lastLoginAlertSentAt"
)
// BaseModelFieldNames returns the field names that all models have (id, created, updated).
@@ -78,6 +79,7 @@ func AuthFieldNames() []string {
FieldNamePasswordHash,
FieldNameLastResetSentAt,
FieldNameLastVerificationSentAt,
FieldNameLastLoginAlertSentAt,
}
}
+1 -1
View File
@@ -31,7 +31,7 @@ func TestSystemFieldNames(t *testing.T) {
func TestAuthFieldNames(t *testing.T) {
result := schema.AuthFieldNames()
expected := 8
expected := 9
if len(result) != expected {
t.Fatalf("Expected %d auth field names, got %d (%v)", expected, len(result), result)