-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathuser.go
84 lines (74 loc) · 1.99 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
package Cx1ClientGo
import "fmt"
func (u User) String() string {
if u.FirstName == "" && u.LastName == "" && u.Email == "" {
return fmt.Sprintf("[%v] %v", ShortenGUID(u.UserID), u.UserName)
}
return fmt.Sprintf("[%v] %v %v (%v)", ShortenGUID(u.UserID), u.FirstName, u.LastName, u.Email)
}
func (u WhoAmI) String() string {
return fmt.Sprintf("[%v] %v", ShortenGUID(u.UserID), u.Name)
}
func (u User) HasRole(role *Role) (bool, error) {
return u.HasRoleByID(role.RoleID)
}
func (u User) HasRoleByID(roleID string) (bool, error) {
if !u.FilledRoles {
return false, fmt.Errorf("user roles have not been retrieved, use GetUserRoles(user)")
}
for _, r := range u.Roles {
if r.RoleID == roleID {
return true, nil
}
}
return false, nil
}
func (u User) HasRoleByName(role string) (bool, error) {
if !u.FilledRoles {
return false, fmt.Errorf("user roles have not been retrieved, use GetUserRoles(user)")
}
for _, r := range u.Roles {
if r.Name == role {
return true, nil
}
}
return false, nil
}
func (u User) IsInGroup(group *Group) (bool, error) {
return u.IsInGroupByID(group.GroupID)
}
func (u User) IsInGroupByID(groupId string) (bool, error) {
if !u.FilledGroups {
return false, fmt.Errorf("user groups have not been retrieved, use GetUserGroups(user)")
}
for _, g := range u.Groups {
if g.GroupID == groupId {
return true, nil
}
}
return false, nil
}
func (u User) IsInGroupByName(groupName string) (bool, error) {
if !u.FilledGroups {
return false, fmt.Errorf("user groups have not been retrieved, use GetUserGroups(user)")
}
for _, g := range u.Groups {
if g.Name == groupName {
return true, nil
}
}
return false, nil
}
func (u *User) AddGroup(client *Cx1Client, group *Group) error {
//client.AddUserRoles( u,
return nil
}
func (u User) Save(client *Cx1Client) error {
return client.UpdateUser(&u)
}
func (u User) Delete(client *Cx1Client) error {
return client.DeleteUser(&u)
}
func (u User) Link(client *Cx1Client) string {
return client.UserLink(&u)
}