Skip to content

Commit

Permalink
W.I.P
Browse files Browse the repository at this point in the history
  • Loading branch information
joelonsql committed Jan 4, 2021
1 parent 1684017 commit 41d23f1
Show file tree
Hide file tree
Showing 24 changed files with 176 additions and 125 deletions.
14 changes: 13 additions & 1 deletion FUNCTIONS/api/create_user.sql
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
CREATE OR REPLACE FUNCTION api.create_user(username text)
RETURNS bigint
LANGUAGE sql
LANGUAGE plpgsql
SECURITY DEFINER
SET search_path TO public, pg_temp
AS $$
DECLARE
_user_id bigint;
BEGIN
INSERT INTO users
(username, parent_user_id)
VALUES
(username, user_id())
RETURNING user_id
INTO STRICT _user_id;

PERFORM api.grant_role_to_user(
role_id := (SELECT role_id FROM roles WHERE role_name = 'signed-in'),
user_id := _user_id
);

RETURN _user_id;
END
$$;
2 changes: 1 addition & 1 deletion FUNCTIONS/api/grant_role_to_user.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LANGUAGE sql
SECURITY DEFINER
SET search_path TO public, pg_temp
AS $$
INSERT INTO user_roles
INSERT INTO role_memberships
(user_id, role_id)
VALUES
(user_id, role_id)
Expand Down
10 changes: 3 additions & 7 deletions FUNCTIONS/api/sign_up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@ AS $$
DECLARE
_user_id bigint;
BEGIN
INSERT INTO users
(username)
VALUES
(username)
RETURNING user_id
INTO STRICT _user_id;

_user_id := api.create_user(username);

--
-- The first user who signs-up
Expand All @@ -32,5 +28,5 @@ IF _user_id = 2 THEN
END IF;

RETURN issue_access_token(_user_id);
END;
END
$$;
2 changes: 1 addition & 1 deletion FUNCTIONS/api/store_credential.sql
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,5 @@ END IF;
-- tell the user the credential has to be marked as valid
-- before it can be used to sign-in
RETURN FALSE;
END;
END
$$;
27 changes: 11 additions & 16 deletions FUNCTIONS/api/update_credential_validity.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,25 @@ SECURITY DEFINER
SET search_path TO public, pg_temp
AS $$
DECLARE
_credential_id constant bigint := update_credential_validity.credential_id;
_valid constant boolean := update_credential_validity.valid;
_user_id constant bigint := user_id();
_ok boolean;
BEGIN
IF _user_id IS NULL THEN
RAISE EXCEPTION 'not logged in';
END IF;

IF (
SELECT users.super_user OR credentials.user_id = _user_id
FROM users
CROSS JOIN credentials
WHERE users.user_id = _user_id
AND credentials.credential_id = _credential_id
) THEN
IF EXISTS (
SELECT 1
FROM credentials
WHERE credentials.credential_id = update_credential_validity.credential_id
AND credentials.user_id = user_id()
)
OR has_role('admin')
THEN
UPDATE credentials
SET valid = _valid
WHERE credentials.credential_id = _credential_id
SET valid = update_credential_validity.valid
WHERE credentials.credential_id = update_credential_validity.credential_id
RETURNING TRUE
INTO STRICT _ok;
END IF;

RETURN _ok;

END;
END
$$;
2 changes: 1 addition & 1 deletion FUNCTIONS/auth.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ IF NOT check_resource_access(_resource_id) THEN
RAISE insufficient_privilege;
END IF;
RETURN;
END;
END
$$;
6 changes: 3 additions & 3 deletions FUNCTIONS/check_resource_access.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ SET search_path TO public, pg_temp
AS $$
SELECT EXISTS (
SELECT 1
FROM user_roles
JOIN permissions ON permissions.role_id = user_roles.role_id
WHERE user_roles.user_id = user_id()
FROM role_memberships
JOIN permissions ON permissions.role_id = role_memberships.role_id
WHERE role_memberships.user_id = user_id()
AND permissions.resource_id = _resource_id
)
$$;
16 changes: 16 additions & 0 deletions FUNCTIONS/has_role.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
CREATE OR REPLACE FUNCTION has_role(role_name text)
RETURNS boolean
STABLE
LANGUAGE sql
SECURITY DEFINER
SET search_path TO public, pg_temp
AS $$
SELECT EXISTS (
SELECT 1
FROM role_memberships
JOIN roles
ON roles.role_id = role_memberships.role_id
WHERE role_memberships.user_id = user_id()
AND roles.role_name = has_role.role_name
)
$$;
2 changes: 1 addition & 1 deletion FUNCTIONS/register_resource.sql
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ INTO STRICT _resource_id;

RETURN _resource_id;

END;
END
$$;
16 changes: 12 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,22 @@ SQL_SRC = \
TABLES/settings.sql \
TABLES/users.sql \
TABLES/credentials.sql \
TABLES/user_roles.sql \
TABLES/role_memberships.sql \
TABLES/access_tokens.sql \
FUNCTIONS/check_resource_access.sql \
FUNCTIONS/auth.sql \
FUNCTIONS/user_id.sql \
FUNCTIONS/has_role.sql \
VIEWS/api/users.sql \
VIEWS/api/current_user.sql \
VIEWS/api/resources.sql \
VIEWS/api/roles.sql \
VIEWS/api/user_roles.sql \
VIEWS/api/user_credentials.sql \
VIEWS/api/user_resources.sql \
VIEWS/api/user_role_memberships.sql \
VIEWS/api/permissions.sql \
VIEWS/api/credentials.sql \
VIEWS/api/role_memberships.sql \
FUNCTIONS/register_resource.sql \
FUNCTIONS/issue_access_token.sql \
FUNCTIONS/api/init_credential.sql \
Expand All @@ -58,18 +62,22 @@ SQL_SRC = \
TABLES/resources.sql \
TABLES/permissions.sql \
TABLES/credentials.sql \
TABLES/user_roles.sql \
TABLES/role_memberships.sql \
VIEWS/api/users.sql \
VIEWS/api/current_user.sql \
VIEWS/api/users.sql \
VIEWS/api/resources.sql \
VIEWS/api/roles.sql \
VIEWS/api/user_roles.sql \
VIEWS/api/user_credentials.sql \
VIEWS/api/user_resources.sql \
VIEWS/api/user_role_memberships.sql \
VIEWS/api/permissions.sql \
VIEWS/api/credentials.sql \
VIEWS/api/role_memberships.sql \
FUNCTIONS/register_resource.sql \
FUNCTIONS/check_resource_access.sql \
FUNCTIONS/auth.sql \
FUNCTIONS/has_role.sql \
FUNCTIONS/api/sign_in.sql \
FUNCTIONS/api/sign_up.sql \
FUNCTIONS/api/create_role.sql \
Expand Down
4 changes: 3 additions & 1 deletion TABLES/permissions.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
CREATE TABLE permissions (
permission_id integer NOT NULL GENERATED ALWAYS AS IDENTITY,
role_id integer NOT NULL REFERENCES roles,
resource_id integer NOT NULL REFERENCES resources,
PRIMARY KEY (role_id, resource_id)
PRIMARY KEY (permission_id),
UNIQUE (role_id, resource_id)
);

SELECT pg_catalog.pg_extension_config_dump('permissions', '');
9 changes: 9 additions & 0 deletions TABLES/role_memberships.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE TABLE role_memberships (
role_membership_id bigint NOT NULL GENERATED ALWAYS AS IDENTITY,
user_id bigint NOT NULL REFERENCES users,
role_id integer NOT NULL REFERENCES roles,
PRIMARY KEY (role_membership_id),
UNIQUE (user_id, role_id)
);

SELECT pg_catalog.pg_extension_config_dump('role_memberships', '');
7 changes: 0 additions & 7 deletions TABLES/user_roles.sql

This file was deleted.

16 changes: 6 additions & 10 deletions VIEWS/api/credentials.sql
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
CREATE OR REPLACE VIEW api.credentials WITH (security_barrier) AS
CREATE OR REPLACE VIEW api.credentials AS
SELECT
credentials.credential_id,
credentials.device_name,
users.username,
credentials.user_id,
credentials.valid
FROM credentials
JOIN users
ON users.user_id = credentials.user_id
WHERE credentials.user_id = user_id();
credential_id,
device_name,
user_id,
valid
FROM credentials;
1 change: 1 addition & 0 deletions VIEWS/api/permissions.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
CREATE OR REPLACE VIEW api.permissions AS
SELECT
permissions.permission_id,
roles.role_name,
resources.resource_name
FROM permissions
Expand Down
5 changes: 2 additions & 3 deletions VIEWS/api/resources.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
CREATE OR REPLACE VIEW api.resources WITH (security_barrier) AS
CREATE OR REPLACE VIEW api.resources AS
SELECT
resource_id,
resource_type,
resource_name,
resource_path
FROM resources
WHERE check_resource_access(resource_id);
FROM resources;
8 changes: 8 additions & 0 deletions VIEWS/api/role_memberships.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE OR REPLACE VIEW api.role_memberships AS
SELECT
role_memberships.role_membership_id,
role_memberships.user_id,
roles.role_name
FROM role_memberships
JOIN roles
ON roles.role_id = role_memberships.role_id;
7 changes: 7 additions & 0 deletions VIEWS/api/user_credentials.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE OR REPLACE VIEW api.user_credentials WITH (security_barrier) AS
SELECT
credential_id,
device_name,
valid
FROM credentials
WHERE user_id = user_id();
8 changes: 8 additions & 0 deletions VIEWS/api/user_resources.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE OR REPLACE VIEW api.user_resources WITH (security_barrier) AS
SELECT
resource_id,
resource_type,
resource_name,
resource_path
FROM resources
WHERE check_resource_access(resource_id);
7 changes: 7 additions & 0 deletions VIEWS/api/user_role_memberships.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE OR REPLACE VIEW api.user_role_memberships AS
SELECT
roles.role_name
FROM role_memberships
JOIN roles
ON roles.role_id = role_memberships.role_id
WHERE role_memberships.user_id = user_id();
10 changes: 0 additions & 10 deletions VIEWS/api/user_roles.sql

This file was deleted.

0 comments on commit 41d23f1

Please sign in to comment.