ipn/ipnlocal: prevent duplicate profiles of the same user

Updates #713

Signed-off-by: Maisem Ali <maisem@tailscale.com>
This commit is contained in:
Maisem Ali
2022-11-16 16:17:36 +05:00
committed by Maisem Ali
parent e9c851b04b
commit 2f4fca65a1
3 changed files with 75 additions and 8 deletions

View File

@@ -14,6 +14,7 @@ import (
"golang.org/x/exp/slices"
"tailscale.com/ipn"
"tailscale.com/tailcfg"
"tailscale.com/types/logger"
"tailscale.com/util/clientmetric"
"tailscale.com/util/strs"
@@ -69,6 +70,15 @@ func (pm *profileManager) SetCurrentUser(uid string) error {
return nil
}
func (pm *profileManager) findProfileByUserID(userID tailcfg.UserID) *ipn.LoginProfile {
for _, p := range pm.knownProfiles {
if p.UserProfile.ID == userID {
return p
}
}
return nil
}
func (pm *profileManager) findProfileByName(name string) *ipn.LoginProfile {
for _, p := range pm.knownProfiles {
if p.Name == name {
@@ -129,8 +139,14 @@ func (pm *profileManager) SetPrefs(prefsIn ipn.PrefsView) error {
wasNamedWithLoginName := cp.Name == cp.UserProfile.LoginName
if pm.isNewProfile {
pm.isNewProfile = false
cp.ID, cp.Key = newUnusedID(pm.knownProfiles)
cp.Name = ps.LoginName
// Check if we already have a profile for this user.
existing := pm.findProfileByUserID(ps.UserProfile.ID)
if existing != nil && existing.ID != "" {
cp = existing
} else {
cp.ID, cp.Key = newUnusedID(pm.knownProfiles)
cp.Name = ps.LoginName
}
cp.UserProfile = ps.UserProfile
cp.LocalUserID = pm.currentUserID
} else {
@@ -140,6 +156,7 @@ func (pm *profileManager) SetPrefs(prefsIn ipn.PrefsView) error {
cp.Name = ps.LoginName
}
pm.knownProfiles[cp.ID] = cp
pm.currentProfile = cp
if err := pm.writeKnownProfiles(); err != nil {
return err
}