add workspaces to user
This commit is contained in:
@@ -112,6 +112,10 @@ func (aH *AccessHandler) AddUser(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(user.Workspaces) > 0 {
|
||||||
|
aH.dbHandler.AddRelation(&user, user.Workspaces, "Workspaces")
|
||||||
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"message": fmt.Sprintf("user '%s' successfully added", user.Name),
|
"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)
|
c.JSON(http.StatusInternalServerError, nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if len(user.Workspaces) > 0 {
|
||||||
|
aH.dbHandler.AddRelation(&user, user.Workspaces, "Workspaces")
|
||||||
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, models.NewJsonMessageResponse("successfully updated user '"+user.Email+"'"))
|
c.JSON(http.StatusOK, models.NewJsonMessageResponse("successfully updated user '"+user.Email+"'"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package handlers
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"slices"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"gitea.tecamino.com/paadi/access-handler/models"
|
"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) {
|
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 {
|
var request struct {
|
||||||
Workspaces []string `json:"workspaces"`
|
Workspaces []models.Workspace `json:"workspaces"`
|
||||||
}
|
}
|
||||||
|
|
||||||
err := c.BindJSON(&request)
|
err := c.BindJSON(&request)
|
||||||
@@ -124,28 +119,29 @@ func (aH *AccessHandler) DeleteWorkspace(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var ownWorkspace string
|
|
||||||
|
|
||||||
for _, workspace := range request.Workspaces {
|
for _, workspace := range request.Workspaces {
|
||||||
if queryWorkspace == workspace {
|
err = aH.dbHandler.DeleteByKey(&models.Workspace{}, "id", workspace, false)
|
||||||
ownWorkspace = workspace
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
err = aH.dbHandler.DeleteByKey(&models.Workspace{}, "workspace", workspace, 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)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if ownWorkspace != "" {
|
var users []models.User
|
||||||
aH.logger.Error("DeleteWorkspace", "can not delete logged in workspace id: "+ownWorkspace)
|
if err := aH.dbHandler.GetById(&users, 0); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{
|
aH.logger.Error("DeleteWorkspace", err)
|
||||||
"message": "can not delete logged in workspace id: " + ownWorkspace,
|
c.JSON(http.StatusInternalServerError, nil)
|
||||||
"workspace": ownWorkspace,
|
return
|
||||||
})
|
}
|
||||||
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{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
|||||||
@@ -7,16 +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 string `json:"workspace,omitempty"`
|
||||||
Workspaces []string `json:"workspaces,omitempty"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Settings) DefaultQuasarSettings() {
|
func (s *Settings) DefaultQuasarSettings() {
|
||||||
|
|||||||
@@ -5,15 +5,16 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
Id uint `gorm:"primaryKey" json:"id"`
|
Id uint `gorm:"primaryKey" json:"id"`
|
||||||
Name string `gorm:"column:user_name" json:"user"`
|
Name string `gorm:"column:user_name" json:"user"`
|
||||||
Email string `gorm:"column:email" json:"email"`
|
Email string `gorm:"column:email" json:"email"`
|
||||||
RoleID *uint `gorm:"column:roleId" json:"roleId,omitempty"`
|
RoleID *uint `gorm:"column:roleId" json:"roleId,omitempty"`
|
||||||
Role *Role `gorm:"foreignKey:RoleID" json:"role,omitempty"`
|
Role *Role `gorm:"foreignKey:RoleID" json:"role,omitempty"`
|
||||||
Password string `gorm:"column:password" json:"password"`
|
Password string `gorm:"column:password" json:"password"`
|
||||||
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;" `
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *User) IsValid() bool {
|
func (u *User) IsValid() bool {
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
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;" `
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user