From 6a29ac0117273e8c764076f7d7f716309b3479de Mon Sep 17 00:00:00 2001 From: Cam Date: Wed, 31 Jan 2024 14:35:28 -0600 Subject: [PATCH] reset token updates --- controller/resetToken.go | 31 ++++++++++++++++++- controller/store/environment.go | 20 ++++++++++++ controller/store/frontend.go | 20 ++++++++++++ controller/store/password_reset_request.go | 12 +++++++ controller/store/share.go | 20 ++++++++++++ ui/src/App.js | 19 +++++++++--- .../detail/account/actions/ResetToken.js | 18 +++++++++-- ui/src/console/login/Login.js | 1 + 8 files changed, 132 insertions(+), 9 deletions(-) diff --git a/controller/resetToken.go b/controller/resetToken.go index b0290ae4..743c86a4 100644 --- a/controller/resetToken.go +++ b/controller/resetToken.go @@ -37,6 +37,35 @@ func (handler *resetTokenHandler) Handle(params account.ResetTokenParams) middle } // Need to create new token and invalidate all other resources + token, err := createToken() + if err != nil { + logrus.Errorf("error creating token for request '%v': %v", params.Body.EmailAddress, err) + return account.NewResetTokenInternalServerError() + } + + a.Token = token + + if _, err := str.UpdateAccount(a, tx); err != nil { + logrus.Errorf("error updating account for request '%v': %v", params.Body.EmailAddress, err) + return account.NewResetTokenInternalServerError() + } + + if err := str.DeletePasswordResetRequestByAccountId(a.Id, tx); err != nil { + logrus.Errorf("error deleting password reset requests for request '%v', but continuing on: %v", params.Body.EmailAddress, err) + } + + environmentIds, err := str.DeleteEnvironmentByAccountID(a.Id, tx) + if err != nil { + logrus.Errorf("error deleting environments for request '%v', but continuing on: %v", params.Body.EmailAddress, err) + } + + if err := str.DeleteFrontendsByEnvironmentIds(tx, environmentIds...); err != nil { + logrus.Errorf("error deleting frontends for request '%v', but continuing on: %v", params.Body.EmailAddress, err) + } + + if err := str.DeleteSharesByEnvironmentIds(tx, environmentIds...); err != nil { + logrus.Errorf("error deleting shares for request '%v', but continuing on: %v", params.Body.EmailAddress, err) + } if err := tx.Commit(); err != nil { logrus.Errorf("error committing '%v' (%v): %v", params.Body.EmailAddress, a.Email, err) @@ -45,5 +74,5 @@ func (handler *resetTokenHandler) Handle(params account.ResetTokenParams) middle logrus.Infof("reset token for '%v'", a.Email) - return account.NewResetTokenOK() + return account.NewResetTokenOK().WithPayload(&account.ResetTokenOKBody{Token: token}) } diff --git a/controller/store/environment.go b/controller/store/environment.go index 4ee783a3..c4e6f93f 100644 --- a/controller/store/environment.go +++ b/controller/store/environment.go @@ -82,3 +82,23 @@ func (str *Store) DeleteEnvironment(id int, tx *sqlx.Tx) error { } return nil } + +func (str *Store) DeleteEnvironmentByAccountID(accountId int, tx *sqlx.Tx) ([]int, error) { + stmt, err := tx.Prepare("update environments set updated_at = current_timestamp, deleted = true where account_id = $1 returning id") + if err != nil { + return nil, errors.Wrap(err, "error preparing environments delete by account_id statement") + } + rows, err := stmt.Query(accountId) + if err != nil { + return nil, errors.Wrap(err, "error executing environments delete by account_id statement") + } + var is []int + for rows.Next() { + var i int + if err := rows.Scan(&i); err != nil { + return nil, errors.Wrap(err, "error scanning environment id") + } + is = append(is, i) + } + return is, nil +} diff --git a/controller/store/frontend.go b/controller/store/frontend.go index 8d34145e..88b59050 100644 --- a/controller/store/frontend.go +++ b/controller/store/frontend.go @@ -1,8 +1,10 @@ package store import ( + "fmt" "github.com/jmoiron/sqlx" "github.com/pkg/errors" + "strings" ) type Frontend struct { @@ -146,3 +148,21 @@ func (str *Store) DeleteFrontend(id int, tx *sqlx.Tx) error { } return nil } + +func (str *Store) DeleteFrontendsByEnvironmentIds(tx *sqlx.Tx, environmentIds ...int) error { + queryStrs := make([]string, 0, len(environmentIds)) + queryVals := make([]interface{}, 0, len(environmentIds)) + for i, v := range environmentIds { + queryStrs = append(queryStrs, fmt.Sprintf("$%d", i)) + queryVals = append(queryVals, v) + } + stmt, err := tx.Prepare(fmt.Sprintf("update frontends set updated_at = current_timestamp, deleted = true where environment_id in (%s)", strings.Join(queryStrs, ","))) + if err != nil { + return errors.Wrap(err, "error preparing frontends delete by environment_id statement") + } + _, err = stmt.Exec(queryVals...) + if err != nil { + return errors.Wrap(err, "error executing frontends delete by environment_id statement") + } + return nil +} diff --git a/controller/store/password_reset_request.go b/controller/store/password_reset_request.go index a6a7b60d..5a98cfb1 100644 --- a/controller/store/password_reset_request.go +++ b/controller/store/password_reset_request.go @@ -98,3 +98,15 @@ func (str *Store) DeleteMultiplePasswordResetRequests(ids []int, tx *sqlx.Tx) er } return nil } + +func (str *Store) DeletePasswordResetRequestByAccountId(accountId int, tx *sqlx.Tx) error { + stmt, err := tx.Prepare("update password_reset_requests set updated_at = current_timestamp, deleted = true where account_id = $1") + if err != nil { + return errors.Wrap(err, "error preparing password_reset_requests by account_id delete statement") + } + _, err = stmt.Exec(accountId) + if err != nil { + return errors.Wrap(err, "error executing password_reset_requests by account_id delete statement") + } + return nil +} diff --git a/controller/store/share.go b/controller/store/share.go index 8161fc2d..2f3f5496 100644 --- a/controller/store/share.go +++ b/controller/store/share.go @@ -1,8 +1,10 @@ package store import ( + "fmt" "github.com/jmoiron/sqlx" "github.com/pkg/errors" + "strings" ) type Share struct { @@ -111,3 +113,21 @@ func (str *Store) DeleteShare(id int, tx *sqlx.Tx) error { } return nil } + +func (str *Store) DeleteSharesByEnvironmentIds(tx *sqlx.Tx, environmentIds ...int) error { + queryStrs := make([]string, 0, len(environmentIds)) + queryVals := make([]interface{}, 0, len(environmentIds)) + for i, v := range environmentIds { + queryStrs = append(queryStrs, fmt.Sprintf("$%d", i)) + queryVals = append(queryVals, v) + } + stmt, err := tx.Prepare(fmt.Sprintf("update shares set updated_at = current_timestamp, deleted = true where environment_id in (%s)", strings.Join(queryStrs, ","))) + if err != nil { + return errors.Wrap(err, "error preparing Shares delete by environment_id statement") + } + _, err = stmt.Exec(queryVals...) + if err != nil { + return errors.Wrap(err, "error executing Shares delete by environment_id statement") + } + return nil +} diff --git a/ui/src/App.js b/ui/src/App.js index aed64f78..a7d14e2d 100644 --- a/ui/src/App.js +++ b/ui/src/App.js @@ -9,12 +9,21 @@ const App = () => { const [user, setUser] = useState(); useEffect(() => { - const localUser = localStorage.getItem("user"); - if(localUser) { - setUser(JSON.parse(localUser)); - console.log("reloaded user", localUser); + function checkUserData() { + const localUser = localStorage.getItem("user"); + if(localUser) { + console.log(localUser) + setUser(JSON.parse(localUser)); + console.log("reloaded user", localUser); + } } - }, []); + + document.addEventListener('storage', checkUserData) + + return () => { + document.removeEventListener('storage', checkUserData) + } + }, []); const logout = () => { setUser(null); diff --git a/ui/src/console/detail/account/actions/ResetToken.js b/ui/src/console/detail/account/actions/ResetToken.js index 087aae79..8ff9baae 100644 --- a/ui/src/console/detail/account/actions/ResetToken.js +++ b/ui/src/console/detail/account/actions/ResetToken.js @@ -8,18 +8,30 @@ const ResetToken = (props) => { console.log("I should reset my token") account.resetToken({ body: { "emailAddress": props.user.email } }).then(resp => { console.log(resp) + let user = JSON.parse(localStorage.getItem('user')) + localStorage.setItem('user', JSON.stringify({ + "email": user.email, + "token": resp.data.token + })); + document.dispatchEvent(new Event('storage')) }).catch(err => { console.log("err", err); }); + props.onHide(); } return (
- Are you Sure? + WARNING - Are you Sure? - TEST - +
+ Reseting your token will remove all environments, frontends, and shares you've created. +
+
+ + +
diff --git a/ui/src/console/login/Login.js b/ui/src/console/login/Login.js index 454cb655..0a9591ea 100644 --- a/ui/src/console/login/Login.js +++ b/ui/src/console/login/Login.js @@ -40,6 +40,7 @@ const Login = (props) => { localStorage.setItem('user', JSON.stringify(user)) console.log(user) console.log('login succeeded', resp) + document.dispatchEvent(new Event('storage')) } else { console.log('login failed') setMessage(errorMessage);