change to api only handler and remove unused files, bring more structure
This commit is contained in:
197
handlers/role.go
Normal file
197
handlers/role.go
Normal file
@@ -0,0 +1,197 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"gitea.tecamino.com/paadi/access-handler/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)
|
||||
}
|
||||
Reference in New Issue
Block a user