初始化2

This commit is contained in:
2026-07-10 17:33:33 +08:00
parent b51ee98afa
commit 94f6ecf901
59 changed files with 3893 additions and 980 deletions

View File

@@ -3,6 +3,8 @@ package repository
import (
"seeyon-filesystem/config"
"seeyon-filesystem/model"
"gorm.io/gorm"
)
// FileRepository 文件数据访问
@@ -43,15 +45,49 @@ func (r *FileRepository) FindByMD5(md5 string) (*model.File, error) {
func (r *FileRepository) ListByFolder(folderID uint64, userID uint64, roleIDs []uint64) ([]model.File, error) {
var files []model.File
// 构建权限条件: 自己的 OR 直接授权 OR 角色授权
query := r.buildListByFolderQuery(folderID, userID, roleIDs)
err := query.Order("f.extension ASC, f.name ASC").Find(&files).Error
return files, err
}
// ListByFolderPaginated 分页查询文件夹下的文件列表(基于权限)
func (r *FileRepository) ListByFolderPaginated(folderID uint64, userID uint64, roleIDs []uint64, page, pageSize int) ([]model.File, int64, error) {
var files []model.File
var total int64
query := r.buildListByFolderQuery(folderID, userID, roleIDs)
// 统计总数
if err := query.Count(&total).Error; err != nil {
return nil, 0, err
}
// 分页查询
offset := (page - 1) * pageSize
err := query.Order("f.extension ASC, f.name ASC").Offset(offset).Limit(pageSize).Find(&files).Error
return files, total, err
}
// buildListByFolderQuery 构建文件夹文件查询(公共逻辑)
// 可见性: 管理员看全部 | 自己的 | 直接授权 | 角色授权
func (r *FileRepository) buildListByFolderQuery(folderID uint64, userID uint64, roleIDs []uint64) *gorm.DB {
// 检查是否为管理员
isAdmin := r.isAdmin(roleIDs)
// 管理员: 无额外条件, 看所有文件
if isAdmin {
query := config.DB.Table("fs_file f").
Where("f.folder_id = ? AND f.status = 1", folderID)
return query
}
// 非管理员: 自己的 + 直接授权 + 角色授权
permCond := "f.owner_id = ?"
args := []interface{}{userID}
// 直接授权
permCond += " OR fp_user.user_id = ?"
args = append(args, userID)
// 角色授权
if len(roleIDs) > 0 {
permCond += " OR fp_role.role_id IN ?"
args = append(args, roleIDs)
@@ -65,11 +101,18 @@ func (r *FileRepository) ListByFolder(folderID uint64, userID uint64, roleIDs []
query = query.Joins("LEFT JOIN fs_file_permission fp_role ON fp_role.file_id = f.id AND fp_role.role_id IN ?", roleIDs)
}
query = query.Where("f.folder_id = ? AND f.status = 1 AND ("+permCond+")", append([]interface{}{folderID}, args...)...).
Order("f.name ASC")
query = query.Where("f.folder_id = ? AND f.status = 1 AND ("+permCond+")", append([]interface{}{folderID}, args...)...)
return query
}
err := query.Find(&files).Error
return files, err
// isAdmin 检查用户角色中是否包含 admin
func (r *FileRepository) isAdmin(roleIDs []uint64) bool {
if len(roleIDs) == 0 {
return false
}
var count int64
config.DB.Model(&model.Role{}).Where("id IN ? AND name = ?", roleIDs, "admin").Count(&count)
return count > 0
}
// ListByOwner 查询用户的所有文件(仅自己的)
@@ -84,24 +127,34 @@ func (r *FileRepository) ListByOwner(ownerID uint64) ([]model.File, error) {
func (r *FileRepository) SearchByName(userID uint64, roleIDs []uint64, keyword string) ([]model.File, error) {
var files []model.File
// 权限条件
permCond := "f.owner_id = ? OR fp_user.user_id = ?"
args := []interface{}{userID, userID}
if len(roleIDs) > 0 {
permCond += " OR fp_role.role_id IN ?"
args = append(args, roleIDs)
isAdmin := r.isAdmin(roleIDs)
var query *gorm.DB
if isAdmin {
// 管理员: 搜所有文件
query = config.DB.Table("fs_file f").
Where("f.status = 1")
} else {
// 非管理员: 自己的 + 直接授权 + 角色授权
permCond := "f.owner_id = ? OR fp_user.user_id = ?"
args := []interface{}{userID, userID}
if len(roleIDs) > 0 {
permCond += " OR fp_role.role_id IN ?"
args = append(args, roleIDs)
}
query = config.DB.Table("fs_file f").
Select("DISTINCT f.*").
Joins("LEFT JOIN fs_file_permission fp_user ON fp_user.file_id = f.id AND fp_user.user_id = ?", userID)
if len(roleIDs) > 0 {
query = query.Joins("LEFT JOIN fs_file_permission fp_role ON fp_role.file_id = f.id AND fp_role.role_id IN ?", roleIDs)
}
query = query.Where("f.status = 1 AND ("+permCond+")", args...)
}
query := config.DB.Table("fs_file f").
Select("DISTINCT f.*").
Joins("LEFT JOIN fs_file_permission fp_user ON fp_user.file_id = f.id AND fp_user.user_id = ?", userID)
if len(roleIDs) > 0 {
query = query.Joins("LEFT JOIN fs_file_permission fp_role ON fp_role.file_id = f.id AND fp_role.role_id IN ?", roleIDs)
}
query = query.Where("f.status = 1 AND ("+permCond+")", args...)
// 多关键词: 用|分隔, OR匹配
if keyword != "" {
keywords := splitKeywords(keyword)