Skip to content

Commit

Permalink
Add AccountType to Database (#835)
Browse files Browse the repository at this point in the history
  • Loading branch information
bbengfort authored Jul 3, 2024
1 parent 4e268af commit 3e80961
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 64 deletions.
6 changes: 2 additions & 4 deletions pkg/quarterdeck/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,15 +551,14 @@ func (s *Server) refreshUser(c *gin.Context, userID, orgID any) (_ *tokens.Claim
}

// Create a new claims object using the user retrieved from the database
// TODO: add account type from database rather than hardcoding "sandbox"
refreshClaims := &tokens.Claims{
RegisteredClaims: jwt.RegisteredClaims{
Subject: user.ID.String(),
},
Name: user.Name,
Email: user.Email,
Picture: gravatar.New(user.Email, nil),
AccountType: "sandbox",
AccountType: user.AccountType,
}

// Add the orgID to the claims
Expand Down Expand Up @@ -742,7 +741,6 @@ func (s *Server) Switch(c *gin.Context) {
// Create access and refresh tokens for new organization
// NOTE: ensure that new claims are created and returned, not the old claims;
// otherwise the user may receive incorrect permissions.
// TODO: add account type from database rather than hardcoding "sandbox"
newClaims := &tokens.Claims{
RegisteredClaims: jwt.RegisteredClaims{
Subject: user.ID.String(),
Expand All @@ -751,7 +749,7 @@ func (s *Server) Switch(c *gin.Context) {
Email: user.Email,
Picture: gravatar.New(user.Email, nil),
OrgID: in.OrgID.String(),
AccountType: "sandbox",
AccountType: user.AccountType,
}

// Add the user permissions to the claims
Expand Down
7 changes: 7 additions & 0 deletions pkg/quarterdeck/db/migrations/0007_account_type.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Add a column for tracking the user account type.

BEGIN;

ALTER TABLE users ADD COLUMN account_type TEXT DEFAULT 'sandbox';

COMMIT;
6 changes: 6 additions & 0 deletions pkg/quarterdeck/db/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ type Base struct {
Modified string
}

// Scanner is an interface for *sql.Rows and *sql.Row so that models can implement how
// they scan fields into their struct without having to specify every field every time.
type Scanner interface {
Scan(dest ...any) error
}

// Return the parsed created timestamp.
func (b *Base) GetCreated() (time.Time, error) {
return time.Parse(time.RFC3339Nano, b.Created)
Expand Down
6 changes: 4 additions & 2 deletions pkg/quarterdeck/db/models/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package models_test

import (
"context"
"fmt"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -29,7 +30,7 @@ func (m *modelTestSuite) SetupSuite() {
// Creates a SQLite database with the current schema and the db module connected in a
// temporary directory that will be cleaned up when the tests are complete.
func (m *modelTestSuite) CreateDB() {
require := m.Require()
require := m.Assert()

// Only create the database path on the first call to CreateDB. Otherwise the call
// to TempDir() will be prefixed with the name of the subtest, which will cause an
Expand Down Expand Up @@ -57,11 +58,12 @@ func (m *modelTestSuite) CreateDB() {
}

tx.Commit()
fmt.Printf("test database created at %s\n", m.dbpath)
}

// Closes the connection to the current database and connects to a new database.
func (m *modelTestSuite) ResetDB() {
require := m.Require()
require := m.Assert()
require.NoError(db.Close(), "could not close connection to db")
require.NoError(os.Remove(m.dbpath), "could not delete old db")
m.CreateDB()
Expand Down
12 changes: 6 additions & 6 deletions pkg/quarterdeck/db/models/testdata/fixtures.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ INSERT INTO organizations (id, name, domain, created, modified) VALUES
-- Sophia Thompson Password: livingthedream
-- Robert Millser ULID: 01GRM2319FNCQM7H5P6H9PJ68A
-- Robert Miller Password: weareonamission
INSERT INTO users (id, name, email, password, terms_agreement, privacy_agreement, email_verified, email_verification_expires, email_verification_token, email_verification_secret, last_login, created, modified) VALUES
(x'0184e32cccecff01c1205fa8dc9e20df', 'Jannel P. Hudson', '[email protected]', '$argon2id$v=19$m=65536,t=1,p=2$Ujy6FI2NBqRIUHmqH0YcQA==$f1lwLv4DpE4OTkMq3sTShZS3NHADg9UvnZNHtuUOmZ8=', 't', 't', 't', '2023-03-01T01:22:39Z', '3s855zxQxp-GEk_tgZkAzBxJUgzsWyUTlxIAee_dOJg', 'secret', '2022-12-13T01:22:39Z', '2022-12-05T16:44:34.924036Z', '2022-12-05T16:44:34.924036Z'),
(x'0185df7210e5d8d7f6d33d7ecad99bbd', 'Edison Edgar Franklin', '[email protected]', '$argon2id$v=19$m=65536,t=1,p=2$x4Zh4ARSD4wK7uZFaauyjg==$eCkUszypW+rLvQ+D9lpfTgVwqPSKH13rCdmzV9vZ8cQ=', 't', 't', 't', '2023-03-02T01:22:39Z', '1HyHx3IHSG0G_1QPyyJugdyJATuXoGhWc2q9a7lg6rs', 'secret', '2023-02-14T14:48:08Z', '2023-01-23T16:24:32.741955Z', '2023-01-23T16:24:32.741955Z'),
(x'0185fde9f80e6439a2ee58de062a3e34', 'Zendaya Longeye', '[email protected]', '$argon2id$v=19$m=65536,t=1,p=2$rQMSo/Lksd+/DazFmcuu4Q==$GtZGSh9SajnzXp/Cd8h/zpzgXrw4coXhRz/DhnG7GEU=', 't', 't', 't', '2023-03-03T01:22:39Z', '6riAupvFNaksIuGJWMn2wLU9PPpII4WYRllXgw_gG2M', 'secret', '2023-02-14T08:09:48.739212Z', '2023-01-29T14:24:07.182624Z', '2023-01-29T14:24:07.182624Z'),
(x'018627cf1e8d2c5f4fdc91ffe01353de', 'Sophia Thompson', '[email protected]', '$argon2id$v=19$m=65536,t=1,p=2$SXvnfVpYqOkm+wFPr4VlaA==$1wSremsvEP6Bc2LjzlncjQZvJrO3sBxTLP3AIAu8weQ=', 't', 't', 't', '2023-03-04T01:22:39Z', 'vrArwUOFgp8iTrxgLj0bVrSmiR5mEIUrHF04N6FWDkM', 'secret', '2023-02-06T05:28:22.09Z', '2023-02-04T17:38:50.637565Z', '2023-02-04T17:38:50.637565Z'),
(x'01862821852fab2f43c4b6345369190a', 'Robert Miller', '[email protected]', '$argon2id$v=19$m=65536,t=1,p=2$VFBxqapTyGeHhf4fkJkRjA==$QhYvYeDp/3KWCIkQDhrPCTvx2RLzPW0P2oc8ST9/Vgo=', 't', 't', 't', '2023-03-05T01:22:39Z', '61W2oUZC93ra9TJENqfhS5ulTjK4KHhL7AGb4_tfPDE', 'secret', '2023-02-06T05:29:23.08Z', '2023-02-04T19:08:50.863919Z', '2023-02-04T19:08:50.863919Z')
INSERT INTO users (id, name, email, password, terms_agreement, privacy_agreement, account_type, email_verified, email_verification_expires, email_verification_token, email_verification_secret, last_login, created, modified) VALUES
(x'0184e32cccecff01c1205fa8dc9e20df', 'Jannel P. Hudson', '[email protected]', '$argon2id$v=19$m=65536,t=1,p=2$Ujy6FI2NBqRIUHmqH0YcQA==$f1lwLv4DpE4OTkMq3sTShZS3NHADg9UvnZNHtuUOmZ8=', 't', 't', 'sandbox', 't', '2023-03-01T01:22:39Z', '3s855zxQxp-GEk_tgZkAzBxJUgzsWyUTlxIAee_dOJg', 'secret', '2022-12-13T01:22:39Z', '2022-12-05T16:44:34.924036Z', '2022-12-05T16:44:34.924036Z'),
(x'0185df7210e5d8d7f6d33d7ecad99bbd', 'Edison Edgar Franklin', '[email protected]', '$argon2id$v=19$m=65536,t=1,p=2$x4Zh4ARSD4wK7uZFaauyjg==$eCkUszypW+rLvQ+D9lpfTgVwqPSKH13rCdmzV9vZ8cQ=', 't', 't', 'sandbox', 't', '2023-03-02T01:22:39Z', '1HyHx3IHSG0G_1QPyyJugdyJATuXoGhWc2q9a7lg6rs', 'secret', '2023-02-14T14:48:08Z', '2023-01-23T16:24:32.741955Z', '2023-01-23T16:24:32.741955Z'),
(x'0185fde9f80e6439a2ee58de062a3e34', 'Zendaya Longeye', '[email protected]', '$argon2id$v=19$m=65536,t=1,p=2$rQMSo/Lksd+/DazFmcuu4Q==$GtZGSh9SajnzXp/Cd8h/zpzgXrw4coXhRz/DhnG7GEU=', 't', 't', 'sandbox', 't', '2023-03-03T01:22:39Z', '6riAupvFNaksIuGJWMn2wLU9PPpII4WYRllXgw_gG2M', 'secret', '2023-02-14T08:09:48.739212Z', '2023-01-29T14:24:07.182624Z', '2023-01-29T14:24:07.182624Z'),
(x'018627cf1e8d2c5f4fdc91ffe01353de', 'Sophia Thompson', '[email protected]', '$argon2id$v=19$m=65536,t=1,p=2$SXvnfVpYqOkm+wFPr4VlaA==$1wSremsvEP6Bc2LjzlncjQZvJrO3sBxTLP3AIAu8weQ=', 't', 't', 'sandbox', 't', '2023-03-04T01:22:39Z', 'vrArwUOFgp8iTrxgLj0bVrSmiR5mEIUrHF04N6FWDkM', 'secret', '2023-02-06T05:28:22.09Z', '2023-02-04T17:38:50.637565Z', '2023-02-04T17:38:50.637565Z'),
(x'01862821852fab2f43c4b6345369190a', 'Robert Miller', '[email protected]', '$argon2id$v=19$m=65536,t=1,p=2$VFBxqapTyGeHhf4fkJkRjA==$QhYvYeDp/3KWCIkQDhrPCTvx2RLzPW0P2oc8ST9/Vgo=', 't', 't', 'sandbox', 't', '2023-03-05T01:22:39Z', '61W2oUZC93ra9TJENqfhS5ulTjK4KHhL7AGb4_tfPDE', 'secret', '2023-02-06T05:29:23.08Z', '2023-02-04T19:08:50.863919Z', '2023-02-04T19:08:50.863919Z')
;

INSERT INTO user_invitations (user_id, organization_id, role, email, expires, token, secret, created_by, created, modified) VALUES
Expand Down
Loading

0 comments on commit 3e80961

Please sign in to comment.