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)
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
package rolesync
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
_ "github.com/gogf/gf/contrib/drivers/sqlite/v2"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/model"
|
||||
)
|
||||
|
||||
func TestEnqueueRoleOnlyAfterCommit(t *testing.T) {
|
||||
if err := gdb.SetConfig(gdb.Config{"default": {{Type: "sqlite", Link: "sqlite::@file(:memory:)"}}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx := context.Background()
|
||||
t.Cleanup(func() { _ = g.DB().Close(ctx) })
|
||||
for _, statement := range []string{
|
||||
"CREATE TABLE sys_role (role_id INTEGER PRIMARY KEY, tenant_id TEXT, dept_id INTEGER, is_pass INTEGER, status TEXT DEFAULT '0', data_scope TEXT DEFAULT '4', created_dept INTEGER DEFAULT 100, deleted_at DATETIME)",
|
||||
"CREATE TABLE sys_config (tenant_id TEXT, config_key TEXT, config_value TEXT)",
|
||||
"CREATE TABLE sys_dept (dept_id INTEGER PRIMARY KEY, tenant_id TEXT, status TEXT)",
|
||||
"CREATE TABLE sys_role_menu (tenant_id TEXT, role_id INTEGER, menu_id INTEGER, data_scope TEXT)",
|
||||
"INSERT INTO sys_dept VALUES(100,'000000','0')",
|
||||
"INSERT INTO sys_role(role_id,tenant_id,dept_id,is_pass) VALUES (18,'000000',100,1),(19,'000000',100,0)",
|
||||
} {
|
||||
if _, err := g.DB().Exec(ctx, statement); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
original := publishRoleSync
|
||||
t.Cleanup(func() { publishRoleSync = original })
|
||||
var messages []*model.AgentRoleSyncMessage
|
||||
publishRoleSync = func(_ context.Context, message *model.AgentRoleSyncMessage) error {
|
||||
messages = append(messages, message)
|
||||
return nil
|
||||
}
|
||||
if err := g.DB().Transaction(ctx, func(txCtx context.Context, _ gdb.TX) error {
|
||||
return EnqueueRole(txCtx, 18, 7)
|
||||
}); err == nil {
|
||||
t.Fatal("must reject publishing before commit")
|
||||
}
|
||||
if len(messages) != 0 {
|
||||
t.Fatal("published inside transaction")
|
||||
}
|
||||
if err := EnqueueRole(ctx, 18, 7); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(messages) != 1 || messages[0].TaskId == "" || messages[0].SourceRoleId != 18 {
|
||||
t.Fatalf("unexpected message: %+v", messages)
|
||||
}
|
||||
if err := EnqueueRole(DeferRecording(ctx), 18, 7); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := EnqueueRole(ctx, 19, 7); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(messages) != 1 {
|
||||
t.Fatal("non-inheritable or deferred role published")
|
||||
}
|
||||
publishRoleSync = func(context.Context, *model.AgentRoleSyncMessage) error { return errors.New("broker offline") }
|
||||
if err := EnqueueRole(ctx, 18, 7); err == nil || !strings.Contains(err.Error(), "角色已保存") {
|
||||
t.Fatalf("must report committed save and failed publication: %v", err)
|
||||
}
|
||||
publishRoleSync = func(_ context.Context, message *model.AgentRoleSyncMessage) error {
|
||||
messages = append(messages, message)
|
||||
return nil
|
||||
}
|
||||
if _, err := g.DB().Exec(ctx, "INSERT INTO sys_config VALUES('000000','agent.role.template.role_id','19')"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := EnqueueRole(ctx, 18, 7); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(messages) != 1 {
|
||||
t.Fatal("legacy local role must not overwrite configured hierarchy")
|
||||
}
|
||||
if err := EnqueueRole(ctx, 19, 7); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(messages) != 2 || !messages[1].TemplateRole || messages[1].SourceRoleId != 19 {
|
||||
t.Fatalf("configured template with is_pass=0 was not queued: %+v", messages)
|
||||
}
|
||||
t.Run("提交后请求取消不丢失下发且保留有限超时", func(t *testing.T) {
|
||||
type requestKey struct{}
|
||||
requestCtx, cancel := context.WithCancel(context.WithValue(ctx, requestKey{}, "actor"))
|
||||
cancel()
|
||||
published := false
|
||||
publishRoleSync = func(publishCtx context.Context, message *model.AgentRoleSyncMessage) error {
|
||||
if err := publishCtx.Err(); err != nil {
|
||||
t.Fatalf("committed publication inherited cancellation: %v", err)
|
||||
}
|
||||
deadline, ok := publishCtx.Deadline()
|
||||
if !ok || time.Until(deadline) <= 0 || time.Until(deadline) > 15*time.Second {
|
||||
t.Fatalf("publication must have bounded timeout: %v %v", deadline, ok)
|
||||
}
|
||||
if publishCtx.Value(requestKey{}) != "actor" || message.ActorId != 7 {
|
||||
t.Fatal("request values or actor lost")
|
||||
}
|
||||
published = true
|
||||
return nil
|
||||
}
|
||||
if err := EnqueueRole(requestCtx, 19, 7); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !published {
|
||||
t.Fatal("committed role was not published")
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user