auth

Paddy 2015-07-15 Parent:73e12d5a1124

178:0a2c3d677161 Go to Latest

auth/scope_postgres.go

Update to use a generic event emitter. Rather can creating a purpose-built event emitter for each and every event we need to emit (I'm looking at you, login verification event) which is _downright silly_, we're now using a generic event publisher that's based on saying "HEY A MODEL UPDATED". This means we need to change all our setup code in authd to use events.NewNSQPublisher or events.NewStdoutPublisher instead of our homegrown solutions. Which also means updating our config to take an events.Publisher instead of our LoginVerificationNotifier (blergh). Our Context also now uses an events.Publisher instead of a LoginVerificationNotifier. Party all around! We also replaced our SendLoginVerification helper method on Context with a SendModelEvent helper method on Context, which is just a light wrapper around events.PublishModelEvent. Of course, all this means we need to update our email_verification listener to listen to the correct channel (based on the model we want updates about) and filter down to a Created action or our new custom action for "the customer wants their verification resent", which I'm OK making a special case and not generic, because c'mon. But we had a subtle change to all our constants, some of which are unofficial constants now. I'm unsure how I feel about this. We also updated our email_verification listener so that we're unmarshalling to a custom loginEvent, which is just an events.Event that overwrites the Data property to be an auth.Login instance. This is to make sure we don't need to wrangle a map[string]interface{}, which is no fun. I'm also OK with special-casing like this, because it's 1) a tiny amount of code, 2) properly utilising composition, and 3) the only way I can think of to cleanly accomplish what I want. I also added a note about GetLogin's deficient handling of logins, namely that it doesn't recognise admins and return Verification codes to them, which would be a useful property for internal tools to take advantage of. Ah well. I updated the Profile and Login implementations so they're now event.Model instances, mainly by just exporting some strings from them through getters that will let us automatically build an Event from them. This lets us use the PublishModelEvent helper. I updated our CreateProfileHandler to properly mangle the login Verification property, and to fire off the ActionCreated events for the new Login and the new Profile. I updated our GetLoginHandler and UpdateLoginHandler to properly mangle the loginVerification property. God that's annoying. :-/ You'll note I didn't start publishing the events.ActionUpdated or events.ActionDeleted events for Profiles or Logins yet, and didn't bother publishing any events for literally any other type. That's because I'm a lazy piece of crap and will end up publishing them when I absolutely have to. Part of that is because if a channel isn't created/being read for a topic, the messages will just stack up in NSQ, and I don't want that. But mostly I'm lazy. Finally, I got to delete the entire profile_verification.go file, because we're no longer special-casing that. Hooray!

History
1 package auth
3 import (
4 "code.secondbit.org/pqarrays.hg"
5 "database/sql/driver"
6 "github.com/lib/pq"
7 "github.com/secondbit/pan"
8 )
10 func (s Scope) GetSQLTableName() string {
11 return "scopes"
12 }
14 func (s Scopes) Value() (driver.Value, error) {
15 ids := make(pqarrays.StringArray, 0, len(s))
16 for _, scope := range s {
17 ids = append(ids, scope.ID)
18 }
19 return ids.Value()
20 }
22 func (s *Scopes) Scan(value interface{}) error {
23 *s = (*s)[:0]
24 var ids pqarrays.StringArray
25 err := ids.Scan(value)
26 if err != nil {
27 return err
28 }
29 for _, id := range ids {
30 *s = append(*s, Scope{ID: id})
31 }
32 return nil
33 }
35 func (p *postgres) createScopesSQL(scopes []Scope) *pan.Query {
36 fields, _ := pan.GetFields(scopes[0])
37 query := pan.New(pan.POSTGRES, "INSERT INTO "+pan.GetTableName(scopes[0]))
38 query.Include("(" + pan.QueryList(fields) + ")")
39 query.Include("VALUES")
40 query.FlushExpressions(" ")
41 for _, scope := range scopes {
42 _, values := pan.GetFields(scope)
43 query.Include("("+pan.VariableList(len(values))+")", values...)
44 }
45 return query.FlushExpressions(", ")
46 }
48 func (p *postgres) createScopes(scopes []Scope) error {
49 if len(scopes) < 1 {
50 return nil
51 }
52 query := p.createScopesSQL(scopes)
53 _, err := p.db.Exec(query.String(), query.Args...)
54 if e, ok := err.(*pq.Error); ok && e.Constraint == "scopes_pkey" {
55 err = ErrScopeAlreadyExists
56 }
57 return err
58 }
60 func (p *postgres) getScopesSQL(ids []string) *pan.Query {
61 var scope Scope
62 intids := make([]interface{}, len(ids))
63 for pos, id := range ids {
64 intids[pos] = id
65 }
66 fields, _ := pan.GetFields(scope)
67 query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(scope))
68 query.IncludeWhere()
69 query.Include(pan.GetUnquotedColumn(scope, "ID") + " IN")
70 query.Include("("+pan.VariableList(len(ids))+")", intids...)
71 return query.FlushExpressions(" ")
72 }
74 func (p *postgres) getScopes(ids []string) ([]Scope, error) {
75 query := p.getScopesSQL(ids)
76 rows, err := p.db.Query(query.String(), query.Args...)
77 if err != nil {
78 return []Scope{}, err
79 }
80 var scopes []Scope
81 for rows.Next() {
82 var scope Scope
83 err := pan.Unmarshal(rows, &scope)
84 if err != nil {
85 return scopes, err
86 }
87 scopes = append(scopes, scope)
88 }
89 if err = rows.Err(); err != nil {
90 return scopes, err
91 }
92 return scopes, nil
93 }
95 func (p *postgres) updateScopeSQL(id string, change ScopeChange) *pan.Query {
96 var scope Scope
97 query := pan.New(pan.POSTGRES, "UPDATE "+pan.GetTableName(scope)+" SET ")
98 query.IncludeIfNotNil(pan.GetUnquotedColumn(scope, "Name")+" = ?", change.Name)
99 query.IncludeIfNotNil(pan.GetUnquotedColumn(scope, "Description")+" = ?", change.Description)
100 query.FlushExpressions(", ")
101 query.IncludeWhere()
102 query.Include(pan.GetUnquotedColumn(scope, "ID")+" = ?", id)
103 return query.FlushExpressions(" ")
104 }
106 func (p *postgres) updateScope(id string, change ScopeChange) error {
107 if change.Empty() {
108 return nil
109 }
110 query := p.updateScopeSQL(id, change)
111 res, err := p.db.Exec(query.String(), query.Args...)
112 if err != nil {
113 return err
114 }
115 rows, err := res.RowsAffected()
116 if err != nil {
117 return err
118 }
119 if rows < 1 {
120 return ErrScopeNotFound
121 }
122 return err
123 }
125 func (p *postgres) removeScopesSQL(ids []string) *pan.Query {
126 var scope Scope
127 intids := make([]interface{}, len(ids))
128 for pos, id := range ids {
129 intids[pos] = id
130 }
131 query := pan.New(pan.POSTGRES, "DELETE FROM "+pan.GetTableName(scope))
132 query.IncludeWhere()
133 query.Include(pan.GetUnquotedColumn(scope, "ID") + " IN")
134 query.Include("("+pan.VariableList(len(ids))+")", intids...)
135 return query.FlushExpressions(" ")
136 }
138 func (p *postgres) removeScopes(ids []string) error {
139 query := p.removeScopesSQL(ids)
140 res, err := p.db.Exec(query.String(), query.Args...)
141 if err != nil {
142 return err
143 }
144 rows, err := res.RowsAffected()
145 if err != nil {
146 return err
147 }
148 if rows < 1 {
149 return ErrScopeNotFound
150 }
151 return nil
152 }
154 func (p *postgres) listScopesSQL() *pan.Query {
155 var scope Scope
156 fields, _ := pan.GetFields(scope)
157 query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(scope))
158 return query.FlushExpressions(" ")
159 }
161 func (p *postgres) listScopes() ([]Scope, error) {
162 query := p.listScopesSQL()
163 rows, err := p.db.Query(query.String(), query.Args...)
164 if err != nil {
165 return []Scope{}, err
166 }
167 var scopes []Scope
168 for rows.Next() {
169 var scope Scope
170 err = pan.Unmarshal(rows, &scope)
171 if err != nil {
172 return scopes, err
173 }
174 scopes = append(scopes, scope)
175 }
176 if err = rows.Err(); err != nil {
177 return scopes, err
178 }
179 return scopes, nil
180 }