-
Notifications
You must be signed in to change notification settings - Fork 1
/
users.go
150 lines (122 loc) · 4.45 KB
/
users.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
package usermanagement
import (
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"github.com/zscaler/zscaler-sdk-go/zia/services/common"
)
const (
usersEndpoint = "/users"
enrollEndpoint = "/enroll"
)
type Users struct {
// User ID
ID int `json:"id"`
// User name. This appears when choosing users for policies.
Name string `json:"name,omitempty"`
// User email consists of a user name and domain name. It does not have to be a valid email address, but it must be unique and its domain must belong to the organization.
Email string `json:"email,omitempty"`
// List of Groups a user belongs to. Groups are used in policies.
Groups []common.IDNameExtensions `json:"groups,omitempty"`
// Department a user belongs to
Department *common.UserDepartment `json:"department,omitempty"`
// Additional information about this user.
Comments string `json:"comments,omitempty"`
// Temporary Authentication Email. If you enabled one-time tokens or links, enter the email address to which the Zscaler service sends the tokens or links. If this is empty, the service sends the email to the User email.
TempAuthEmail string `json:"tempAuthEmail,omitempty"`
// Accepted Authentication Methods. Support values are "BASIC" and "DIGEST"
AuthMethods []string `json:"authMethods,omitempty"`
// User's password. Applicable only when authentication type is Hosted DB. Password strength must follow what is defined in the auth settings.
Password string `json:"password,omitempty"`
// True if this user is an Admin user
AdminUser bool `json:"adminUser"`
// User type. Provided only if this user is not an end user.
Type string `json:"type,omitempty"`
Deleted bool `json:"deleted"`
}
type EnrollResult struct {
UserID int `json:"userId"`
}
type EnrollUserRequest struct {
AuthMethods []string `json:"authMethods"`
Password string `json:"password"`
}
func (service *Service) Get(userID int) (*Users, error) {
var user Users
err := service.Client.Read(fmt.Sprintf("%s/%d", usersEndpoint, userID), &user)
if err != nil {
return nil, err
}
service.Client.Logger.Printf("[DEBUG]returning user from Get: %d", user.ID)
return &user, nil
}
func (service *Service) GetUserByName(userName string) (*Users, error) {
var users []Users
err := service.Client.Read(fmt.Sprintf("%s?name=%s", usersEndpoint, url.QueryEscape(userName)), &users)
if err != nil {
return nil, err
}
for _, user := range users {
if strings.EqualFold(user.Name, userName) {
return &user, nil
}
}
return nil, fmt.Errorf("no user found with name: %s", userName)
}
func (service *Service) EnrollUser(userID int, request EnrollUserRequest) (*EnrollResult, error) {
if len(request.AuthMethods) == 0 {
err := errors.New("authMethods is required")
service.Client.Logger.Printf("[ERROR] enroll user failed: %v", err)
}
for _, method := range request.AuthMethods {
// method most be one of the following: BASIC, DIGEST
if method != "BASIC" && method != "DIGEST" {
err := fmt.Errorf("authMethods must be one of the following: BASIC, DIGEST. Found: %s", method)
service.Client.Logger.Printf("[ERROR] enroll user failed: %v", err)
return nil, err
}
}
_, err := service.Client.Create(fmt.Sprintf("%s/%d%s", usersEndpoint, userID, enrollEndpoint), request)
if err != nil {
return nil, err
}
return &EnrollResult{UserID: userID}, nil
}
func (service *Service) Create(user *Users) (*Users, error) {
resp, err := service.Client.Create(usersEndpoint, *user)
if err != nil {
return nil, err
}
createdUsers, ok := resp.(*Users)
if !ok {
return nil, errors.New("object returned from api was not a user pointer")
}
service.Client.Logger.Printf("[DEBUG]returning user from create: %v", createdUsers.ID)
return createdUsers, nil
}
func (service *Service) Update(userID int, users *Users) (*Users, *http.Response, error) {
resp, err := service.Client.UpdateWithPut(fmt.Sprintf("%s/%d", usersEndpoint, userID), *users)
if err != nil {
return nil, nil, err
}
updatedUser, _ := resp.(*Users)
service.Client.Logger.Printf("[DEBUG]returning user from update: %d", updatedUser.ID)
return updatedUser, nil, nil
}
func (service *Service) Delete(userID int) (*http.Response, error) {
err := service.Client.Delete(fmt.Sprintf("%s/%d", usersEndpoint, userID))
if err != nil {
return nil, err
}
return nil, nil
}
func (service *Service) GetAllUsers() ([]Users, error) {
var users []Users
err := common.ReadAllPages(service.Client, usersEndpoint, &users)
if err != nil {
return nil, err
}
return users, nil
}