Improve collection permission.

This commit is contained in:
2025-08-01 17:30:12 +08:00
parent 63b3a075c8
commit 0c841f2723
4 changed files with 31 additions and 25 deletions

View File

@@ -6,7 +6,7 @@ import { Collection } from "../network/models";
import Markdown from "react-markdown";
import ResourcesView from "../components/resources_view";
import Loading from "../components/loading";
import { MdOutlineAdd, MdOutlineDelete, MdOutlineEdit } from "react-icons/md";
import { MdOutlineDelete, MdOutlineEdit, MdOutlineLock } from "react-icons/md";
import { app } from "../app";
import { useTranslation } from "../utils/i18n";
import Button from "../components/button";
@@ -163,8 +163,8 @@ export default function CollectionPage() {
)}
<span className="flex-1" />
{!collection.isPublic && (
<Badge className="badge-soft badge-error text-xs mr-2">
<MdOutlineAdd size={16} className="inline-block" /> {t("Private")}
<Badge className="badge-soft badge-error text-xs mr-2 shadow-xs">
<MdOutlineLock size={16} className="inline-block" /> {t("Private")}
</Badge>
)}
</div>
@@ -344,15 +344,15 @@ function EditCollectionDialog({
onChange={(e) => setEditArticle(e.target.value)}
disabled={editLoading}
/>
<label className="flex items-center mb-4">
<label className="flex items-center mb-4 mt-2">
<input
type="checkbox"
checked={editIsPublic}
onChange={(e) => setEditIsPublic(e.target.checked)}
checked={!editIsPublic}
onChange={(e) => setEditIsPublic(!e.target.checked)}
className="checkbox mr-2"
disabled={editLoading}
/>
{t("Public")}
{t("Private")}
</label>
<div className="modal-action">
<button className="btn" onClick={onClose} disabled={editLoading}>

View File

@@ -111,15 +111,15 @@ export default function CreateCollectionPage() {
onChange={(e) => setArticle(e.target.value)}
className="textarea mt-1 w-full min-h-80"
/>
<div className="mt-4">
<label className="flex items-center">
<div className="mt-4 mx-1">
<label className="flex items-center py-2">
<input
type="checkbox"
checked={isPublic}
onChange={(e) => setIsPublic(e.target.checked)}
className="checkbox mr-2"
checked={!isPublic}
onChange={(e) => setIsPublic(!e.target.checked)}
className="checkbox mr-2 checkbox-primary"
/>
{t("Public")}
{t("Private")}
</label>
</div>
</div>

View File

@@ -18,6 +18,7 @@ import {
MdOutlineAdd,
MdOutlineArchive,
MdOutlineComment,
MdOutlineLock,
MdOutlinePhotoAlbum,
} from "react-icons/md";
import { useTranslation } from "../utils/i18n";
@@ -526,8 +527,8 @@ function CollectionCard({ collection }: { collection: Collection }) {
</Badge>
<span className="flex-1" />
{!collection.isPublic && (
<Badge className="badge-soft badge-error text-xs mr-2">
<MdOutlineAdd size={16} className="inline-block" /> {t("Private")}
<Badge className="badge-soft badge-error text-xs mr-2 shadow-xs">
<MdOutlineLock size={16} className="inline-block" /> {t("Private")}
</Badge>
)}
</div>

View File

@@ -35,16 +35,21 @@ func CreateCollection(uid uint, title string, article string, images []uint, pub
func UpdateCollection(id uint, title string, article string, images []uint, public bool) error {
return db.Transaction(func(tx *gorm.DB) error {
collection := &model.Collection{
Model: gorm.Model{
ID: id,
},
Title: title,
Article: article,
Public: public, // 新增
collection := &model.Collection{}
// First find the existing collection
if err := tx.Where("id = ?", id).First(collection).Error; err != nil {
return err
}
if err := tx.Model(collection).Updates(collection).Error; err != nil {
// Update the fields
updates := map[string]interface{}{
"title": title,
"article": article,
"public": public,
}
if err := tx.Model(collection).Updates(updates).Error; err != nil {
return err
}