Skip to content

Commit

Permalink
Handle user lastLogin null time
Browse files Browse the repository at this point in the history
Signed-off-by: bcmmbaga <bethuelmbaga12@gmail.com>
  • Loading branch information
bcmmbaga committed Dec 30, 2024
1 parent 0207a32 commit 9ee234a
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion management/server/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion management/server/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package server
import (
"context"
"crypto/sha256"
"database/sql"
b64 "encoding/base64"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -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"},
Expand Down
3 changes: 2 additions & 1 deletion management/server/store/sql_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package store

import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -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
}
Expand Down
9 changes: 5 additions & 4 deletions management/server/types/user.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
"database/sql"
"fmt"
"strings"
"time"
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion management/server/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion management/server/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package server

import (
"context"
"database/sql"
"fmt"
"reflect"
"testing"
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit 9ee234a

Please sign in to comment.