-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
66 lines (53 loc) · 1.4 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
package goscm
import (
"encoding/base64"
"encoding/json"
)
type UserData struct {
Name string `json:"name"`
DisplayName string `json:"displayName"`
Mail string `json:"mail"`
External bool `json:"external"`
Password string `json:"password"`
Active bool `json:"active"`
}
// LoginUser attempts to use the user data to log in.
// This can be used to initialize a user profile that
// is authorized to but was never logged in before.
func LoginUser(baseUrl string, username string, password string) error {
c, err := NewClient(baseUrl, "")
if err != nil {
return err
}
headers := make(map[string]string)
token := "Basic " + base64.StdEncoding.EncodeToString([]byte(username+":"+password))
headers["Authorization"] = token
return c.getJson("/api/v2/me", nil, headers)
}
func (c *Client) CreateUser(userData UserData) error {
headers := make(map[string]string)
headers["Content-Type"] = mimeTypeUser
bytes, err := json.Marshal(userData)
if err != nil {
return err
}
err = c.post("/api/v2/users", bytes, headers)
if err != nil {
return err
}
return nil
}
func (c *Client) DeleteUser(name string) error {
return c.delete("/api/v2/users/"+name, nil)
}
func (c *Client) DeleteUserAndGroupMembership(name string) error {
err := c.DeleteUser(name)
if err != nil {
return err
}
err = c.DeleteUserFromAllGroups(name)
if err != nil {
return err
}
return nil
}