Skip to content

Commit

Permalink
feat: 应用获取其他用户信息接口
Browse files Browse the repository at this point in the history
  • Loading branch information
Mmx233 committed Mar 14, 2024
1 parent 253cec9 commit 4c032aa
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/api/controllers/public/token.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package public
package controllers

import (
"context"
Expand Down
26 changes: 26 additions & 0 deletions internal/api/controllers/public/user.go
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions internal/api/router/public/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
13 changes: 13 additions & 0 deletions internal/api/router/public/user.go
Original file line number Diff line number Diff line change
@@ -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)
}
9 changes: 9 additions & 0 deletions internal/db/dao/User.go
Original file line number Diff line number Diff line change
Expand Up @@ -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").
Expand Down
7 changes: 7 additions & 0 deletions internal/db/dto/User.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:"-"`
}
4 changes: 4 additions & 0 deletions internal/service/User.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 4c032aa

Please sign in to comment.