提交
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -37,7 +38,7 @@ func (s *ShareService) Create(req *dto.ShareCreateRequest, ownerID uint64) (*dto
|
||||
if err != nil {
|
||||
return nil, errors.New("文件不存在")
|
||||
}
|
||||
if file.OwnerID != ownerID {
|
||||
if !s.canAccessFile(file.ID, file.OwnerID, ownerID) {
|
||||
return nil, errors.New("无权分享此文件")
|
||||
}
|
||||
|
||||
@@ -48,6 +49,7 @@ func (s *ShareService) Create(req *dto.ShareCreateRequest, ownerID uint64) (*dto
|
||||
}
|
||||
|
||||
share := &model.Share{
|
||||
ID: utils.GenID(),
|
||||
FileID: fileID,
|
||||
OwnerID: ownerID,
|
||||
ShareCode: shareCode,
|
||||
@@ -176,7 +178,7 @@ func (s *ShareService) toShareVO(share *model.Share) *dto.ShareVO {
|
||||
// 生成完整分享链接
|
||||
publicURL := config.AppConfig.Server.PublicURL
|
||||
if publicURL == "" {
|
||||
publicURL = "http://localhost:8080"
|
||||
publicURL = fmt.Sprintf("http://localhost:%d", config.AppConfig.Server.Port)
|
||||
}
|
||||
publicURL = strings.TrimRight(publicURL, "/")
|
||||
|
||||
@@ -194,6 +196,29 @@ func (s *ShareService) toShareVO(share *model.Share) *dto.ShareVO {
|
||||
}
|
||||
}
|
||||
|
||||
// canAccessFile 检查用户是否有权操作该文件(owner / admin / 被授权)
|
||||
func (s *ShareService) canAccessFile(fileID, fileOwnerID, userID uint64) bool {
|
||||
if fileOwnerID == userID {
|
||||
return true
|
||||
}
|
||||
// 管理员
|
||||
var count int64
|
||||
config.DB.Model(&model.UserRole{}).
|
||||
Joins("JOIN fs_role ON fs_role.id = fs_user_role.role_id").
|
||||
Where("fs_user_role.user_id = ? AND fs_role.name = ?", userID, "admin").
|
||||
Count(&count)
|
||||
if count > 0 {
|
||||
return true
|
||||
}
|
||||
// 文件级授权
|
||||
config.DB.Model(&model.FilePermission{}).
|
||||
Where("file_id = ? AND (user_id = ? OR role_id IN (?))",
|
||||
fileID, userID,
|
||||
config.DB.Model(&model.UserRole{}).Select("role_id").Where("user_id = ?", userID),
|
||||
).Count(&count)
|
||||
return count > 0
|
||||
}
|
||||
|
||||
// generateShareCode 生成8字节随机分享码(16位十六进制字符串)
|
||||
func generateShareCode() (string, error) {
|
||||
bytes := make([]byte, 8)
|
||||
|
||||
Reference in New Issue
Block a user