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

View File

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

View File

@@ -18,6 +18,7 @@ import {
MdOutlineAdd, MdOutlineAdd,
MdOutlineArchive, MdOutlineArchive,
MdOutlineComment, MdOutlineComment,
MdOutlineLock,
MdOutlinePhotoAlbum, MdOutlinePhotoAlbum,
} from "react-icons/md"; } from "react-icons/md";
import { useTranslation } from "../utils/i18n"; import { useTranslation } from "../utils/i18n";
@@ -526,8 +527,8 @@ function CollectionCard({ collection }: { collection: Collection }) {
</Badge> </Badge>
<span className="flex-1" /> <span className="flex-1" />
{!collection.isPublic && ( {!collection.isPublic && (
<Badge className="badge-soft badge-error text-xs mr-2"> <Badge className="badge-soft badge-error text-xs mr-2 shadow-xs">
<MdOutlineAdd size={16} className="inline-block" /> {t("Private")} <MdOutlineLock size={16} className="inline-block" /> {t("Private")}
</Badge> </Badge>
)} )}
</div> </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 { func UpdateCollection(id uint, title string, article string, images []uint, public bool) error {
return db.Transaction(func(tx *gorm.DB) error { return db.Transaction(func(tx *gorm.DB) error {
collection := &model.Collection{ collection := &model.Collection{}
Model: gorm.Model{
ID: id, // First find the existing collection
}, if err := tx.Where("id = ?", id).First(collection).Error; err != nil {
Title: title, return err
Article: article,
Public: public, // 新增
} }
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 return err
} }