auth
161:849f3820b164 Browse Files
Stop soft-deleting Profiles and actually delete them. The information we're storing in Profiles isn't unique enough that we should go through the hassle we're going through to soft-delete it. Add a deleteProfile method to our profileStore, and implement it for our postgres and memstore implementations. Add a DeleteProfile wrapper for our Context. Remove the Deleted property from the Profile type and the ProfileChange type, and update references to it. Stop cleaning up after our Profile in the UpdateProfileHandler, because there's no longer any way to delete the Profile from the UpdateProfileHandler. Update our get/list* methods so they don't filter on the non-existent Deleted property anymore. Update our SQL schema definition to not include the deleted column. Update our profile tests to use the DeleteProfile method and stop comparing the no-longer-existing Deleted property.
context.go profile.go profile_postgres.go profile_test.go sql/postgres_init.sql
1.1 --- a/context.go Sat Apr 11 17:23:15 2015 -0400 1.2 +++ b/context.go Sat Apr 11 17:58:15 2015 -0400 1.3 @@ -246,6 +246,14 @@ 1.4 return c.profiles.updateProfiles(ids, change) 1.5 } 1.6 1.7 +// DeleteProfile removes the specified Profile from the profileStore associated with the Context. 1.8 +func (c Context) DeleteProfile(id uuid.ID) error { 1.9 + if c.profiles == nil { 1.10 + return ErrNoProfileStore 1.11 + } 1.12 + return c.profiles.deleteProfile(id) 1.13 +} 1.14 + 1.15 // AddLogin stores the passed Login in the profileStore associated with the Context. It also associates 1.16 // the newly-created Login with the Orofile in login.ProfileID. 1.17 func (c Context) AddLogin(login Login) error {
2.1 --- a/profile.go Sat Apr 11 17:23:15 2015 -0400 2.2 +++ b/profile.go Sat Apr 11 17:58:15 2015 -0400 2.3 @@ -79,7 +79,6 @@ 2.4 PassphraseResetCreated time.Time `json:"-"` 2.5 Created time.Time `json:"created,omitempty"` 2.6 LastSeen time.Time `json:"last_seen,omitempty"` 2.7 - Deleted bool `json:"deleted,omitempty"` 2.8 } 2.9 2.10 // ApplyChange applies the properties of the passed ProfileChange 2.11 @@ -115,9 +114,6 @@ 2.12 if change.LastSeen != nil { 2.13 p.LastSeen = *change.LastSeen 2.14 } 2.15 - if change.Deleted != nil { 2.16 - p.Deleted = *change.Deleted 2.17 - } 2.18 } 2.19 2.20 // ApplyBulkChange applies the properties of the passed BulkProfileChange 2.21 @@ -140,11 +136,10 @@ 2.22 PassphraseReset *string 2.23 PassphraseResetCreated *time.Time 2.24 LastSeen *time.Time 2.25 - Deleted *bool 2.26 } 2.27 2.28 func (c ProfileChange) Empty() bool { 2.29 - return (c.Name == nil && c.Passphrase == nil && c.Iterations == nil && c.Salt == nil && c.PassphraseScheme == nil && c.Compromised == nil && c.LockedUntil == nil && c.PassphraseReset == nil && c.PassphraseResetCreated == nil && c.LastSeen == nil && c.Deleted == nil) 2.30 + return (c.Name == nil && c.Passphrase == nil && c.Iterations == nil && c.Salt == nil && c.PassphraseScheme == nil && c.Compromised == nil && c.LockedUntil == nil && c.PassphraseReset == nil && c.PassphraseResetCreated == nil && c.LastSeen == nil) 2.31 } 2.32 2.33 // Validate checks the ProfileChange it is called on 2.34 @@ -262,6 +257,7 @@ 2.35 saveProfile(profile Profile) error 2.36 updateProfile(id uuid.ID, change ProfileChange) error 2.37 updateProfiles(ids []uuid.ID, change BulkProfileChange) error 2.38 + deleteProfile(id uuid.ID) error 2.39 2.40 addLogin(login Login) error 2.41 removeLogin(value string, profile uuid.ID) error 2.42 @@ -277,9 +273,6 @@ 2.43 if !ok { 2.44 return Profile{}, ErrProfileNotFound 2.45 } 2.46 - if p.Deleted { 2.47 - return Profile{}, ErrProfileNotFound 2.48 - } 2.49 return p, nil 2.50 } 2.51 2.52 @@ -296,9 +289,6 @@ 2.53 if !ok { 2.54 return Profile{}, ErrProfileNotFound 2.55 } 2.56 - if profile.Deleted { 2.57 - return Profile{}, ErrProfileNotFound 2.58 - } 2.59 return profile, nil 2.60 } 2.61 2.62 @@ -340,6 +330,16 @@ 2.63 return nil 2.64 } 2.65 2.66 +func (m *memstore) deleteProfile(id uuid.ID) error { 2.67 + m.profileLock.Lock() 2.68 + defer m.profileLock.Unlock() 2.69 + if _, ok := m.profiles[id.String()]; !ok { 2.70 + return ErrProfileNotFound 2.71 + } 2.72 + delete(m.profiles, id.String()) 2.73 + return nil 2.74 +} 2.75 + 2.76 func (m *memstore) addLogin(login Login) error { 2.77 m.loginLock.Lock() 2.78 defer m.loginLock.Unlock() 2.79 @@ -630,9 +630,6 @@ 2.80 encode(w, r, http.StatusInternalServerError, actOfGodResponse) 2.81 return 2.82 } 2.83 - if !profile.Deleted && req.Deleted != nil && *req.Deleted { 2.84 - go cleanUpAfterProfileDeletion(profile.ID, context) 2.85 - } 2.86 profile.ApplyChange(req) 2.87 encode(w, r, http.StatusOK, response{Profiles: []Profile{profile}}) 2.88 return 2.89 @@ -674,10 +671,7 @@ 2.90 encode(w, r, http.StatusForbidden, response{Errors: errors}) 2.91 return 2.92 } 2.93 - var change ProfileChange 2.94 - deleted := true 2.95 - change.Deleted = &deleted 2.96 - err = context.UpdateProfile(id, change) 2.97 + err = context.DeleteProfile(id) 2.98 if err != nil { 2.99 if err == ErrProfileNotFound { 2.100 errors = append(errors, requestError{Slug: requestErrNotFound}) 2.101 @@ -687,7 +681,6 @@ 2.102 encode(w, r, http.StatusInternalServerError, actOfGodResponse) 2.103 return 2.104 } 2.105 - profile.ApplyChange(change) 2.106 encode(w, r, http.StatusOK, response{Profiles: []Profile{profile}}) 2.107 go cleanUpAfterProfileDeletion(profile.ID, context) 2.108 }
3.1 --- a/profile_postgres.go Sat Apr 11 17:23:15 2015 -0400 3.2 +++ b/profile_postgres.go Sat Apr 11 17:58:15 2015 -0400 3.3 @@ -21,7 +21,7 @@ 3.4 fields, _ := pan.GetFields(profile) 3.5 query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(profile)) 3.6 query.IncludeWhere() 3.7 - query.Include(pan.GetUnquotedColumn(profile, "ID")+" = ? AND "+pan.GetUnquotedColumn(profile, "Deleted")+" = ?", id, false) 3.8 + query.Include(pan.GetUnquotedColumn(profile, "ID")+" = ?", id) 3.9 return query.FlushExpressions(" ") 3.10 } 3.11 3.12 @@ -57,7 +57,7 @@ 3.13 query.Include("INNER JOIN " + pan.GetTableName(login)) 3.14 query.Include("ON " + pan.GetUnquotedAbsoluteColumn(profile, "ID") + " = " + pan.GetUnquotedAbsoluteColumn(login, "ProfileID")) 3.15 query.IncludeWhere() 3.16 - query.Include(pan.GetUnquotedAbsoluteColumn(login, "Value")+" = ? AND "+pan.GetUnquotedAbsoluteColumn(profile, "Deleted")+" = ?", value, false) 3.17 + query.Include(pan.GetUnquotedAbsoluteColumn(login, "Value")+" = ?", value) 3.18 return query.FlushExpressions(" ") 3.19 } 3.20 3.21 @@ -116,7 +116,6 @@ 3.22 query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "PassphraseReset")+" = ?", change.PassphraseReset) 3.23 query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "PassphraseResetCreated")+" = ?", change.PassphraseResetCreated) 3.24 query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "LastSeen")+" = ?", change.LastSeen) 3.25 - query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "Deleted")+" = ?", change.Deleted) 3.26 query.FlushExpressions(", ") 3.27 query.IncludeWhere() 3.28 query.Include(pan.GetUnquotedColumn(profile, "ID")+" = ?", id) 3.29 @@ -155,6 +154,30 @@ 3.30 return err 3.31 } 3.32 3.33 +func (p *postgres) deleteProfileSQL(id uuid.ID) *pan.Query { 3.34 + var profile Profile 3.35 + query := pan.New(pan.POSTGRES, "DELETE FROM "+pan.GetTableName(profile)) 3.36 + query.IncludeWhere() 3.37 + query.Include(pan.GetUnquotedColumn(profile, "ID")+" = ?", id) 3.38 + return query.FlushExpressions(" ") 3.39 +} 3.40 + 3.41 +func (p *postgres) deleteProfile(id uuid.ID) error { 3.42 + query := p.deleteProfileSQL(id) 3.43 + res, err := p.db.Exec(query.String(), query.Args...) 3.44 + if err != nil { 3.45 + return err 3.46 + } 3.47 + rows, err := res.RowsAffected() 3.48 + if err != nil { 3.49 + return err 3.50 + } 3.51 + if rows == 0 { 3.52 + return ErrProfileNotFound 3.53 + } 3.54 + return nil 3.55 +} 3.56 + 3.57 func (p *postgres) addLoginSQL(login Login) *pan.Query { 3.58 fields, values := pan.GetFields(login) 3.59 query := pan.New(pan.POSTGRES, "INSERT INTO "+pan.GetTableName(login))
4.1 --- a/profile_test.go Sat Apr 11 17:23:15 2015 -0400 4.2 +++ b/profile_test.go Sat Apr 11 17:58:15 2015 -0400 4.3 @@ -71,9 +71,6 @@ 4.4 if !profile1.LastSeen.Equal(profile2.LastSeen) { 4.5 return false, "last seen", profile1.LastSeen, profile2.LastSeen 4.6 } 4.7 - if profile1.Deleted != profile2.Deleted { 4.8 - return false, "deleted", profile1.Deleted, profile2.Deleted 4.9 - } 4.10 return true, "", nil, nil 4.11 } 4.12 4.13 @@ -130,8 +127,7 @@ 4.14 if !match { 4.15 t.Errorf("Expected `%v` in the `%s` field of profile retrieved from %T, got `%v`", expectation, field, store, result) 4.16 } 4.17 - deleted := true 4.18 - err = context.UpdateProfile(profile.ID, ProfileChange{Deleted: &deleted}) 4.19 + err = context.DeleteProfile(profile.ID) 4.20 if err != nil { 4.21 t.Errorf("Error removing profile from %T: %s", store, err) 4.22 }
5.1 --- a/sql/postgres_init.sql Sat Apr 11 17:23:15 2015 -0400 5.2 +++ b/sql/postgres_init.sql Sat Apr 11 17:58:15 2015 -0400 5.3 @@ -10,8 +10,7 @@ 5.4 passphrase_reset VARCHAR(64) NOT NULL, 5.5 passphrase_reset_created TIMESTAMPTZ NOT NULL, 5.6 created TIMESTAMPTZ NOT NULL, 5.7 - last_seen TIMESTAMPTZ NOT NULL, 5.8 - deleted BOOLEAN NOT NULL 5.9 + last_seen TIMESTAMPTZ NOT NULL 5.10 ); 5.11 5.12 CREATE TABLE IF NOT EXISTS logins (