Compare commits

...

7 Commits

Author SHA1 Message Date
Adrian Zürcher
a99189b31a add remove folder in delete workspace 2026-02-17 07:34:34 +01:00
Adrian Zürcher
38a7660d41 add new uuid for workspace and create uuid folder 2026-02-17 07:30:10 +01:00
Adrian Zürcher
7d604189af fixes 2026-02-16 22:24:05 +01:00
Adrian Zürcher
4b574d4486 fix wrong relation name 2026-02-16 22:00:25 +01:00
Adrian Zürcher
a094b70b5b add new function replace realtions 2026-02-16 21:47:26 +01:00
Adrian Zürcher
3eb40bc02f add workspaces to user 2026-02-16 19:29:41 +01:00
Adrian Zürcher
81959425a1 check error 2026-02-16 19:29:10 +01:00
8 changed files with 114 additions and 54 deletions

4
go.mod
View File

@@ -3,11 +3,12 @@ module gitea.tecamino.com/paadi/access-handler
go 1.24.5
require (
gitea.tecamino.com/paadi/dbHandler v1.1.10
gitea.tecamino.com/paadi/dbHandler v1.1.12
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
github.com/golang-jwt/jwt/v5 v5.3.0
github.com/google/uuid v1.3.0
golang.org/x/crypto v0.43.0
)
@@ -25,7 +26,6 @@ require (
github.com/go-playground/validator/v10 v10.27.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/goccy/go-yaml v1.18.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/json-iterator/go v1.1.12 // indirect

4
go.sum
View File

@@ -1,5 +1,5 @@
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/dbHandler v1.1.12 h1:F1ARSTUm0MZmF84FfD/g5RQNMYyDYXHYrB3cXPSi4qw=
gitea.tecamino.com/paadi/dbHandler v1.1.12/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=

View File

@@ -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"],

View File

@@ -45,6 +45,7 @@ func (aH *AccessHandler) AddDefaultUser() (err error) {
if err := aH.dbHandler.GetByKey(role, "role", "admin", false); err != nil {
return err
}
return aH.dbHandler.AddRelation(user, role, "Role")
}
@@ -112,6 +113,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 +133,7 @@ func (aH *AccessHandler) ChangePassword(c *gin.Context) {
// get user to check ChangePassword
var dbRecord models.User
err = aH.dbHandler.GetById(&dbRecord, user.Id, "Role")
err = aH.dbHandler.GetById(&dbRecord, user.Id, "Role", "Workspaces")
if err != nil {
aH.logger.Error("ChangePassword", err)
c.JSON(http.StatusInternalServerError, nil)
@@ -154,7 +159,7 @@ func (aH *AccessHandler) ChangePassword(c *gin.Context) {
aH.logger.Debug("ChangePassword", "change user "+user.Name+" password")
// Update user
aH.dbHandler.UpdateValuesById(&user, user.Id, "Role")
aH.dbHandler.UpdateValuesById(&user, user.Id, "Role", "Workspaces")
c.JSON(http.StatusOK, gin.H{
"message": fmt.Sprintf("password of user '%s' changed", user.Name),
@@ -178,12 +183,18 @@ func (aH *AccessHandler) GetUser(c *gin.Context) {
}
var users []models.User
err = aH.dbHandler.GetById(&users, uint(i), "Role")
err = aH.dbHandler.GetById(&users, uint(i), "Role", "Workspaces")
if err != nil {
aH.logger.Error("GetUser", err)
c.JSON(http.StatusInternalServerError, nil)
return
}
//remove password
for i := range users {
users[i].Password = ""
}
c.JSON(http.StatusOK, users)
}
@@ -194,12 +205,29 @@ func (aH *AccessHandler) UpdateUser(c *gin.Context) {
c.JSON(http.StatusInternalServerError, nil)
return
}
err := aH.dbHandler.UpdateValuesById(&user, user.Id, "Role")
var currentUser models.User
err := aH.dbHandler.GetById(&currentUser, user.Id, "Role", "Workspaces")
if err != nil {
aH.logger.Error("UpdateUser", err)
c.JSON(http.StatusInternalServerError, nil)
return
}
err = aH.dbHandler.ReplaceRelation(&user, "Workspaces", user.Workspaces)
if err != nil {
aH.logger.Error("UpdateUser", err)
c.JSON(http.StatusInternalServerError, nil)
return
}
err = aH.dbHandler.UpdateValuesById(&user, user.Id, "Role", "Workspaces")
if err != nil {
aH.logger.Error("UpdateUser", err)
c.JSON(http.StatusInternalServerError, nil)
return
}
c.JSON(http.StatusOK, models.NewJsonMessageResponse("successfully updated user '"+user.Email+"'"))
}

View File

@@ -3,10 +3,13 @@ package handlers
import (
"fmt"
"net/http"
"os"
"slices"
"strconv"
"gitea.tecamino.com/paadi/access-handler/models"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
func (aH *AccessHandler) AddWorkspaceTable() error {
@@ -23,16 +26,32 @@ func (aH *AccessHandler) AddWorkspace(c *gin.Context) {
}
// Check if a workspace with this name already exists
if aH.dbHandler.Exists(&models.Workspace{}, "workspace", workspace.Name, false) {
if aH.dbHandler.Exists(&models.Workspace{}, "name", 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
}
uid, err := uuid.NewUUID()
if err != nil {
aH.logger.Error("AddWorkspace", err)
c.JSON(http.StatusBadRequest, models.NewJsonErrorResponse(err))
return
}
// create new folder
err = os.MkdirAll(uid.String(), 0644)
if err != nil {
aH.logger.Error("AddWorkspace", err)
c.JSON(http.StatusBadRequest, models.NewJsonErrorResponse(err))
return
}
// Insert new workspace with provided permissions
aH.dbHandler.AddNewColum(&models.Workspace{
Name: workspace.Name,
Description: workspace.Description,
Uuid: uid,
})
c.JSON(http.StatusOK, gin.H{
@@ -100,15 +119,9 @@ func (aH *AccessHandler) UpdateWorkspace(c *gin.Context) {
}
func (aH *AccessHandler) DeleteWorkspace(c *gin.Context) {
queryWorkspace := c.Query("workspace")
if queryWorkspace == "" || queryWorkspace == "null" || queryWorkspace == "undefined" {
aH.logger.Error("DeleteWorkspace", "id query missing or wrong value: "+queryWorkspace)
c.JSON(http.StatusInternalServerError, nil)
return
}
var request struct {
Workspaces []string `json:"workspaces"`
Workspaces []models.Workspace `json:"workspaces"`
}
err := c.BindJSON(&request)
@@ -124,28 +137,37 @@ func (aH *AccessHandler) DeleteWorkspace(c *gin.Context) {
return
}
var ownWorkspace string
for _, workspace := range request.Workspaces {
if queryWorkspace == workspace {
ownWorkspace = workspace
continue
}
err = aH.dbHandler.DeleteByKey(&models.Workspace{}, "workspace", workspace, false)
err = aH.dbHandler.DeleteByKey(&models.Workspace{}, "id", workspace.Id, false)
if err != nil {
aH.logger.Error("DeleteWorkspace", err)
c.JSON(http.StatusInternalServerError, nil)
return
}
}
if ownWorkspace != "" {
aH.logger.Error("DeleteWorkspace", "can not delete logged in workspace id: "+ownWorkspace)
c.JSON(http.StatusBadRequest, gin.H{
"message": "can not delete logged in workspace id: " + ownWorkspace,
"workspace": ownWorkspace,
})
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
})
}
// crearemove folder
err = os.RemoveAll(workspace.Uuid.String())
if err != nil {
aH.logger.Error("DeleteWorkspace", err)
c.JSON(http.StatusBadRequest, models.NewJsonErrorResponse(err))
return
}
}
c.JSON(http.StatusOK, gin.H{

View File

@@ -7,16 +7,15 @@ import (
)
type Settings struct {
PrimaryColor string `json:"primaryColor,omitempty"`
PrimaryColorText string `json:"primaryColorText,omitempty"`
SecondaryColor string `json:"secondaryColor,omitempty"`
SecondaryColorText string `json:"secondaryColorText,omitempty"`
Icon string `json:"icon,omitempty"`
AppName string `json:"appName,omitempty"`
DatabaseName string `json:"databaseName,omitempty"`
DatabaseToken string `json:"databaseToken,omitempty"`
Workspace string `json:"workspace,omitempty"`
Workspaces []string `json:"workspaces,omitempty"`
PrimaryColor string `json:"primaryColor,omitempty"`
PrimaryColorText string `json:"primaryColorText,omitempty"`
SecondaryColor string `json:"secondaryColor,omitempty"`
SecondaryColorText string `json:"secondaryColorText,omitempty"`
Icon string `json:"icon,omitempty"`
AppName string `json:"appName,omitempty"`
DatabaseName string `json:"databaseName,omitempty"`
DatabaseToken string `json:"databaseToken,omitempty"`
Workspace string `json:"workspace,omitempty"`
}
func (s *Settings) DefaultQuasarSettings() {

View File

@@ -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,omitempty"`
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;" json:"workspaces"`
}
func (u *User) IsValid() bool {

View File

@@ -1,7 +1,11 @@
package models
import "github.com/google/uuid"
type Workspace struct {
Id uint `gorm:"primaryKey" json:"id"`
Name string `gorm:"column:name" json:"name"`
Description string `gorm:"type:json" json:"description"`
Id uint `gorm:"primaryKey" json:"id"`
Name string `gorm:"column:name" json:"name"`
Description string `gorm:"type:json" json:"description"`
Uuid uuid.UUID `gorm:"type:text" json:"uuid"`
Users []*User `gorm:"many2many:users_workspaces;" `
}