/
user.go
138 lines (116 loc) · 3.73 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
// Copyright 2018 Bernhard Reitinger. All rights reserved.
package rex
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"github.com/tidwall/gjson"
)
// User stores information of the current user.
//
// The user can either contain the currentUser information,
// but also information from a user query. The SelfLink can be
// used to directly access the data, but is also often required
// for other operations (e.g. insert a project).
type User struct {
UserID string `json:"userId"`
Username string `json:"username,omitempty"`
Email string `json:"email,omitempty"`
FirstName string `json:"firstName,omitempty"`
LastName string `json:"lastName,omitempty"`
LastLogin string `json:"lastLogin,omitempty"`
SelfLink string
Roles []string `json:"roles,omitempty"`
Links struct {
User struct {
Href string `json:"href"`
} `json:"user"`
} `json:"_links,omitempty"`
}
var (
apiCurrentUser = "/api/v2/users/current"
apiUsers = "/api/v2/users"
apiFindByEmail = "/api/v2/users/search/findUserIdByEmail?email="
apiFindByID = "/api/v2/users/search/findByUserId?userId="
)
// String nicely prints out the user information.
func (u User) String() string {
s := fmt.Sprintf("|-------------------------------------------------------------------------------|\n")
s += fmt.Sprintf("| UserId | %-65s |\n", u.UserID)
s += fmt.Sprintf("| Username | %-65s |\n", u.Username)
s += fmt.Sprintf("| Firstname | %-65s |\n", u.FirstName)
s += fmt.Sprintf("| Lastname | %-65s |\n", u.LastName)
s += fmt.Sprintf("| Email | %-65s |\n", u.Email)
s += fmt.Sprintf("| LastLogin | %-65s |\n", u.LastLogin)
s += fmt.Sprintf("| Self | %-65s |\n", u.SelfLink)
s += fmt.Sprintf("|-------------------------------------------------------------------------------|\n")
return s
}
// GetCurrentUser gets the user details of the current user.
//
// The current user is the one which has been identified by the authentication token.
// When a new client is created by NewClient, this function will already be called implicitly.
func GetCurrentUser(e Executor) (*User, error) {
req, _ := http.NewRequest("GET", RexBaseURL+apiCurrentUser, nil)
resp, err := e.Execute(req)
if err != nil {
return nil, err
}
defer func() {
io.Copy(ioutil.Discard, resp.Body)
}()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var u User
err = json.Unmarshal(body, &u)
u.SelfLink = u.Links.User.Href // assign self link
return &u, err
}
// GetTotalNumberOfUsers returns the number of registered users.
//
// Requires admin permissions!
func GetTotalNumberOfUsers(e Executor) (uint64, error) {
req, _ := http.NewRequest("GET", RexBaseURL+apiUsers, nil)
resp, err := e.Execute(req)
if err != nil {
return 0, err
}
defer func() {
io.Copy(ioutil.Discard, resp.Body)
}()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return 0, err
}
return gjson.Get(string(body), "page.totalElements").Uint(), nil
}
// GetUserByEmail retrieves the user information based on a given email address
func GetUserByEmail(e Executor, email string) (*User, error) {
req, _ := http.NewRequest("GET", RexBaseURL+apiFindByEmail+email, nil)
r, err := e.Execute(req)
if err != nil {
return nil, err
}
// check if the user can be found
var user User
err = json.NewDecoder(r.Body).Decode(&user)
io.Copy(ioutil.Discard, r.Body)
if err != nil || user.UserID == "" {
return &User{}, fmt.Errorf("user not found")
}
// Fetch actual user information based on the retrieved UserID
req, _ = http.NewRequest("GET", RexBaseURL+apiFindByID+user.UserID, nil)
r, err = e.Execute(req)
if err != nil {
return nil, err
}
defer func() {
io.Copy(ioutil.Discard, r.Body)
}()
err = json.NewDecoder(r.Body).Decode(&user)
return &user, nil
}