diff --git a/handlers/user.go b/handlers/user.go index cb51cc9..f838ee2 100644 --- a/handlers/user.go +++ b/handlers/user.go @@ -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), }) @@ -200,6 +204,10 @@ func (aH *AccessHandler) UpdateUser(c *gin.Context) { 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+"'")) } diff --git a/handlers/workspace.go b/handlers/workspace.go index 5809cff..f3fbe71 100644 --- a/handlers/workspace.go +++ b/handlers/workspace.go @@ -3,6 +3,7 @@ package handlers import ( "fmt" "net/http" + "slices" "strconv" "gitea.tecamino.com/paadi/access-handler/models" @@ -100,15 +101,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 +119,29 @@ 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, 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 + }) + } } c.JSON(http.StatusOK, gin.H{ diff --git a/models/settings.go b/models/settings.go index 540d26e..8cc3fa7 100644 --- a/models/settings.go +++ b/models/settings.go @@ -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() { diff --git a/models/user.go b/models/user.go index 39f8c96..069e33a 100644 --- a/models/user.go +++ b/models/user.go @@ -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 { diff --git a/models/workspace.go b/models/workspace.go index ca92b34..9654ccf 100644 --- a/models/workspace.go +++ b/models/workspace.go @@ -1,7 +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"` + 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;" ` }