96 lines
2.7 KiB
Go
96 lines
2.7 KiB
Go
package sys
|
|
|
|
import (
|
|
"context"
|
|
"hotgo/internal/consts"
|
|
"hotgo/internal/dao"
|
|
"hotgo/internal/library/contexts"
|
|
"hotgo/internal/library/imadmin"
|
|
"hotgo/internal/library/imtenant"
|
|
"hotgo/internal/model/entity"
|
|
"hotgo/internal/service"
|
|
"hotgo/utility/simple"
|
|
|
|
"github.com/gogf/gf/v2/encoding/gjson"
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"github.com/gogf/gf/v2/net/ghttp"
|
|
"github.com/gogf/gf/v2/os/gctx"
|
|
"github.com/gogf/gf/v2/os/gtime"
|
|
)
|
|
|
|
const (
|
|
imAdminOperationAuditTable = "im_admin_operation_audit"
|
|
)
|
|
|
|
func resolveImAdminScope(ctx context.Context, selectedTenantId int64) (scope imadmin.AdminScope, err error) {
|
|
operatorId := contexts.GetUserId(ctx)
|
|
scope.OperatorId = operatorId
|
|
scope.RoleKey = contexts.GetRoleKey(ctx)
|
|
scope.RequestId = gctx.CtxId(ctx)
|
|
if service.AdminMember().VerifySuperId(ctx, operatorId) {
|
|
if selectedTenantId <= 0 {
|
|
return scope, gerror.New("请选择IM租户")
|
|
}
|
|
if err = ensureImTenantActive(ctx, selectedTenantId); err != nil {
|
|
return scope, err
|
|
}
|
|
scope.TenantId = selectedTenantId
|
|
return scope, nil
|
|
}
|
|
|
|
if contexts.GetDeptType(ctx) != consts.DeptTypeTenant {
|
|
return scope, gerror.New("当前账号不是租户管理员,不能管理邀请码")
|
|
}
|
|
tenantId := contexts.GetTenantId(ctx)
|
|
if tenantId <= 0 {
|
|
return scope, gerror.New("当前管理员未关联租户")
|
|
}
|
|
if err = ensureImTenantActive(ctx, tenantId); err != nil {
|
|
return scope, err
|
|
}
|
|
scope.TenantId = tenantId
|
|
return scope, nil
|
|
}
|
|
|
|
func ensureImTenantActive(ctx context.Context, tenantId int64) (err error) {
|
|
var tenant *entity.ImTenant
|
|
if err = imtenant.RegistryDB(ctx).Model(dao.ImTenant.Table()).Safe().Ctx(ctx).WherePri(tenantId).Scan(&tenant); err != nil {
|
|
return gerror.Wrap(err, "获取IM租户失败")
|
|
}
|
|
if tenant == nil || tenant.Id <= 0 {
|
|
return gerror.New("IM租户不存在")
|
|
}
|
|
if tenant.State != consts.ImTenantStateActive {
|
|
return gerror.New("IM租户未启用")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func writeImAdminAudit(ctx context.Context, tenantId int64, action string, targetType string, targetId int64, result string, detail g.Map) error {
|
|
operatorId := contexts.GetUserId(ctx)
|
|
request := ghttp.RequestFromCtx(ctx)
|
|
ip := ""
|
|
if request != nil {
|
|
ip = request.GetClientIp()
|
|
}
|
|
detailJson := gjson.New(detail)
|
|
_, err := g.DB().Exec(ctx,
|
|
"INSERT INTO "+imAdminOperationAuditTable+" (request_id, tenant_id, operator_id, action, target_type, target_id, result, ip, detail_json, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, CAST(? AS JSON), ?)",
|
|
gctx.CtxId(ctx),
|
|
tenantId,
|
|
operatorId,
|
|
action,
|
|
targetType,
|
|
targetId,
|
|
result,
|
|
ip,
|
|
detailJson.String(),
|
|
gtime.Now(),
|
|
)
|
|
if err != nil && !simple.IsDemo(ctx) {
|
|
g.Log().Warningf(ctx, "写入IM后台审计失败 action:%s err:%+v", action, err)
|
|
}
|
|
return err
|
|
}
|