2024-06-22 05:04:21 +00:00
|
|
|
package api
|
2024-05-03 17:32:44 +00:00
|
|
|
|
|
|
|
import (
|
2024-10-31 12:38:50 +00:00
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/internal/database"
|
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/internal/http/exts"
|
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/internal/services"
|
2024-05-03 17:32:44 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
2024-11-30 17:57:05 +00:00
|
|
|
"strconv"
|
2024-05-03 17:32:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func listRealmMembers(c *fiber.Ctx) error {
|
|
|
|
alias := c.Params("realm")
|
2024-12-01 03:32:54 +00:00
|
|
|
take := c.QueryInt("take", 0)
|
|
|
|
offset := c.QueryInt("offset", 0)
|
2024-06-08 16:12:03 +00:00
|
|
|
|
2024-05-03 17:32:44 +00:00
|
|
|
if realm, err := services.GetRealmWithAlias(alias); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
2024-12-01 03:32:54 +00:00
|
|
|
} else if count, err := services.CountRealmMember(realm.ID); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
} else if members, err := services.ListRealmMember(realm.ID, take, offset); err != nil {
|
2024-05-03 17:32:44 +00:00
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
} else {
|
2024-12-01 03:32:54 +00:00
|
|
|
return c.JSON(fiber.Map{
|
|
|
|
"count": count,
|
|
|
|
"data": members,
|
|
|
|
})
|
2024-05-03 17:32:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-08 16:12:03 +00:00
|
|
|
func getMyRealmMember(c *fiber.Ctx) error {
|
|
|
|
alias := c.Params("realm")
|
2024-06-22 05:04:21 +00:00
|
|
|
if err := exts.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-10-30 16:17:53 +00:00
|
|
|
user := c.Locals("user").(models.Account)
|
2024-06-08 16:12:03 +00:00
|
|
|
|
|
|
|
if realm, err := services.GetRealmWithAlias(alias); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
} else if member, err := services.GetRealmMember(user.ID, realm.ID); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
} else {
|
|
|
|
return c.JSON(member)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-03 17:32:44 +00:00
|
|
|
func addRealmMember(c *fiber.Ctx) error {
|
2024-06-22 05:04:21 +00:00
|
|
|
if err := exts.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-10-30 16:17:53 +00:00
|
|
|
user := c.Locals("user").(models.Account)
|
2024-05-03 17:32:44 +00:00
|
|
|
alias := c.Params("realm")
|
|
|
|
|
|
|
|
var data struct {
|
2024-11-30 17:57:05 +00:00
|
|
|
Related string `json:"related" validate:"required"`
|
2024-05-03 17:32:44 +00:00
|
|
|
}
|
|
|
|
|
2024-06-22 05:04:21 +00:00
|
|
|
if err := exts.BindAndValidate(c, &data); err != nil {
|
2024-05-03 17:32:44 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
realm, err := services.GetRealmWithAlias(alias)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
var account models.Account
|
2024-11-30 17:57:05 +00:00
|
|
|
var numericId int
|
|
|
|
if numericId, err = strconv.Atoi(data.Related); err == nil {
|
|
|
|
err = database.C.Where(&models.Account{
|
|
|
|
BaseModel: models.BaseModel{ID: uint(numericId)},
|
|
|
|
}).First(&account).Error
|
|
|
|
} else {
|
|
|
|
err = database.C.Where(&models.Account{
|
|
|
|
Name: data.Related,
|
|
|
|
}).First(&account).Error
|
|
|
|
}
|
|
|
|
if err != nil {
|
2024-05-03 17:32:44 +00:00
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
2024-10-30 16:17:53 +00:00
|
|
|
if err := services.AddRealmMember(user, account, realm); err != nil {
|
2024-05-03 17:32:44 +00:00
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
} else {
|
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeRealmMember(c *fiber.Ctx) error {
|
2024-06-22 05:04:21 +00:00
|
|
|
if err := exts.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-10-30 16:17:53 +00:00
|
|
|
user := c.Locals("user").(models.Account)
|
2024-05-03 17:32:44 +00:00
|
|
|
alias := c.Params("realm")
|
2024-11-30 17:27:42 +00:00
|
|
|
memberId, _ := c.ParamsInt("memberId", 0)
|
2024-05-03 17:32:44 +00:00
|
|
|
|
|
|
|
realm, err := services.GetRealmWithAlias(alias)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
var account models.Account
|
|
|
|
if err := database.C.Where(&models.Account{
|
2024-11-30 17:27:42 +00:00
|
|
|
BaseModel: models.BaseModel{ID: uint(memberId)},
|
2024-05-03 17:32:44 +00:00
|
|
|
}).First(&account).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
2024-11-30 17:26:33 +00:00
|
|
|
var member models.RealmMember
|
|
|
|
if err := database.C.Where(&models.RealmMember{
|
|
|
|
RealmID: realm.ID,
|
|
|
|
AccountID: account.ID,
|
|
|
|
}).First(&member).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := services.RemoveRealmMember(user, member, realm); err != nil {
|
2024-05-03 17:32:44 +00:00
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
} else {
|
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func leaveRealm(c *fiber.Ctx) error {
|
2024-06-22 05:04:21 +00:00
|
|
|
if err := exts.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-10-30 16:17:53 +00:00
|
|
|
user := c.Locals("user").(models.Account)
|
2024-05-03 17:32:44 +00:00
|
|
|
alias := c.Params("realm")
|
|
|
|
|
|
|
|
realm, err := services.GetRealmWithAlias(alias)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
} else if user.ID == realm.AccountID {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, "you cannot leave your own realm")
|
|
|
|
}
|
|
|
|
|
2024-11-30 17:26:33 +00:00
|
|
|
var member models.RealmMember
|
|
|
|
if err := database.C.Where(&models.RealmMember{
|
|
|
|
RealmID: realm.ID,
|
|
|
|
AccountID: user.ID,
|
|
|
|
}).First(&member).Error; err != nil {
|
2024-05-03 17:32:44 +00:00
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
2024-11-30 17:26:33 +00:00
|
|
|
if err := services.RemoveRealmMember(user, member, realm); err != nil {
|
2024-05-03 17:32:44 +00:00
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
} else {
|
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
|
|
}
|
|
|
|
}
|