Skip to content

Commit

Permalink
fix for reserved unique name collision checking
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelquigley committed Feb 15, 2024
1 parent 73340f0 commit 28d3002
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
19 changes: 9 additions & 10 deletions controller/share.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ func (h *shareHandler) Handle(params share.ShareParams, principal *rest_model_zr
logrus.Errorf("invalid unique name '%v' for account '%v'", uniqueName, principal.Email)
return share.NewShareUnprocessableEntity()
}
shareExists, err := str.ShareWithTokenExists(uniqueName, trx)
if err != nil {
logrus.Errorf("error checking share for token collision: %v", err)
return share.NewUpdateShareInternalServerError()
}
if shareExists {
logrus.Errorf("token '%v' already exists; cannot create share", uniqueName)
return share.NewShareConflict()
}
shrToken = uniqueName
}

Expand Down Expand Up @@ -135,16 +144,6 @@ func (h *shareHandler) Handle(params share.ShareParams, principal *rest_model_zr
sshr.FrontendEndpoint = &sshr.ShareMode
}

sh, err := str.FindShareWithToken(sshr.Token, trx)
if err != nil {
logrus.Errorf("error checking share for token collision: %v", err)
return share.NewShareInternalServerError()
}
if sh != nil {
logrus.Errorf("token '%v' already exists; cannot create share", sshr.Token)
return share.NewShareConflict()
}

sid, err := str.CreateShare(envId, sshr, trx)
if err != nil {
logrus.Errorf("error creating share record: %v", err)
Expand Down
8 changes: 8 additions & 0 deletions controller/store/share.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ func (str *Store) FindShareWithToken(shrToken string, tx *sqlx.Tx) (*Share, erro
return shr, nil
}

func (str *Store) ShareWithTokenExists(shrToken string, tx *sqlx.Tx) (bool, error) {
count := 0
if err := tx.QueryRowx("select count(0) from shares where token = $1 and not deleted", shrToken).Scan(&count); err != nil {
return true, errors.Wrap(err, "error selecting share count by token")
}
return count > 0, nil
}

func (str *Store) FindShareWithZIdAndDeleted(zId string, tx *sqlx.Tx) (*Share, error) {
shr := &Share{}
if err := tx.QueryRowx("select * from shares where z_id = $1", zId).StructScan(shr); err != nil {
Expand Down

0 comments on commit 28d3002

Please sign in to comment.