Files
garage-ui/internal/handlers/users.go
T
2025-11-24 18:16:25 +01:00

164 lines
4.8 KiB
Go

package handlers
import (
"Noooste/garage-ui/internal/models"
"github.com/gofiber/fiber/v3"
)
// UserHandler handles user/key management operations
// Note: Garage user management typically requires administrative API access
// This is a placeholder implementation that you'll need to extend based on
// your Garage setup and how you manage keys/users
type UserHandler struct {
// In a real implementation, you might have a Garage admin client here
// or interact with Garage's administrative API
}
// NewUserHandler creates a new user handler
func NewUserHandler() *UserHandler {
return &UserHandler{}
}
// ListUsers returns all users/keys
// GET /api/v1/users
func (h *UserHandler) ListUsers(c fiber.Ctx) error {
// NOTE: This is a placeholder implementation
// Garage manages keys/users through its administrative RPC interface
// You'll need to implement this based on your Garage setup
// For now, return a not implemented response
return c.Status(fiber.StatusNotImplemented).JSON(
models.ErrorResponse(models.ErrCodeInternalError,
"User management not yet implemented. Requires Garage Admin API integration."),
)
}
// CreateUser creates a new user/key pair
// POST /api/v1/users
func (h *UserHandler) CreateUser(c fiber.Ctx) error {
// NOTE: This is a placeholder implementation
// To implement this, you need to:
// 1. Connect to Garage's admin RPC interface
// 2. Call the appropriate key creation endpoint
// 3. Return the generated access key and secret key
var req models.CreateUserRequest
if err := c.Bind().JSON(&req); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(
models.ErrorResponse(models.ErrCodeBadRequest, "Invalid request body: "+err.Error()),
)
}
return c.Status(fiber.StatusNotImplemented).JSON(
models.ErrorResponse(models.ErrCodeInternalError,
"User creation not yet implemented. Requires Garage Admin API integration."),
)
}
// DeleteUser deletes a user/key
// DELETE /api/v1/users/:access_key
func (h *UserHandler) DeleteUser(c fiber.Ctx) error {
// NOTE: This is a placeholder implementation
accessKey := c.Params("access_key")
if accessKey == "" {
return c.Status(fiber.StatusBadRequest).JSON(
models.ErrorResponse(models.ErrCodeBadRequest, "Access key is required"),
)
}
return c.Status(fiber.StatusNotImplemented).JSON(
models.ErrorResponse(models.ErrCodeInternalError,
"User deletion not yet implemented. Requires Garage Admin API integration."),
)
}
// GetUser returns information about a specific user/key
// GET /api/v1/users/:access_key
func (h *UserHandler) GetUser(c fiber.Ctx) error {
accessKey := c.Params("access_key")
if accessKey == "" {
return c.Status(fiber.StatusBadRequest).JSON(
models.ErrorResponse(models.ErrCodeBadRequest, "Access key is required"),
)
}
return c.Status(fiber.StatusNotImplemented).JSON(
models.ErrorResponse(models.ErrCodeInternalError,
"User info not yet implemented. Requires Garage Admin API integration."),
)
}
// UpdateUserPermissions updates user permissions
// PATCH /api/v1/users/:access_key
func (h *UserHandler) UpdateUserPermissions(c fiber.Ctx) error {
accessKey := c.Params("access_key")
if accessKey == "" {
return c.Status(fiber.StatusBadRequest).JSON(
models.ErrorResponse(models.ErrCodeBadRequest, "Access key is required"),
)
}
var req models.UpdateUserRequest
if err := c.Bind().JSON(&req); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(
models.ErrorResponse(models.ErrCodeBadRequest, "Invalid request body: "+err.Error()),
)
}
return c.Status(fiber.StatusNotImplemented).JSON(
models.ErrorResponse(models.ErrCodeInternalError,
"User permission update not yet implemented. Requires Garage Admin API integration."),
)
}
/*
IMPLEMENTATION NOTES FOR USER MANAGEMENT:
Garage uses an administrative RPC interface for managing keys and buckets.
To implement user management, you need to:
1. Install garage-admin client or use HTTP RPC calls
2. Connect to Garage's admin port (typically 3903)
3. Implement the following operations:
- List keys: GET /v1/key
- Create key: POST /v1/key
- Get key info: GET /v1/key?id=<access_key>
- Delete key: DELETE /v1/key?id=<access_key>
- Update key: POST /v1/key?id=<access_key>
Example using HTTP client:
import (
"bytes"
"encoding/json"
"net/http"
)
type GarageAdminClient struct {
baseURL string
token string
}
func (g *GarageAdminClient) ListKeys() ([]KeyInfo, error) {
req, _ := http.NewRequest("GET", g.baseURL+"/v1/key", nil)
req.Header.Set("Authorization", "Bearer "+g.token)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var keys []KeyInfo
json.NewDecoder(resp.Body).Decode(&keys)
return keys, nil
}
For more information, see:
https://garagehq.deuxfleurs.fr/documentation/reference-manual/admin-api/
*/