完成博客权限修改
This commit is contained in:
@@ -11,7 +11,10 @@ import {
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
import { UpdatePasswordDto } from 'src/admin/dto/admin-user/update-password.dto';
|
||||
import { CreateBlogDto } from 'src/admin/dto/admin-web/create-blog.dto';
|
||||
import { SetBlogPasswordDto } from 'src/admin/dto/admin-web/set-blog-password.dto';
|
||||
import { UpdateBlogDto } from 'src/admin/dto/admin-web/update-blog.dto';
|
||||
import { Role } from 'src/auth/role.enum';
|
||||
import { BlogPermission } from 'src/blog/Blog.Permission.enum';
|
||||
import { BlogService } from 'src/blog/blog.service';
|
||||
@@ -38,11 +41,19 @@ export class AdminWebBlogController {
|
||||
@Put(':id')
|
||||
async update(
|
||||
@Param('id', new ParseUUIDPipe({ version: '4' })) id: string,
|
||||
@Body() dto: CreateBlogDto,
|
||||
@Body() dto: UpdateBlogDto,
|
||||
) {
|
||||
return this.adminWebBlogService.update(id, dto);
|
||||
}
|
||||
|
||||
@Post(':id/password')
|
||||
async setPassword(
|
||||
@Param('id', new ParseUUIDPipe({ version: '4' })) id: string,
|
||||
@Body() dto: SetBlogPasswordDto,
|
||||
) {
|
||||
return this.adminWebBlogService.setPassword(id, dto.password);
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
async get(@Param('id', new ParseUUIDPipe({ version: '4' })) id: string) {
|
||||
return this.adminWebBlogService.findById(id);
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
import { IsString } from "class-validator";
|
||||
|
||||
export class SetBlogPasswordDto {
|
||||
@IsString()
|
||||
password: string;
|
||||
}
|
||||
16
tone-page-server/src/admin/dto/admin-web/update-blog.dto.ts
Normal file
16
tone-page-server/src/admin/dto/admin-web/update-blog.dto.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { IsEnum, IsString } from 'class-validator';
|
||||
import { BlogPermission } from 'src/blog/Blog.Permission.enum';
|
||||
|
||||
export class UpdateBlogDto {
|
||||
@IsString()
|
||||
title: string;
|
||||
|
||||
@IsString()
|
||||
description: string;
|
||||
|
||||
@IsString()
|
||||
contentUrl: string;
|
||||
|
||||
@IsEnum(BlogPermission, { each: true, message: '请求类型错误' })
|
||||
permissions: BlogPermission[];
|
||||
}
|
||||
@@ -36,6 +36,18 @@ export class BlogService {
|
||||
return this.blogRepository.save(newBlog);
|
||||
}
|
||||
|
||||
async setPassword(id: string, password: string) {
|
||||
const blog = await this.findById(id);
|
||||
if (!blog) {
|
||||
throw new Error('博客不存在');
|
||||
}
|
||||
|
||||
return (await this.blogRepository.update(id, {
|
||||
...blog,
|
||||
password_hash: createHash('sha256').update(`${password}`).digest('hex'),
|
||||
})).affected > 0;
|
||||
}
|
||||
|
||||
async update(id: string, blog: Partial<Blog>) {
|
||||
await this.blogRepository.update(id, blog);
|
||||
return this.blogRepository.findOneBy({ id });
|
||||
|
||||
@@ -16,6 +16,9 @@ import { toast } from "sonner"
|
||||
import { AdminApi } from "@/lib/api"
|
||||
import useSWR from "swr"
|
||||
import { ApiError } from "next/dist/server/api-utils"
|
||||
import { BlogPermissionCheckBoxs } from "./BlogPermissionCheckBoxs"
|
||||
import { BlogPermission } from "@/lib/types/Blog.Permission.enum"
|
||||
import { SetPasswordDialog } from "./SetPasswordDialog"
|
||||
|
||||
interface BlogEditProps {
|
||||
id: string;
|
||||
@@ -43,6 +46,7 @@ export default function BlogEdit({ id, children, onRefresh }: BlogEditProps) {
|
||||
title: blog.title,
|
||||
description: blog.description,
|
||||
contentUrl: blog.contentUrl,
|
||||
permissions: blog.permissions,
|
||||
});
|
||||
toast.success("更新成功")
|
||||
setOpen(false);
|
||||
@@ -109,6 +113,35 @@ export default function BlogEdit({ id, children, onRefresh }: BlogEditProps) {
|
||||
onChange={(e) => mutate({ ...blog, contentUrl: e.target.value }, false)}
|
||||
/>
|
||||
</div>
|
||||
<div className="grid grid-cols-4 items-center gap-4">
|
||||
<Label htmlFor="permissions" className="text-right">
|
||||
文章权限
|
||||
</Label>
|
||||
<div className="col-span-3">
|
||||
<BlogPermissionCheckBoxs
|
||||
permissions={blog.permissions}
|
||||
onCheckedChange={(permission, newState) => {
|
||||
mutate({
|
||||
...blog,
|
||||
permissions: newState ?
|
||||
[...blog.permissions, permission] :
|
||||
blog.permissions.filter(p => p !== permission)
|
||||
}, false)
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
{
|
||||
blog.permissions.includes(BlogPermission.ByPassword) &&
|
||||
<div className="grid grid-cols-4 items-center gap-4">
|
||||
<Label htmlFor="permissions" className="text-right">
|
||||
文章保护密码
|
||||
</Label>
|
||||
<SetPasswordDialog id={id}>
|
||||
<Button variant='outline'>修改</Button>
|
||||
</SetPasswordDialog>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
<DialogFooter>
|
||||
<div className="w-full flex justify-between">
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
'use client';
|
||||
|
||||
import { Button } from "@/components/ui/button"
|
||||
import {
|
||||
Dialog,
|
||||
DialogClose,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogTrigger,
|
||||
} from "@/components/ui/dialog"
|
||||
import { Input } from "@/components/ui/input"
|
||||
import { Label } from "@/components/ui/label"
|
||||
import { AdminApi } from "@/lib/api";
|
||||
import React, { useEffect, useState } from "react";
|
||||
import { toast } from "sonner";
|
||||
|
||||
interface SetPasswordDialogProps {
|
||||
id: string;
|
||||
children: React.ReactNode;
|
||||
}
|
||||
|
||||
export function SetPasswordDialog({ id, children }: SetPasswordDialogProps) {
|
||||
const [open, setOpen] = useState(false);
|
||||
const [password, setPassword] = useState('');
|
||||
|
||||
const handleSubmit = async () => {
|
||||
if (!password) {
|
||||
return toast.error('请输入密码');
|
||||
}
|
||||
|
||||
await AdminApi.web.blog.setPassword(id, password).then(() => {
|
||||
toast.success('修改成功');
|
||||
setOpen(false);
|
||||
}).catch(e => {
|
||||
toast.error(`${e.message || e || '请求失败'}`)
|
||||
});
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (open) {
|
||||
setPassword('');
|
||||
}
|
||||
}, [open])
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={v => setOpen(v)}>
|
||||
<form>
|
||||
<DialogTrigger asChild>
|
||||
{children}
|
||||
</DialogTrigger>
|
||||
<DialogContent className="sm:max-w-[425px]">
|
||||
<DialogHeader>
|
||||
<DialogTitle>修改密码</DialogTitle>
|
||||
<DialogDescription>
|
||||
通过密码访问受保护的文章,需开启“受密码保护”权限
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
<div className="grid gap-4">
|
||||
<div className="grid gap-3">
|
||||
<Label htmlFor="new-password">新密码</Label>
|
||||
<Input
|
||||
id="new-password"
|
||||
name="password"
|
||||
value={password}
|
||||
onChange={e => setPassword(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<DialogFooter>
|
||||
<DialogClose asChild>
|
||||
<Button variant="outline">取消</Button>
|
||||
</DialogClose>
|
||||
<Button type="submit" onClick={handleSubmit}>保存</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</form>
|
||||
</Dialog>
|
||||
)
|
||||
}
|
||||
@@ -2,4 +2,5 @@ export * from './create';
|
||||
export * from './remove';
|
||||
export * from './list';
|
||||
export * from './update';
|
||||
export * from './get';
|
||||
export * from './get';
|
||||
export * from './setPassword';
|
||||
10
tone-page-web/lib/api/admin/web/blog/setPassword.ts
Normal file
10
tone-page-web/lib/api/admin/web/blog/setPassword.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import fetcher from "@/lib/api/fetcher";
|
||||
|
||||
export async function setPassword(id: string, password: string) {
|
||||
return fetcher<boolean>(`/api/admin/web/blog/${id}/password`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
password,
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
import fetcher from "@/lib/api/fetcher";
|
||||
import { BlogPermission } from "@/lib/types/Blog.Permission.enum";
|
||||
|
||||
type UpdateBlogParams = {
|
||||
title: string;
|
||||
description: string;
|
||||
contentUrl: string;
|
||||
permissions: BlogPermission[],
|
||||
}
|
||||
|
||||
export async function update(id: string, data: UpdateBlogParams) {
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { BlogPermission } from "./Blog.Permission.enum";
|
||||
|
||||
export interface Blog {
|
||||
id: string;
|
||||
title: string;
|
||||
@@ -5,4 +7,5 @@ export interface Blog {
|
||||
viewCount: number;
|
||||
contentUrl: string;
|
||||
createdAt: string;
|
||||
permissions: BlogPermission[];
|
||||
}
|
||||
Reference in New Issue
Block a user