Skip to content

Commit

Permalink
move userprofiles into method on user struct (#2014)
Browse files Browse the repository at this point in the history
Signed-off-by: Kristoffer Dalby <[email protected]>
  • Loading branch information
kradalby authored Jul 19, 2024
1 parent 7e62031 commit 9e523d4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
15 changes: 2 additions & 13 deletions hscontrol/mapper/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion hscontrol/mapper/mapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ func (s *Suite) TestGetMapResponseUserProfiles(c *check.C) {
types.Nodes{
nodeInShared2, nodeInShared3, node2InShared1,
},
"",
)

c.Assert(len(userProfiles), check.Equals, 3)
Expand Down
40 changes: 27 additions & 13 deletions hscontrol/types/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 9e523d4

Please sign in to comment.