From 1f4f9a12aa9ad47bb07baa1b0523a9f86fa75b88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrian=20Z=C3=BCrcher?= Date: Sat, 14 Feb 2026 14:47:41 +0100 Subject: [PATCH] add new database table workspace --- handlers/access.go | 8 +++ handlers/workspace.go | 154 ++++++++++++++++++++++++++++++++++++++++++ models/settings.go | 18 ++--- models/workspace.go | 7 ++ 4 files changed, 179 insertions(+), 8 deletions(-) create mode 100644 handlers/workspace.go create mode 100644 models/workspace.go diff --git a/handlers/access.go b/handlers/access.go index 6632e45..ed94cd0 100644 --- a/handlers/access.go +++ b/handlers/access.go @@ -82,6 +82,14 @@ 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 diff --git a/handlers/workspace.go b/handlers/workspace.go new file mode 100644 index 0000000..5809cff --- /dev/null +++ b/handlers/workspace.go @@ -0,0 +1,154 @@ +package handlers + +import ( + "fmt" + "net/http" + "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) { + 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"` + } + + 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 + } + + var ownWorkspace string + + for _, workspace := range request.Workspaces { + if queryWorkspace == workspace { + ownWorkspace = workspace + continue + } + err = aH.dbHandler.DeleteByKey(&models.Workspace{}, "workspace", workspace, 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 + } + + c.JSON(http.StatusOK, gin.H{ + "message": "workspace(s) deleted", + }) +} diff --git a/models/settings.go b/models/settings.go index f9924c6..540d26e 100644 --- a/models/settings.go +++ b/models/settings.go @@ -7,14 +7,16 @@ 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"` + 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"` } func (s *Settings) DefaultQuasarSettings() { diff --git a/models/workspace.go b/models/workspace.go new file mode 100644 index 0000000..bc2d760 --- /dev/null +++ b/models/workspace.go @@ -0,0 +1,7 @@ +package models + +type Workspace struct { + Id uint `gorm:"primaryKey" json:"id"` + Name string `gorm:"column:name" json:"name"` + Description Permissions `gorm:"type:json" json:"description"` +}