This repository has been archived by the owner on Sep 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
group.go
162 lines (137 loc) · 3.94 KB
/
group.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
package main
import (
"fmt"
"github.com/jmoiron/sqlx"
"github.com/ory/hydra/warden/group"
"github.com/pborman/uuid"
"github.com/pkg/errors"
)
var groupSchema = func(table string) []string {
return []string{
fmt.Sprintf(`CREATE TABLE %s (
id varchar(255) NOT NULL PRIMARY KEY
)`, table),
fmt.Sprintf(`CREATE TABLE %s_m (
member varchar(255) NOT NULL,
group_id varchar(255) NOT NULL,
FOREIGN KEY (group_id) REFERENCES %s(id) ON DELETE CASCADE,
PRIMARY KEY (member, group_id)
)`, table, table),
}
}
type GroupManager struct {
DB *sqlx.DB
Table string
}
func (m *GroupManager) GetTable() string {
if m.Table == "" {
return "hydgr"
}
return m.Table
}
func (m *GroupManager) CreateSchemas() (int, error) {
tx, err := m.DB.Begin()
if err != nil {
return 0, errors.WithStack(err)
}
for _, schema := range groupSchema(m.GetTable()) {
if _, err := tx.Exec(schema); err != nil {
if err := tx.Rollback(); err != nil {
return 0, errors.WithStack(err)
}
return 0, errors.Wrapf(err, "Could not migrate group sql schema: %s", schema)
}
}
if err := tx.Commit(); err != nil {
if err := tx.Rollback(); err != nil {
return 0, errors.WithStack(err)
}
return 0, errors.WithStack(err)
}
return 1, nil
}
func (m *GroupManager) CreateGroup(g *group.Group) error {
if g.ID == "" {
g.ID = uuid.New()
}
query := fmt.Sprintf("INSERT INTO %s (id) VALUES (?)", m.GetTable())
if _, err := m.DB.Exec(m.DB.Rebind(query), g.ID); err != nil {
return errors.WithStack(err)
}
return m.AddGroupMembers(g.ID, g.Members)
}
func (m *GroupManager) GetGroup(id string) (*group.Group, error) {
var found string
query := fmt.Sprintf("SELECT id from %s WHERE id = ?", m.GetTable())
if err := m.DB.Get(&found, m.DB.Rebind(query), id); err != nil {
return nil, errors.WithStack(err)
}
var q []string
query = fmt.Sprintf("SELECT member from %s_m WHERE group_id = ?", m.GetTable())
if err := m.DB.Select(&q, m.DB.Rebind(query), found); err != nil {
return nil, errors.WithStack(err)
}
return &group.Group{
ID: found,
Members: q,
}, nil
}
func (m *GroupManager) DeleteGroup(id string) error {
query := fmt.Sprintf("DELETE FROM %s WHERE id=?", m.GetTable())
if _, err := m.DB.Exec(m.DB.Rebind(query), id); err != nil {
return errors.WithStack(err)
}
return nil
}
func (m *GroupManager) AddGroupMembers(group string, subjects []string) error {
tx, err := m.DB.Beginx()
if err != nil {
return errors.Wrap(err, "Could not begin transaction")
}
query := fmt.Sprintf("INSERT INTO %s_m (group_id, member) VALUES (?, ?)", m.GetTable())
for _, subject := range subjects {
if _, err := tx.Exec(m.DB.Rebind(query), group, subject); err != nil {
if err := tx.Rollback(); err != nil {
return errors.WithStack(err)
}
return errors.WithStack(err)
}
}
if err := tx.Commit(); err != nil {
if err := tx.Rollback(); err != nil {
return errors.WithStack(err)
}
return errors.Wrap(err, "Could not commit transaction")
}
return nil
}
func (m *GroupManager) RemoveGroupMembers(group string, subjects []string) error {
tx, err := m.DB.Beginx()
if err != nil {
return errors.Wrap(err, "Could not begin transaction")
}
query := fmt.Sprintf("DELETE FROM %s_m WHERE member=? AND group_id=?", m.GetTable())
for _, subject := range subjects {
if _, err := m.DB.Exec(m.DB.Rebind(query), subject, group); err != nil {
if err := tx.Rollback(); err != nil {
return errors.WithStack(err)
}
return errors.WithStack(err)
}
}
if err := tx.Commit(); err != nil {
if err := tx.Rollback(); err != nil {
return errors.WithStack(err)
}
return errors.Wrap(err, "Could not commit transaction")
}
return nil
}
func (m *GroupManager) FindGroupNames(subject string) ([]string, error) {
var q []string
query := fmt.Sprintf("SELECT group_id from %s_m WHERE member = ? GROUP BY group_id", m.GetTable())
if err := m.DB.Select(&q, m.DB.Rebind(query), subject); err != nil {
return nil, errors.WithStack(err)
}
return q, nil
}