move userprofiles into method on user struct (#2014)

Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
This commit is contained in:
Kristoffer Dalby 2024-07-19 09:03:18 +02:00 committed by GitHub
parent 7e62031444
commit 9e523d4687
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 29 additions and 27 deletions

View file

@ -94,7 +94,6 @@ func (m *Mapper) String() string {
func generateUserProfiles( func generateUserProfiles(
node *types.Node, node *types.Node,
peers types.Nodes, peers types.Nodes,
baseDomain string,
) []tailcfg.UserProfile { ) []tailcfg.UserProfile {
userMap := make(map[string]types.User) userMap := make(map[string]types.User)
userMap[node.User.Name] = node.User userMap[node.User.Name] = node.User
@ -104,18 +103,8 @@ func generateUserProfiles(
var profiles []tailcfg.UserProfile var profiles []tailcfg.UserProfile
for _, user := range userMap { for _, user := range userMap {
displayName := user.Name
if baseDomain != "" {
displayName = fmt.Sprintf("%s@%s", user.Name, baseDomain)
}
profiles = append(profiles, profiles = append(profiles,
tailcfg.UserProfile{ user.TailscaleUserProfile())
ID: tailcfg.UserID(user.ID),
LoginName: user.Name,
DisplayName: displayName,
})
} }
return profiles return profiles
@ -569,7 +558,7 @@ func appendPeerChanges(
changed = policy.FilterNodesByACL(node, changed, packetFilter) changed = policy.FilterNodesByACL(node, changed, packetFilter)
} }
profiles := generateUserProfiles(node, changed, cfg.BaseDomain) profiles := generateUserProfiles(node, changed)
dnsConfig := generateDNSConfig( dnsConfig := generateDNSConfig(
cfg, cfg,

View file

@ -43,7 +43,6 @@ func (s *Suite) TestGetMapResponseUserProfiles(c *check.C) {
types.Nodes{ types.Nodes{
nodeInShared2, nodeInShared3, node2InShared1, nodeInShared2, nodeInShared3, node2InShared1,
}, },
"",
) )
c.Assert(len(userProfiles), check.Equals, 3) c.Assert(len(userProfiles), check.Equals, 3)

View file

@ -19,32 +19,46 @@ type User struct {
Name string `gorm:"unique"` Name string `gorm:"unique"`
} }
func (n *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 // TODO(kradalby): See if we can fill in Gravatar here
ProfilePicURL: "", func (u *User) profilePicURL() string {
return ""
}
func (u *User) TailscaleUser() *tailcfg.User {
user := tailcfg.User{
ID: tailcfg.UserID(u.ID),
LoginName: u.Name,
DisplayName: u.Name,
ProfilePicURL: u.profilePicURL(),
Logins: []tailcfg.LoginID{}, Logins: []tailcfg.LoginID{},
Created: n.CreatedAt, Created: u.CreatedAt,
} }
return &user return &user
} }
func (n *User) TailscaleLogin() *tailcfg.Login { func (u *User) TailscaleLogin() *tailcfg.Login {
login := tailcfg.Login{ login := tailcfg.Login{
ID: tailcfg.LoginID(n.ID), ID: tailcfg.LoginID(u.ID),
LoginName: n.Name, // TODO(kradalby): this should reflect registration method.
DisplayName: n.Name, Provider: "",
// TODO(kradalby): See if we can fill in Gravatar here LoginName: u.Name,
ProfilePicURL: "", DisplayName: u.Name,
ProfilePicURL: u.profilePicURL(),
} }
return &login 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 { func (n *User) Proto() *v1.User {
return &v1.User{ return &v1.User{
Id: strconv.FormatUint(uint64(n.ID), util.Base10), Id: strconv.FormatUint(uint64(n.ID), util.Base10),