forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
173 lines (145 loc) · 4.3 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
package models
import (
"reflect"
"strings"
"github.com/imdario/mergo"
"github.com/pydio/cells/common/proto/idm"
)
type User struct {
*idm.User
}
func (u *User) Equal(other *idm.User) bool {
result := u.Login == other.Login
result = result && (u.Uuid == other.Uuid)
result = result && (u.GroupLabel == other.GroupLabel)
result = result && (u.IsGroup == other.IsGroup)
result = result && (strings.TrimRight(u.GroupPath, "/") == strings.TrimRight(other.GroupPath, "/"))
result = result && roleEqual(u.Roles, other.Roles)
return result
}
// test if two user can be mergeable whose the same login name and auth source
func (u *User) IsMergeable(other *idm.User) bool {
result := (u.Login == other.Login) && !u.IsGroup && !other.IsGroup
result = result && (u.Attributes[idm.UserAttrOrigin] == other.Attributes[idm.UserAttrOrigin])
return result && (u.Attributes[idm.UserAttrAuthSource] == other.Attributes[idm.UserAttrAuthSource])
}
func (u *User) IsEmpty() bool {
return (u.Login == "") && (u.Uuid == "")
}
func (u *User) getRolesWithPrefix(prefix string) []*idm.Role {
var ret []*idm.Role
for _, role := range u.Roles {
if strings.HasPrefix(role.Uuid, prefix) {
ret = append(ret, role)
}
}
return ret
}
func (u *User) getRolesWithoutPrefix(prefix string) []*idm.Role {
var ret []*idm.Role
for _, role := range u.Roles {
if !strings.HasPrefix(role.Uuid, prefix) {
ret = append(ret, role)
}
}
return ret
}
func (u *User) GetUniqueId() string {
return u.Attributes["AuthSource"] + "/" + u.Login
}
func (u *User) Merge(idmUser *idm.User, options *MergeOptions) (*idm.User, error, bool) {
// If target user is Empty, that means it does not exist
// Then do the merge.
IdmUser := User{idmUser}
if IdmUser.IsEmpty() {
return u.User, nil, true
}
if u.Equal(idmUser) {
return idmUser, nil, false
}
// If users are mergeable
if u.IsMergeable(idmUser) {
newUser := clone(idmUser)
mergo.Merge(newUser, u.User, mergo.WithOverride, mergo.WithTransformers(&rolesTransfomer{options.ToMap()}))
// This means something has changed
if !reflect.DeepEqual(u.User, newUser) {
// Making sure the source is set
newUser.Attributes[idm.UserAttrOrigin] = options.Origin
newUser.Attributes[idm.UserAttrAuthSource] = options.AuthSource
return newUser, nil, true
}
}
return nil, nil, false
}
func clone(idmUser *idm.User) *idm.User {
ret := new(idm.User)
ret.Password = idmUser.Password
ret.GroupPath = idmUser.GroupPath
ret.GroupLabel = idmUser.GroupLabel
ret.Login = idmUser.Login
ret.Uuid = idmUser.Uuid
ret.Policies = idmUser.Policies
for _, role := range idmUser.Roles {
ret.Roles = append(ret.Roles, role)
}
ret.Attributes = make(map[string]string)
for key, value := range idmUser.Attributes {
ret.Attributes[key] = value
}
return ret
}
// Roles Transformers allows roles to be overriden during a merge
type rolesTransfomer struct {
options map[string]string
}
func (t rolesTransfomer) Transformer(typ reflect.Type) func(dst, src reflect.Value) error {
if typ == reflect.TypeOf([]*idm.Role{}) {
return func(dst, src reflect.Value) error {
rolesMap := make(map[string][]*Role)
for _, dstRole := range dst.Interface().([]*idm.Role) {
dstRoleModel := (*Role)(dstRole)
uuid := dstRoleModel.GetUniqueId()
if !strings.HasPrefix(uuid, t.options["RolePrefix"]) {
rolesMap[dstRoleModel.GetUniqueId()] = []*Role{dstRoleModel}
}
}
for _, srcRole := range src.Interface().([]*idm.Role) {
srcRoleModel := (*Role)(srcRole)
if tmp, ok := rolesMap[srcRoleModel.GetUniqueId()]; ok {
rolesMap[srcRoleModel.GetUniqueId()] = append(tmp, srcRoleModel)
} else {
rolesMap[srcRoleModel.GetUniqueId()] = []*Role{srcRoleModel}
}
}
var roles []*idm.Role
for _, rolesToMerge := range rolesMap {
if len(rolesToMerge) > 1 {
rolesToMerge[0].Merge(rolesToMerge[1], t.options)
}
roles = append(roles, (*idm.Role)(rolesToMerge[0]))
}
if dst.CanSet() {
dst.Set(reflect.ValueOf(roles))
}
return nil
}
}
return nil
}
func roleEqual(roles1 []*idm.Role, roles2 []*idm.Role) bool {
if roles1 == nil && roles2 == nil {
return true
}
if roles1 == nil || roles2 == nil {
return false
}
if len(roles1) != len(roles2) {
return false
}
for i := range roles1 {
if roles1[i].Uuid != roles2[i].Uuid {
return false
}
}
return true
}