move handlers to root folder

This commit is contained in:
Adrian Zürcher
2025-10-24 10:43:59 +02:00
parent 42c4f1d915
commit e469b61bb2
6 changed files with 1248 additions and 0 deletions

197
role.go Normal file
View File

@@ -0,0 +1,197 @@
package AccessHandler
import (
"fmt"
"gitea.tecamino.com/paadi/AccessHandler/models"
)
// AddRoleTable
//
// Description:
//
// Creates a new database table for storing role definitions if it does not already exist.
//
// Behavior:
// - Uses the DBHandler to add a table based on the `models.Role` struct.
// - Returns an error if table creation fails.
//
// Returns:
// - error: Any database error encountered.
func (aH *AccessHandler) AddRoleTable() error {
return aH.dbHandler.addNewTable(models.Role{})
}
// AddDefaultRole
//
// Description:
//
// Ensures that a default administrative role exists in the database.
// If a role named "admin" is already present, it logs and skips creation.
//
// Behavior:
// 1. Checks for an existing "admin" role.
// 2. If not found, initializes default permissions using
// `models.Permissions.DefaultPermissions()`.
// 3. Creates a new role record with those permissions.
//
// Default Role:
// - Role: "admin"
// - Permissions: all default permissions defined in `models.Permissions`.
//
// Returns:
// - error: Any database or creation error encountered.
func (aH *AccessHandler) AddDefaultRole() (err error) {
role := "admin"
// 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")
return nil
}
// 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
}
// AddNewRole
//
// Description:
//
// Adds a new role with a specific set of permissions to the database.
//
// Behavior:
// 1. Checks whether a role with the same name already exists.
// 2. If it does not exist, creates a new role record.
//
// Parameters:
// - role: The role name (e.g., "manager", "viewer").
// - permissions: A `models.Permissions` struct defining allowed actions.
//
// Returns:
// - error: If the role already exists or insertion fails.
func (aH *AccessHandler) AddNewRole(role string, permissions models.Permissions) (err error) {
// 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
return fmt.Errorf("role with name %s already exists", role)
}
// Insert new role with provided permissions
aH.dbHandler.addNewColum(&models.Role{
Role: role,
Permissions: permissions,
})
return
}
// GetRoleById
//
// Description:
//
// Retrieves a role record from the database by its numeric ID.
//
// Parameters:
// - id: The unique ID of the role.
//
// Returns:
// - roles: A slice containing the matched role (usually length 1).
// - err: Any database error encountered.
func (aH *AccessHandler) GetRoleById(id uint) (roles []models.Role, err error) {
err = aH.dbHandler.getById(&roles, id)
return
}
// GetRoleByKey
//
// Description:
//
// Retrieves one or more roles based on a key/value query.
//
// Parameters:
// - key: The column name to search by (e.g., "role").
// - value: The value to match (e.g., "admin").
// - likeSearch: Whether to use SQL LIKE for partial matches.
//
// Returns:
// - roles: A list of matched roles.
// - err: Any database error encountered.
func (aH *AccessHandler) GetRoleByKey(key string, value any, likeSearch bool) (roles []models.Role, err error) {
err = aH.dbHandler.getByKey(&roles, key, value, likeSearch)
return
}
// UpdateRoleById
//
// Description:
//
// Updates a role record identified by its numeric ID.
//
// Parameters:
// - id: The ID of the role to update.
// - role: A struct containing updated role data.
//
// Returns:
// - error: Any database error encountered.
func (aH *AccessHandler) UpdateRoleById(id uint, role models.Role) error {
return aH.dbHandler.updateValuesById(&role, id)
}
// UpdateRoleByKey
//
// Description:
//
// Updates a role record using a column key/value lookup.
//
// Parameters:
// - role: The updated role data.
// - key: The column name to search by.
// - value: The value to match against the key column.
//
// Returns:
// - error: Any database error encountered.
func (aH *AccessHandler) UpdateRoleByKey(role models.Role, key string, value any) error {
return aH.dbHandler.updateValuesByKey(&role, key, value)
}
// DeleteRoleById
//
// Description:
//
// Deletes a role record from the database by its numeric ID.
//
// Parameters:
// - id: The ID of the role to delete.
//
// Returns:
// - error: Any database error encountered during deletion.
func (aH *AccessHandler) DeleteRoleById(id uint) (err error) {
return aH.dbHandler.deleteById(&models.Role{}, id)
}
// DeleteRoleByKey
//
// Description:
//
// Deletes one or more roles from the database matching a given key/value pair.
//
// Parameters:
// - key: The column name used for filtering (e.g., "role").
// - value: The matching value (e.g., "admin").
// - likeSearch: If true, performs a LIKE (partial) match.
//
// Returns:
// - error: Any database error encountered.
func (aH *AccessHandler) DeleteRoleByKey(key string, value any, likeSearch bool) (err error) {
return aH.dbHandler.deleteByKey(&models.Role{}, key, value, likeSearch)
}