Compare commits

..

8 Commits

Author SHA1 Message Date
Adrian Zürcher
6e3df3ffa0 fix removing workspace from users 2026-02-17 17:47:36 +01:00
Adrian Zürcher
9dc6c217fa fix nil pointer error 2026-02-17 16:44:29 +01:00
Adrian Zürcher
c4bd121388 change workspace from string to model 2026-02-17 16:34:47 +01:00
Adrian Zürcher
e26c2af201 add uuid nil check 2026-02-17 12:16:21 +01:00
Adrian Zürcher
093c55616c add new function to read files in workspace 2026-02-17 12:07:56 +01:00
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
5 changed files with 101 additions and 19 deletions

2
go.mod
View File

@@ -8,6 +8,7 @@ require (
github.com/gin-gonic/gin v1.11.0 github.com/gin-gonic/gin v1.11.0
github.com/go-playground/assert/v2 v2.2.0 github.com/go-playground/assert/v2 v2.2.0
github.com/golang-jwt/jwt/v5 v5.3.0 github.com/golang-jwt/jwt/v5 v5.3.0
github.com/google/uuid v1.3.0
golang.org/x/crypto v0.43.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/go-playground/validator/v10 v10.27.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect github.com/goccy/go-json v0.10.2 // indirect
github.com/goccy/go-yaml v1.18.0 // 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/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect github.com/jinzhu/now v1.1.5 // indirect
github.com/json-iterator/go v1.1.12 // indirect github.com/json-iterator/go v1.1.12 // indirect

View File

@@ -3,11 +3,15 @@ package handlers
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"os"
"path/filepath"
"slices" "slices"
"strconv" "strconv"
"strings"
"gitea.tecamino.com/paadi/access-handler/models" "gitea.tecamino.com/paadi/access-handler/models"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/google/uuid"
) )
func (aH *AccessHandler) AddWorkspaceTable() error { func (aH *AccessHandler) AddWorkspaceTable() error {
@@ -24,16 +28,33 @@ func (aH *AccessHandler) AddWorkspace(c *gin.Context) {
} }
// Check if a workspace with this name already exists // 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)) 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))) c.JSON(http.StatusBadRequest, models.NewJsonMessageResponse(fmt.Sprintf("workspace with name %s already exists", workspace.Name)))
return 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 // Insert new workspace with provided permissions
aH.dbHandler.AddNewColum(&models.Workspace{ aH.dbHandler.AddNewColum(&models.Workspace{
Name: workspace.Name, Name: workspace.Name,
Description: workspace.Description, Description: workspace.Description,
Uuid: &uid,
}) })
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{
@@ -120,7 +141,7 @@ func (aH *AccessHandler) DeleteWorkspace(c *gin.Context) {
} }
for _, workspace := range request.Workspaces { for _, workspace := range request.Workspaces {
err = aH.dbHandler.DeleteByKey(&models.Workspace{}, "id", workspace, false) err = aH.dbHandler.DeleteByKey(&models.Workspace{}, "id", workspace.Id, false)
if err != nil { if err != nil {
aH.logger.Error("DeleteWorkspace", err) aH.logger.Error("DeleteWorkspace", err)
c.JSON(http.StatusInternalServerError, nil) c.JSON(http.StatusInternalServerError, nil)
@@ -133,18 +154,76 @@ func (aH *AccessHandler) DeleteWorkspace(c *gin.Context) {
c.JSON(http.StatusInternalServerError, nil) c.JSON(http.StatusInternalServerError, nil)
return return
} }
for _, u := range users { for _, u := range users {
if u.Settings.Workspace == workspace.Name { if u.Settings.Workspace == nil {
u.Settings.Workspace = "" continue
} else if u.Settings.Workspace.Uuid.String() == workspace.Uuid.String() {
u.Settings.Workspace = nil
} }
u.Workspaces = slices.DeleteFunc(u.Workspaces, func(w *models.Workspace) bool { u.Workspaces = slices.DeleteFunc(u.Workspaces, func(w *models.Workspace) bool {
return w != nil && w.Name == workspace.Name return w != nil && w.Name == workspace.Name
}) })
if err := aH.dbHandler.UpdateValuesById(&u, u.Id); err != nil {
aH.logger.Error("DeleteWorkspace", err)
c.JSON(http.StatusInternalServerError, nil)
return
}
} }
// remove 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{ c.JSON(http.StatusOK, gin.H{
"message": "workspace(s) deleted", "message": "workspace(s) deleted",
}) })
} }
func (aH *AccessHandler) ReadWorkspaceData(c *gin.Context) {
var workspace models.Workspace
err := c.ShouldBindBodyWithJSON(&workspace)
if err != nil {
aH.logger.Error("ReadWorkspaceData", err)
c.JSON(http.StatusBadRequest, nil)
return
}
if workspace.Uuid == nil {
aH.logger.Error("ReadWorkspaceData", "uuid nil")
c.JSON(http.StatusBadRequest, nil)
return
}
fmt.Println(123, workspace)
files, err := os.ReadDir(workspace.Uuid.String())
fmt.Println(124, err)
if err != nil {
aH.logger.Error("ReadWorkspaceData", err)
c.JSON(http.StatusBadRequest, nil)
return
}
var response []string
for _, f := range files {
if f.IsDir() || !strings.Contains(filepath.Ext(f.Name()), ".db") {
continue
}
response = append(response, f.Name())
}
c.JSON(http.StatusOK, gin.H{
"data": response,
})
}

View File

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

View File

@@ -14,7 +14,7 @@ type User struct {
NewPassword string `gorm:"-" json:"newPassword,omitempty"` NewPassword string `gorm:"-" json:"newPassword,omitempty"`
Expiration string `gorm:"column:expiration" json:"expiration,omitempty"` Expiration string `gorm:"column:expiration" json:"expiration,omitempty"`
Settings Settings `gorm:"type:json" json:"settings"` Settings Settings `gorm:"type:json" json:"settings"`
Workspaces []*Workspace `gorm:"many2many:users_workspaces;" ` Workspaces []*Workspace `gorm:"many2many:users_workspaces;" json:"workspaces"`
} }
func (u *User) IsValid() bool { func (u *User) IsValid() bool {

View File

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