Skip to content

Commit

Permalink
Merge pull request #381 from aXenDeveloper/blog/permissions
Browse files Browse the repository at this point in the history
feat(blog): Add permissions table in AdminCP for blog plugin
  • Loading branch information
aXenDeveloper authored Jun 17, 2024
2 parents 25b6956 + 30fce58 commit 874cec2
Show file tree
Hide file tree
Showing 54 changed files with 1,346 additions and 2,958 deletions.
5 changes: 4 additions & 1 deletion backend/drizzle.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { DATABASE_ENVS } from "@/database/client";

export default defineConfig({
dialect: "postgresql",
dbCredentials: DATABASE_ENVS,
dbCredentials: {
...DATABASE_ENVS,
ssl: false
},
schema: "./src/plugins/**/admin/database/schema/*.ts"
});
18 changes: 17 additions & 1 deletion backend/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ type GroupUser {
name: [TextLanguage!]!
}

input GroupsPermissionsCreatePluginCategories {
can_create: Boolean!
can_download_files: Boolean!
can_read: Boolean!
can_reply: Boolean!
group_id: Int!
}

type HslColor {
h: Int!
l: Int!
Expand Down Expand Up @@ -153,7 +161,7 @@ type LayoutAdminInstallObj {
}

type Mutation {
admin__blog_categories__create(color: String!, description: [TextLanguageInput!]!, name: [TextLanguageInput!]!): ShowBlogCategories!
admin__blog_categories__create(color: String!, description: [TextLanguageInput!]!, name: [TextLanguageInput!]!, permissions: PermissionsCreatePluginCategories!): ShowBlogCategories!
admin__core_email_settings__edit(color_primary: String!, color_primary_foreground: String!, smtp_host: String!, smtp_password: String!, smtp_port: Int!, smtp_secure: Boolean!, smtp_user: String!): ShowAdminEmailSettingsServiceObj!
admin__core_email_settings__test(from: String!, message: String!, subject: String!, to: String!): String!
admin__core_files__delete(id: Int!): String!
Expand Down Expand Up @@ -218,6 +226,14 @@ type PageInfo {
totalCount: Float!
}

input PermissionsCreatePluginCategories {
can_all_create: Boolean!
can_all_download_files: Boolean!
can_all_read: Boolean!
can_all_reply: Boolean!
groups: [GroupsPermissionsCreatePluginCategories!]!
}

type Query {
admin__core_email_settings__show: ShowAdminEmailSettingsServiceObj!
admin__core_files__show(cursor: Int, first: Int, last: Int, search: String, sortBy: ShowCoreFilesSortByArgs): ShowAdminFilesObj!
Expand Down
19 changes: 16 additions & 3 deletions backend/src/plugins/blog/admin/categories/create/create.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { ShowBlogCategories } from "@/plugins/blog/categories/show/dto/show.obj"
import {
blog_categories,
blog_categories_description,
blog_categories_name
blog_categories_name,
blog_categories_permissions
} from "../../database/schema/categories";
import { ParserTextLanguageCoreHelpersService } from "@/plugins/core/helpers/text_language/parser/parser.service";
import { DatabaseService } from "@/database/database.service";
Expand All @@ -21,11 +22,12 @@ export class CreateBlogCategoriesService {
async create({
color,
description,
name
name,
permissions
}: CreatePluginCategoriesArgs): Promise<ShowBlogCategories> {
const categories = await this.databaseService.db
.insert(blog_categories)
.values({ color })
.values({ color, ...permissions })
.returning();

const categoryId = categories[0].id;
Expand All @@ -50,6 +52,17 @@ export class CreateBlogCategoriesService {
}
});

// Set permissions
if (permissions.groups.length > 0) {
await this.databaseService.db.insert(blog_categories_permissions).values(
permissions.groups.map(item => ({
blog_id: data[0].id,
group_id: item.group_id,
...item
}))
);
}

return data;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ArgsType, Field } from "@nestjs/graphql";
import { ArgsType, Field, InputType, Int } from "@nestjs/graphql";
import { Transform } from "class-transformer";
import { ArrayMinSize, ValidateNested, IsArray } from "class-validator";
import {
Expand All @@ -7,6 +7,42 @@ import {
TransformTextLanguageInput
} from "@vitnode/backend";

@InputType()
class GroupsPermissionsCreatePluginCategories {
@Field(() => Int)
group_id: number;

@Field(() => Boolean)
can_read: boolean;

@Field(() => Boolean)
can_create: boolean;

@Field(() => Boolean)
can_reply: boolean;

@Field(() => Boolean)
can_download_files: boolean;
}

@InputType()
export class PermissionsCreatePluginCategories {
@Field(() => Boolean)
can_all_read: boolean;

@Field(() => Boolean)
can_all_create: boolean;

@Field(() => Boolean)
can_all_reply: boolean;

@Field(() => Boolean)
can_all_download_files: boolean;

@Field(() => [GroupsPermissionsCreatePluginCategories])
groups: GroupsPermissionsCreatePluginCategories[];
}

@ArgsType()
export class CreatePluginCategoriesArgs {
@IsArray()
Expand All @@ -25,4 +61,7 @@ export class CreatePluginCategoriesArgs {

@Field(() => String)
color: string;

@Field(() => PermissionsCreatePluginCategories)
permissions: PermissionsCreatePluginCategories;
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ CREATE TABLE IF NOT EXISTS "blog_categories" (
"id" serial PRIMARY KEY NOT NULL,
"created" timestamp DEFAULT now() NOT NULL,
"position" integer DEFAULT 0 NOT NULL,
"color" varchar(30) DEFAULT '' NOT NULL
"color" varchar(30) DEFAULT '' NOT NULL,
"can_all_read" boolean DEFAULT true NOT NULL,
"can_all_create" boolean DEFAULT true NOT NULL,
"can_all_reply" boolean DEFAULT true NOT NULL,
"can_all_download_files" boolean DEFAULT true NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "blog_categories_description" (
Expand All @@ -42,6 +46,16 @@ CREATE TABLE IF NOT EXISTS "blog_categories_name" (
"value" varchar(100) NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "blog_categories_permissions" (
"id" serial PRIMARY KEY NOT NULL,
"blog_id" integer,
"group_id" integer,
"can_read" boolean DEFAULT false NOT NULL,
"can_create" boolean DEFAULT false NOT NULL,
"can_reply" boolean DEFAULT false NOT NULL,
"can_download_files" boolean DEFAULT false NOT NULL
);
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "blog_articles" ADD CONSTRAINT "blog_articles_author_id_core_users_id_fk" FOREIGN KEY ("author_id") REFERENCES "public"."core_users"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
Expand Down Expand Up @@ -102,13 +116,27 @@ EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "blog_articles_author_id_idx" ON "blog_articles" ("author_id");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "blog_articles_category_id_idx" ON "blog_articles" ("category_id");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "blog_articles_content_item_id_idx" ON "blog_articles_content" ("item_id");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "blog_articles_content_language_code_idx" ON "blog_articles_content" ("language_code");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "blog_articles_title_item_id_idx" ON "blog_articles_title" ("item_id");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "blog_articles_title_language_code_idx" ON "blog_articles_title" ("language_code");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "blog_categories_description_item_id_idx" ON "blog_categories_description" ("item_id");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "blog_categories_description_language_code_idx" ON "blog_categories_description" ("language_code");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "blog_categories_name_item_id_idx" ON "blog_categories_name" ("item_id");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "blog_categories_name_language_code_idx" ON "blog_categories_name" ("language_code");
DO $$ BEGIN
ALTER TABLE "blog_categories_permissions" ADD CONSTRAINT "blog_categories_permissions_blog_id_blog_categories_id_fk" FOREIGN KEY ("blog_id") REFERENCES "public"."blog_categories"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "blog_categories_permissions" ADD CONSTRAINT "blog_categories_permissions_group_id_core_groups_id_fk" FOREIGN KEY ("group_id") REFERENCES "public"."core_groups"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "blog_articles_author_id_idx" ON "blog_articles" USING btree ("author_id");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "blog_articles_category_id_idx" ON "blog_articles" USING btree ("category_id");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "blog_articles_content_item_id_idx" ON "blog_articles_content" USING btree ("item_id");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "blog_articles_content_language_code_idx" ON "blog_articles_content" USING btree ("language_code");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "blog_articles_title_item_id_idx" ON "blog_articles_title" USING btree ("item_id");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "blog_articles_title_language_code_idx" ON "blog_articles_title" USING btree ("language_code");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "blog_categories_description_item_id_idx" ON "blog_categories_description" USING btree ("item_id");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "blog_categories_description_language_code_idx" ON "blog_categories_description" USING btree ("language_code");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "blog_categories_name_item_id_idx" ON "blog_categories_name" USING btree ("item_id");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "blog_categories_name_language_code_idx" ON "blog_categories_name" USING btree ("language_code");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "blog_categories_permissions_blog_id_idx" ON "blog_categories_permissions" USING btree ("blog_id");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "blog_categories_permissions_group_id_idx" ON "blog_categories_permissions" USING btree ("group_id");

This file was deleted.

Loading

0 comments on commit 874cec2

Please sign in to comment.