-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathgroups.go
229 lines (194 loc) · 5.44 KB
/
groups.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// +build !oss
/*
* Copyright 2018 Dgraph Labs, Inc. All rights reserved.
*
* Licensed under the Dgraph Community License (the "License"); you
* may not use this file except in compliance with the License. You
* may obtain a copy of the License at
*
* https://github.com/dgraph-io/dgraph/blob/master/licenses/DCL.txt
*/
package acl
import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/dgraph-io/dgo"
"github.com/dgraph-io/dgo/protos/api"
"github.com/dgraph-io/dgraph/ee/acl"
"github.com/dgraph-io/dgraph/x"
"github.com/golang/glog"
)
func groupAdd(dc *dgo.Dgraph) error {
groupId := GroupAdd.Conf.GetString("group")
if len(groupId) == 0 {
return fmt.Errorf("the group id should not be empty")
}
ctx := context.Background()
txn := dc.NewTxn()
defer txn.Discard(ctx)
group, err := queryGroup(txn, ctx, groupId, []string{"uid"})
if err != nil {
return err
}
if group != nil {
return fmt.Errorf("The group with id %v already exists.", groupId)
}
createGroupNQuads := []*api.NQuad{
{
Subject: "_:newgroup",
Predicate: "dgraph.xid",
ObjectValue: &api.Value{Val: &api.Value_StrVal{StrVal: groupId}},
},
}
mu := &api.Mutation{
CommitNow: true,
Set: createGroupNQuads,
}
_, err = txn.Mutate(ctx, mu)
if err != nil {
return fmt.Errorf("Unable to create user: %v", err)
}
glog.Infof("Created new group with id %v", groupId)
return nil
}
func groupDel(dc *dgo.Dgraph) error {
groupId := GroupDel.Conf.GetString("group")
if len(groupId) == 0 {
return fmt.Errorf("the group id should not be empty")
}
ctx := context.Background()
txn := dc.NewTxn()
defer txn.Discard(ctx)
group, err := queryGroup(txn, ctx, groupId, []string{"uid"})
if err != nil {
return err
}
if group == nil || len(group.Uid) == 0 {
return fmt.Errorf("Unable to delete group because it does not exist: %v", groupId)
}
deleteGroupNQuads := []*api.NQuad{
{
Subject: group.Uid,
Predicate: x.Star,
ObjectValue: &api.Value{Val: &api.Value_DefaultVal{DefaultVal: x.Star}},
}}
mu := &api.Mutation{
CommitNow: true,
Del: deleteGroupNQuads,
}
_, err = txn.Mutate(ctx, mu)
if err != nil {
return fmt.Errorf("Unable to delete group: %v", err)
}
glog.Infof("Deleted user with id %v", groupId)
return nil
}
func queryGroup(txn *dgo.Txn, ctx context.Context, groupid string,
fields []string) (group *acl.Group, err error) {
var queryBuilder bytes.Buffer
// write query header
queryBuilder.WriteString(`
query search($groupid: string){
group(func: eq(dgraph.xid, $groupid)) {
`)
for _, f := range fields {
queryBuilder.WriteString(f)
queryBuilder.WriteString("\n")
}
// write query footer
queryBuilder.WriteString(`
}
}`)
query := queryBuilder.String()
queryVars := make(map[string]string)
queryVars["$groupid"] = groupid
queryResp, err := txn.QueryWithVars(ctx, query, queryVars)
if err != nil {
glog.Errorf("Error while query group with id %s: %v", groupid, err)
return nil, err
}
group, err = acl.UnmarshallGroup(queryResp, "group")
if err != nil {
return nil, err
}
return group, nil
}
type Acl struct {
Predicate string `json:"predicate"`
Perm int32 `json:"perm"`
}
func chMod(dc *dgo.Dgraph) error {
groupId := ChMod.Conf.GetString("group")
predicate := ChMod.Conf.GetString("pred")
perm := ChMod.Conf.GetInt("perm")
if len(groupId) == 0 {
return fmt.Errorf("the groupid must not be empty")
}
if len(predicate) == 0 {
return fmt.Errorf("the predicate must not be empty")
}
ctx := context.Background()
txn := dc.NewTxn()
defer txn.Discard(ctx)
group, err := queryGroup(txn, ctx, groupId, []string{"uid", "dgraph.group.acl"})
if err != nil {
return err
}
if group == nil || len(group.Uid) == 0 {
return fmt.Errorf("Unable to change permission for group because it does not exist: %v", groupId)
}
currentAcls := []Acl{}
if len(group.Acls) != 0 {
if err := json.Unmarshal([]byte(group.Acls), ¤tAcls); err != nil {
return fmt.Errorf("Unable to unmarshal the acls associated with the group %v:%v", groupId, err)
}
}
newAcls, updated := updateAcl(currentAcls, &Acl{
Predicate: predicate,
Perm: int32(perm),
})
if !updated {
glog.Infof("Nothing needs to be changed for the permission of group:%v", groupId)
return nil
}
newAclBytes, err := json.Marshal(newAcls)
if err != nil {
return fmt.Errorf("Unable to marshal the updated acls:%v", err)
}
chModNQuads := &api.NQuad{
Subject: group.Uid,
Predicate: "dgraph.group.acl",
ObjectValue: &api.Value{Val: &api.Value_BytesVal{BytesVal: newAclBytes}},
}
mu := &api.Mutation{
CommitNow: true,
Set: []*api.NQuad{chModNQuads},
}
_, err = txn.Mutate(ctx, mu)
if err != nil {
return fmt.Errorf("Unable to change mutations for the group %v on predicate %v: %v", groupId, predicate, err)
}
glog.Infof("Successfully changed permission for group %v on predicate %v to %v", groupId, predicate, perm)
return nil
}
// returns whether the existing acls slice is changed
func updateAcl(acls []Acl, newAcl *Acl) ([]Acl, bool) {
for idx, acl := range acls {
if acl.Predicate == newAcl.Predicate {
if acl.Perm == newAcl.Perm {
return acls, false
}
if newAcl.Perm < 0 {
// remove the current acl from the array
copy(acls[idx:], acls[idx+1:])
return acls[:len(acls)-1], true
}
acls[idx].Perm = newAcl.Perm
return acls, true
}
}
// we do not find any existing acl matching the newAcl predicate
return append(acls, *newAcl), true
}