move handlers to root folder
This commit is contained in:
218
handlers/user.go
218
handlers/user.go
@@ -1,218 +0,0 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"gitea.tecamino.com/paadi/AccessHandler/models"
|
||||
"gitea.tecamino.com/paadi/AccessHandler/utils"
|
||||
)
|
||||
|
||||
// AddUserTable
|
||||
//
|
||||
// Description:
|
||||
//
|
||||
// Creates a new database table for storing user records if it does not already exist.
|
||||
//
|
||||
// Behavior:
|
||||
// - Uses the DBHandler to add a table based on the `models.User` struct.
|
||||
// - Returns any error encountered during table creation.
|
||||
//
|
||||
// Returns:
|
||||
// - error: Any database error that occurs while creating the table.
|
||||
func (aH *AccessHandler) AddUserTable() error {
|
||||
return aH.dbHandler.addNewTable(models.User{})
|
||||
}
|
||||
|
||||
// AddDefaultUser
|
||||
//
|
||||
// Description:
|
||||
//
|
||||
// Ensures a default administrative user exists in the database.
|
||||
// If a user with the predefined email already exists, the function logs
|
||||
// a debug message and exits without making changes.
|
||||
//
|
||||
// Behavior:
|
||||
// 1. Checks if the default user (admin) already exists by email.
|
||||
// 2. If not found, creates default Quasar UI settings and adds the user.
|
||||
//
|
||||
// Default User:
|
||||
// - Name: "admin"
|
||||
// - Role: "admin"
|
||||
// - Email: "zuercher@tecamino.ch"
|
||||
// - Password: (empty or hashed later)
|
||||
//
|
||||
// Returns:
|
||||
// - error: Any database or creation error encountered.
|
||||
func (aH *AccessHandler) AddDefaultUser() (err error) {
|
||||
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
|
||||
return nil
|
||||
}
|
||||
|
||||
// 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,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// AddNewUser
|
||||
//
|
||||
// Description:
|
||||
//
|
||||
// Adds a new user record to the database with a hashed password.
|
||||
//
|
||||
// Behavior:
|
||||
// 1. Verifies that the email does not already exist.
|
||||
// 2. Hashes the password using utils.HashPassword().
|
||||
// 3. Inserts the new user record into the database.
|
||||
//
|
||||
// Parameters:
|
||||
// - userName: The user's display name.
|
||||
// - email: The user's unique email address.
|
||||
// - password: The user's raw password (will be hashed).
|
||||
// - role: The role assigned to the user.
|
||||
//
|
||||
// Returns:
|
||||
// - error: If the user already exists or if hashing/insertion fails.
|
||||
func (aH *AccessHandler) AddNewUser(userName, email, password, role string) (err error) {
|
||||
// Check if a user with this email already exists
|
||||
if err := aH.dbHandler.exists(&models.User{}, "email", email, false); err == nil {
|
||||
// Found a user → skip create
|
||||
aH.logger.Error("AddNewUser", "user with email "+email+" already exists")
|
||||
return fmt.Errorf("user with email %s already exists", email)
|
||||
}
|
||||
|
||||
// Hash the provided password before saving
|
||||
hash, err := utils.HashPassword(password)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
aH.logger.Debug("AddNewUser", "add new user "+userName+" with role "+role)
|
||||
|
||||
// Insert the new user record
|
||||
aH.dbHandler.addNewColum(&models.User{
|
||||
Name: userName,
|
||||
Role: role,
|
||||
Email: email,
|
||||
Password: hash,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// GetUserById
|
||||
//
|
||||
// Description:
|
||||
//
|
||||
// Retrieves user(s) from the database by their unique ID.
|
||||
//
|
||||
// Parameters:
|
||||
// - id: The numeric user ID.
|
||||
//
|
||||
// Returns:
|
||||
// - users: A slice containing the matched user (usually length 1).
|
||||
// - err: Any database error encountered.
|
||||
func (aH *AccessHandler) GetUserById(id uint) (users []models.User, err error) {
|
||||
err = aH.dbHandler.getById(&users, id)
|
||||
return
|
||||
}
|
||||
|
||||
// GetUserByKey
|
||||
//
|
||||
// Description:
|
||||
//
|
||||
// Queries users based on a given column key and value.
|
||||
//
|
||||
// Parameters:
|
||||
// - key: The column name to search by (e.g., "email").
|
||||
// - value: The value to match.
|
||||
// - likeSearch: If true, performs a LIKE (partial) search.
|
||||
//
|
||||
// Returns:
|
||||
// - users: A list of users that match the search criteria.
|
||||
// - err: Any database error encountered.
|
||||
func (aH *AccessHandler) GetUserByKey(key string, value any, likeSearch bool) (users []models.User, err error) {
|
||||
err = aH.dbHandler.getByKey(&users, key, value, likeSearch)
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateUserById
|
||||
//
|
||||
// Description:
|
||||
//
|
||||
// Updates an existing user record identified by its numeric ID.
|
||||
//
|
||||
// Parameters:
|
||||
// - id: The user ID to update.
|
||||
// - user: A struct containing updated field values.
|
||||
//
|
||||
// Returns:
|
||||
// - error: Any error encountered during the update.
|
||||
func (aH *AccessHandler) UpdateUserById(id uint, user models.User) error {
|
||||
return aH.dbHandler.updateValuesById(&user, id)
|
||||
}
|
||||
|
||||
// UpdateUserByKey
|
||||
//
|
||||
// Description:
|
||||
//
|
||||
// Updates a user record based on a specified column key and value.
|
||||
//
|
||||
// Parameters:
|
||||
// - user: The updated user data.
|
||||
// - key: The column name used for lookup.
|
||||
// - value: The value to match against the key column.
|
||||
//
|
||||
// Returns:
|
||||
// - error: Any error encountered during the update.
|
||||
func (aH *AccessHandler) UpdateUserByKey(user models.User, key string, value any) error {
|
||||
return aH.dbHandler.updateValuesByKey(&user, key, value)
|
||||
}
|
||||
|
||||
// DeleteUserById
|
||||
//
|
||||
// Description:
|
||||
//
|
||||
// Deletes a user from the database using their numeric ID.
|
||||
//
|
||||
// Parameters:
|
||||
// - id: The ID of the user to delete.
|
||||
//
|
||||
// Returns:
|
||||
// - error: Any database error encountered during deletion.
|
||||
func (aH *AccessHandler) DeleteUserById(id uint) (err error) {
|
||||
return aH.dbHandler.deleteById(&models.User{}, id)
|
||||
}
|
||||
|
||||
// DeleteUserByKey
|
||||
//
|
||||
// Description:
|
||||
//
|
||||
// Deletes users matching a specific key/value pair from the database.
|
||||
//
|
||||
// Parameters:
|
||||
// - key: The column name to search by.
|
||||
// - value: The value to match.
|
||||
// - likeSearch: Whether to use a partial match (LIKE).
|
||||
//
|
||||
// Returns:
|
||||
// - error: Any database error encountered during deletion.
|
||||
func (aH *AccessHandler) DeleteUserByKey(key string, value any, likeSearch bool) (err error) {
|
||||
return aH.dbHandler.deleteByKey(&models.User{}, key, value, likeSearch)
|
||||
}
|
||||
Reference in New Issue
Block a user