feat: 后端认证和工具模块更新
- main.go: 添加 Swagger 文档、初始化默认管理员 - 认证模块: 完善用户角色管理 - 新增工具模块: tool_handler, tool_repo, tool_service, tool model - 更新 go.mod 依赖 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,7 +1,12 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"x-agents/server/internal/model"
|
||||
@@ -123,10 +128,11 @@ func (s *AuthService) Register(username, password, email string) (*model.User, e
|
||||
role, err := s.userRepo.FindRoleByID(user.RoleID)
|
||||
if err != nil {
|
||||
// 创建默认角色
|
||||
perms, _ := json.Marshal([]int{int(model.PermissionRead), int(model.PermissionWrite)})
|
||||
role = &model.Role{
|
||||
ID: "user",
|
||||
Name: "user",
|
||||
Permissions: []model.PermissionLevel{model.PermissionRead, model.PermissionWrite},
|
||||
Permissions: string(perms),
|
||||
}
|
||||
s.userRepo.CreateRole(role)
|
||||
user.Role = role
|
||||
@@ -136,10 +142,74 @@ func (s *AuthService) Register(username, password, email string) (*model.User, e
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 创建用户工作空间目录
|
||||
if err := s.createUserWorkspace(username); err != nil {
|
||||
// 工作空间创建失败不影响注册成功,仅记录日志
|
||||
println("Warning: failed to create user workspace:", err.Error())
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
// createUserWorkspace 创建用户工作空间目录
|
||||
func (s *AuthService) createUserWorkspace(username string) error {
|
||||
// 获取当前可执行文件所在目录
|
||||
execPath, err := os.Getwd()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 尝试多种方式获取项目根目录
|
||||
projectRoot := execPath
|
||||
|
||||
// 方式1: 如果当前目录名为 server,向上找一级
|
||||
baseName := filepath.Base(execPath)
|
||||
// 处理 Windows 和 Unix 风格路径
|
||||
if baseName == "server" || strings.HasSuffix(execPath, "/server") || strings.HasSuffix(execPath, "\\server") {
|
||||
projectRoot = filepath.Dir(execPath)
|
||||
}
|
||||
|
||||
// 方式2: 尝试向上查找包含 .git 或 package.json 的目录
|
||||
if _, err := os.Stat(filepath.Join(projectRoot, ".git")); os.IsNotExist(err) {
|
||||
// 继续向上查找
|
||||
for i := 0; i < 3; i++ {
|
||||
parent := filepath.Dir(projectRoot)
|
||||
if parent == projectRoot {
|
||||
break
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(parent, ".git")); err == nil {
|
||||
projectRoot = parent
|
||||
break
|
||||
}
|
||||
projectRoot = parent
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf("[Workspace] Creating workspace at: %s", filepath.Join(projectRoot, "account", username))
|
||||
|
||||
// 创建 account 目录和用户目录
|
||||
workspacePath := filepath.Join(projectRoot, "account", username)
|
||||
if err := os.MkdirAll(workspacePath, 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 创建子目录
|
||||
subDirs := []string{"projects", "files", "temp"}
|
||||
for _, dir := range subDirs {
|
||||
if err := os.MkdirAll(filepath.Join(workspacePath, dir), 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetUserByID 根据ID获取用户
|
||||
func (s *AuthService) GetUserByID(id string) (*model.User, error) {
|
||||
return s.userRepo.FindByID(id)
|
||||
}
|
||||
|
||||
// GetAllUsers 获取所有用户
|
||||
func (s *AuthService) GetAllUsers() ([]model.User, error) {
|
||||
return s.userRepo.FindAll()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user