Repository navigation
Expand file tree
/
Copy pathcrud.go
More file actions
125 lines (114 loc) · 3.64 KB
/
Copy pathcrud.go
File metadata and controls
125 lines (114 loc) · 3.64 KB
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
package access
import (
"webtyp.com/auth"
"webtyp.com/fmt"
"webtyp.com/model"
"webtyp.com/rbac"
)
// SaveRole creates the role or updates the existing one with that code.
// actorUserID is recorded in the audit.
func (m *Module) SaveRole(actorUserID string, rec RoleRecord) error {
if rec.Code == "" {
return ErrNoRoleCode
}
code := model.RoleCode(rec.Code)
id := ""
if existing, err := m.roles.GetRoleByCode(m.project, code); err == nil {
id = existing.Id
} else if !rbac.IsRoleNotFound(err) {
return err
} else {
id = m.ids.NewID()
}
if err := m.roles.CreateRole(m.project, id, code, rec.Name, rec.Description); err != nil {
return err
}
if err := m.roles.SetRoleSessionTTL(m.project, id, rec.SessionTtl); err != nil {
return err
}
return m.Record(actorUserID, AuditRoleSave, rec.Code, rec.Name)
}
// DeleteRole removes the role with that code.
func (m *Module) DeleteRole(actorUserID, code string) error {
if code == "" {
return ErrNoRoleCode
}
if err := m.roles.DeleteRoleByCode(m.project, model.RoleCode(code)); err != nil {
return err
}
return m.Record(actorUserID, AuditRoleDelete, code, "")
}
// AssignUser gives roleCode to an existing user, picked from ListPeople.
// Example: a, err := m.AssignUser(actorID, person.Id, "reception")
func (m *Module) AssignUser(actorUserID, userID, roleCode string) (Assignment, error) {
if userID == "" {
return Assignment{}, ErrNoPerson
}
if roleCode == "" {
return Assignment{}, ErrNoRoleCode
}
u, err := m.users.UserByID(userID)
if err != nil {
return Assignment{}, ErrUserNotFound
}
return m.assign(actorUserID, u, roleCode)
}
// AssignByEmail gives roleCode to the person with that e-mail, creating the
// user when there is none: an invitation, before their first sign-in.
// Example: a, err := m.AssignByEmail(actorID, "ana@example.com", "reception")
func (m *Module) AssignByEmail(actorUserID, email, roleCode string) (Assignment, error) {
email = normalizeEmail(email)
if email == "" {
return Assignment{}, ErrNoEmail
}
if roleCode == "" {
return Assignment{}, ErrNoRoleCode
}
u, err := m.userByEmailOrCreate(email)
if err != nil {
return Assignment{}, err
}
return m.assign(actorUserID, u, roleCode)
}
const assignmentSep = ":"
func joinAssignmentID(roleCode, userID string) string {
return roleCode + assignmentSep + userID
}
func splitAssignmentID(id string) (roleCode, userID string, ok bool) {
parts := fmt.Split(id, assignmentSep)
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
return "", "", false
}
return parts[0], parts[1], true
}
func (m *Module) assign(actorUserID string, u auth.User, roleCode string) (Assignment, error) {
if err := m.roles.AssignRoleByCode(m.project, u.Id, model.RoleCode(roleCode)); err != nil {
return Assignment{}, err
}
if err := m.Record(actorUserID, AuditAssign, roleCode, auditTarget(u)); err != nil {
return Assignment{}, err
}
return Assignment{Id: joinAssignmentID(roleCode, u.Id), Email: u.Email, RoleCode: roleCode, UserId: u.Id, Name: u.Name}, nil
}
// auditTarget names a person in the audit: the e-mail, or the name when there is none.
func auditTarget(u auth.User) string {
if u.Email != "" {
return u.Email
}
return u.Name
}
// Revoke removes an assignment by its id "<role_code>:<user_id>".
func (m *Module) Revoke(actorUserID, assignmentID string) error {
roleCode, userID, ok := splitAssignmentID(assignmentID)
if !ok {
return ErrAssignmentID
}
u, err := m.users.UserByID(userID)
if err != nil {
return ErrUserNotFound
}
if err := m.roles.RevokeRoleByCode(m.project, u.Id, model.RoleCode(roleCode)); err != nil {
return err
}
return m.Record(actorUserID, AuditRevoke, roleCode, auditTarget(u))
}