/
handler_list_users.go
100 lines (90 loc) · 3.16 KB
/
handler_list_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
package charond
import (
"context"
"github.com/piotrkowalczuk/charon"
charonrpc "github.com/piotrkowalczuk/charon/pb/rpc/charond/v1"
"github.com/piotrkowalczuk/charon/internal/grpcerr"
"github.com/piotrkowalczuk/charon/internal/mapping"
"github.com/piotrkowalczuk/charon/internal/model"
"github.com/piotrkowalczuk/charon/internal/session"
"github.com/piotrkowalczuk/ntypes"
"github.com/piotrkowalczuk/qtypes"
"google.golang.org/grpc/codes"
)
type listUsersHandler struct {
*handler
}
func (luh *listUsersHandler) List(ctx context.Context, req *charonrpc.ListUsersRequest) (*charonrpc.ListUsersResponse, error) {
act, err := luh.Actor(ctx)
if err != nil {
return nil, err
}
if err = luh.firewall(req, act); err != nil {
return nil, err
}
cri := &model.UserCriteria{
IsSuperuser: allocNilBool(req.IsSuperuser),
IsStaff: allocNilBool(req.IsStaff),
CreatedBy: req.CreatedBy,
}
if !act.User.IsSuperuser {
cri.IsSuperuser = *ntypes.False()
}
if !act.Permissions.Contains(charon.UserCanRetrieveStaffAsStranger) {
cri.IsStaff = *ntypes.False()
}
if act.Permissions.Contains(charon.UserCanRetrieveAsOwner, charon.UserCanRetrieveStaffAsOwner) {
cri.CreatedBy = qtypes.EqualInt64(act.User.ID)
}
ents, err := luh.repository.user.Find(ctx, &model.UserFindExpr{
OrderBy: mapping.OrderBy(req.OrderBy),
Offset: req.Offset.Int64Or(0),
Limit: req.Limit.Int64Or(10),
Where: cri,
})
if err != nil {
return nil, grpcerr.E(codes.Internal, "find users query failed", err)
}
return luh.response(ents)
}
func (luh *listUsersHandler) firewall(req *charonrpc.ListUsersRequest, act *session.Actor) error {
if act.User.IsSuperuser {
return nil
}
if req.IsSuperuser.BoolOr(false) {
return grpcerr.E(codes.PermissionDenied, "only superuser is permitted to retrieve other superusers")
}
// STAFF USERS
if req.IsStaff.BoolOr(false) {
if req.CreatedBy != nil && req.CreatedBy.Value() == act.User.ID {
if !act.Permissions.Contains(charon.UserCanRetrieveStaffAsStranger, charon.UserCanRetrieveStaffAsOwner) {
return grpcerr.E(codes.PermissionDenied, "list of staff users cannot be retrieved as an owner, missing permission")
}
return nil
}
if !act.Permissions.Contains(charon.UserCanRetrieveStaffAsStranger) {
return grpcerr.E(codes.PermissionDenied, "list of staff users cannot be retrieved as a stranger, missing permission")
}
return nil
}
// NON STAFF USERS
if req.CreatedBy != nil && req.CreatedBy.Value() == act.User.ID {
if !act.Permissions.Contains(charon.UserCanRetrieveAsStranger, charon.UserCanRetrieveAsOwner) {
return grpcerr.E(codes.PermissionDenied, "list of users cannot be retrieved as an owner, missing permission")
}
return nil
}
if !act.Permissions.Contains(charon.UserCanRetrieveAsStranger) {
return grpcerr.E(codes.PermissionDenied, "list of users cannot be retrieved as a stranger, missing permission")
}
return nil
}
func (luh *listUsersHandler) response(ents []*model.UserEntity) (*charonrpc.ListUsersResponse, error) {
msg, err := mapping.ReverseUsers(ents)
if err != nil {
return nil, grpcerr.E(codes.Internal, "user reverse mapping failure")
}
return &charonrpc.ListUsersResponse{
Users: msg,
}, nil
}