|
| 1 | +package rbac |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "time" |
| 6 | +) |
| 7 | + |
| 8 | +const ( |
| 9 | + requestUsersURI = "/users" // #nosec - this is the uri to g et RBAC tokens |
| 10 | + requestCurrentUserURI = "/users/current" // #nosec - this is the uri to authenticate RBAC tokens |
| 11 | + requestUserURI = "/users/" // #nosec - this is the uri to revoke individual RBAC tokens |
| 12 | +) |
| 13 | + |
| 14 | +// User describes the user keys. |
| 15 | +type User struct { |
| 16 | + ID string `json:"id"` |
| 17 | + Login string `json:"login"` |
| 18 | + Email string `json:"email,omitempty"` |
| 19 | + DisplayName string `json:"display_name,omitempty"` |
| 20 | + RoleIDs []int `json:"role_ids,omitempty"` |
| 21 | + IsGroup bool `json:"is_group,omitempty"` |
| 22 | + IsRemote bool `json:"is_remote,omitempty"` |
| 23 | + IsUser bool `json:"is_user,omitempty"` |
| 24 | + IsSuperUser bool `json:"is_superuser,omitempty"` |
| 25 | + IsRevoked bool `json:"is_revoked,omitempty"` |
| 26 | + LastLogin time.Time `json:"last_login,omitempty"` |
| 27 | + InheritedRoleIDs []int `json:"inherited_role_ids,omitempty"` |
| 28 | + GroupIDs []string `json:"group_ids,omitempty"` |
| 29 | +} |
| 30 | + |
| 31 | +// GetUsers returns all the users in the system. |
| 32 | +func (c *Client) GetUsers(token string) ([]User, error) { |
| 33 | + users := []User{} |
| 34 | + r, err := c.resty.R(). |
| 35 | + SetHeader("X-Authentication", token). |
| 36 | + SetResult(&users). |
| 37 | + Get(requestUsersURI) |
| 38 | + if err != nil { |
| 39 | + return nil, FormatError(r, err.Error()) |
| 40 | + } |
| 41 | + if r.IsError() { |
| 42 | + if r.Error() != nil { |
| 43 | + return nil, FormatError(r) |
| 44 | + } |
| 45 | + return nil, FormatError(r) |
| 46 | + } |
| 47 | + return users, nil |
| 48 | +} |
| 49 | + |
| 50 | +// GetCurrentUser will return the current user details. |
| 51 | +func (c *Client) GetCurrentUser(token string) (*User, error) { |
| 52 | + user := User{} |
| 53 | + r, err := c.resty.R(). |
| 54 | + SetHeader("X-Authentication", token). |
| 55 | + SetResult(&user). |
| 56 | + Get(requestCurrentUserURI) |
| 57 | + if err != nil { |
| 58 | + return nil, FormatError(r, err.Error()) |
| 59 | + } |
| 60 | + if r.IsError() { |
| 61 | + if r.Error() != nil { |
| 62 | + return nil, FormatError(r) |
| 63 | + } |
| 64 | + return nil, FormatError(r) |
| 65 | + } |
| 66 | + return &user, nil |
| 67 | +} |
| 68 | + |
| 69 | +// GetSpecificUser will return a specific user details |
| 70 | +func (c *Client) GetSpecificUser(token string, sid string) (*User, error) { |
| 71 | + user := User{} |
| 72 | + r, err := c.resty.R(). |
| 73 | + SetHeader("X-Authentication", token). |
| 74 | + SetResult(&user). |
| 75 | + Get(fmt.Sprintf("%s%s", requestUserURI, sid)) |
| 76 | + if err != nil { |
| 77 | + return nil, FormatError(r, err.Error()) |
| 78 | + } |
| 79 | + if r.IsError() { |
| 80 | + if r.Error() != nil { |
| 81 | + return nil, FormatError(r) |
| 82 | + } |
| 83 | + return nil, FormatError(r) |
| 84 | + } |
| 85 | + return &user, nil |
| 86 | +} |
0 commit comments