-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathuser_access_token.go
112 lines (93 loc) · 3.43 KB
/
user_access_token.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package zammad
import (
"fmt"
"time"
)
// UserAccessToken is a Zammad User access token. See https://docs.zammad.org/en/latest/api/user-access-token.html.
type UserAccessToken struct {
ID int `json:"id"`
Token string `json:"token,omitempty"` // Token is only set in the returned UserAccessToken in UserAccessTokenCreate.
Name string `json:"name,omitempty"` // Name is must be set and is only used in UserAccessTokenCreate.
UserID int `json:"user_id"`
Action string `json:"action"`
Label string `json:"label"`
Permission []string `json:"permission,omitempty"` // Permission must be set and is only used in UserAccessTokenCreate.
Preferences struct {
Permission []string `json:"permission"`
} `json:"preferences"`
LastUsedAt time.Time `json:"last_used_at"`
ExpiresAt Date `json:"expires_at,omitempty"` // ExpiresAt must be set when using in UserAccessTokenCreate.
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
// Permissions holds all available Zammad User access token permissions. It's only used UserAccessTokenList.
// When returns UserAccessTokens from UserAccessTokenList only the first access token will have a filled out
// permissions struct. For the remaining token this will be empty.
Permissions []Permission `json:"permissions,omitempty"`
}
// Permission is a Zammad permission.
type Permission struct {
ID int `json:"id"`
Name string `json:"name"`
Note string `json:"note"`
Preferences struct {
Translations []string `json:"translations"`
Disabled bool `json:"disabled"`
} `json:"preferences,omitempty"`
Active bool `json:"active"`
AllowSignup bool `json:"allow_signup"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// Date is a timestamp that is only specified as YYYY-MM-DD, without time zone or a clock time.
type Date struct {
time.Time
}
func (d *Date) UnmarshalJSON(data []byte) error {
t, err := time.Parse(`"2006-01-02"`, string(data))
if err != nil {
return err
}
(*d).Time = t
return nil
}
func (c *Client) UserAccessTokenList() ([]UserAccessToken, error) {
type TockList struct {
Tokens []UserAccessToken `json:"tokens"`
Permissions []Permission `json:"permissions"`
}
var tockList TockList
req, err := c.NewRequest("GET", fmt.Sprintf("%s%s", c.Url, "/api/v1/user_access_token"), nil)
if err != nil {
return nil, err
}
if err = c.sendWithAuth(req, &tockList); err != nil {
return nil, err
}
userAccessTokens := make([]UserAccessToken, len(tockList.Tokens))
copy(userAccessTokens, tockList.Tokens)
if len(tockList.Tokens) > 0 {
userAccessTokens[0].Permissions = tockList.Permissions
}
return userAccessTokens, nil
}
func (c *Client) UserAccessTokenCreate(t UserAccessToken) (UserAccessToken, error) {
var userAccessToken UserAccessToken
req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.Url, "/api/v1/user_access_token"), t)
if err != nil {
return userAccessToken, err
}
if err = c.sendWithAuth(req, &userAccessToken); err != nil {
return userAccessToken, err
}
return userAccessToken, nil
}
func (c *Client) UserAccessTokenDelete(tokenID int) error {
req, err := c.NewRequest("DELETE", fmt.Sprintf("%s%s", c.Url, fmt.Sprintf("/api/v1/user_access_token/%d", tokenID)), nil)
if err != nil {
return err
}
if err = c.sendWithAuth(req, nil); err != nil {
return err
}
return nil
}