diff --git a/migrations/20241204225155_create_interaction_table.up.sql b/migrations/20241204225155_create_interaction_table.up.sql index 51b002b..b5f88c9 100644 --- a/migrations/20241204225155_create_interaction_table.up.sql +++ b/migrations/20241204225155_create_interaction_table.up.sql @@ -1,5 +1,5 @@ -- Add up migration script here CREATE TABLE IF NOT EXISTS "interaction"( id BIGSERIAL PRIMARY KEY NOT NULL UNIQUE, - name VARCHAR(10) UNIQUE NOT NULL + name VARCHAR(50) UNIQUE NOT NULL ); \ No newline at end of file diff --git a/migrations/20241213115604_permission.down.sql b/migrations/20241213115604_permission.down.sql new file mode 100644 index 0000000..d2f607c --- /dev/null +++ b/migrations/20241213115604_permission.down.sql @@ -0,0 +1 @@ +-- Add down migration script here diff --git a/migrations/20241213115604_permission.up.sql b/migrations/20241213115604_permission.up.sql new file mode 100644 index 0000000..875e43e --- /dev/null +++ b/migrations/20241213115604_permission.up.sql @@ -0,0 +1,5 @@ +-- Add up migration script here +CREATE TABLE IF NOT EXISTS "permission"( + id BIGSERIAL PRIMARY KEY UNIQUE NOT NULL, + name VARCHAR(50) UNIQUE NOT NULL +); \ No newline at end of file diff --git a/migrations/20241213120203_role_permission.down.sql b/migrations/20241213120203_role_permission.down.sql new file mode 100644 index 0000000..f73c9b3 --- /dev/null +++ b/migrations/20241213120203_role_permission.down.sql @@ -0,0 +1,2 @@ +-- Add down migration script here +DROP TABLE IF EXISTS "role_permission"; \ No newline at end of file diff --git a/migrations/20241213120203_role_permission.up.sql b/migrations/20241213120203_role_permission.up.sql new file mode 100644 index 0000000..1c6dd70 --- /dev/null +++ b/migrations/20241213120203_role_permission.up.sql @@ -0,0 +1,6 @@ +-- Add up migration script here +CREATE TABLE IF NOT EXISTS "role_permission"( + role_id BIGSERIAL NOT NULL REFERENCES "role"(id), + permission_id BIGSERIAL NOT NULL REFERENCES "permission"(id), + PRIMARY KEY (role_id, permission_id) +); \ No newline at end of file diff --git a/src/database/permission.rs b/src/database/permission.rs index 49bed5e..e7a821c 100644 --- a/src/database/permission.rs +++ b/src/database/permission.rs @@ -1,76 +1,21 @@ -use std::fmt::Display; - use sqlx::{Pool, Postgres}; use crate::feature::permission::Permission; -#[derive(Debug)] -pub enum PermissionTable { - Role, - User, - Post, - Comment, - Interaction, - PostInteraction, - CommentInteraction, -} - -impl Display for PermissionTable { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let default_identifier = "permission_"; - match self { - PermissionTable::Role => write!(f, "{}{}", default_identifier, "role"), - PermissionTable::User => write!(f, "{}{}", default_identifier, "user"), - PermissionTable::Post => write!(f, "{}{}", default_identifier, "post"), - PermissionTable::Comment => write!(f, "{}{}", default_identifier, "comment"), - PermissionTable::Interaction => write!(f, "{}{}", default_identifier, "interaction"), - PermissionTable::PostInteraction => { - write!(f, "{}{}", default_identifier, "post_interaction") - } - PermissionTable::CommentInteraction => { - write!(f, "{}{}", default_identifier, "comment_interaction") - } - } - } -} - pub async fn create( role_id: &i64, - create_self: &bool, - read_self: &bool, - update_self: &bool, - delete_self: &bool, - create_other: &bool, - read_other: &bool, - update_other: &bool, - delete_other: &bool, - create_lower: &bool, - read_lower: &bool, - update_lower: &bool, - delete_lower: &bool, - permission_table: &PermissionTable, + permission_id: &i64, database_connection: &Pool, ) -> Result { sqlx::query_as!( Permission, r#" - INSERT INTO "permission_role"(role_id, create_self, read_self, update_self, delete_self, create_other, read_other, update_other, delete_other, create_lower, read_lower, update_lower, delete_lower) - VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13) + INSERT INTO "role_permission"(role_id, permission_id) + VALUES ($1, $2) RETURNING * "#, role_id, - create_self, - read_self, - update_self, - delete_self, - create_other, - read_other, - update_other, - delete_other, - create_lower, - read_lower, - update_lower, - delete_lower, + permission_id ) .fetch_one(database_connection) .await @@ -78,14 +23,16 @@ pub async fn create( pub async fn read( role_id: &i64, + permission_id: &i64, database_connection: &Pool, ) -> Result { sqlx::query_as!( Permission, r#" - SELECT * FROM "permission_role" WHERE "role_id" = $1 + SELECT * FROM "role_permission" WHERE "role_id" = $1 AND "permission_id" = $2 "#, - role_id + role_id, + permission_id ) .fetch_one(database_connection) .await @@ -93,39 +40,17 @@ pub async fn read( pub async fn update( role_id: &i64, - create_self: &bool, - read_self: &bool, - update_self: &bool, - delete_self: &bool, - create_other: &bool, - read_other: &bool, - update_other: &bool, - delete_other: &bool, - create_lower: &bool, - read_lower: &bool, - update_lower: &bool, - delete_lower: &bool, + permission_id: &i64, database_connection: &Pool, ) -> Result { sqlx::query_as!( Permission, r#" - UPDATE "permission_role" SET "create_self" = $2, "read_self" = $3, "update_self" = $4, "delete_self" = $5, "create_other" = $6, "read_other" = $7, "update_other" = $8, "delete_other" = $9, "create_lower" = $10, "read_lower" = $11, "update_lower" = $12, "delete_lower" = $13 WHERE "role_id" = $1 + UPDATE "role_permission" SET "permission_id" = $2 WHERE "role_id" = $1 RETURNING * "#, role_id, - create_self, - read_self, - update_self, - delete_self, - create_other, - read_other, - update_other, - delete_other, - create_lower, - read_lower, - update_lower, - delete_lower, + permission_id ) .fetch_one(database_connection) .await @@ -138,7 +63,7 @@ pub async fn delete( sqlx::query_as!( Permission, r#" - DELETE FROM "permission_role" where "role_id" = $1 + DELETE FROM "role_permission" where "role_id" = $1 RETURNING * "#, role_id @@ -146,3 +71,18 @@ pub async fn delete( .fetch_one(database_connection) .await } + +pub async fn read_all( + role_id: &i64, + database_connection: &Pool, +) -> Result, sqlx::Error> { + sqlx::query_as!( + Permission, + r#" + SELECT * FROM "role_permission" WHERE "role_id" = $1 + "#, + role_id, + ) + .fetch_all(database_connection) + .await +} diff --git a/src/feature/permission.rs b/src/feature/permission.rs index 2f61a3c..edee9fc 100644 --- a/src/feature/permission.rs +++ b/src/feature/permission.rs @@ -3,16 +3,5 @@ use serde::{Deserialize, Serialize}; #[derive(Debug, Serialize, Deserialize)] pub struct Permission { pub role_id: i64, - pub create_self: bool, - pub read_self: bool, - pub update_self: bool, - pub delete_self: bool, - pub create_other: bool, - pub read_other: bool, - pub update_other: bool, - pub delete_other: bool, - pub create_lower: bool, - pub read_lower: bool, - pub update_lower: bool, - pub delete_lower: bool, + pub permission_id: i64, }