-
Notifications
You must be signed in to change notification settings - Fork 7
/
user_service.go
121 lines (101 loc) · 2.52 KB
/
user_service.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
113
114
115
116
117
118
119
120
121
package yext
import "fmt"
const (
userPath = "users"
rolePath = "roles"
)
var UserListMaxLimit = 50
type UserService struct {
client *Client
}
type UserListResponse struct {
Count int `json:"count"`
Users []*User `json:"users"`
}
type RolesListResponse struct {
Count int `json:"count"`
Roles []*Role `json:"roles"`
}
func (u *UserService) ListAll() ([]*User, error) {
var users []*User
var lr listRetriever = func(opts *ListOptions) (int, int, error) {
ulr, _, err := u.List(opts)
if err != nil {
return 0, 0, err
}
users = append(users, ulr.Users...)
return len(ulr.Users), ulr.Count, err
}
if err := listHelper(lr, &ListOptions{Limit: UserListMaxLimit}); err != nil {
return nil, err
} else {
return users, nil
}
}
func (u *UserService) List(opts *ListOptions) (*UserListResponse, *Response, error) {
requrl, err := addListOptions(userPath, opts)
if err != nil {
return nil, nil, err
}
v := &UserListResponse{}
r, err := u.client.DoRequest("GET", requrl, v)
if err != nil {
return nil, r, err
}
return v, r, nil
}
func (u *User) pathToUser() string {
return pathToUserId(u.GetId())
}
func pathToUserId(id string) string {
return fmt.Sprintf("%s/%s", userPath, id)
}
func (u *UserService) Get(id string) (*User, *Response, error) {
var v = &User{}
r, err := u.client.DoRequest("GET", pathToUserId(id), v)
if err != nil {
return nil, r, err
}
return v, r, nil
}
func (u *UserService) Edit(y *User) (*Response, error) {
return u.client.DoRequestJSON("PUT", y.pathToUser(), y, nil)
}
func (u *UserService) Create(y *User) (*Response, error) {
return u.client.DoRequestJSON("POST", userPath, y, nil)
}
func (u *UserService) Delete(y *User) (*Response, error) {
return u.client.DoRequest("DELETE", y.pathToUser(), nil)
}
func (u *UserService) ListRoles() (*RolesListResponse, *Response, error) {
v := &RolesListResponse{}
r, err := u.client.DoRequest("GET", rolePath, v)
if err != nil {
return nil, r, err
}
return v, r, nil
}
func (u *UserService) NewFolderACL(f *Folder, r Role) ACL {
return ACL{
Role: r,
On: f.Id,
AccountId: u.client.Config.AccountId,
AccessOn: ACCESS_FOLDER,
}
}
func (u *UserService) NewAccountACL(r Role) ACL {
return ACL{
Role: r,
On: u.client.Config.AccountId,
AccountId: u.client.Config.AccountId,
AccessOn: ACCESS_ACCOUNT,
}
}
func (u *UserService) NewLocationACL(l *Location, r Role) ACL {
return ACL{
Role: r,
On: l.GetId(),
AccountId: u.client.Config.AccountId,
AccessOn: ACCESS_LOCATION,
}
}