From 4c032aac066305294a0a3ebbee95fac275358e50 Mon Sep 17 00:00:00 2001 From: Mmx233 Date: Thu, 14 Mar 2024 18:24:28 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=BA=94=E7=94=A8=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E5=85=B6=E4=BB=96=E7=94=A8=E6=88=B7=E4=BF=A1=E6=81=AF=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/api/controllers/public/token.go | 2 +- internal/api/controllers/public/user.go | 26 ++++++++++++++++++++++++ internal/api/router/public/init.go | 1 + internal/api/router/public/user.go | 13 ++++++++++++ internal/db/dao/User.go | 9 ++++++++ internal/db/dto/User.go | 7 +++++++ internal/service/User.go | 4 ++++ 7 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 internal/api/controllers/public/user.go create mode 100644 internal/api/router/public/user.go diff --git a/internal/api/controllers/public/token.go b/internal/api/controllers/public/token.go index 7f8c30ac..3fa69892 100644 --- a/internal/api/controllers/public/token.go +++ b/internal/api/controllers/public/token.go @@ -1,4 +1,4 @@ -package public +package controllers import ( "context" diff --git a/internal/api/controllers/public/user.go b/internal/api/controllers/public/user.go new file mode 100644 index 00000000..412023ea --- /dev/null +++ b/internal/api/controllers/public/user.go @@ -0,0 +1,26 @@ +package controllers + +import ( + "github.com/gin-gonic/gin" + "github.com/ncuhome/GeniusAuthoritarian/internal/api/callback" + "github.com/ncuhome/GeniusAuthoritarian/internal/service" + "github.com/ncuhome/GeniusAuthoritarian/internal/tools" +) + +func GetUserPublicInfo(c *gin.Context) { + var f struct { + ID []uint `json:"id" form:"id" binding:"required"` + } + if err := tools.ShouldBindReused(c, &f); err != nil { + callback.Error(c, callback.ErrForm, err) + return + } + + data, err := service.User.GetUserInfoPublic(f.ID...) + if err != nil { + callback.Error(c, callback.ErrDBOperation, err) + return + } + + callback.Success(c, data) +} diff --git a/internal/api/router/public/init.go b/internal/api/router/public/init.go index 9aa8af1d..7d041ff3 100644 --- a/internal/api/router/public/init.go +++ b/internal/api/router/public/init.go @@ -8,5 +8,6 @@ import ( func Router(G *gin.RouterGroup) { routerApp(G.Group("app")) routerToken(G.Group("token")) + routerUser(G.Group("user")) login.Router(G.Group("login")) } diff --git a/internal/api/router/public/user.go b/internal/api/router/public/user.go new file mode 100644 index 00000000..2814ce25 --- /dev/null +++ b/internal/api/router/public/user.go @@ -0,0 +1,13 @@ +package public + +import ( + "github.com/gin-gonic/gin" + controllers "github.com/ncuhome/GeniusAuthoritarian/internal/api/controllers/public" + "github.com/ncuhome/GeniusAuthoritarian/internal/api/middlewares" +) + +func routerUser(G *gin.RouterGroup) { + G.Use(middlewares.RequireAppSignature) + + G.GET("info", controllers.GetUserPublicInfo) +} diff --git a/internal/db/dao/User.go b/internal/db/dao/User.go index 4c584ad6..ac624b8d 100644 --- a/internal/db/dao/User.go +++ b/internal/db/dao/User.go @@ -89,6 +89,15 @@ func (a *User) GetNoSshDevIds(tx *gorm.DB) ([]uint, error) { return t, tx.Select("users.id").Where("base_groups.name=? AND user_sshes.id IS NULL", departments.UDev).Find(&t).Error } +func (a *User) GetUserInfoPubByIds(tx *gorm.DB, ids ...uint) ([]dto.UserInfoPublic, error) { + var t = make([]dto.UserInfoPublic, 0) + tx = tx.Model(a) + tx = a.sqlJoinUserGroups(tx) + return t, tx.Where("id IN ?", ids). + Clauses(clause.OrderBy{Expression: clause.Expr{SQL: "FIELD(id,?)", Vars: []interface{}{ids}, WithoutParentheses: true}}). + Find(&t).Error +} + func (a *User) U2fStatus(tx *gorm.DB) (*dto.UserU2fStatus, error) { var t dto.UserU2fStatus return &t, tx.Model(&User{}).Select("users.prefer_u2f AS prefer", "1 AS phone", "(users.mfa IS NOT NULL AND users.mfa!='') AS mfa", "user_webauthns.id IS NOT NULL AS passkey"). diff --git a/internal/db/dto/User.go b/internal/db/dto/User.go index 32a0fb89..86487733 100644 --- a/internal/db/dto/User.go +++ b/internal/db/dto/User.go @@ -19,3 +19,10 @@ type UserU2fStatus struct { Mfa bool `json:"mfa"` Passkey bool `json:"passkey"` } + +type UserInfoPublic struct { + ID uint `json:"id"` + Name string `json:"name"` + AvatarUrl string `json:"avatar_url"` + Groups []Group `json:"groups" gorm:"-"` +} diff --git a/internal/service/User.go b/internal/service/User.go index fbfebb28..02e0af3b 100644 --- a/internal/service/User.go +++ b/internal/service/User.go @@ -29,6 +29,10 @@ func (a UserSrv) GetUserNotInPhoneSlice(phone []string) ([]dao.User, error) { return (&dao.User{}).GetNotInPhoneSlice(a.DB, phone) } +func (a UserSrv) GetUserInfoPublic(id ...uint) ([]dto.UserInfoPublic, error) { + return (&dao.User{}).GetUserInfoPubByIds(a.DB, id...) +} + func (a UserSrv) CreateAll(users []dao.User) error { return (&dao.User{}).InsertAll(a.DB, users) }