chore: 并入 iotlcm-admin-svr 历史到 admin-svr/(原提交 49818c6)
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
package rolesync
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/google/uuid"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/consts"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/dao"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/rabbitmq"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/rolepolicy"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/model"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/model/entity"
|
||||
)
|
||||
|
||||
type deferRecordingContextKey struct{}
|
||||
|
||||
func DeferRecording(ctx context.Context) context.Context {
|
||||
return context.WithValue(ctx, deferRecordingContextKey{}, true)
|
||||
}
|
||||
|
||||
func EnqueueRole(ctx context.Context, roleID, actorID int64) error {
|
||||
if deferred, _ := ctx.Value(deferRecordingContextKey{}).(bool); deferred {
|
||||
return nil
|
||||
}
|
||||
if gdb.TXFromCtx(ctx, g.DB().GetGroup()) != nil {
|
||||
return gerror.New("权限下发必须在数据库事务提交后入队")
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 15*time.Second)
|
||||
defer cancel()
|
||||
var role *entity.SysRole
|
||||
if err := dao.SysRole.Ctx(ctx).Master().Where("role_id", roleID).Scan(&role); err != nil {
|
||||
return err
|
||||
}
|
||||
if role == nil {
|
||||
return nil
|
||||
}
|
||||
target, err := rolepolicy.TemplateForRoles(ctx, roleID)
|
||||
if err != nil {
|
||||
return gerror.Wrap(err, "角色已保存,但无法识别代理模板,请检查系统参数后重新保存")
|
||||
}
|
||||
if target == nil {
|
||||
if role.IsPass != 1 {
|
||||
return nil
|
||||
}
|
||||
configured, err := dao.SysConfig.Ctx(ctx).Master().Where("tenant_id", consts.DefaultSystemTenantCode).Where("config_key", rolepolicy.TemplateConfigKey).Count()
|
||||
if err != nil {
|
||||
return gerror.Wrap(err, "角色已保存,但无法识别代理模板,请稍后重新保存")
|
||||
}
|
||||
if configured > 0 {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
template, err := rolepolicy.Template(ctx)
|
||||
if err != nil {
|
||||
return gerror.Wrap(err, "角色已保存,但代理模板配置异常,无法下发;请修复模板后重新保存")
|
||||
}
|
||||
message := &model.AgentRoleSyncMessage{
|
||||
TaskId: uuid.NewString(), TenantId: role.TenantId, SourceRoleId: role.RoleId,
|
||||
SourceDeptId: role.DeptId, ActorId: actorID,
|
||||
TemplateRole: template != nil && template.RoleId == role.RoleId,
|
||||
}
|
||||
if err := publishRoleSync(ctx, message); err != nil {
|
||||
return gerror.Wrapf(err, "角色已保存,但下级权限下发任务入队失败,请稍后重新保存(任务ID:%s)", message.TaskId)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var publishRoleSync = func(ctx context.Context, message *model.AgentRoleSyncMessage) error {
|
||||
return rabbitmq.PublishConfirmed(ctx, consts.QueueAgentRoleSync, message)
|
||||
}
|
||||
Reference in New Issue
Block a user