2024-06-22 05:04:21 +00:00
|
|
|
package api
|
2024-05-03 17:32:44 +00:00
|
|
|
|
|
|
|
import (
|
2024-06-17 14:21:34 +00:00
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
|
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
|
2024-06-22 05:04:21 +00:00
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/server/exts"
|
2024-06-17 14:21:34 +00:00
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
|
2024-05-03 17:32:44 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
2024-10-14 14:23:51 +00:00
|
|
|
"strconv"
|
2024-05-03 17:32:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func getRealm(c *fiber.Ctx) error {
|
|
|
|
alias := c.Params("realm")
|
|
|
|
if realm, err := services.GetRealmWithAlias(alias); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
} else {
|
|
|
|
return c.JSON(realm)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func listCommunityRealm(c *fiber.Ctx) error {
|
|
|
|
realms, err := services.ListCommunityRealm()
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(realms)
|
|
|
|
}
|
|
|
|
|
|
|
|
func listOwnedRealm(c *fiber.Ctx) error {
|
2024-06-22 05:04:21 +00:00
|
|
|
if err := exts.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
user := c.Locals("user").(models.Account)
|
2024-05-03 17:47:44 +00:00
|
|
|
if realms, err := services.ListOwnedRealm(user); err != nil {
|
2024-05-03 17:32:44 +00:00
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
} else {
|
|
|
|
return c.JSON(realms)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func listAvailableRealm(c *fiber.Ctx) error {
|
2024-06-22 05:04:21 +00:00
|
|
|
if err := exts.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
user := c.Locals("user").(models.Account)
|
2024-05-03 17:47:44 +00:00
|
|
|
if realms, err := services.ListAvailableRealm(user); err != nil {
|
2024-05-03 17:32:44 +00:00
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
} else {
|
|
|
|
return c.JSON(realms)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createRealm(c *fiber.Ctx) error {
|
2024-06-22 05:04:21 +00:00
|
|
|
if err := exts.EnsureGrantedPerm(c, "CreateRealms", true); err != nil {
|
2024-05-17 11:24:14 +00:00
|
|
|
return err
|
2024-05-03 17:32:44 +00:00
|
|
|
}
|
2024-06-22 05:04:21 +00:00
|
|
|
user := c.Locals("user").(models.Account)
|
2024-05-03 17:32:44 +00:00
|
|
|
|
|
|
|
var data struct {
|
2024-09-11 14:31:30 +00:00
|
|
|
Alias string `json:"alias" validate:"required,lowercase,min=4,max=32"`
|
|
|
|
Name string `json:"name" validate:"required"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
Avatar *string `json:"avatar"`
|
|
|
|
Banner *string `json:"banner"`
|
|
|
|
AccessPolicy map[string]any `json:"access_policy"`
|
|
|
|
IsPublic bool `json:"is_public"`
|
|
|
|
IsCommunity bool `json:"is_community"`
|
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.NewRealm(models.Realm{
|
2024-09-11 14:31:30 +00:00
|
|
|
Alias: data.Alias,
|
|
|
|
Name: data.Name,
|
|
|
|
Description: data.Description,
|
|
|
|
Avatar: data.Avatar,
|
|
|
|
Banner: data.Banner,
|
|
|
|
AccessPolicy: data.AccessPolicy,
|
|
|
|
IsPublic: data.IsPublic,
|
|
|
|
IsCommunity: data.IsCommunity,
|
|
|
|
AccountID: user.ID,
|
2024-05-05 03:51:30 +00:00
|
|
|
}, user)
|
2024-05-03 17:32:44 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
2024-10-14 14:23:51 +00:00
|
|
|
} else {
|
|
|
|
services.AddEvent(user.ID, "realms.new", strconv.Itoa(int(realm.ID)), c.IP(), c.Get(fiber.HeaderUserAgent))
|
2024-05-03 17:32:44 +00:00
|
|
|
}
|
2024-10-14 14:23:51 +00:00
|
|
|
|
2024-05-03 17:32:44 +00:00
|
|
|
return c.JSON(realm)
|
|
|
|
}
|
|
|
|
|
|
|
|
func editRealm(c *fiber.Ctx) error {
|
2024-06-22 05:04:21 +00:00
|
|
|
if err := exts.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
user := c.Locals("user").(models.Account)
|
2024-05-03 17:32:44 +00:00
|
|
|
id, _ := c.ParamsInt("realmId", 0)
|
|
|
|
|
|
|
|
var data struct {
|
2024-09-11 14:31:30 +00:00
|
|
|
Alias string `json:"alias" validate:"required,lowercase,min=4,max=32"`
|
|
|
|
Name string `json:"name" validate:"required"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
Avatar *string `json:"avatar"`
|
|
|
|
Banner *string `json:"banner"`
|
|
|
|
AccessPolicy map[string]any `json:"access_policy"`
|
|
|
|
IsPublic bool `json:"is_public"`
|
|
|
|
IsCommunity bool `json:"is_community"`
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
var realm models.Realm
|
|
|
|
if err := database.C.Where(&models.Realm{
|
|
|
|
BaseModel: models.BaseModel{ID: uint(id)},
|
|
|
|
AccountID: user.ID,
|
|
|
|
}).First(&realm).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
realm.Alias = data.Alias
|
|
|
|
realm.Name = data.Name
|
|
|
|
realm.Description = data.Description
|
2024-09-11 14:31:30 +00:00
|
|
|
realm.Avatar = data.Avatar
|
|
|
|
realm.Banner = data.Banner
|
|
|
|
realm.AccessPolicy = data.AccessPolicy
|
2024-05-03 17:32:44 +00:00
|
|
|
realm.IsPublic = data.IsPublic
|
|
|
|
realm.IsCommunity = data.IsCommunity
|
|
|
|
|
|
|
|
realm, err := services.EditRealm(realm)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
2024-10-14 14:23:51 +00:00
|
|
|
} else {
|
|
|
|
services.AddEvent(user.ID, "realms.edit", strconv.Itoa(int(realm.ID)), c.IP(), c.Get(fiber.HeaderUserAgent))
|
2024-05-03 17:32:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(realm)
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteRealm(c *fiber.Ctx) error {
|
2024-06-22 05:04:21 +00:00
|
|
|
if err := exts.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
user := c.Locals("user").(models.Account)
|
2024-05-03 17:32:44 +00:00
|
|
|
id, _ := c.ParamsInt("realmId", 0)
|
|
|
|
|
|
|
|
var realm models.Realm
|
|
|
|
if err := database.C.Where(&models.Realm{
|
|
|
|
BaseModel: models.BaseModel{ID: uint(id)},
|
|
|
|
AccountID: user.ID,
|
|
|
|
}).First(&realm).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := services.DeleteRealm(realm); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
2024-10-14 14:23:51 +00:00
|
|
|
} else {
|
|
|
|
services.AddEvent(user.ID, "realms.delete", strconv.Itoa(int(realm.ID)), c.IP(), c.Get(fiber.HeaderUserAgent))
|
2024-05-03 17:32:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
|
|
}
|