Passport/pkg/internal/http/api/userinfo_api.go

130 lines
3.6 KiB
Go
Raw Normal View History

package api
2024-04-02 20:23:25 +08:00
import (
2024-12-08 20:21:40 +08:00
"context"
2024-07-24 18:09:18 +08:00
"fmt"
"strconv"
"strings"
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
"git.solsynth.dev/hypernet/passport/pkg/authkit/models"
2024-12-08 20:21:40 +08:00
localCache "git.solsynth.dev/hypernet/passport/pkg/internal/cache"
"git.solsynth.dev/hypernet/passport/pkg/internal/http/exts"
2024-12-08 20:21:40 +08:00
"github.com/eko/gocache/lib/v4/cache"
"github.com/eko/gocache/lib/v4/marshaler"
2024-08-24 15:17:26 +08:00
"gorm.io/gorm"
2024-07-28 20:04:22 +08:00
"git.solsynth.dev/hypernet/passport/pkg/internal/database"
"git.solsynth.dev/hypernet/passport/pkg/internal/services"
2024-04-02 20:23:25 +08:00
"github.com/gofiber/fiber/v2"
)
func getOtherUserinfo(c *fiber.Ctx) error {
alias := c.Params("alias")
2024-12-08 20:21:40 +08:00
cacheManager := cache.New[any](localCache.S)
marshal := marshaler.New(cacheManager)
ctx := context.Background()
if val, err := marshal.Get(ctx, services.GetAccountCacheKey(alias), new(models.Account)); err == nil {
return c.JSON(*val.(*models.Account))
}
tx := database.C.Where("name = ?", alias)
numericId, err := strconv.Atoi(alias)
if err == nil {
2024-12-08 20:21:40 +08:00
if val, err := marshal.Get(ctx, services.GetAccountCacheKey(numericId), new(models.Account)); err == nil {
return c.JSON(*val.(*models.Account))
}
tx = tx.Or("id = ?", numericId)
}
2024-04-02 20:23:25 +08:00
var account models.Account
if err := tx.
2024-04-06 01:07:36 +08:00
Preload("Profile").
2024-08-24 15:17:26 +08:00
Preload("Badges", func(db *gorm.DB) *gorm.DB {
return db.Order("badges.type DESC")
2024-08-24 15:17:26 +08:00
}).
2024-04-02 20:23:25 +08:00
First(&account).Error; err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
2024-07-24 18:09:18 +08: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-12-08 20:21:40 +08:00
services.CacheAccount(account)
2024-04-02 20:23:25 +08:00
return c.JSON(account)
}
func getOtherUserinfoBatch(c *fiber.Ctx) error {
2024-08-01 12:47:19 +08:00
idFilter := c.Query("id")
nameFilter := c.Query("name")
idSet := strings.Split(idFilter, ",")
nameSet := strings.Split(nameFilter, ",")
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 12:47:19 +08:00
if len(idFilter) > 0 {
tx = tx.Where("id IN ?", idSet)
}
2024-08-01 12:47:19 +08:00
if len(nameFilter) > 0 {
tx = tx.Where("name IN ?", nameSet)
}
var accounts []models.Account
if err := tx.Find(&accounts).Error; err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
return c.JSON(accounts)
}
func getUserinfoForOidc(c *fiber.Ctx) error {
if err := exts.EnsureAuthenticated(c); err != nil {
return err
}
user := c.Locals("user").(models.Account)
var data models.Account
if err := database.C.
Where(&models.Account{BaseModel: models.BaseModel{ID: user.ID}}).
Preload("Profile").
Preload("Contacts").
Preload("Badges").
First(&data).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else {
data.PermNodes = c.Locals("nex_user").(*sec.UserInfo).PermNodes
}
return c.JSON(fiber.Map{
"sub": fmt.Sprintf("%d", data.ID),
"family_name": data.Profile.FirstName,
"given_name": data.Profile.LastName,
"name": data.Name,
"email": data.GetPrimaryEmail().Content,
"email_verified": data.GetPrimaryEmail().VerifiedAt != nil,
"preferred_username": data.Nick,
"picture": data.GetAvatar(),
"birthdate": data.Profile.Birthday,
"updated_at": data.UpdatedAt,
})
}