-
Notifications
You must be signed in to change notification settings - Fork 4
/
user.go
152 lines (123 loc) · 4.05 KB
/
user.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package onelogin
import (
"context"
"fmt"
)
// UserService handles communications with the authentication related methods on OneLogin.
type UserService service
// User represents a OneLogin user.
type User struct {
ActivatedAt string `json:"activated_at"`
CreatedAt string `json:"created_at"`
Email string `json:"email"`
Username string `json:"username"`
FirstName string `json:"firstname"`
GroupID int64 `json:"group_id"`
ID int64 `json:"id"`
InvalidLoginAttempts int64 `json:"invalid_login_attempts"`
InvitationSentAt string `json:"invitation_sent_at"`
LastLogin string `json:"last_login"`
LastName string `json:"lastname"`
LockedUntil string `json:"locked_until"`
Notes string `json:"notes"`
OpenidName string `json:"openid_name"`
LocaleCode string `json:"locale_code"`
PasswordChangedAt string `json:"password_changed_at"`
Phone string `json:"phone"`
Status int64 `json:"status"`
UpdatedAt string `json:"updated_at"`
DistinguishedName string `json:"distinguished_name"`
ExternalID string `json:"external_id"`
DirectoryID int64 `json:"directory_id"`
MemberOf []string `json:"member_of"`
SamAccountName string `json:"samaccountname"`
UserPrincipalName string `json:"userprincipalname"`
ManagerAdID int `json:"manager_ad_id"`
RoleIDs []int64 `json:"role_id"`
CustomAttributes map[string]string `json:"custom_attributes"`
}
type getUserQuery struct {
AfterCursor string `url:"after_cursor,omitempty"`
}
// GetUsers returns all the OneLogin users.
func (s *UserService) GetUsers(ctx context.Context) ([]*User, error) {
u := "/api/1/users"
var users []*User
var afterCursor string
for {
uu, err := addOptions(u, &getUserQuery{AfterCursor: afterCursor})
if err != nil {
return nil, err
}
req, err := s.client.NewRequest("GET", uu, nil)
if err != nil {
return nil, err
}
if err := s.client.AddAuthorization(ctx, req); err != nil {
return nil, err
}
var us []*User
resp, err := s.client.Do(ctx, req, &us)
if err != nil {
return nil, err
}
users = append(users, us...)
if resp.PaginationAfterCursor == nil {
break
}
afterCursor = *resp.PaginationAfterCursor
}
return users, nil
}
// GetUser returns a OneLogin user.
func (s *UserService) GetUser(ctx context.Context, id int64) (*User, error) {
u := fmt.Sprintf("/api/1/users/%v", id)
var users []*User
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, err
}
if err := s.client.AddAuthorization(ctx, req); err != nil {
return nil, err
}
_, err = s.client.Do(ctx, req, &users)
if err != nil {
return nil, err
}
return users[0], nil
}
// UpdateCustomAttributes returns a OneLogin user.
func (s *UserService) UpdateCustomAttributes(ctx context.Context, id int64, attributes map[string]string) error {
u := fmt.Sprintf("/api/1/users/%v/set_custom_attributes", id)
post := map[string]interface{}{
"custom_attributes": attributes,
}
req, err := s.client.NewRequest("PUT", u, post)
if err != nil {
return err
}
if err := s.client.AddAuthorization(ctx, req); err != nil {
return err
}
_, err = s.client.Do(ctx, req, nil)
if err != nil {
return err
}
return nil
}
// DeleteUser delete a OneLogin user.
func (s *UserService) DeleteUser(ctx context.Context, id int64) error {
u := fmt.Sprintf("/api/1/users/%v", id)
req, err := s.client.NewRequest("DELETE", u, nil)
if err != nil {
return err
}
if err := s.client.AddAuthorization(ctx, req); err != nil {
return err
}
_, err = s.client.Do(ctx, req, nil)
if err != nil {
return err
}
return nil
}