add api handler

This commit is contained in:
Adrian Zürcher
2025-10-24 16:09:01 +02:00
parent a797700685
commit 090cf0c1cc
10 changed files with 535 additions and 357 deletions

158
accessHandlerAPI.go Normal file
View File

@@ -0,0 +1,158 @@
package AccessHandler
import (
"gitea.tecamino.com/paadi/access-handler/models"
"gitea.tecamino.com/paadi/dbHandler"
"gitea.tecamino.com/paadi/tecamino-logger/logging"
)
// AccessHandler
//
// Description:
//
// AccessHandler manages access-related functionality, including
// database operations for users and roles, as well as logging.
// It encapsulates a database handler and a logger so that
// authentication and authorization operations can be performed
// consistently across the application.
type AccessHandlerAPI struct {
dbHandler *dbHandler.DBHandler // Database handler used for managing users and roles
logger *logging.Logger // Centralized application logger
}
// NewAccessHandlerAPI
//
// Description:
//
// Creates and initializes a new AccessHandler instance.
//
// Behavior:
// 1. If a logger is not provided (nil), it creates a new logger instance
// that writes to "accessHandler.log".
// 2. Initializes the AccessHandler struct.
// 3. Sets up the internal DBAHandler with the same logger.
// 4. Automatically creates required database tables and default data:
// - User table
// - Default user(s)
// - Role table
// - Default role(s)
//
// Parameters:
// - dbPath: The file path to the database.
// - logger: Optional pointer to a logging.Logger instance. If nil, a new one is created.
//
// Returns:
// - aH: A pointer to the fully initialized AccessHandler.
// - err: Any error that occurs during initialization.
//
// Example:
//
// handler, err := NewAccessHandlerAPI("data/app.db", appLogger)
// if err != nil {
// log.Fatal(err)
// }
func NewAccessHandlerAPIAPI(path string, logger *logging.Logger) (aH *AccessHandlerAPI, err error) {
if logger == nil {
logger, err = logging.NewLogger("accessHandler.log", nil)
if err != nil {
return
}
}
logger.Debug("NewAccessHandlerAPI", "initialize new access handler")
// Initialize AccessHandler with logger
aH = &AccessHandlerAPI{
logger: logger,
}
logger.Debug("NewAccessHandlerAPI", "initialize db handler")
// Create a new DB handler instance
aH.dbHandler, err = dbHandler.NewDBHandler(path, "user", logger)
if err != nil {
aH.logger.Error("NewAccessHandlerAPI", err)
return
}
logger.Debug("NewAccessHandlerAPI", "add user table")
// Add the user table to the database
err = aH.dbHandler.AddNewTable(models.User{})
if err != nil {
aH.logger.Error("NewAccessHandlerAPI", err)
return
}
logger.Debug("NewAccessHandlerAPI", "add default user")
// Add default users to the system
name := "admin"
role := "admin"
email := "zuercher@tecamino.ch"
// Check if a user with this email already exists
if err := aH.dbHandler.Exists(&models.User{}, "email", email, false); err == nil {
aH.logger.Debug("AddDefaultUser", "user email "+email+" exists already")
// Found a user → skip create
} else {
// Create default settings for the new user
settings := models.Settings{}
aH.logger.Debug("AddDefaultUser", "set default quasar settings")
settings.DefaultQuasarSettings()
// Insert default admin user into the database
aH.dbHandler.AddNewColum(&models.User{
Name: name,
Role: role,
Email: email,
Password: "$2a$10$sZZOWBP8DSFLrLFQNoXw8OsEEr0tez1B8lPzKCHofaHg6PMNxx1pG",
Settings: settings,
})
}
logger.Debug("NewAccessHandlerAPI", "add role table")
// Add the role table to the database
err = aH.dbHandler.AddNewTable(models.Role{})
if err != nil {
aH.logger.Error("NewAccessHandlerAPI", err)
return
}
// Add default roles to the system
logger.Debug("NewAccessHandlerAPI", "add default role")
// Check if a role with this name already exists
if err := aH.dbHandler.Exists(&models.Role{}, "role", role, false); err == nil {
// Found a role → skip creation
aH.logger.Debug("AddDefaultRole", "role "+role+" exists already")
} else {
// Initialize default permissions for admin
permissions := models.Permissions{}
aH.logger.Debug("AddDefaultRole", "set default Permissions")
permissions.DefaultPermissions()
// Create the default admin role
aH.dbHandler.AddNewColum(&models.Role{
Role: role,
Permissions: permissions,
})
}
return
}
// GetLogger
//
// Description:
//
// Returns the logger associated with this AccessHandler instance.
// Useful when another component or handler needs to reuse the
// same logging instance for consistent log output.
//
// Returns:
// - *logging.Logger: The logger assigned to this AccessHandler.
//
// Example:
//
// log := accessHandler.GetLogger()
// log.Info("Some event")
func (aH *AccessHandlerAPI) GetLogger() *logging.Logger {
return aH.logger
}