feat: add group

This commit is contained in:
2024-11-16 23:50:34 +07:00
parent b88b04a235
commit c8e61ed4aa
24 changed files with 534 additions and 86 deletions
+23 -1
View File
@@ -23,19 +23,37 @@ func NewRepository(r *Hosts) *Hosts {
func (r *Hosts) GetAll(opt GetAllOpt) ([]*models.Host, error) {
query := r.db.Order("id DESC")
if len(opt.ID) > 0 {
query = query.Where("hosts.id IN (?)", opt.ID)
}
if opt.TeamID != "" {
query = query.Where("hosts.team_id = ?", opt.TeamID)
} else {
query = query.Where("hosts.owner_id = ? AND hosts.team_id IS NULL", r.User.ID)
}
if opt.ParentID != nil {
if *opt.ParentID != "" {
query = query.Where("hosts.parent_id = ?", *opt.ParentID)
} else {
query = query.Where("hosts.parent_id IS NULL")
}
}
var rows []*models.Host
ret := query.Find(&rows)
return rows, ret.Error
}
func (r *Hosts) Get(id string) (*models.HostDecrypted, error) {
func (r *Hosts) Get(id string) (*models.Host, error) {
var host models.Host
ret := r.db.Where("hosts.id = ?", id).First(&host)
return &host, ret.Error
}
func (r *Hosts) GetWithKeys(id string) (*models.HostDecrypted, error) {
var host models.Host
ret := r.db.Joins("Key").Joins("AltKey").Where("hosts.id = ?", id).First(&host)
if ret.Error != nil {
@@ -67,3 +85,7 @@ func (r *Hosts) Update(id string, item *models.Host) error {
func (r *Hosts) Delete(id string) error {
return r.db.Delete(&models.Host{Model: models.Model{ID: id}}).Error
}
func (r *Hosts) SetParentId(id *string, hostIds []string) error {
return r.db.Model(&models.Host{}).Where("id IN (?)", hostIds).Update("parent_id", id).Error
}
+57 -1
View File
@@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"net/http"
"strings"
"github.com/gofiber/fiber/v2"
"rul.sh/vaulterm/server/models"
@@ -17,10 +18,13 @@ func Router(app fiber.Router) {
router.Post("/", create)
router.Put("/:id", update)
router.Delete("/:id", delete)
router.Post("/move", move)
}
func getAll(c *fiber.Ctx) error {
teamId := c.Query("teamId")
parentId := c.Query("parentId")
user := utils.GetUser(c)
repo := NewRepository(&Hosts{User: user})
@@ -28,7 +32,7 @@ func getAll(c *fiber.Ctx) error {
return utils.ResponseError(c, errors.New("no access"), 403)
}
rows, err := repo.GetAll(GetAllOpt{TeamID: teamId})
rows, err := repo.GetAll(GetAllOpt{TeamID: teamId, ParentID: &parentId})
if err != nil {
return utils.ResponseError(c, err, 500)
}
@@ -143,3 +147,55 @@ func delete(c *fiber.Ctx) error {
"message": "Successfully deleted",
})
}
func move(c *fiber.Ctx) error {
user := utils.GetUser(c)
repo := NewRepository(&Hosts{User: user})
// validate request
var body MoveHostSchema
if err := c.BodyParser(&body); err != nil {
return utils.ResponseError(c, err, 500)
}
if body.HostID == "" {
return utils.ResponseError(c, errors.New("invalid request"), 400)
}
// get parent
var parentId *string
if body.ParentID != "" {
parent, err := repo.Get(body.ParentID)
if err != nil {
return utils.ResponseError(c, err, 500)
}
if !parent.CanWrite(&user.User) {
return utils.ResponseError(c, errors.New("no access"), 403)
}
parentId = &body.ParentID
}
// get hosts
hostIds := strings.Split(body.HostID, ",")
hosts, err := repo.GetAll(GetAllOpt{TeamID: body.TeamID, ID: hostIds})
if err != nil {
return utils.ResponseError(c, err, 500)
}
if len(hosts) != len(hostIds) {
return utils.ResponseError(c, errors.New("one or more hosts not found"), 400)
}
for _, host := range hosts {
if !host.CanWrite(&user.User) {
return utils.ResponseError(c, errors.New("no access"), 403)
}
}
// move the hosts to new parent
if err := repo.SetParentId(parentId, hostIds); err != nil {
return utils.ResponseError(c, err, 500)
}
return c.JSON(true)
}
+9 -1
View File
@@ -16,5 +16,13 @@ type CreateHostSchema struct {
}
type GetAllOpt struct {
TeamID string
TeamID string
ParentID *string
ID []string
}
type MoveHostSchema struct {
TeamID string `json:"teamId"`
ParentID string `json:"parentId"`
HostID string `json:"hostId"`
}
+7 -1
View File
@@ -149,7 +149,13 @@ func getDiskUsage(client *lib.SSHClient, wg *sync.WaitGroup, result chan<- strin
return
}
fields := strings.Fields(lines[1])
fields := strings.Fields(lines[len(lines)-2])
if len(fields) < 5 {
return
}
if !strings.HasPrefix(fields[0], "/") {
fields = append([]string{"/"}, fields...)
}
result <- fmt.Sprintf("\x03%s,%s,%s", fields[1], fields[2], fields[4])
}
+1 -1
View File
@@ -13,7 +13,7 @@ func HandleStats(c *websocket.Conn) {
user := utils.GetUserWs(c)
hostRepo := hosts.NewRepository(&hosts.Hosts{User: user})
data, _ := hostRepo.Get(hostId)
data, _ := hostRepo.GetWithKeys(hostId)
if data == nil || !data.HasAccess(&user.User) {
c.WriteMessage(websocket.TextMessage, []byte("Host not found"))
+1 -1
View File
@@ -17,7 +17,7 @@ func HandleTerm(c *websocket.Conn) {
user := utils.GetUserWs(c)
hostRepo := hosts.NewRepository(&hosts.Hosts{User: user})
data, err := hostRepo.Get(hostId)
data, err := hostRepo.GetWithKeys(hostId)
if data == nil || !data.HasAccess(&user.User) {
log.Printf("Cannot find host! %v\n", err)