Handle user lastLogin null time

Signed-off-by: bcmmbaga <bethuelmbaga12@gmail.com>
This commit is contained in:
bcmmbaga 2024-12-30 13:56:05 +03:00
parent 0207a326dc
commit 9ee234ac35
No known key found for this signature in database
GPG Key ID: 511EED5C928AD547
6 changed files with 13 additions and 9 deletions

View File

@ -789,7 +789,7 @@ func (am *DefaultAccountManager) lookupUserInCache(ctx context.Context, userID s
if user.Issued == types.UserIssuedIntegration { if user.Issued == types.UserIssuedIntegration {
continue 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) log.WithContext(ctx).Debugf("looking up user %s of account %s in cache", userID, account.Id)
userData, err := am.lookupCache(ctx, users, account.Id) userData, err := am.lookupCache(ctx, users, account.Id)

View File

@ -3,6 +3,7 @@ package server
import ( import (
"context" "context"
"crypto/sha256" "crypto/sha256"
"database/sql"
b64 "encoding/base64" b64 "encoding/base64"
"encoding/json" "encoding/json"
"fmt" "fmt"
@ -1096,7 +1097,7 @@ func genUsers(p string, n int) map[string]*types.User {
users[fmt.Sprintf("%s-%d", p, i)] = &types.User{ users[fmt.Sprintf("%s-%d", p, i)] = &types.User{
Id: fmt.Sprintf("%s-%d", p, i), Id: fmt.Sprintf("%s-%d", p, i),
Role: types.UserRoleAdmin, Role: types.UserRoleAdmin,
LastLogin: now, LastLogin: sql.NullTime{Time: now, Valid: !now.IsZero()},
CreatedAt: now, CreatedAt: now,
Issued: "api", Issued: "api",
AutoGroups: []string{"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"}, AutoGroups: []string{"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"},

View File

@ -2,6 +2,7 @@ package store
import ( import (
"context" "context"
"database/sql"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
@ -900,7 +901,7 @@ func (s *SqlStore) SaveUserLastLogin(ctx context.Context, accountID, userID stri
} }
return status.NewGetUserFromStoreError() return status.NewGetUserFromStoreError()
} }
user.LastLogin = lastLogin user.LastLogin = sql.NullTime{Time: lastLogin, Valid: !lastLogin.IsZero()}
return s.db.Save(&user).Error return s.db.Save(&user).Error
} }

View File

@ -1,6 +1,7 @@
package types package types
import ( import (
"database/sql"
"fmt" "fmt"
"strings" "strings"
"time" "time"
@ -84,7 +85,7 @@ type User struct {
// Blocked indicates whether the user is blocked. Blocked users can't use the system. // Blocked indicates whether the user is blocked. Blocked users can't use the system.
Blocked bool Blocked bool
// LastLogin is the last time the user logged in to IdP // 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 records the time the user was created
CreatedAt time.Time CreatedAt time.Time
@ -100,7 +101,7 @@ func (u *User) IsBlocked() bool {
} }
func (u *User) LastDashboardLoginChanged(LastLogin time.Time) 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 // 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), Status: string(UserStatusActive),
IsServiceUser: u.IsServiceUser, IsServiceUser: u.IsServiceUser,
IsBlocked: u.Blocked, IsBlocked: u.Blocked,
LastLogin: u.LastLogin, LastLogin: u.LastLogin.Time,
Issued: u.Issued, Issued: u.Issued,
Permissions: UserPermissions{ Permissions: UserPermissions{
DashboardView: dashboardViewPermissions, DashboardView: dashboardViewPermissions,
@ -168,7 +169,7 @@ func (u *User) ToUserInfo(userData *idp.UserData, settings *Settings) (*UserInfo
Status: string(userStatus), Status: string(userStatus),
IsServiceUser: u.IsServiceUser, IsServiceUser: u.IsServiceUser,
IsBlocked: u.Blocked, IsBlocked: u.Blocked,
LastLogin: u.LastLogin, LastLogin: u.LastLogin.Time,
Issued: u.Issued, Issued: u.Issued,
Permissions: UserPermissions{ Permissions: UserPermissions{
DashboardView: dashboardViewPermissions, DashboardView: dashboardViewPermissions,

View File

@ -880,7 +880,7 @@ func (am *DefaultAccountManager) GetUsersFromAccount(ctx context.Context, accoun
continue continue
} }
if !user.IsServiceUser { 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) queriedUsers, err = am.lookupCache(ctx, users, accountID)

View File

@ -2,6 +2,7 @@ package server
import ( import (
"context" "context"
"database/sql"
"fmt" "fmt"
"reflect" "reflect"
"testing" "testing"
@ -328,7 +329,7 @@ func TestUser_Copy(t *testing.T) {
}, },
}, },
Blocked: false, Blocked: false,
LastLogin: time.Now().UTC(), LastLogin: sql.NullTime{Time: time.Now().UTC(), Valid: true},
CreatedAt: time.Now().UTC(), CreatedAt: time.Now().UTC(),
Issued: "test", Issued: "test",
IntegrationReference: integration_reference.IntegrationReference{ IntegrationReference: integration_reference.IntegrationReference{