From 9e523d4687f9504146b60f246e01256893ba47a6 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Fri, 19 Jul 2024 09:03:18 +0200 Subject: [PATCH] move userprofiles into method on user struct (#2014) Signed-off-by: Kristoffer Dalby --- hscontrol/mapper/mapper.go | 15 ++----------- hscontrol/mapper/mapper_test.go | 1 - hscontrol/types/users.go | 40 ++++++++++++++++++++++----------- 3 files changed, 29 insertions(+), 27 deletions(-) diff --git a/hscontrol/mapper/mapper.go b/hscontrol/mapper/mapper.go index d7a6cfce..73420419 100644 --- a/hscontrol/mapper/mapper.go +++ b/hscontrol/mapper/mapper.go @@ -94,7 +94,6 @@ func (m *Mapper) String() string { func generateUserProfiles( node *types.Node, peers types.Nodes, - baseDomain string, ) []tailcfg.UserProfile { userMap := make(map[string]types.User) userMap[node.User.Name] = node.User @@ -104,18 +103,8 @@ func generateUserProfiles( var profiles []tailcfg.UserProfile for _, user := range userMap { - displayName := user.Name - - if baseDomain != "" { - displayName = fmt.Sprintf("%s@%s", user.Name, baseDomain) - } - profiles = append(profiles, - tailcfg.UserProfile{ - ID: tailcfg.UserID(user.ID), - LoginName: user.Name, - DisplayName: displayName, - }) + user.TailscaleUserProfile()) } return profiles @@ -569,7 +558,7 @@ func appendPeerChanges( changed = policy.FilterNodesByACL(node, changed, packetFilter) } - profiles := generateUserProfiles(node, changed, cfg.BaseDomain) + profiles := generateUserProfiles(node, changed) dnsConfig := generateDNSConfig( cfg, diff --git a/hscontrol/mapper/mapper_test.go b/hscontrol/mapper/mapper_test.go index be48c6fa..0484fc02 100644 --- a/hscontrol/mapper/mapper_test.go +++ b/hscontrol/mapper/mapper_test.go @@ -43,7 +43,6 @@ func (s *Suite) TestGetMapResponseUserProfiles(c *check.C) { types.Nodes{ nodeInShared2, nodeInShared3, node2InShared1, }, - "", ) c.Assert(len(userProfiles), check.Equals, 3) diff --git a/hscontrol/types/users.go b/hscontrol/types/users.go index 0b8324f2..63e73a56 100644 --- a/hscontrol/types/users.go +++ b/hscontrol/types/users.go @@ -19,32 +19,46 @@ type User struct { Name string `gorm:"unique"` } -func (n *User) TailscaleUser() *tailcfg.User { +// TODO(kradalby): See if we can fill in Gravatar here +func (u *User) profilePicURL() string { + return "" +} + +func (u *User) TailscaleUser() *tailcfg.User { user := tailcfg.User{ - ID: tailcfg.UserID(n.ID), - LoginName: n.Name, - DisplayName: n.Name, - // TODO(kradalby): See if we can fill in Gravatar here - ProfilePicURL: "", + ID: tailcfg.UserID(u.ID), + LoginName: u.Name, + DisplayName: u.Name, + ProfilePicURL: u.profilePicURL(), Logins: []tailcfg.LoginID{}, - Created: n.CreatedAt, + Created: u.CreatedAt, } return &user } -func (n *User) TailscaleLogin() *tailcfg.Login { +func (u *User) TailscaleLogin() *tailcfg.Login { login := tailcfg.Login{ - ID: tailcfg.LoginID(n.ID), - LoginName: n.Name, - DisplayName: n.Name, - // TODO(kradalby): See if we can fill in Gravatar here - ProfilePicURL: "", + ID: tailcfg.LoginID(u.ID), + // TODO(kradalby): this should reflect registration method. + Provider: "", + LoginName: u.Name, + DisplayName: u.Name, + ProfilePicURL: u.profilePicURL(), } return &login } +func (u *User) TailscaleUserProfile() tailcfg.UserProfile { + return tailcfg.UserProfile{ + ID: tailcfg.UserID(u.ID), + LoginName: u.Name, + DisplayName: u.Name, + ProfilePicURL: u.profilePicURL(), + } +} + func (n *User) Proto() *v1.User { return &v1.User{ Id: strconv.FormatUint(uint64(n.ID), util.Base10),