refactor: ♻️ permission system

This commit is contained in:
Ahmet Kaan GÜMÜŞ 2024-12-13 16:59:34 +03:00
parent 29ce92bdf7
commit 00d6bd5b93
7 changed files with 43 additions and 100 deletions

View file

@ -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
);

View file

@ -0,0 +1 @@
-- Add down migration script here

View file

@ -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
);

View file

@ -0,0 +1,2 @@
-- Add down migration script here
DROP TABLE IF EXISTS "role_permission";

View file

@ -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)
);

View file

@ -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<Postgres>,
) -> Result<Permission, sqlx::Error> {
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<Postgres>,
) -> Result<Permission, sqlx::Error> {
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<Postgres>,
) -> Result<Permission, sqlx::Error> {
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<Postgres>,
) -> Result<Vec<Permission>, sqlx::Error> {
sqlx::query_as!(
Permission,
r#"
SELECT * FROM "role_permission" WHERE "role_id" = $1
"#,
role_id,
)
.fetch_all(database_connection)
.await
}

View file

@ -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,
}