This repository has been archived by the owner on Mar 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
group.go
533 lines (412 loc) · 12.6 KB
/
group.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
/*** Copyright (c) 2016, The BioTeam, Inc. ***
*** For more information please refer to the LICENSE.md file ***/
package gorods
// #include "wrapper.h"
import "C"
import (
"fmt"
"strconv"
"strings"
"time"
"unsafe"
)
// Group holds info about iRODS groups
type Group struct {
name string
createTime time.Time
modifyTime time.Time
id int
typ int
zone *Zone
info string
comment string
parentSlice *Groups
hasInit bool
users Users
con *Connection
metaCol *MetaCollection
}
// Groups is a slice of *Group structs.
type Groups []*Group
// initGroup only accepts what's needed for lazing loading later on.
func initGroup(name string, con *Connection) (*Group, error) {
grp := new(Group)
grp.name = name
grp.con = con
grp.typ = GroupType
if z, err := con.LocalZone(); err != nil {
return nil, err
} else {
grp.zone = z
}
return grp, nil
}
// Name returns the name of the group.
func (grp *Group) Name() string {
return grp.name
}
// Path returns the name of the group. Used in gorods.MetaObj interface.
func (grp *Group) Path() string {
return grp.name
}
// Zone returns the *Zone struct that this group belongs to.
func (grp *Group) Zone() *Zone {
return grp.zone
}
// Comment loads data from iRODS if needed, and returns the group's comment attribute.
func (grp *Group) Comment() (string, error) {
if err := grp.init(); err != nil {
return grp.comment, err
}
return grp.comment, nil
}
// CreateTime loads data from iRODS if needed, and returns the group's createTime attribute.
func (grp *Group) CreateTime() (time.Time, error) {
if err := grp.init(); err != nil {
return grp.createTime, err
}
return grp.createTime, nil
}
// ModifyTime loads data from iRODS if needed, and returns the group's modifyTime attribute.
func (grp *Group) ModifyTime() (time.Time, error) {
if err := grp.init(); err != nil {
return grp.modifyTime, err
}
return grp.modifyTime, nil
}
// Id loads data from iRODS if needed, and returns the group's Id attribute.
func (grp *Group) Id() (int, error) {
if err := grp.init(); err != nil {
return grp.id, err
}
return grp.id, nil
}
// Info loads data from iRODS if needed, and returns the group's Info attribute.
func (grp *Group) Info() (string, error) {
if err := grp.init(); err != nil {
return grp.info, err
}
return grp.info, nil
}
// Type returns GroupType, used in AccessObject and MetaObj interfaces
func (grp *Group) Type() int {
return grp.typ
}
// Con returns the *Connection used to fetch group info
func (grp *Group) Con() *Connection {
return grp.con
}
// Users loads data from iRODS if needed, and returns the group's Users slice.
func (grp *Group) Users() (Users, error) {
if err := grp.init(); err != nil {
return nil, err
}
return grp.users, nil
}
// Delete deletes the group from iCAT server. Refreshes Connection group cache.
func (grp *Group) Delete() error {
if err := deleteGroup(grp.Name(), grp.Zone(), grp.con); err != nil {
return err
}
if err := grp.con.RefreshGroups(); err != nil {
return err
}
return nil
}
func (grp *Group) init() error {
if !grp.hasInit {
if err := grp.RefreshInfo(); err != nil {
return err
}
if err := grp.RefreshUsers(); err != nil {
return err
}
grp.hasInit = true
}
return nil
}
// FindByName searches the slice (itself) and attempts to return a match based on name.
// If no match is found, a new group with that name is created and returned.
// This was designed to resolve issues of casting resources for DataObjects and Collections, even though the cache was empty due to permissions.
func (grps Groups) FindByName(name string, con *Connection) *Group {
for _, grp := range grps {
if grp.name == name {
return grp
}
}
grp, _ := initGroup(name, con)
return grp
}
func (grps *Groups) Remove(index int) {
*grps = append((*grps)[:index], (*grps)[index+1:]...)
}
// Remove deletes the group from the parent slice
func (grp *Group) Remove() bool {
for n, p := range *grp.parentSlice {
if p.name == grp.name {
grp.parentSlice.Remove(n)
return true
}
}
return false
}
// String returns the group name
func (grp *Group) String() string {
return fmt.Sprintf("%v", grp.name)
}
// RefreshInfo refreshes the attributes of the group, pulling fresh data from the iCAT server.
func (grp *Group) RefreshInfo() error {
// r_comment:
// create_ts:01471444167
// modify_ts:01471444167
// user_id:10019
// user_name:designers
// user_type_name:rodsgroup
// zone_name:tempZone
// user_info:
if infoMap, err := grp.FetchInfo(); err == nil {
grp.comment = infoMap["r_comment"]
grp.createTime = timeStringToTime(infoMap["create_ts"])
grp.modifyTime = timeStringToTime(infoMap["modify_ts"])
grp.id, _ = strconv.Atoi(infoMap["user_id"])
grp.info = infoMap["user_info"]
if zones, err := grp.con.Zones(); err != nil {
return err
} else {
if zne := zones.FindByName(infoMap["zone_name"], grp.con); zne != nil {
grp.zone = zne
} else {
return newError(Fatal, -1, fmt.Sprintf("iRODS Refresh Group Info Failed: Unable to locate zone in cache"))
}
}
} else {
return err
}
return nil
}
// RefreshUsers pulls fresh data from the iCAT server and sets the internal field returned by *Group.Users
func (grp *Group) RefreshUsers() error {
if usrs, err := grp.FetchUsers(); err == nil {
grp.users = usrs
} else {
return err
}
return nil
}
// FetchInfo returns a map of fresh group info from the iCAT server
func (grp *Group) FetchInfo() (map[string]string, error) {
var (
result C.goRodsStringResult_t
err *C.char
)
result.size = C.int(0)
cGroup := C.CString(grp.name)
defer C.free(unsafe.Pointer(cGroup))
ccon := grp.con.GetCcon()
if status := C.gorods_get_user(cGroup, ccon, &result, &err); status != 0 {
grp.con.ReturnCcon(ccon)
return nil, newError(Fatal, status, fmt.Sprintf("iRODS Get Group Info Failed: %v", C.GoString(err)))
}
grp.con.ReturnCcon(ccon)
defer C.gorods_free_string_result(&result)
unsafeArr := unsafe.Pointer(result.strArr)
arrLen := int(result.size)
// Convert C array to slice, backed by arr *C.char
slice := (*[1 << 30]*C.char)(unsafeArr)[:arrLen:arrLen]
response := make(map[string]string)
for _, groupInfo := range slice {
groupAttributes := strings.Split(strings.Trim(C.GoString(groupInfo), " \n"), "\n")
for _, attr := range groupAttributes {
split := strings.Split(attr, ": ")
attrName := split[0]
attrVal := split[1]
response[attrName] = attrVal
}
}
return response, nil
}
// FetchUsers returns a slice of fresh *User from the iCAT server
func (grp *Group) FetchUsers() (Users, error) {
var (
result C.goRodsStringResult_t
err *C.char
)
result.size = C.int(0)
cGroupName := C.CString(grp.name)
defer C.free(unsafe.Pointer(cGroupName))
ccon := grp.con.GetCcon()
if status := C.gorods_get_group(ccon, &result, cGroupName, &err); status != 0 {
grp.con.ReturnCcon(ccon)
if status == C.CAT_NO_ROWS_FOUND {
return make(Users, 0), nil
} else {
return nil, newError(Fatal, status, fmt.Sprintf("iRODS Get Group %v Failed: %v", grp.name, C.GoString(err)))
}
}
grp.con.ReturnCcon(ccon)
defer C.gorods_free_string_result(&result)
unsafeArr := unsafe.Pointer(result.strArr)
arrLen := int(result.size)
// Convert C array to slice, backed by arr *C.char
slice := (*[1 << 30]*C.char)(unsafeArr)[:arrLen:arrLen]
if usrs, err := grp.con.Users(); err == nil {
response := make(Users, 0)
for _, userNames := range slice {
usrFrags := strings.Split(C.GoString(userNames), "#")
if usr := usrs.FindByName(usrFrags[0], grp.con); usr != nil {
response = append(response, usr)
} else {
return nil, newError(Fatal, -1, fmt.Sprintf("iRODS FetchUsers Failed: User in response not found in cache"))
}
}
return response, nil
} else {
return nil, err
}
}
// AddUser adds an iRODS uset to the group. Accepts a string or *User struct.
func (grp *Group) AddUser(usr interface{}) error {
switch usr.(type) {
case string:
// Need to lookup user by string in cache for zone info
if usrs, err := grp.con.Users(); err == nil {
usrName := usr.(string)
if existingUsr := usrs.FindByName(usrName, grp.con); existingUsr != nil {
zoneName := existingUsr.zone
return addToGroup(usrName, zoneName, grp.name, grp.con)
} else {
return newError(Fatal, -1, fmt.Sprintf("iRODS AddUser Failed: can't find iRODS user by string"))
}
} else {
return err
}
case *User:
aUsr := usr.(*User)
return addToGroup(aUsr.name, aUsr.zone, grp.name, aUsr.con)
default:
}
return newError(Fatal, -1, fmt.Sprintf("iRODS AddUser Failed: unknown type passed"))
}
// RemoveUser removes an iRODS user from the group. Accepts a string or *User struct.
func (grp *Group) RemoveUser(usr interface{}) error {
switch usr.(type) {
case string:
// Need to lookup user by string in cache for zone info
if usrs, err := grp.con.Users(); err == nil {
usrName := usr.(string)
if existingUsr := usrs.FindByName(usrName, grp.con); existingUsr != nil {
zoneName := existingUsr.zone
return removeFromGroup(usrName, zoneName, grp.name, grp.con)
} else {
return newError(Fatal, -1, fmt.Sprintf("iRODS RemoveUser Failed: can't find iRODS user by string"))
}
} else {
return err
}
case *User:
aUsr := usr.(*User)
return removeFromGroup(aUsr.name, aUsr.zone, grp.name, aUsr.con)
default:
}
return newError(Fatal, -1, fmt.Sprintf("iRODS RemoveUser Failed: unknown type passed"))
}
// Attribute gets slice of Meta AVU triples, matching by Attribute name for Group
func (grp *Group) Attribute(attrName string) (Metas, error) {
if meta, err := grp.Meta(); err == nil {
return meta.Get(attrName)
} else {
return nil, err
}
}
// AddMeta adds a single Meta triple struct
func (grp *Group) AddMeta(m Meta) (nm *Meta, err error) {
var mc *MetaCollection
if mc, err = grp.Meta(); err != nil {
return
}
nm, err = mc.Add(m)
return
}
// DeleteMeta deletes a single Meta triple struct, identified by Attribute field
func (grp *Group) DeleteMeta(attr string) (*MetaCollection, error) {
if mc, err := grp.Meta(); err == nil {
return mc, mc.Delete(attr)
} else {
return nil, err
}
}
// Meta returns collection of Meta AVU triple structs of the group object
func (grp *Group) Meta() (*MetaCollection, error) {
if grp.metaCol == nil {
if mc, err := newMetaCollection(grp); err == nil {
grp.metaCol = mc
} else {
return nil, err
}
}
return grp.metaCol, nil
}
func addToGroup(userName string, zone *Zone, groupName string, con *Connection) error {
var (
err *C.char
)
cUserName := C.CString(userName)
cZoneName := C.CString(zone.Name())
cGroupName := C.CString(groupName)
defer C.free(unsafe.Pointer(cUserName))
defer C.free(unsafe.Pointer(cZoneName))
defer C.free(unsafe.Pointer(cGroupName))
ccon := con.GetCcon()
defer con.ReturnCcon(ccon)
if status := C.gorods_add_user_to_group(cUserName, cZoneName, cGroupName, ccon, &err); status != 0 {
return newError(Fatal, status, fmt.Sprintf("iRODS AddToGroup %v Failed: %v", groupName, C.GoString(err)))
}
return nil
}
func removeFromGroup(userName string, zone *Zone, groupName string, con *Connection) error {
var (
err *C.char
)
cUserName := C.CString(userName)
cZoneName := C.CString(zone.Name())
cGroupName := C.CString(groupName)
defer C.free(unsafe.Pointer(cUserName))
defer C.free(unsafe.Pointer(cZoneName))
defer C.free(unsafe.Pointer(cGroupName))
ccon := con.GetCcon()
defer con.ReturnCcon(ccon)
if status := C.gorods_remove_user_from_group(cUserName, cZoneName, cGroupName, ccon, &err); status != 0 {
return newError(Fatal, status, fmt.Sprintf("iRODS AddToGroup %v Failed: %v", groupName, C.GoString(err)))
}
return nil
}
func deleteGroup(groupName string, zone *Zone, con *Connection) error {
var (
err *C.char
)
cZoneName := C.CString(zone.Name())
cGroupName := C.CString(groupName)
defer C.free(unsafe.Pointer(cZoneName))
defer C.free(unsafe.Pointer(cGroupName))
ccon := con.GetCcon()
defer con.ReturnCcon(ccon)
if status := C.gorods_delete_group(cGroupName, cZoneName, ccon, &err); status != 0 {
return newError(Fatal, status, fmt.Sprintf("iRODS DeleteGroup %v Failed: %v", groupName, C.GoString(err)))
}
return nil
}
func createGroup(groupName string, zone *Zone, con *Connection) error {
var (
err *C.char
)
cZoneName := C.CString(zone.Name())
cGroupName := C.CString(groupName)
defer C.free(unsafe.Pointer(cZoneName))
defer C.free(unsafe.Pointer(cGroupName))
ccon := con.GetCcon()
defer con.ReturnCcon(ccon)
if status := C.gorods_create_group(cGroupName, cZoneName, ccon, &err); status != 0 {
return newError(Fatal, status, fmt.Sprintf("iRODS CreateGroup %v Failed: %v", groupName, C.GoString(err)))
}
return nil
}