mirror of
https://github.com/get-drexa/drive.git
synced 2026-02-02 14:51:18 +00:00
refactor: account model overhaul
This commit is contained in:
@@ -7,30 +7,37 @@ import (
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
// Account represents a storage account with quota information
|
||||
// @Description Storage account with usage and quota details
|
||||
type Role string
|
||||
|
||||
const (
|
||||
RoleAdmin Role = "admin"
|
||||
RoleMember Role = "member"
|
||||
)
|
||||
|
||||
type Status string
|
||||
|
||||
const (
|
||||
StatusInvited Status = "invited"
|
||||
StatusActive Status = "active"
|
||||
StatusSuspended Status = "suspended"
|
||||
)
|
||||
|
||||
// Account represents a user's identity within an organization (principal / membership).
|
||||
type Account struct {
|
||||
bun.BaseModel `bun:"accounts" swaggerignore:"true"`
|
||||
|
||||
// Unique account identifier
|
||||
ID uuid.UUID `bun:",pk,type:uuid" json:"id" example:"550e8400-e29b-41d4-a716-446655440000"`
|
||||
ID uuid.UUID `bun:",pk,type:uuid" json:"id"`
|
||||
OrgID uuid.UUID `bun:"org_id,notnull,type:uuid" json:"orgId"`
|
||||
UserID uuid.UUID `bun:"user_id,notnull,type:uuid" json:"userId"`
|
||||
|
||||
// ID of the user who owns this account
|
||||
UserID uuid.UUID `bun:"user_id,notnull,type:uuid" json:"userId" example:"550e8400-e29b-41d4-a716-446655440001"`
|
||||
Role Role `bun:"role,notnull" json:"role" example:"member"`
|
||||
Status Status `bun:"status,notnull" json:"status" example:"active"`
|
||||
|
||||
// Current storage usage in bytes
|
||||
StorageUsageBytes int64 `bun:"storage_usage_bytes,notnull" json:"storageUsageBytes" example:"1073741824"`
|
||||
|
||||
// Maximum storage quota in bytes
|
||||
StorageQuotaBytes int64 `bun:"storage_quota_bytes,notnull" json:"storageQuotaBytes" example:"10737418240"`
|
||||
|
||||
// When the account was created (ISO 8601)
|
||||
CreatedAt time.Time `bun:"created_at,notnull,nullzero" json:"createdAt" example:"2024-12-13T15:04:05Z"`
|
||||
|
||||
// When the account was last updated (ISO 8601)
|
||||
UpdatedAt time.Time `bun:"updated_at,notnull,nullzero" json:"updatedAt" example:"2024-12-13T16:30:00Z"`
|
||||
CreatedAt time.Time `bun:"created_at,notnull,nullzero" json:"createdAt"`
|
||||
UpdatedAt time.Time `bun:"updated_at,notnull,nullzero" json:"updatedAt"`
|
||||
}
|
||||
|
||||
func newAccountID() (uuid.UUID, error) {
|
||||
return uuid.NewV7()
|
||||
}
|
||||
|
||||
|
||||
@@ -3,113 +3,33 @@ package account
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/get-drexa/drexa/internal/auth"
|
||||
"github.com/get-drexa/drexa/internal/httperr"
|
||||
"github.com/get-drexa/drexa/internal/reqctx"
|
||||
"github.com/get-drexa/drexa/internal/user"
|
||||
"github.com/get-drexa/drexa/internal/virtualfs"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/google/uuid"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
type HTTPHandler struct {
|
||||
accountService *Service
|
||||
authService *auth.Service
|
||||
vfs *virtualfs.VirtualFS
|
||||
db *bun.DB
|
||||
authMiddleware fiber.Handler
|
||||
cookieConfig auth.CookieConfig
|
||||
accountService *Service
|
||||
db *bun.DB
|
||||
authMiddleware fiber.Handler
|
||||
}
|
||||
|
||||
// registerAccountRequest represents a new account registration
|
||||
// @Description Request to create a new account and user
|
||||
type registerAccountRequest struct {
|
||||
// Email address for the new account
|
||||
Email string `json:"email" example:"newuser@example.com"`
|
||||
// Password for the new account (min 8 characters)
|
||||
Password string `json:"password" example:"securepassword123"`
|
||||
// Display name for the user
|
||||
DisplayName string `json:"displayName" example:"Jane Doe"`
|
||||
// How to deliver tokens: "cookie" (set HTTP-only cookies) or "body" (include in response)
|
||||
TokenDelivery string `json:"tokenDelivery" example:"body" enums:"cookie,body"`
|
||||
func NewHTTPHandler(accountService *Service, db *bun.DB, authMiddleware fiber.Handler) *HTTPHandler {
|
||||
return &HTTPHandler{
|
||||
accountService: accountService,
|
||||
db: db,
|
||||
authMiddleware: authMiddleware,
|
||||
}
|
||||
}
|
||||
|
||||
// registerAccountResponse represents a successful registration
|
||||
// @Description Response after successful account registration
|
||||
type registerAccountResponse struct {
|
||||
// The created account
|
||||
Account *Account `json:"account"`
|
||||
// The created user
|
||||
User *user.User `json:"user"`
|
||||
// JWT access token for immediate authentication (only included when tokenDelivery is "body")
|
||||
AccessToken string `json:"accessToken,omitempty" example:"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1NTBlODQwMC1lMjliLTQxZDQtYTcxNi00NDY2NTU0NDAwMDAifQ.signature"`
|
||||
// Base64 URL encoded refresh token (only included when tokenDelivery is "body")
|
||||
RefreshToken string `json:"refreshToken,omitempty" example:"dR4nD0mUu1DkZXlCeXRlc0FuZFJhbmRvbURhdGFIZXJlMTIzNDU2Nzg5MGFi"`
|
||||
}
|
||||
|
||||
func NewHTTPHandler(accountService *Service, authService *auth.Service, vfs *virtualfs.VirtualFS, db *bun.DB, authMiddleware fiber.Handler, cookieConfig auth.CookieConfig) *HTTPHandler {
|
||||
return &HTTPHandler{accountService: accountService, authService: authService, db: db, authMiddleware: authMiddleware, cookieConfig: cookieConfig}
|
||||
}
|
||||
|
||||
func (h *HTTPHandler) RegisterRoutes(api fiber.Router) *ScopedRouter {
|
||||
func (h *HTTPHandler) RegisterRoutes(api fiber.Router) {
|
||||
api.Get("/accounts", h.authMiddleware, h.listAccounts)
|
||||
api.Post("/accounts", h.registerAccount)
|
||||
|
||||
account := api.Group("/accounts/:accountID")
|
||||
account.Use(h.authMiddleware)
|
||||
account.Use(h.accountMiddleware)
|
||||
|
||||
account.Get("/", h.getAccount)
|
||||
|
||||
return &ScopedRouter{virtualfs.ScopedRouter{account}}
|
||||
api.Get("/accounts/:accountID", h.authMiddleware, h.getAccount)
|
||||
}
|
||||
|
||||
func (h *HTTPHandler) accountMiddleware(c *fiber.Ctx) error {
|
||||
u := reqctx.AuthenticatedUser(c).(*user.User)
|
||||
|
||||
accountID, err := uuid.Parse(c.Params("accountID"))
|
||||
if err != nil {
|
||||
return c.SendStatus(fiber.StatusNotFound)
|
||||
}
|
||||
|
||||
account, err := h.accountService.AccountByID(c.Context(), h.db, u.ID, accountID)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrAccountNotFound) {
|
||||
return c.SendStatus(fiber.StatusNotFound)
|
||||
}
|
||||
return httperr.Internal(err)
|
||||
}
|
||||
|
||||
root, err := h.vfs.FindRootDirectory(c.Context(), h.db, account.ID)
|
||||
if err != nil {
|
||||
return httperr.Internal(err)
|
||||
}
|
||||
|
||||
scope := &virtualfs.Scope{
|
||||
AccountID: account.ID,
|
||||
RootNodeID: root.ID,
|
||||
AllowedOps: virtualfs.AllAllowedOps,
|
||||
AllowedNodes: nil,
|
||||
ActorKind: virtualfs.ScopeActorAccount,
|
||||
ActorID: u.ID,
|
||||
}
|
||||
|
||||
reqctx.SetVFSAccessScope(c, scope)
|
||||
reqctx.SetCurrentAccount(c, account)
|
||||
|
||||
return c.Next()
|
||||
}
|
||||
|
||||
// listAccounts lists all accounts for the authenticated user
|
||||
// @Summary List accounts
|
||||
// @Description Retrieve all accounts for the authenticated user
|
||||
// @Tags accounts
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Success 200 {array} Account "List of accounts for the authenticated user"
|
||||
// @Failure 401 {string} string "Not authenticated"
|
||||
// @Router /accounts [get]
|
||||
func (h *HTTPHandler) listAccounts(c *fiber.Ctx) error {
|
||||
u := reqctx.AuthenticatedUser(c).(*user.User)
|
||||
accounts, err := h.accountService.ListAccounts(c.Context(), h.db, u.ID)
|
||||
@@ -119,91 +39,19 @@ func (h *HTTPHandler) listAccounts(c *fiber.Ctx) error {
|
||||
return c.JSON(accounts)
|
||||
}
|
||||
|
||||
// getAccount retrieves account information
|
||||
// @Summary Get account
|
||||
// @Description Retrieve account details including storage usage and quota
|
||||
// @Tags accounts
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param accountID path string true "Account ID" format(uuid)
|
||||
// @Success 200 {object} Account "Account details"
|
||||
// @Failure 401 {string} string "Not authenticated"
|
||||
// @Failure 404 {string} string "Account not found"
|
||||
// @Router /accounts/{accountID} [get]
|
||||
func (h *HTTPHandler) getAccount(c *fiber.Ctx) error {
|
||||
account, ok := reqctx.CurrentAccount(c).(*Account)
|
||||
if !ok || account == nil {
|
||||
u := reqctx.AuthenticatedUser(c).(*user.User)
|
||||
accountID, err := uuid.Parse(c.Params("accountID"))
|
||||
if err != nil {
|
||||
return c.SendStatus(fiber.StatusNotFound)
|
||||
}
|
||||
return c.JSON(account)
|
||||
}
|
||||
|
||||
// registerAccount creates a new account and user
|
||||
// @Summary Register new account
|
||||
// @Description Create a new user account with email and password. Returns the account, user, and authentication tokens. Tokens can be delivered via HTTP-only cookies or in the response body based on the tokenDelivery field.
|
||||
// @Tags accounts
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param request body registerAccountRequest true "Registration details"
|
||||
// @Success 200 {object} registerAccountResponse "Account created successfully"
|
||||
// @Failure 400 {string} string "Invalid request body or token delivery method"
|
||||
// @Failure 409 {string} string "Email already registered"
|
||||
// @Router /accounts [post]
|
||||
func (h *HTTPHandler) registerAccount(c *fiber.Ctx) error {
|
||||
req := new(registerAccountRequest)
|
||||
if err := c.BodyParser(req); err != nil {
|
||||
return c.SendStatus(fiber.StatusBadRequest)
|
||||
}
|
||||
|
||||
tx, err := h.db.BeginTx(c.Context(), nil)
|
||||
acc, err := h.accountService.AccountByID(c.Context(), h.db, u.ID, accountID)
|
||||
if err != nil {
|
||||
return httperr.Internal(err)
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
acc, u, err := h.accountService.Register(c.Context(), tx, RegisterOptions{
|
||||
Email: req.Email,
|
||||
Password: req.Password,
|
||||
DisplayName: req.DisplayName,
|
||||
})
|
||||
if err != nil {
|
||||
var ae *user.AlreadyExistsError
|
||||
if errors.As(err, &ae) {
|
||||
return c.SendStatus(fiber.StatusConflict)
|
||||
}
|
||||
if errors.Is(err, ErrAccountAlreadyExists) {
|
||||
return c.SendStatus(fiber.StatusConflict)
|
||||
if errors.Is(err, ErrAccountNotFound) {
|
||||
return c.SendStatus(fiber.StatusNotFound)
|
||||
}
|
||||
return httperr.Internal(err)
|
||||
}
|
||||
|
||||
result, err := h.authService.GrantForUser(c.Context(), tx, u)
|
||||
if err != nil {
|
||||
return httperr.Internal(err)
|
||||
}
|
||||
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
return httperr.Internal(err)
|
||||
}
|
||||
|
||||
switch req.TokenDelivery {
|
||||
default:
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "invalid token delivery method"})
|
||||
|
||||
case auth.TokenDeliveryCookie:
|
||||
auth.SetAuthCookies(c, result.AccessToken, result.RefreshToken, h.cookieConfig)
|
||||
return c.JSON(registerAccountResponse{
|
||||
Account: acc,
|
||||
User: u,
|
||||
})
|
||||
|
||||
case auth.TokenDeliveryBody:
|
||||
return c.JSON(registerAccountResponse{
|
||||
Account: acc,
|
||||
User: u,
|
||||
AccessToken: result.AccessToken,
|
||||
RefreshToken: result.RefreshToken,
|
||||
})
|
||||
}
|
||||
return c.JSON(acc)
|
||||
}
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
package account
|
||||
|
||||
import "github.com/get-drexa/drexa/internal/virtualfs"
|
||||
|
||||
// ScopedRouter is a router with auth + account middleware applied.
|
||||
// Routes registered on this router have access to:
|
||||
// - The authenticated user via reqctx.AuthenticatedUser()
|
||||
// - The current account via reqctx.CurrentAccount()
|
||||
// - The VFS scope via reqctx.VFSAccessScope()
|
||||
//
|
||||
// This embeds virtualfs.ScopedRouter, so it can be passed to functions
|
||||
// that only require VFS scope by calling VFSRouter().
|
||||
type ScopedRouter struct {
|
||||
virtualfs.ScopedRouter
|
||||
}
|
||||
|
||||
// VFSRouter returns the embedded virtualfs.ScopedRouter for use with
|
||||
// functions that only require VFS scope access.
|
||||
func (r *ScopedRouter) VFSRouter() *virtualfs.ScopedRouter {
|
||||
return &r.ScopedRouter
|
||||
}
|
||||
@@ -6,88 +6,38 @@ import (
|
||||
"errors"
|
||||
|
||||
"github.com/get-drexa/drexa/internal/database"
|
||||
"github.com/get-drexa/drexa/internal/password"
|
||||
"github.com/get-drexa/drexa/internal/user"
|
||||
"github.com/get-drexa/drexa/internal/virtualfs"
|
||||
"github.com/google/uuid"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
userService user.Service
|
||||
vfs *virtualfs.VirtualFS
|
||||
type Service struct{}
|
||||
|
||||
func NewService() *Service {
|
||||
return &Service{}
|
||||
}
|
||||
|
||||
type RegisterOptions struct {
|
||||
Email string
|
||||
Password string
|
||||
DisplayName string
|
||||
}
|
||||
|
||||
type CreateAccountOptions struct {
|
||||
OrganizationID uuid.UUID
|
||||
QuotaBytes int64
|
||||
}
|
||||
|
||||
func NewService(userService *user.Service, vfs *virtualfs.VirtualFS) *Service {
|
||||
return &Service{
|
||||
userService: *userService,
|
||||
vfs: vfs,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) Register(ctx context.Context, db bun.IDB, opts RegisterOptions) (*Account, *user.User, error) {
|
||||
hashed, err := password.HashString(opts.Password)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
u, err := s.userService.RegisterUser(ctx, db, user.UserRegistrationOptions{
|
||||
Email: opts.Email,
|
||||
Password: hashed,
|
||||
DisplayName: opts.DisplayName,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
acc, err := s.CreateAccount(ctx, db, u.ID, CreateAccountOptions{
|
||||
// TODO: make quota configurable
|
||||
QuotaBytes: 1024 * 1024 * 1024, // 1GB
|
||||
})
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
_, err = s.vfs.CreateRootDirectory(ctx, db, acc.ID)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return acc, u, nil
|
||||
}
|
||||
|
||||
func (s *Service) CreateAccount(ctx context.Context, db bun.IDB, userID uuid.UUID, opts CreateAccountOptions) (*Account, error) {
|
||||
func (s *Service) CreateAccount(ctx context.Context, db bun.IDB, orgID uuid.UUID, userID uuid.UUID, role Role, status Status) (*Account, error) {
|
||||
id, err := newAccountID()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
account := &Account{
|
||||
ID: id,
|
||||
UserID: userID,
|
||||
StorageQuotaBytes: opts.QuotaBytes,
|
||||
acc := &Account{
|
||||
ID: id,
|
||||
OrgID: orgID,
|
||||
UserID: userID,
|
||||
Role: role,
|
||||
Status: status,
|
||||
}
|
||||
|
||||
_, err = db.NewInsert().Model(account).Returning("*").Exec(ctx)
|
||||
_, err = db.NewInsert().Model(acc).Returning("*").Exec(ctx)
|
||||
if err != nil {
|
||||
if database.IsUniqueViolation(err) {
|
||||
return nil, ErrAccountAlreadyExists
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return account, nil
|
||||
return acc, nil
|
||||
}
|
||||
|
||||
func (s *Service) ListAccounts(ctx context.Context, db bun.IDB, userID uuid.UUID) ([]*Account, error) {
|
||||
@@ -102,26 +52,29 @@ func (s *Service) ListAccounts(ctx context.Context, db bun.IDB, userID uuid.UUID
|
||||
return accounts, nil
|
||||
}
|
||||
|
||||
func (s *Service) AccountByUserID(ctx context.Context, db bun.IDB, userID uuid.UUID) (*Account, error) {
|
||||
var account Account
|
||||
err := db.NewSelect().Model(&account).Where("user_id = ?", userID).Scan(ctx)
|
||||
func (s *Service) AccountByID(ctx context.Context, db bun.IDB, userID uuid.UUID, id uuid.UUID) (*Account, error) {
|
||||
var acc Account
|
||||
err := db.NewSelect().Model(&acc).Where("user_id = ?", userID).Where("id = ?", id).Scan(ctx)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, ErrAccountNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &account, nil
|
||||
return &acc, nil
|
||||
}
|
||||
|
||||
func (s *Service) AccountByID(ctx context.Context, db bun.IDB, userID uuid.UUID, id uuid.UUID) (*Account, error) {
|
||||
var account Account
|
||||
err := db.NewSelect().Model(&account).Where("user_id = ?", userID).Where("id = ?", id).Scan(ctx)
|
||||
func (s *Service) FindUserAccountInOrg(ctx context.Context, db bun.IDB, orgID uuid.UUID, userID uuid.UUID) (*Account, error) {
|
||||
var acc Account
|
||||
err := db.NewSelect().Model(&acc).
|
||||
Where("org_id = ?", orgID).
|
||||
Where("user_id = ?", userID).
|
||||
Scan(ctx)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, ErrAccountNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &account, nil
|
||||
return &acc, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user