mirror of
https://github.com/juanfont/headscale.git
synced 2026-07-08 09:00:22 +09:00
types: add Node StringID and UserView accessors
Add StringID() on Node/NodeView and Username/Display/CreatedAt on UserView, so the HTTP read paths render ids and read users through view accessors instead of cloning with AsStruct().
This commit is contained in:
@@ -92,6 +92,16 @@ func (id NodeID) String() string {
|
||||
return strconv.FormatUint(id.Uint64(), util.Base10)
|
||||
}
|
||||
|
||||
// StringID returns the node's id as a decimal string, the form the HTTP APIs
|
||||
// render it as.
|
||||
func (node *Node) StringID() string {
|
||||
if node == nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
return node.ID.String()
|
||||
}
|
||||
|
||||
func ParseNodeID(s string) (NodeID, error) {
|
||||
id, err := strconv.ParseUint(s, util.Base10, 64)
|
||||
return NodeID(id), err
|
||||
@@ -852,6 +862,16 @@ func (nv NodeView) RequestTagsSlice() views.Slice[string] {
|
||||
return nv.Hostinfo().RequestTags()
|
||||
}
|
||||
|
||||
// StringID returns the node's id as a decimal string, the form the HTTP APIs
|
||||
// render it as.
|
||||
func (nv NodeView) StringID() string {
|
||||
if !nv.Valid() {
|
||||
return ""
|
||||
}
|
||||
|
||||
return nv.ID().String()
|
||||
}
|
||||
|
||||
// IsTagged reports if a device is tagged
|
||||
// and therefore should not be treated as a
|
||||
// user owned device.
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/juanfont/headscale/hscontrol/util"
|
||||
"github.com/juanfont/headscale/hscontrol/util/zlog/zf"
|
||||
@@ -133,6 +134,33 @@ func (u *User) Display() string {
|
||||
return cmp.Or(u.DisplayName, u.Username())
|
||||
}
|
||||
|
||||
// Username returns the user's login name via the view; see [User.Username].
|
||||
func (v UserView) Username() string {
|
||||
if !v.Valid() {
|
||||
return ""
|
||||
}
|
||||
|
||||
return v.ж.Username()
|
||||
}
|
||||
|
||||
// Display returns the user's display name via the view; see [User.Display].
|
||||
func (v UserView) Display() string {
|
||||
if !v.Valid() {
|
||||
return ""
|
||||
}
|
||||
|
||||
return v.ж.Display()
|
||||
}
|
||||
|
||||
// CreatedAt returns when the user was created.
|
||||
func (v UserView) CreatedAt() time.Time {
|
||||
if !v.Valid() {
|
||||
return time.Time{}
|
||||
}
|
||||
|
||||
return v.ж.CreatedAt
|
||||
}
|
||||
|
||||
func (u *User) TailscaleUser() tailcfg.User {
|
||||
return tailcfg.User{
|
||||
ID: tailcfg.UserID(u.ID), //nolint:gosec // UserID is bounded
|
||||
|
||||
Reference in New Issue
Block a user