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
/
user.go
506 lines (396 loc) · 11.8 KB
/
user.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
/*** 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"
)
// User contains information relating to an iRODS user
type User struct {
name string
zone *Zone
createTime time.Time
modifyTime time.Time
id int
typ int
info string
comment string
parentSlice *Users
hasInit bool
groups Groups
con *Connection
metaCol *MetaCollection
}
// Users is a slice of *User
type Users []*User
func initUser(name string, zone *Zone, con *Connection) (*User, error) {
usr := new(User)
usr.name = name
usr.zone = zone
usr.con = con
return usr, nil
}
// Remove removed the user from it's parent slice.
func (usr *User) Remove() bool {
for n, p := range *usr.parentSlice {
if p.name == usr.name {
usr.parentSlice.Remove(n)
return true
}
}
return false
}
// Name returns the users name.
func (usr *User) Name() string {
return usr.name
}
// Path returns the users name. Used in gorods.MetaObj interface.
func (usr *User) Path() string {
return usr.name
}
// Zone returns the *Zone to which the user belongs.
func (usr *User) Zone() *Zone {
return usr.zone
}
// Comment loads data from iRODS if needed, and returns the user's comment attribute.
func (usr *User) Comment() (string, error) {
if err := usr.init(); err != nil {
return usr.comment, err
}
return usr.comment, nil
}
// Info loads data from iRODS if needed, and returns the user's info attribute.
func (usr *User) Info() (string, error) {
if err := usr.init(); err != nil {
return usr.info, err
}
return usr.info, nil
}
// CreateTime loads data from iRODS if needed, and returns the user's createTime attribute.
func (usr *User) CreateTime() (time.Time, error) {
if err := usr.init(); err != nil {
return usr.createTime, err
}
return usr.createTime, nil
}
// ModifyTime loads data from iRODS if needed, and returns the user's modifyTime attribute.
func (usr *User) ModifyTime() (time.Time, error) {
if err := usr.init(); err != nil {
return usr.modifyTime, err
}
return usr.modifyTime, nil
}
// Id loads data from iRODS if needed, and returns the user's id attribute.
func (usr *User) Id() (int, error) {
if err := usr.init(); err != nil {
return usr.id, err
}
return usr.id, nil
}
// Type loads data from iRODS if needed, and returns the user's typ attribute. Used in AccessObject and MetaObj interfaces.
func (usr *User) Type() int {
if err := usr.init(); err != nil {
return UnknownType
}
return usr.typ
}
// Con returns the connection used to initalize the user
func (usr *User) Con() *Connection {
return usr.con
}
// Groups loads data from iRODS if needed, and returns the user's groups slice.
func (usr *User) Groups() (Groups, error) {
if err := usr.init(); err != nil {
return nil, err
}
return usr.groups, nil
}
// Delete deletes the user from the iCAT server
func (usr *User) Delete() error {
if err := deleteUser(usr.Name(), usr.Zone(), usr.con); err != nil {
return err
}
if err := usr.con.RefreshUsers(); err != nil {
return err
}
return nil
}
func (usr *User) init() error {
if !usr.hasInit {
if err := usr.RefreshInfo(); err != nil {
return err
}
if err := usr.RefreshGroups(); err != nil {
return err
}
usr.hasInit = true
}
return nil
}
// RefreshInfo fetches user data from iCAT, and unloads it into the *User fields
func (usr *User) RefreshInfo() error {
// create_ts:01471441907
// modify_ts:01471441907
// user_id:10011
// user_name:john
// user_type_name:rodsuser
// zone_name:tempZone
// user_info:
// r_comment:
typeMap := map[string]int{
"rodsuser": UserType,
"rodsadmin": AdminType,
"groupadmin": GroupAdminType,
}
if infoMap, err := usr.FetchInfo(); err == nil {
usr.comment = infoMap["r_comment"]
usr.createTime = timeStringToTime(infoMap["create_ts"])
usr.modifyTime = timeStringToTime(infoMap["modify_ts"])
usr.id, _ = strconv.Atoi(infoMap["user_id"])
usr.typ = typeMap[infoMap["user_type_name"]]
usr.info = infoMap["user_info"]
if zones, err := usr.con.Zones(); err != nil {
return err
} else {
if zne := zones.FindByName(infoMap["zone_name"], usr.con); zne != nil {
usr.zone = zne
} else {
return newError(Fatal, -1, fmt.Sprintf("iRODS Refresh User Info Failed: Unable to locate zone in cache"))
}
}
} else {
return err
}
return nil
}
// RefreshGroups fetches group data from iCAT, and unloads it into the *User fields
func (usr *User) RefreshGroups() error {
if grps, err := usr.FetchGroups(); err == nil {
usr.groups = grps
} else {
return err
}
return nil
}
// FindByName searches the slice for a user by name.
// If no match is found, a new user 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 (usrs Users) FindByName(name string, con *Connection) *User {
for _, usr := range usrs {
if usr.name == name {
return usr
}
}
zne, err := con.LocalZone()
if err != nil {
return nil
}
usr, _ := initUser(name, zne, con)
return usr
}
// Remove deletes an item from the slice based on the index.
func (usrs *Users) Remove(index int) {
*usrs = append((*usrs)[:index], (*usrs)[index+1:]...)
}
// String returns a user's type, name, and zone.
func (usr *User) String() string {
usr.init()
return fmt.Sprintf("%v:%v#%v", getTypeString(usr.typ), usr.name, usr.zone)
}
// FetchGroups fetches and returns fresh data about the user's groups from the iCAT server.
func (usr *User) FetchGroups() (Groups, error) {
var (
result C.goRodsStringResult_t
err *C.char
)
cName := C.CString(usr.name)
defer C.free(unsafe.Pointer(cName))
result.size = C.int(0)
ccon := usr.con.GetCcon()
if status := C.gorods_get_user_groups(ccon, cName, &result, &err); status != 0 {
usr.con.ReturnCcon(ccon)
return nil, newError(Fatal, status, fmt.Sprintf("iRODS Get Groups Failed: %v", C.GoString(err)))
}
usr.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 grps, err := usr.con.Groups(); err == nil {
response := make(Groups, 0)
for _, groupName := range slice {
gName := C.GoString(groupName)
if gName != usr.name {
grp := grps.FindByName(gName, usr.con)
if grp != nil {
response = append(response, grp)
} else {
return nil, newError(Fatal, -1, fmt.Sprintf("iRODS FetchGroups Failed: Group in response not found in cache"))
}
}
}
return response, nil
} else {
return nil, err
}
}
// FetchInfo fetches fresh user info from the iCAT server, and returns it as a map.
func (usr *User) FetchInfo() (map[string]string, error) {
var (
result C.goRodsStringResult_t
err *C.char
)
result.size = C.int(0)
cUser := C.CString(usr.name)
defer C.free(unsafe.Pointer(cUser))
ccon := usr.con.GetCcon()
if status := C.gorods_get_user(cUser, ccon, &result, &err); status != 0 {
usr.con.ReturnCcon(ccon)
return nil, newError(Fatal, status, fmt.Sprintf("iRODS Get Users Failed: %v", C.GoString(err)))
}
usr.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(Users, 0)
response := make(map[string]string)
for _, userInfo := range slice {
userAttributes := strings.Split(strings.Trim(C.GoString(userInfo), " \n"), "\n")
for _, attr := range userAttributes {
split := strings.Split(attr, ": ")
attrName := split[0]
attrVal := split[1]
response[attrName] = attrVal
}
}
return response, nil
}
// AddToGroup adds the user to an existing iRODS group.
// Accepts string or *Group types.
func (usr *User) AddToGroup(grp interface{}) error {
switch grp.(type) {
case string:
return addToGroup(usr.name, usr.zone, grp.(string), usr.con)
case *Group:
return addToGroup(usr.name, usr.zone, (grp.(*Group)).name, usr.con)
default:
}
return newError(Fatal, -1, fmt.Sprintf("iRODS AddToGroup Failed: unknown type passed"))
}
// RemoveFromGroup removes the user from an iRODS group.
// Accepts string or *Group types.
func (usr *User) RemoveFromGroup(grp interface{}) error {
switch grp.(type) {
case string:
return removeFromGroup(usr.name, usr.zone, grp.(string), usr.con)
case *Group:
return removeFromGroup(usr.name, usr.zone, (grp.(*Group)).name, usr.con)
default:
}
return newError(Fatal, -1, fmt.Sprintf("iRODS RemoveFromGroup Failed: unknown type passed"))
}
// ChangePassword changes the user's password.
// You will need to be a rodsadmin for this to succeed (I think).
func (usr *User) ChangePassword(newPass string) error {
var (
err *C.char
)
cUserName := C.CString(usr.Name())
cNewPass := C.CString(newPass)
cMyPass := C.CString(usr.Con().Options.Password)
defer C.free(unsafe.Pointer(cUserName))
defer C.free(unsafe.Pointer(cNewPass))
defer C.free(unsafe.Pointer(cMyPass))
ccon := usr.Con().GetCcon()
defer usr.Con().ReturnCcon(ccon)
if status := C.gorods_change_user_password(cUserName, cNewPass, cMyPass, ccon, &err); status != 0 {
return newError(Fatal, status, fmt.Sprintf("iRODS ChangePassword Failed: %v", C.GoString(err)))
}
return nil
}
// Attribute gets slice of Meta AVU triples, matching by Attribute name for User
func (usr *User) Attribute(attrName string) (Metas, error) {
if meta, err := usr.Meta(); err == nil {
return meta.Get(attrName)
} else {
return nil, err
}
}
// AddMeta adds a single Meta triple struct
func (usr *User) AddMeta(m Meta) (nm *Meta, err error) {
var mc *MetaCollection
if mc, err = usr.Meta(); err != nil {
return
}
nm, err = mc.Add(m)
return
}
// DeleteMeta deletes a single Meta triple struct, identified by Attribute field
func (usr *User) DeleteMeta(attr string) (*MetaCollection, error) {
if mc, err := usr.Meta(); err == nil {
return mc, mc.Delete(attr)
} else {
return nil, err
}
}
// Meta returns collection of Meta AVU triple structs of the user object
func (usr *User) Meta() (*MetaCollection, error) {
if usr.metaCol == nil {
if mc, err := newMetaCollection(usr); err == nil {
usr.metaCol = mc
} else {
return nil, err
}
}
return usr.metaCol, nil
}
func deleteUser(userName string, zone *Zone, con *Connection) error {
var (
err *C.char
)
cZoneName := C.CString(zone.Name())
cUserName := C.CString(userName)
defer C.free(unsafe.Pointer(cZoneName))
defer C.free(unsafe.Pointer(cUserName))
ccon := con.GetCcon()
defer con.ReturnCcon(ccon)
if status := C.gorods_delete_user(cUserName, cZoneName, ccon, &err); status != 0 {
return newError(Fatal, status, fmt.Sprintf("iRODS DeleteUser %v Failed: %v", userName, C.GoString(err)))
}
return nil
}
func createUser(userName string, zoneName string, typ int, con *Connection) error {
var (
err *C.char
cType *C.char
)
switch typ {
case AdminType:
cType = C.CString("rodsadmin")
case UserType:
cType = C.CString("rodsuser")
case GroupAdminType:
cType = C.CString("groupadmin")
default:
return newError(Fatal, -1, fmt.Sprintf("iRODS CreateUser Failed: Unknown user type passed"))
}
cZoneName := C.CString(zoneName)
cUserName := C.CString(userName)
defer C.free(unsafe.Pointer(cZoneName))
defer C.free(unsafe.Pointer(cUserName))
defer C.free(unsafe.Pointer(cType))
ccon := con.GetCcon()
defer con.ReturnCcon(ccon)
if status := C.gorods_create_user(cUserName, cZoneName, cType, ccon, &err); status != 0 {
return newError(Fatal, status, fmt.Sprintf("iRODS CreateUser %v Failed: %v", userName, C.GoString(err)))
}
return nil
}