2024-06-22 05:04:21 +00:00
|
|
|
package api
|
2024-04-02 12:23:25 +00:00
|
|
|
|
|
|
|
import (
|
2024-07-24 10:09:18 +00:00
|
|
|
"fmt"
|
2024-10-31 12:38:50 +00:00
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
2024-08-24 07:17:26 +00:00
|
|
|
"gorm.io/gorm"
|
2024-11-16 16:17:56 +00:00
|
|
|
"strconv"
|
2024-07-30 12:28:38 +00:00
|
|
|
"strings"
|
2024-07-28 12:04:22 +00:00
|
|
|
|
2024-10-31 12:38:50 +00:00
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/internal/database"
|
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/internal/services"
|
2024-04-02 12:23:25 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func getOtherUserinfo(c *fiber.Ctx) error {
|
|
|
|
alias := c.Params("alias")
|
|
|
|
|
2024-11-16 16:17:56 +00:00
|
|
|
tx := database.C.Where("name = ?")
|
|
|
|
|
|
|
|
numericId, err := strconv.Atoi(alias)
|
|
|
|
if err == nil {
|
|
|
|
tx = tx.Where("id = ?", numericId)
|
|
|
|
}
|
|
|
|
|
2024-04-02 12:23:25 +00:00
|
|
|
var account models.Account
|
2024-11-16 16:17:56 +00:00
|
|
|
if err := tx.
|
2024-04-05 17:07:36 +00:00
|
|
|
Preload("Profile").
|
2024-08-24 07:17:26 +00:00
|
|
|
Preload("Badges", func(db *gorm.DB) *gorm.DB {
|
2024-11-16 16:17:56 +00:00
|
|
|
return db.Order("badges.type DESC")
|
2024-08-24 07:17:26 +00:00
|
|
|
}).
|
2024-04-02 12:23:25 +00:00
|
|
|
First(&account).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
2024-07-24 10:09:18 +00:00
|
|
|
groups, err := services.GetUserAccountGroup(account)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("unable to get account groups: %v", err))
|
|
|
|
}
|
|
|
|
for _, group := range groups {
|
|
|
|
for k, v := range group.PermNodes {
|
|
|
|
if _, ok := account.PermNodes[k]; !ok {
|
|
|
|
account.PermNodes[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-02 12:23:25 +00:00
|
|
|
return c.JSON(account)
|
|
|
|
}
|
2024-07-30 12:28:38 +00:00
|
|
|
|
|
|
|
func getOtherUserinfoBatch(c *fiber.Ctx) error {
|
2024-08-01 04:47:19 +00:00
|
|
|
idFilter := c.Query("id")
|
|
|
|
nameFilter := c.Query("name")
|
|
|
|
idSet := strings.Split(idFilter, ",")
|
|
|
|
nameSet := strings.Split(nameFilter, ",")
|
2024-08-01 04:11:57 +00:00
|
|
|
if len(idSet) == 0 && len(nameSet) == 0 {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, "query filter is required")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(idSet)+len(nameSet) > 100 {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, "only support 100 users in a single batch")
|
|
|
|
}
|
|
|
|
|
|
|
|
tx := database.C.Model(&models.Account{}).Limit(100)
|
2024-08-01 04:47:19 +00:00
|
|
|
if len(idFilter) > 0 {
|
2024-08-01 04:11:57 +00:00
|
|
|
tx = tx.Where("id IN ?", idSet)
|
|
|
|
}
|
2024-08-01 04:47:19 +00:00
|
|
|
if len(nameFilter) > 0 {
|
2024-08-01 04:11:57 +00:00
|
|
|
tx = tx.Where("name IN ?", nameSet)
|
2024-07-30 12:28:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var accounts []models.Account
|
2024-08-01 04:11:57 +00:00
|
|
|
if err := tx.Find(&accounts).Error; err != nil {
|
2024-07-30 12:28:38 +00:00
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(accounts)
|
|
|
|
}
|