feat: add user management system with roles and permissions
Some checks failed
/ build ushare (push) Failing after 1m40s
/ clean (push) Successful in 2s

- Introduce SQLite persistence via GORM (stored at <data>/.ushare.db)
- Add Role model with two built-in roles: admin (all perms) and user (upload only)
- Add three permissions: user_manage, upload, token_manage (reserved)
- Rewrite UserManager: DB-backed login with in-memory session tokens
- Auto-seed default roles and admin user on first startup
- Add AuthPermission middleware for fine-grained permission checks
- Add /api/uauth/me endpoint for current session info
- Add /api/admin/* CRUD routes for user and role management
- Add admin console page (/admin) with user table and role permissions view
- Show admin console link in share page for users with user_manage permission

🤖 Generated with [Qoder][https://qoder.com]
This commit is contained in:
loveuer
2026-02-27 19:40:31 -08:00
parent 909a016a44
commit 5f187bb5d6
13 changed files with 1119 additions and 93 deletions

20
main.go
View File

@@ -3,10 +3,15 @@ package main
import (
"context"
"flag"
"os"
"path/filepath"
"github.com/loveuer/nf/nft/log"
"github.com/loveuer/ushare/internal/api"
"github.com/loveuer/ushare/internal/controller"
"github.com/loveuer/ushare/internal/model"
"github.com/loveuer/ushare/internal/opt"
"github.com/loveuer/ushare/internal/pkg/db"
"github.com/loveuer/ushare/internal/pkg/tool"
"os/signal"
"syscall"
@@ -32,6 +37,21 @@ func main() {
defer cancel()
opt.Init(ctx)
if err := os.MkdirAll(opt.Cfg.DataPath, 0755); err != nil {
log.Fatal("main: create data path failed: %s", err.Error())
}
dbPath := filepath.Join(opt.Cfg.DataPath, ".ushare.db")
if err := db.Init(ctx, "sqlite::"+dbPath); err != nil {
log.Fatal("main: init db failed: %s", err.Error())
}
log.Debug("main: db initialized at %s", dbPath)
if err := db.Default.Migrate(&model.Role{}, &model.User{}); err != nil {
log.Fatal("main: db migrate failed: %s", err.Error())
}
controller.UserManager.Start(ctx)
controller.MetaManager.Start(ctx)
controller.RoomController.Start(ctx)