From a424a084dc4613ec01503aa40c17f4338901bc1a Mon Sep 17 00:00:00 2001 From: Zhiwei Liang <121905282+zliang-akamai@users.noreply.github.com> Date: Thu, 19 Sep 2024 11:10:08 -0400 Subject: [PATCH] Add `last_login` support in `User` struct (#583) --- account_users.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/account_users.go b/account_users.go index e01e6dde4..88615fa81 100644 --- a/account_users.go +++ b/account_users.go @@ -17,10 +17,17 @@ const ( UserTypeDefault UserType = "default" ) +// LastLogin represents a LastLogin object +type LastLogin struct { + LoginDatetime *time.Time `json:"-"` + Status string `json:"status"` +} + // User represents a User object type User struct { Username string `json:"username"` Email string `json:"email"` + LastLogin *LastLogin `json:"last_login"` UserType UserType `json:"user_type"` Restricted bool `json:"restricted"` TFAEnabled bool `json:"tfa_enabled"` @@ -42,6 +49,26 @@ type UserUpdateOptions struct { Restricted *bool `json:"restricted,omitempty"` } +// UnmarshalJSON implements the json.Unmarshaler interface +func (ll *LastLogin) UnmarshalJSON(b []byte) error { + type Mask LastLogin + + p := struct { + *Mask + LoginDatetime *parseabletime.ParseableTime `json:"login_datetime"` + }{ + Mask: (*Mask)(ll), + } + + if err := json.Unmarshal(b, &p); err != nil { + return err + } + + ll.LoginDatetime = (*time.Time)(p.LoginDatetime) + + return nil +} + // UnmarshalJSON implements the json.Unmarshaler interface func (i *User) UnmarshalJSON(b []byte) error { type Mask User