Skip to content

Commit

Permalink
Migrate from role-based to permission-based auth (#336)
Browse files Browse the repository at this point in the history
  • Loading branch information
AmirAgassi authored Jan 21, 2025
2 parents 561c09e + 016cc93 commit 17355b8
Show file tree
Hide file tree
Showing 40 changed files with 1,064 additions and 804 deletions.
14 changes: 5 additions & 9 deletions backend/.sqlc/migrations/20241215194302_initial_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE EXTENSION IF NOT EXISTS pgcrypto;
SET TIME ZONE 'UTC';

CREATE TYPE user_role AS ENUM (
'admin',
'startup_owner',
'investor'
);

CREATE TYPE project_status AS ENUM (
'draft',
'pending',
Expand All @@ -22,7 +16,7 @@ CREATE TABLE IF NOT EXISTS users (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
email varchar UNIQUE NOT NULL,
password char(256) NOT NULL,
role user_role NOT NULL,
permissions integer NOT NULL DEFAULT 0,
email_verified boolean NOT NULL DEFAULT false,
created_at bigint NOT NULL DEFAULT extract(epoch from now()),
updated_at bigint NOT NULL DEFAULT extract(epoch from now()),
Expand Down Expand Up @@ -118,7 +112,10 @@ CREATE TABLE IF NOT EXISTS transactions (
tx_hash varchar NOT NULL,
from_address varchar NOT NULL,
to_address varchar NOT NULL,
value_amount decimal(65,18) NOT NULL
value_amount decimal(65,18) NOT NULL,
created_by uuid NOT NULL REFERENCES users(id),
created_at bigint NOT NULL DEFAULT extract(epoch from now()),
updated_at bigint NOT NULL DEFAULT extract(epoch from now())
);

CREATE INDEX IF NOT EXISTS idx_users_email ON users(email);
Expand Down Expand Up @@ -152,5 +149,4 @@ DROP TABLE IF EXISTS verify_email_tokens;
DROP TABLE IF EXISTS users;

DROP TYPE IF EXISTS project_status;
DROP TYPE IF EXISTS user_role;
-- +goose StatementEnd
10 changes: 3 additions & 7 deletions backend/.sqlc/queries/projects.sql
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ ORDER BY created_at DESC;

-- name: GetProjectByID :one
SELECT * FROM projects
WHERE id = $1 AND company_id = $2
LIMIT 1;
WHERE id = $1
AND (company_id = $2 OR $3 & 1 = 1) -- Check for PermViewAllProjects (1 << 0)
LIMIT 1;

-- name: UpdateProjectAnswer :one
UPDATE project_answers
Expand Down Expand Up @@ -169,11 +170,6 @@ RETURNING *;
DELETE FROM project_comments
WHERE id = $1;

-- name: GetProjectByIDAdmin :one
SELECT * FROM projects
WHERE id = $1
LIMIT 1;

-- name: ResolveProjectComment :one
UPDATE project_comments
SET
Expand Down
11 changes: 8 additions & 3 deletions backend/.sqlc/queries/transactions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ INSERT INTO transactions (
tx_hash,
from_address,
to_address,
value_amount
value_amount,
created_by,
created_at,
updated_at
) VALUES (
$1, $2, $3, $4, $5, $6, $7
) RETURNING *;
$1, $2, $3, $4, $5, $6, $7, $8,
extract(epoch from now()),
extract(epoch from now())
) RETURNING *;
6 changes: 3 additions & 3 deletions backend/.sqlc/queries/users.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- name: GetUserByID :one
SELECT id, email, role, email_verified, token_salt
SELECT id, email, permissions, email_verified, token_salt
FROM users
WHERE id = $1;

Expand All @@ -11,9 +11,9 @@ SELECT EXISTS(SELECT 1 FROM users WHERE email = $1);

-- name: NewUser :one
INSERT INTO users
(email, password, role)
(email, password, permissions)
VALUES
($1, $2, $3) RETURNING id, email, email_verified, role, token_salt;
($1, $2, $3) RETURNING id, email, email_verified, permissions, token_salt;

-- name: GetUserByEmail :one
SELECT * FROM users WHERE email = $1 LIMIT 1;
Expand Down
80 changes: 11 additions & 69 deletions backend/db/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 6 additions & 25 deletions backend/db/projects.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 13 additions & 3 deletions backend/db/transactions.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 21 additions & 21 deletions backend/db/users.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 17355b8

Please sign in to comment.