forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
214 lines (182 loc) · 5.67 KB
/
handler.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
* Copyright (c) 2018. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
package grpc
import (
"context"
"fmt"
"strings"
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/errors"
"go.uber.org/zap"
"github.com/pydio/cells/common"
"github.com/pydio/cells/common/log"
"github.com/pydio/cells/common/proto/idm"
"github.com/pydio/cells/common/service/context"
"github.com/pydio/cells/common/service/proto"
"github.com/pydio/cells/idm/role"
)
var (
defaultPolicies = []*service.ResourcePolicy{
{Subject: "profile:standard", Action: service.ResourcePolicyAction_READ, Effect: service.ResourcePolicy_allow},
{Subject: "profile:admin", Action: service.ResourcePolicyAction_WRITE, Effect: service.ResourcePolicy_allow},
}
)
// Handler definition
type Handler struct{}
// CreateRole adds a role and its policies in database
func (h *Handler) CreateRole(ctx context.Context, req *idm.CreateRoleRequest, resp *idm.CreateRoleResponse) error {
dao := servicecontext.GetDAO(ctx).(role.DAO)
if req.Role.Uuid != "" && strings.Contains(req.Role.Uuid, ",") {
return errors.BadRequest("forbidden.characters", "commas are not allowed in role uuid")
}
r, update, err := dao.Add(req.Role)
if err != nil {
return err
}
resp.Role = r
if len(r.Policies) == 0 {
r.Policies = defaultPolicies
} /* else {
for i, pol := range r.Policies {
fmt.Printf("%d. %s - action: %s\n", i, pol.Subject, pol.Action)
}
} */
err = dao.AddPolicies(update, r.Uuid, r.Policies)
if err != nil {
return err
}
if update {
// Propagate event
client.Publish(ctx, client.NewPublication(common.TopicIdmEvent, &idm.ChangeEvent{
Type: idm.ChangeEventType_UPDATE,
Role: r,
}))
log.Logger(ctx).Info(
fmt.Sprintf("Role [%s] has been updated", r.Label),
log.GetAuditId(common.AUDIT_ROLE_UPDATE),
r.ZapUuid(),
)
log.Auditer(ctx).Info(
fmt.Sprintf("Updated role [%s]", r.Label),
log.GetAuditId(common.AUDIT_ROLE_UPDATE),
r.ZapUuid(),
)
} else {
client.Publish(ctx, client.NewPublication(common.TopicIdmEvent, &idm.ChangeEvent{
Type: idm.ChangeEventType_CREATE,
Role: r,
}))
log.Logger(ctx).Info(
fmt.Sprintf("Role [%s] has been created", r.Label),
log.GetAuditId(common.AUDIT_ROLE_CREATE),
r.ZapUuid(),
)
log.Auditer(ctx).Info(
fmt.Sprintf("Created role [%s]", r.Label),
log.GetAuditId(common.AUDIT_ROLE_CREATE),
r.ZapUuid(),
)
}
return nil
}
// DeleteRole from database
func (h *Handler) DeleteRole(ctx context.Context, req *idm.DeleteRoleRequest, response *idm.DeleteRoleResponse) error {
dao := servicecontext.GetDAO(ctx).(role.DAO)
if req.Query == nil {
return errors.BadRequest(common.ServiceRole, "cannot send a DeleteRole request with an empty query")
}
var roles []*idm.Role
if err := dao.Search(req.Query, &roles); err != nil {
return err
}
numRows, err := dao.Delete(req.Query)
response.RowsDeleted = numRows
if err != nil {
return err
}
for _, r := range roles {
// FIXME errors where ignored until now. Should we stop and return an error
// or better handle the error?
err2 := dao.DeletePoliciesForResource(r.Uuid)
if err2 != nil {
log.Logger(ctx).Error("could not delete policies for removed role "+r.Label, zap.Error(err2))
continue
}
err2 = dao.DeletePoliciesBySubject(fmt.Sprintf("role:%s", r.Uuid))
if err2 != nil {
log.Logger(ctx).Error("could not delete policies by subject for removed role "+r.Label, zap.Error(err2))
continue
}
// propagate event
client.Publish(ctx, client.NewPublication(common.TopicIdmEvent, &idm.ChangeEvent{
Type: idm.ChangeEventType_DELETE,
Role: r,
}))
log.Auditer(ctx).Info(
fmt.Sprintf("Deleted role [%s]", r.Label),
log.GetAuditId(common.AUDIT_ROLE_DELETE),
r.ZapUuid(),
)
}
return nil
}
// SearchRole in database
func (h *Handler) SearchRole(ctx context.Context, request *idm.SearchRoleRequest, response idm.RoleService_SearchRoleStream) error {
dao := servicecontext.GetDAO(ctx).(role.DAO)
var roles []*idm.Role
defer response.Close()
if err := dao.Search(request.Query, &roles); err != nil {
return err
}
for _, r := range roles {
response.Send(&idm.SearchRoleResponse{Role: r})
}
return nil
}
// CountRole in database
func (h *Handler) CountRole(ctx context.Context, request *idm.SearchRoleRequest, response *idm.CountRoleResponse) error {
dao := servicecontext.GetDAO(ctx).(role.DAO)
count, err := dao.Count(request.Query);
if err != nil {
return err
}
response.Count = count
return nil
}
// StreamRole from database
func (h *Handler) StreamRole(ctx context.Context, streamer idm.RoleService_StreamRoleStream) error {
dao := servicecontext.GetDAO(ctx).(role.DAO)
defer streamer.Close()
for {
incoming, err := streamer.Recv()
if incoming == nil || err != nil {
break
}
var roles []*idm.Role
if err := dao.Search(incoming.Query, &roles); err != nil {
return err
}
for _, r := range roles {
streamer.Send(&idm.SearchRoleResponse{Role: r})
}
streamer.Send(nil)
}
return nil
}