Skip to content

Commit

Permalink
Merge pull request #83 from luthermonson/users
Browse files Browse the repository at this point in the history
adding users and tokens
  • Loading branch information
luthermonson authored Aug 26, 2023
2 parents 41c1e13 + f8f066b commit c38a376
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 0 deletions.
27 changes: 27 additions & 0 deletions access.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,30 @@ func (g *Group) Update() error {
func (g *Group) Delete() error {
return g.client.Delete(fmt.Sprintf("/access/groups/%s", g.GroupID), nil)
}

func (c *Client) User(userid string) (user *User, err error) {
err = c.Get(fmt.Sprintf("/access/users/%s", userid), &user)
if nil == err {
user.UserID = userid
user.client = c
}
return
}

func (c *Client) Users() (users Users, err error) {
err = c.Get("/access/users", &users)
if nil == err {
for _, g := range users {
g.client = c
}
}
return
}

func (u *User) Update() error {
return u.client.Put(fmt.Sprintf("/access/users/%s", u.UserID), u, nil)
}

func (u *User) Delete() error {
return u.client.Delete(fmt.Sprintf("/access/users/%s", u.UserID), nil)
}
20 changes: 20 additions & 0 deletions access_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,23 @@ func TestGroups(t *testing.T) {
assert.NotEmpty(t, g.Users)
}
}

func TestUser(t *testing.T) {
mocks.On(mockConfig)
defer mocks.Off()
client := mockClient()

u, err := client.User("root@pam")
assert.Nil(t, err)
assert.Equal(t, u.UserID, "root@pam")
}

func TestUsers(t *testing.T) {
mocks.On(mockConfig)
defer mocks.Off()
client := mockClient()

users, err := client.Users()
assert.Nil(t, err)
assert.Len(t, users, 4)
}
67 changes: 67 additions & 0 deletions tests/mocks/pve7x/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,5 +606,72 @@ func access() {
]
}
}`)
gock.New(config.C.URI).
Get("^/access/users$").
Reply(200).
JSON(`{
"data": [
{
"expire": 0,
"lastname": "pamlast",
"enable": 1,
"firstname": "pamfirst",
"userid": "pam@pam",
"realm-type": "pam"
},
{
"expire": 0,
"realm-type": "pam",
"email": "root@email.com",
"userid": "root@pam",
"enable": 1
},
{
"expire": 0,
"lastname": "last1",
"email": "first1.last1@email.com",
"enable": 1,
"firstname": "first1",
"realm-type": "pve",
"userid": "user1@pve"
},
{
"userid": "user2@pve",
"realm-type": "pve",
"firstname": "first2",
"email": "first2.last2@email.com",
"enable": 1,
"lastname": "last2",
"expire": 0
}
]
}`)

gock.New(config.C.URI).
Get("^/access/users/root@pam$").
Reply(200).
JSON(`{
"data": {
"groups": [
"cloud-init",
"test"
],
"expire": 0,
"email": "root@email.com",
"enable": 1,
"firstname": "firstname",
"lastname": "lastname",
"tokens": {
"token1": {
"privsep": 0,
"expire": 1000
},
"token2": {
"expire": 2000,
"privsep": 1
}
}
}
}`)

}
26 changes: 26 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1004,3 +1004,29 @@ type Group struct {
Users string `json:"users,omitempty"` // only populated via Groups lister
Members []string `json:"members,omitempty"` // only populated via Group read
}

type Users []*User
type User struct {
client *Client
UserID string `json:"userid,omitempty"`
Comment string `json:"comment,omitempty"`
Email string `json:"email,omitempty"`
Enable IntOrBool `json:"enable,omitempty"`
Expire int `json:"expire,omitempty"`
Firstname string `json:"firstname,omitempty"`
Lastname string `json:"lastname,omitempty"`
Groups []string `json:"groups,omitempty"`
Keys string `json:"keys,omitempty"`
Tokens map[string]Token `json:"tokens,omitempty"`
RealmType string `json:"realm-type,omitempty"`
TFALockedUntil string `json:"tfa-locked-until,omitempty"`
TOTPLocked IntOrBool `json:"totp-locked,omitempty"`
}

type Tokens []*Token
type Token struct {
TokenID string `json:"tokenid,omitempty"`
Comment string `json:"comment,omitempty"`
Expire int `json:"expire,omitempty"`
Privsep IntOrBool `json:"privsep,omitempty"`
}

0 comments on commit c38a376

Please sign in to comment.