Skip to content

Commit

Permalink
Update user's last login when authenticating a peer (#1437)
Browse files Browse the repository at this point in the history
* Update user's last login when authenticating a peer

Prior to this update the user's last login only updated on dashboard authentication

* use account and user methods
  • Loading branch information
mlsmaycon authored Jan 6, 2024
1 parent 1f3a12d commit 8b4ec96
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
18 changes: 17 additions & 1 deletion management/server/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (
"strings"
"time"

"github.com/netbirdio/management-integrations/additions"
"github.com/rs/xid"

"github.com/netbirdio/management-integrations/additions"

"github.com/netbirdio/netbird/management/server/activity"
nbpeer "github.com/netbirdio/netbird/management/server/peer"
"github.com/netbirdio/netbird/management/server/status"
Expand Down Expand Up @@ -440,6 +441,14 @@ func (am *DefaultAccountManager) AddPeer(setupKey, userID string, peer *nbpeer.P
}
}

if addedByUser {
user, err := account.FindUser(userID)
if err != nil {
return nil, nil, status.Errorf(status.Internal, "couldn't find user")
}
user.updateLastLogin(newPeer.LastLogin)
}

account.Peers[newPeer.ID] = newPeer
account.Network.IncSerial()
err = am.Store.SaveAccount(account)
Expand Down Expand Up @@ -550,6 +559,13 @@ func (am *DefaultAccountManager) LoginPeer(login PeerLogin) (*nbpeer.Peer, *Netw
updateRemotePeers = true
shouldStoreAccount = true

// sync user last login with peer last login
user, err := account.FindUser(login.UserID)
if err != nil {
return nil, nil, status.Errorf(status.Internal, "couldn't find user")
}
user.updateLastLogin(peer.LastLogin)

am.StoreEvent(login.UserID, peer.ID, account.Id, activity.UserLoggedInPeer, peer.EventMeta(am.GetDNSDomain()))
}

Expand Down
4 changes: 4 additions & 0 deletions management/server/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ func (u *User) LastDashboardLoginChanged(LastLogin time.Time) bool {
return LastLogin.After(u.LastLogin) && !u.LastLogin.IsZero()
}

func (u *User) updateLastLogin(login time.Time) {
u.LastLogin = login
}

// HasAdminPower returns true if the user has admin or owner roles, false otherwise
func (u *User) HasAdminPower() bool {
return u.Role == UserRoleAdmin || u.Role == UserRoleOwner
Expand Down

0 comments on commit 8b4ec96

Please sign in to comment.