muyu-apiserver/gateway/internal/logic/auth/getUserInfoLogic.go
Chever John 1f4ccfb54e feat: multi-tenant CRM with PostgreSQL graph, tenant isolation, and Casbin RBAC
- JWT claims extended with tenantId; login enforces strict tenant verification
- AuthorityMiddleware: tenant scope check + Casbin path permission + anti-spoofing
- CRM relation API (upstream/downstream one-hop, create/update/history, full graph)
- CrmRepo backed by PostgreSQL with $N placeholders
- gRPC tenant propagation via UnaryClientInterceptor (x-tenant-id metadata)
- All legacy tables (12) gain tenant_id column with indexes
- All model queries inject WHERE tenant_id filter
- Casbin gorm-adapter downgraded to v3.28.0 for v2 compatibility
- GraphSyncWorker (Kafka -> Neo4j) with idempotent MERGE
- Full graph API restricted to admin role only
- Database migrations for MySQL (CRM tables + tenant columns) and PostgreSQL (CRM init)
- Docker Compose: added postgres service to main stack, graph stack with Kafka/Debezium/Neo4j

Made-with: Cursor
2026-03-30 02:53:55 +00:00

83 lines
1.9 KiB
Go

package auth
import (
"context"
"muyu-apiserver/gateway/internal/svc"
"muyu-apiserver/gateway/internal/types"
"muyu-apiserver/pkg/ctxdata"
"muyu-apiserver/rpc/system/pb"
"github.com/zeromicro/go-zero/core/logx"
)
type GetUserInfoLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewGetUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserInfoLogic {
return &GetUserInfoLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
type UserInfoWithMenus struct {
types.UserInfo
MenuPaths []string `json:"menuPaths"`
}
func (l *GetUserInfoLogic) GetUserInfo() (*UserInfoWithMenus, error) {
userId := ctxdata.GetUserId(l.ctx)
tenantId := ctxdata.GetTenantId(l.ctx)
rpcResp, err := l.svcCtx.SystemRpc.GetUser(l.ctx, &pb.GetUserReq{
UserId: userId,
})
if err != nil {
return nil, err
}
// Get user's role menus
var menuPaths []string
if rpcResp.RoleId != "" {
role, _ := l.svcCtx.SystemRpc.GetRole(l.ctx, &pb.GetRoleReq{RoleId: rpcResp.RoleId})
if role != nil && len(role.MenuIds) > 0 {
menuList, _ := l.svcCtx.SystemRpc.ListMenu(l.ctx, &pb.ListMenuReq{})
if menuList != nil {
menuIdSet := make(map[string]bool)
for _, id := range role.MenuIds {
menuIdSet[id] = true
}
for _, m := range menuList.List {
if menuIdSet[m.MenuId] && m.Path != "" {
menuPaths = append(menuPaths, m.Path)
}
}
}
}
}
return &UserInfoWithMenus{
UserInfo: types.UserInfo{
UserId: rpcResp.UserId,
Username: rpcResp.Username,
RealName: rpcResp.RealName,
Phone: rpcResp.Phone,
Email: rpcResp.Email,
Avatar: rpcResp.Avatar,
Status: rpcResp.Status,
RoleId: rpcResp.RoleId,
RoleName: rpcResp.RoleName,
TenantId: tenantId,
LastLoginTime: rpcResp.LastLoginTime,
CreatedAt: rpcResp.CreatedAt,
UpdatedAt: rpcResp.UpdatedAt,
},
MenuPaths: menuPaths,
}, nil
}