Compare commits
9 Commits
v1.0.27
...
3eb40bc02f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3eb40bc02f | ||
|
|
81959425a1 | ||
|
|
857d84d3a9 | ||
|
|
1f4f9a12aa | ||
|
|
206639cf72 | ||
|
|
9a47a74764 | ||
|
|
8cbf36c5b3 | ||
|
|
21daaf1819 | ||
|
|
8156fea488 |
2
go.mod
2
go.mod
@@ -3,7 +3,7 @@ module gitea.tecamino.com/paadi/access-handler
|
||||
go 1.24.5
|
||||
|
||||
require (
|
||||
gitea.tecamino.com/paadi/dbHandler v1.1.5
|
||||
gitea.tecamino.com/paadi/dbHandler v1.1.10
|
||||
gitea.tecamino.com/paadi/tecamino-logger v0.2.1
|
||||
github.com/gin-gonic/gin v1.11.0
|
||||
github.com/go-playground/assert/v2 v2.2.0
|
||||
|
||||
4
go.sum
4
go.sum
@@ -1,5 +1,5 @@
|
||||
gitea.tecamino.com/paadi/dbHandler v1.1.5 h1:dxRfe095qlvaH/fiLQ1+/qwzX2zwN3PXlmd1UxC5Mc4=
|
||||
gitea.tecamino.com/paadi/dbHandler v1.1.5/go.mod h1:y/xn/POJg1DO++67uKvnO23lJQgh+XFQq7HZCS9Getw=
|
||||
gitea.tecamino.com/paadi/dbHandler v1.1.10 h1:zZQbDTJ0bu6CIW90Zms8yYIzTLHtWPNhVKRxLUXEDuE=
|
||||
gitea.tecamino.com/paadi/dbHandler v1.1.10/go.mod h1:y/xn/POJg1DO++67uKvnO23lJQgh+XFQq7HZCS9Getw=
|
||||
gitea.tecamino.com/paadi/tecamino-logger v0.2.1 h1:sQTBKYPdzn9mmWX2JXZBtGBvNQH7cuXIwsl4TD0aMgE=
|
||||
gitea.tecamino.com/paadi/tecamino-logger v0.2.1/go.mod h1:FkzRTldUBBOd/iy2upycArDftSZ5trbsX5Ira5OzJgM=
|
||||
github.com/bytedance/sonic v1.14.0 h1:/OfKt8HFw0kh2rj8N0F6C/qPGRESq0BbaNZgcNXXzQQ=
|
||||
|
||||
@@ -15,8 +15,9 @@ import (
|
||||
// authentication and authorization operations can be performed
|
||||
// consistently across the application.
|
||||
type AccessHandler struct {
|
||||
dbHandler *dbHandler.DBHandler // Database handler used for managing users and roles
|
||||
logger *logging.Logger // Centralized application logger
|
||||
dbHandler *dbHandler.DBHandler // Database handler used for managing users and roles
|
||||
logger *logging.Logger // Centralized application logger
|
||||
NewDBCreated bool // inticator new user db created
|
||||
}
|
||||
|
||||
// NewAccessHandler
|
||||
@@ -81,6 +82,17 @@ func NewAccessHandler(path string, logger *logging.Logger) (aH *AccessHandler, e
|
||||
return
|
||||
}
|
||||
|
||||
logger.Debug("NewAccessHandler", "add workspace table")
|
||||
// Add the role table to the database
|
||||
err = aH.AddWorkspaceTable()
|
||||
if err != nil {
|
||||
aH.logger.Error("NewAccessHandler", err)
|
||||
return
|
||||
}
|
||||
|
||||
//indicator that user database was created
|
||||
aH.NewDBCreated = aH.dbHandler.NewCreatedDB
|
||||
|
||||
logger.Debug("NewAccessHandler", "add default role")
|
||||
// Add default roles to the system
|
||||
err = aH.AddDefaultRole()
|
||||
|
||||
@@ -215,7 +215,13 @@ func (aH *AccessHandler) Me(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
claims := token.Claims.(jwt.MapClaims)
|
||||
claims, ok := token.Claims.(jwt.MapClaims)
|
||||
if !ok {
|
||||
aH.logger.Error("Me", "error: token.Claims.(jwt.MapClaims)")
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"id": claims["id"],
|
||||
"user": claims["username"],
|
||||
@@ -241,7 +247,8 @@ func (aH *AccessHandler) getUserFromDB(c *gin.Context, userName string) (user mo
|
||||
hasError = true
|
||||
// Fetch user record from DB
|
||||
var dbRecord []models.User
|
||||
err := aH.dbHandler.GetByKey(&dbRecord, "Role", "user_name", userName, false)
|
||||
|
||||
err := aH.dbHandler.GetByKey(&dbRecord, "user_name", userName, false, "Role")
|
||||
if err != nil {
|
||||
aH.logger.Error("Login", err)
|
||||
c.JSON(http.StatusBadRequest, models.NewJsonErrorResponse(err))
|
||||
|
||||
@@ -132,7 +132,7 @@ func (aH *AccessHandler) AuthorizeRole(suffix string, exeptions ...string) gin.H
|
||||
|
||||
// Fetch roles and associated permissions from the database or store
|
||||
var roles []models.Role
|
||||
err := aH.dbHandler.GetByKey(&roles, "", "role", role, false)
|
||||
err := aH.dbHandler.GetByKey(&roles, "role", role, false)
|
||||
if err != nil {
|
||||
aH.logger.Error("AuthorizeRole", err)
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"message": http.StatusInternalServerError})
|
||||
|
||||
@@ -17,7 +17,7 @@ func (aH *AccessHandler) AddDefaultRole() (err error) {
|
||||
role := "admin"
|
||||
|
||||
// Check if a role with this name already exists
|
||||
if aH.dbHandler.Exists(&models.Role{}, "", "role", role, false) {
|
||||
if aH.dbHandler.Exists(&models.Role{}, "role", role, false) {
|
||||
// Found a role → skip creation
|
||||
aH.logger.Debug("AddDefaultRole", "role "+role+" exists already")
|
||||
return nil
|
||||
@@ -52,7 +52,7 @@ func (aH *AccessHandler) AddRole(c *gin.Context) {
|
||||
}
|
||||
|
||||
// Check if a role with this name already exists
|
||||
if aH.dbHandler.Exists(&models.Role{}, "", "role", role.Role, false) {
|
||||
if aH.dbHandler.Exists(&models.Role{}, "role", role.Role, false) {
|
||||
aH.logger.Error("AddRole", fmt.Sprintf("role with name %s already exists", role.Role))
|
||||
c.JSON(http.StatusBadRequest, models.NewJsonMessageResponse(fmt.Sprintf("role with name %s already exists", role.Role)))
|
||||
return
|
||||
@@ -78,7 +78,7 @@ func (aH *AccessHandler) GetRole(c *gin.Context) {
|
||||
id := c.Query("id")
|
||||
|
||||
if role != "" {
|
||||
err = aH.dbHandler.GetByKey(&roles, "", "role", role, false)
|
||||
err = aH.dbHandler.GetByKey(&roles, "role", role, false)
|
||||
} else if id != "" {
|
||||
i, err = strconv.Atoi(id)
|
||||
if err != nil {
|
||||
@@ -87,9 +87,9 @@ func (aH *AccessHandler) GetRole(c *gin.Context) {
|
||||
})
|
||||
return
|
||||
}
|
||||
err = aH.dbHandler.GetById(&roles, "", uint(i))
|
||||
err = aH.dbHandler.GetById(&roles, uint(i))
|
||||
} else {
|
||||
err = aH.dbHandler.GetById(&roles, "", 0)
|
||||
err = aH.dbHandler.GetById(&roles, 0)
|
||||
|
||||
}
|
||||
|
||||
@@ -110,7 +110,7 @@ func (aH *AccessHandler) UpdateRole(c *gin.Context) {
|
||||
}
|
||||
|
||||
if role.Id != 0 {
|
||||
err := aH.dbHandler.UpdateValuesById(&role, "", role.Id)
|
||||
err := aH.dbHandler.UpdateValuesById(&role, role.Id)
|
||||
if err != nil {
|
||||
aH.logger.Error("UpdateRole", err)
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
@@ -160,7 +160,7 @@ func (aH *AccessHandler) DeleteRole(c *gin.Context) {
|
||||
ownRole = role
|
||||
continue
|
||||
}
|
||||
err = aH.dbHandler.DeleteByKey(&models.Role{}, "", "role", role, false)
|
||||
err = aH.dbHandler.DeleteByKey(&models.Role{}, "role", role, false)
|
||||
if err != nil {
|
||||
aH.logger.Error("DeleteRole", err)
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
|
||||
@@ -29,7 +29,7 @@ func (aH *AccessHandler) AddDefaultUser() (err error) {
|
||||
}
|
||||
|
||||
// Check if a user with this email already exists
|
||||
if aH.dbHandler.Exists(&models.User{}, "", "email", user.Email, false) {
|
||||
if aH.dbHandler.Exists(&models.User{}, "email", user.Email, false) {
|
||||
aH.logger.Debug("AddDefaultUser", "user email "+user.Email+" exists already")
|
||||
// Found a user → skip create
|
||||
return nil
|
||||
@@ -42,7 +42,7 @@ func (aH *AccessHandler) AddDefaultUser() (err error) {
|
||||
}
|
||||
|
||||
role := &models.Role{}
|
||||
if err := aH.dbHandler.GetByKey(role, "", "role", "admin", false); err != nil {
|
||||
if err := aH.dbHandler.GetByKey(role, "role", "admin", false); err != nil {
|
||||
return err
|
||||
}
|
||||
return aH.dbHandler.AddRelation(user, role, "Role")
|
||||
@@ -64,7 +64,7 @@ func (aH *AccessHandler) AddUser(c *gin.Context) {
|
||||
}
|
||||
|
||||
// Check if a user with this email already exists
|
||||
if aH.dbHandler.Exists(&models.User{}, "", "email", user.Email, false) {
|
||||
if aH.dbHandler.Exists(&models.User{}, "email", user.Email, false) {
|
||||
// Found a user → skip create
|
||||
aH.logger.Error("AddUser", "user with email "+user.Email+" already exists")
|
||||
c.JSON(http.StatusBadRequest, models.NewJsonMessageResponse(fmt.Sprintf("user with email %s already exists", user.Email)))
|
||||
@@ -91,13 +91,13 @@ func (aH *AccessHandler) AddUser(c *gin.Context) {
|
||||
aH.logger.Debug("AddUser", "add new user "+user.Name+" with role "+user.Role.Role)
|
||||
|
||||
if user.Role.Id != 0 {
|
||||
if err := aH.dbHandler.GetById(&user.Role, "", user.Role.Id); err != nil {
|
||||
if err := aH.dbHandler.GetById(&user.Role, user.Role.Id); err != nil {
|
||||
aH.logger.Error("AddUser", err)
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
if err := aH.dbHandler.GetByKey(&user.Role, "", "role", user.Role.Role, false); err != nil {
|
||||
if err := aH.dbHandler.GetByKey(&user.Role, "role", user.Role.Role, false); err != nil {
|
||||
aH.logger.Error("AddUser", err)
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
return
|
||||
@@ -112,6 +112,10 @@ func (aH *AccessHandler) AddUser(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if len(user.Workspaces) > 0 {
|
||||
aH.dbHandler.AddRelation(&user, user.Workspaces, "Workspaces")
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": fmt.Sprintf("user '%s' successfully added", user.Name),
|
||||
})
|
||||
@@ -128,7 +132,7 @@ func (aH *AccessHandler) ChangePassword(c *gin.Context) {
|
||||
|
||||
// get user to check ChangePassword
|
||||
var dbRecord models.User
|
||||
err = aH.dbHandler.GetById(&dbRecord, "Role", user.Id)
|
||||
err = aH.dbHandler.GetById(&dbRecord, user.Id, "Role")
|
||||
if err != nil {
|
||||
aH.logger.Error("ChangePassword", err)
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
@@ -154,7 +158,7 @@ func (aH *AccessHandler) ChangePassword(c *gin.Context) {
|
||||
aH.logger.Debug("ChangePassword", "change user "+user.Name+" password")
|
||||
|
||||
// Update user
|
||||
aH.dbHandler.UpdateValuesById(&user, "Role", user.Id)
|
||||
aH.dbHandler.UpdateValuesById(&user, user.Id, "Role")
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": fmt.Sprintf("password of user '%s' changed", user.Name),
|
||||
@@ -178,7 +182,7 @@ func (aH *AccessHandler) GetUser(c *gin.Context) {
|
||||
}
|
||||
|
||||
var users []models.User
|
||||
err = aH.dbHandler.GetById(&users, "Role", uint(i))
|
||||
err = aH.dbHandler.GetById(&users, uint(i), "Role")
|
||||
if err != nil {
|
||||
aH.logger.Error("GetUser", err)
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
@@ -194,12 +198,16 @@ func (aH *AccessHandler) UpdateUser(c *gin.Context) {
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
return
|
||||
}
|
||||
err := aH.dbHandler.UpdateValuesById(&user, "Role", user.Id)
|
||||
err := aH.dbHandler.UpdateValuesById(&user, user.Id, "Role")
|
||||
if err != nil {
|
||||
aH.logger.Error("UpdateUser", err)
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
return
|
||||
}
|
||||
if len(user.Workspaces) > 0 {
|
||||
aH.dbHandler.AddRelation(&user, user.Workspaces, "Workspaces")
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, models.NewJsonMessageResponse("successfully updated user '"+user.Email+"'"))
|
||||
}
|
||||
|
||||
|
||||
150
handlers/workspace.go
Normal file
150
handlers/workspace.go
Normal file
@@ -0,0 +1,150 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"slices"
|
||||
"strconv"
|
||||
|
||||
"gitea.tecamino.com/paadi/access-handler/models"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func (aH *AccessHandler) AddWorkspaceTable() error {
|
||||
return aH.dbHandler.AddNewTable(models.Workspace{})
|
||||
}
|
||||
|
||||
func (aH *AccessHandler) AddWorkspace(c *gin.Context) {
|
||||
var workspace models.Workspace
|
||||
err := c.BindJSON(&workspace)
|
||||
if err != nil {
|
||||
aH.logger.Error("AddWorkspace", err)
|
||||
c.JSON(http.StatusBadRequest, models.NewJsonErrorResponse(err))
|
||||
return
|
||||
}
|
||||
|
||||
// Check if a workspace with this name already exists
|
||||
if aH.dbHandler.Exists(&models.Workspace{}, "workspace", workspace.Name, false) {
|
||||
aH.logger.Error("AddWorkspace", fmt.Sprintf("workspace with name %s already exists", workspace.Name))
|
||||
c.JSON(http.StatusBadRequest, models.NewJsonMessageResponse(fmt.Sprintf("workspace with name %s already exists", workspace.Name)))
|
||||
return
|
||||
}
|
||||
|
||||
// Insert new workspace with provided permissions
|
||||
aH.dbHandler.AddNewColum(&models.Workspace{
|
||||
Name: workspace.Name,
|
||||
Description: workspace.Description,
|
||||
})
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": fmt.Sprintf("workspace '%s' successfully added", workspace.Name),
|
||||
})
|
||||
}
|
||||
|
||||
func (aH *AccessHandler) GetWorkspace(c *gin.Context) {
|
||||
var i int
|
||||
var err error
|
||||
var workspaces []models.Workspace
|
||||
|
||||
workspace := c.Query("workspace")
|
||||
id := c.Query("id")
|
||||
|
||||
if workspace != "" {
|
||||
err = aH.dbHandler.GetByKey(&workspaces, "workspace", workspace, false)
|
||||
} else if id != "" {
|
||||
i, err = strconv.Atoi(id)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"message": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
err = aH.dbHandler.GetById(&workspaces, uint(i))
|
||||
} else {
|
||||
err = aH.dbHandler.GetById(&workspaces, 0)
|
||||
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
aH.logger.Error("GetWorkspace", err)
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, workspaces)
|
||||
}
|
||||
|
||||
func (aH *AccessHandler) UpdateWorkspace(c *gin.Context) {
|
||||
var workspace models.Workspace
|
||||
if err := c.BindJSON(&workspace); err != nil {
|
||||
aH.logger.Error("UpdateWorkspace", err)
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
return
|
||||
}
|
||||
|
||||
if workspace.Id != 0 {
|
||||
err := aH.dbHandler.UpdateValuesById(&workspace, workspace.Id)
|
||||
if err != nil {
|
||||
aH.logger.Error("UpdateWorkspace", err)
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
err := aH.dbHandler.UpdateValuesByKey(&workspace, "", "workspace", workspace.Name)
|
||||
if err != nil {
|
||||
aH.logger.Error("UpdateWorkspace", err)
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, models.NewJsonMessageResponse("successfully updated workspace '"+workspace.Name+"'"))
|
||||
}
|
||||
|
||||
func (aH *AccessHandler) DeleteWorkspace(c *gin.Context) {
|
||||
|
||||
var request struct {
|
||||
Workspaces []models.Workspace `json:"workspaces"`
|
||||
}
|
||||
|
||||
err := c.BindJSON(&request)
|
||||
if err != nil {
|
||||
aH.logger.Error("DeleteWorkspace", err)
|
||||
c.JSON(http.StatusBadRequest, nil)
|
||||
return
|
||||
}
|
||||
|
||||
if len(request.Workspaces) == 0 {
|
||||
aH.logger.Error("DeleteWorkspace", "no ids given to be deleted")
|
||||
c.JSON(http.StatusBadRequest, models.NewJsonMessageResponse("no workspaces given to be deleted"))
|
||||
return
|
||||
}
|
||||
|
||||
for _, workspace := range request.Workspaces {
|
||||
err = aH.dbHandler.DeleteByKey(&models.Workspace{}, "id", workspace, false)
|
||||
if err != nil {
|
||||
aH.logger.Error("DeleteWorkspace", err)
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
return
|
||||
}
|
||||
|
||||
var users []models.User
|
||||
if err := aH.dbHandler.GetById(&users, 0); err != nil {
|
||||
aH.logger.Error("DeleteWorkspace", err)
|
||||
c.JSON(http.StatusInternalServerError, nil)
|
||||
return
|
||||
}
|
||||
for _, u := range users {
|
||||
if u.Settings.Workspace == workspace.Name {
|
||||
u.Settings.Workspace = ""
|
||||
}
|
||||
|
||||
u.Workspaces = slices.DeleteFunc(u.Workspaces, func(w *models.Workspace) bool {
|
||||
return w != nil && w.Name == workspace.Name
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "workspace(s) deleted",
|
||||
})
|
||||
}
|
||||
@@ -15,6 +15,7 @@ type Settings struct {
|
||||
AppName string `json:"appName,omitempty"`
|
||||
DatabaseName string `json:"databaseName,omitempty"`
|
||||
DatabaseToken string `json:"databaseToken,omitempty"`
|
||||
Workspace string `json:"workspace,omitempty"`
|
||||
}
|
||||
|
||||
func (s *Settings) DefaultQuasarSettings() {
|
||||
|
||||
@@ -5,15 +5,16 @@ import (
|
||||
)
|
||||
|
||||
type User struct {
|
||||
Id uint `gorm:"primaryKey" json:"id"`
|
||||
Name string `gorm:"column:user_name" json:"user"`
|
||||
Email string `gorm:"column:email" json:"email"`
|
||||
RoleID *uint `gorm:"column:roleId" json:"roleId,omitempty"`
|
||||
Role *Role `gorm:"foreignKey:RoleID" json:"role,omitempty"`
|
||||
Password string `gorm:"column:password" json:"password"`
|
||||
NewPassword string `gorm:"-" json:"newPassword,omitempty"`
|
||||
Expiration string `gorm:"column:expiration" json:"expiration,omitempty"`
|
||||
Settings Settings `gorm:"type:json" json:"settings"`
|
||||
Id uint `gorm:"primaryKey" json:"id"`
|
||||
Name string `gorm:"column:user_name" json:"user"`
|
||||
Email string `gorm:"column:email" json:"email"`
|
||||
RoleID *uint `gorm:"column:roleId" json:"roleId,omitempty"`
|
||||
Role *Role `gorm:"foreignKey:RoleID" json:"role,omitempty"`
|
||||
Password string `gorm:"column:password" json:"password"`
|
||||
NewPassword string `gorm:"-" json:"newPassword,omitempty"`
|
||||
Expiration string `gorm:"column:expiration" json:"expiration,omitempty"`
|
||||
Settings Settings `gorm:"type:json" json:"settings"`
|
||||
Workspaces []*Workspace `gorm:"many2many:users_workspaces;" `
|
||||
}
|
||||
|
||||
func (u *User) IsValid() bool {
|
||||
|
||||
8
models/workspace.go
Normal file
8
models/workspace.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package models
|
||||
|
||||
type Workspace struct {
|
||||
Id uint `gorm:"primaryKey" json:"id"`
|
||||
Name string `gorm:"column:name" json:"name"`
|
||||
Description string `gorm:"type:json" json:"description"`
|
||||
Users []*User `gorm:"many2many:users_workspaces;" `
|
||||
}
|
||||
Reference in New Issue
Block a user