32 lines
641 B
Go
32 lines
641 B
Go
package utils
|
|
|
|
import (
|
|
"context"
|
|
|
|
"gitea.redpowerfuture.com/red-future/common/beans"
|
|
)
|
|
|
|
// GetCurrentUser 从 context 中获取当前登录用户名
|
|
func GetCurrentUser(ctx context.Context) string {
|
|
if u := getUser(ctx); u != nil {
|
|
return u.UserName
|
|
}
|
|
return "unknown"
|
|
}
|
|
|
|
// GetCurrentTenantId 从 context 中获取当前租户 ID
|
|
func GetCurrentTenantId(ctx context.Context) uint64 {
|
|
if u := getUser(ctx); u != nil {
|
|
return u.TenantId
|
|
}
|
|
return 0
|
|
}
|
|
|
|
// getUser 从 context 中获取 *beans.User
|
|
func getUser(ctx context.Context) *beans.User {
|
|
if user, ok := ctx.Value("user").(*beans.User); ok {
|
|
return user
|
|
}
|
|
return nil
|
|
}
|