/
roles.go
103 lines (92 loc) · 2.41 KB
/
roles.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
package model
import (
pb "github.com/cownetwork/mooapis-go/cow/indigo/v1"
"github.com/thoas/go-funk"
)
type Role struct {
Id string `db:"id"`
Name string `db:"name"`
Type string `db:"type"`
Priority int32 `db:"priority"`
Transient bool `db:"transient"`
Color string `db:"color"`
Permissions []string
}
func FromProtoRole(r *pb.Role) *Role {
return &Role{
Id: r.Id,
Name: r.Name,
Type: r.Type,
Priority: r.Priority,
Transient: r.Transient,
Color: r.Color,
Permissions: r.Permissions,
}
}
func (r *Role) ToProtoRole() *pb.Role {
return &pb.Role{
Id: r.Id,
Name: r.Name,
Type: r.Type,
Priority: r.Priority,
Transient: r.Transient,
Color: r.Color,
Permissions: r.Permissions,
}
}
func (r *Role) SetPermissions(perms []*RolePermissionBinding) {
r.Permissions = []string{}
for _, perm := range perms {
r.Permissions = append(r.Permissions, perm.Permission)
}
}
func (r *Role) AddPermissions(perms []string) {
r.Permissions = append(r.Permissions, perms...)
}
func (r *Role) RemovePermissions(perms []string) {
r.Permissions = funk.SubtractString(r.Permissions, perms)
}
func (r *Role) Merge(r2 *pb.Role, fm pb.UpdateRoleRequest_FieldMask) {
switch fm {
case pb.UpdateRoleRequest_FIELD_MASK_ALL:
r.Permissions = r2.Permissions
fallthrough
case pb.UpdateRoleRequest_FIELD_MASK_ALL_PROPERTIES:
r.Name = r2.Name
r.Type = r2.Type
r.Priority = r2.Priority
r.Transient = r2.Transient
r.Color = r2.Color
case pb.UpdateRoleRequest_FIELD_MASK_NAME:
r.Name = r2.Name
case pb.UpdateRoleRequest_FIELD_MASK_TYPE:
r.Type = r2.Type
case pb.UpdateRoleRequest_FIELD_MASK_PRIORITY:
r.Priority = r2.Priority
case pb.UpdateRoleRequest_FIELD_MASK_TRANSIENCE:
r.Transient = r2.Transient
case pb.UpdateRoleRequest_FIELD_MASK_COLOR:
r.Color = r2.Color
case pb.UpdateRoleRequest_FIELD_MASK_PERMISSIONS:
r.Permissions = r2.Permissions
}
}
type RolePermissionBinding struct {
RoleId string `db:"role_id"`
Permission string `db:"permission"`
}
func ToRoleUuidIdentifier(roleId string) *pb.RoleIdentifier {
return &pb.RoleIdentifier{
Id: &pb.RoleIdentifier_Uuid{
Uuid: roleId,
},
}
}
func ToRoleNameIdentifier(name string, t string) *pb.RoleIdentifier {
return &pb.RoleIdentifier{
Id: &pb.RoleIdentifier_NameId{NameId: &pb.RoleNameIdentifier{
Name: name,
Type: t,
}},
}
}