-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathroles.go
73 lines (60 loc) · 1.75 KB
/
roles.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
package solus
import (
"context"
"fmt"
)
// RolesService handles all available methods with roles.
type RolesService service
// Role represents a role.
type Role struct {
ID int `json:"id"`
Name string `json:"name"`
IsDefault bool `json:"is_default"`
UsersCount int `json:"users_count"`
}
// RoleCreateRequest represents available properties for creating a new role.
type RoleCreateRequest struct {
Name string `json:"name"`
Permissions []int `json:"permissions,omitempty"`
}
// RolesResponse represents paginated list of roles.
// This cursor can be used for iterating over all available roles.
type RolesResponse struct {
paginatedResponse
Data []Role `json:"data"`
}
type roleResponse struct {
Data Role `json:"data"`
}
// Create creates new role.
func (s *RolesService) Create(ctx context.Context, data RoleCreateRequest) (Role, error) {
var resp roleResponse
return resp.Data, s.client.create(ctx, "roles", data, &resp)
}
// List lists roles.
func (s *RolesService) List(ctx context.Context) (RolesResponse, error) {
resp := RolesResponse{
paginatedResponse: paginatedResponse{
service: (*service)(s),
},
}
return resp, s.client.list(ctx, "roles", &resp)
}
// Get gets specified role.
func (s *RolesService) Get(ctx context.Context, id int) (Role, error) {
var resp roleResponse
return resp.Data, s.client.get(ctx, fmt.Sprintf("roles/%d", id), &resp)
}
// GetByName gets specified role by name.
func (s *RolesService) GetByName(ctx context.Context, name string) (Role, error) {
roles, err := s.List(ctx)
if err != nil {
return Role{}, err
}
for _, role := range roles.Data {
if role.Name == name {
return role, nil
}
}
return Role{}, fmt.Errorf("failed to get role by name %q: role not found", name)
}