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 {
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)

View File

@ -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"},

View File

@ -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
}

View File

@ -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,

View File

@ -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)

View File

@ -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{