diff --git a/management/server/account.go b/management/server/account.go index afa166ed2..2fbfea731 100644 --- a/management/server/account.go +++ b/management/server/account.go @@ -789,7 +789,7 @@ func (am *DefaultAccountManager) lookupUserInCache(ctx context.Context, userID s if user.Issued == types.UserIssuedIntegration { continue } - users[user.Id] = userLoggedInOnce(!user.LastLogin.IsZero()) + users[user.Id] = userLoggedInOnce(!user.LastLogin.Time.IsZero()) } log.WithContext(ctx).Debugf("looking up user %s of account %s in cache", userID, account.Id) userData, err := am.lookupCache(ctx, users, account.Id) diff --git a/management/server/account_test.go b/management/server/account_test.go index d83eab6d1..1323b2235 100644 --- a/management/server/account_test.go +++ b/management/server/account_test.go @@ -3,6 +3,7 @@ package server import ( "context" "crypto/sha256" + "database/sql" b64 "encoding/base64" "encoding/json" "fmt" @@ -1096,7 +1097,7 @@ func genUsers(p string, n int) map[string]*types.User { users[fmt.Sprintf("%s-%d", p, i)] = &types.User{ Id: fmt.Sprintf("%s-%d", p, i), Role: types.UserRoleAdmin, - LastLogin: now, + LastLogin: sql.NullTime{Time: now, Valid: !now.IsZero()}, CreatedAt: now, Issued: "api", AutoGroups: []string{"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"}, diff --git a/management/server/store/sql_store.go b/management/server/store/sql_store.go index 941af5e01..5e103dfd6 100644 --- a/management/server/store/sql_store.go +++ b/management/server/store/sql_store.go @@ -2,6 +2,7 @@ package store import ( "context" + "database/sql" "encoding/json" "errors" "fmt" @@ -900,7 +901,7 @@ func (s *SqlStore) SaveUserLastLogin(ctx context.Context, accountID, userID stri } return status.NewGetUserFromStoreError() } - user.LastLogin = lastLogin + user.LastLogin = sql.NullTime{Time: lastLogin, Valid: !lastLogin.IsZero()} return s.db.Save(&user).Error } diff --git a/management/server/types/user.go b/management/server/types/user.go index 9a6c734e9..06cc9858e 100644 --- a/management/server/types/user.go +++ b/management/server/types/user.go @@ -1,6 +1,7 @@ package types import ( + "database/sql" "fmt" "strings" "time" @@ -84,7 +85,7 @@ type User struct { // Blocked indicates whether the user is blocked. Blocked users can't use the system. Blocked bool // LastLogin is the last time the user logged in to IdP - LastLogin time.Time `gorm:"type:TIMESTAMP;null;default:null"` + LastLogin sql.NullTime // CreatedAt records the time the user was created CreatedAt time.Time @@ -100,7 +101,7 @@ func (u *User) IsBlocked() bool { } func (u *User) LastDashboardLoginChanged(LastLogin time.Time) bool { - return LastLogin.After(u.LastLogin) && !u.LastLogin.IsZero() + return LastLogin.After(u.LastLogin.Time) && !u.LastLogin.Time.IsZero() } // HasAdminPower returns true if the user has admin or owner roles, false otherwise @@ -143,7 +144,7 @@ func (u *User) ToUserInfo(userData *idp.UserData, settings *Settings) (*UserInfo Status: string(UserStatusActive), IsServiceUser: u.IsServiceUser, IsBlocked: u.Blocked, - LastLogin: u.LastLogin, + LastLogin: u.LastLogin.Time, Issued: u.Issued, Permissions: UserPermissions{ DashboardView: dashboardViewPermissions, @@ -168,7 +169,7 @@ func (u *User) ToUserInfo(userData *idp.UserData, settings *Settings) (*UserInfo Status: string(userStatus), IsServiceUser: u.IsServiceUser, IsBlocked: u.Blocked, - LastLogin: u.LastLogin, + LastLogin: u.LastLogin.Time, Issued: u.Issued, Permissions: UserPermissions{ DashboardView: dashboardViewPermissions, diff --git a/management/server/user.go b/management/server/user.go index 457721917..1f40f9732 100644 --- a/management/server/user.go +++ b/management/server/user.go @@ -880,7 +880,7 @@ func (am *DefaultAccountManager) GetUsersFromAccount(ctx context.Context, accoun continue } if !user.IsServiceUser { - users[user.Id] = userLoggedInOnce(!user.LastLogin.IsZero()) + users[user.Id] = userLoggedInOnce(!user.LastLogin.Time.IsZero()) } } queriedUsers, err = am.lookupCache(ctx, users, accountID) diff --git a/management/server/user_test.go b/management/server/user_test.go index 75d88f9c8..8c40e2e0c 100644 --- a/management/server/user_test.go +++ b/management/server/user_test.go @@ -2,6 +2,7 @@ package server import ( "context" + "database/sql" "fmt" "reflect" "testing" @@ -328,7 +329,7 @@ func TestUser_Copy(t *testing.T) { }, }, Blocked: false, - LastLogin: time.Now().UTC(), + LastLogin: sql.NullTime{Time: time.Now().UTC(), Valid: true}, CreatedAt: time.Now().UTC(), Issued: "test", IntegrationReference: integration_reference.IntegrationReference{