修复漏洞,重构初始化功能,优化媒体库 (#1024)

* 媒体库增加 普通上传、压缩上传按钮,方便媒体库直接上传图片

* 增加数据类型切换后的的校验,避免使用错误的查询条件和字典条件。

* refactor: 重构初始化逻辑

* 媒体库功能丰富

* 修复注入漏洞和路径穿越

* 修复自动化接口获取数据库表失败后未能终止的bug

* 微调媒体库样式

Co-authored-by: bypanghu <bypanghu@163.com>
Co-authored-by: tesun <36953434+tesun@users.noreply.github.com>
Co-authored-by: pnck <hio131@gmail.com>
Co-authored-by: task <121913992@qq.com>
This commit is contained in:
奇淼(piexlmax
2022-04-12 17:57:11 +08:00
committed by GitHub
co-authored by bypanghu tesun pnck task
parent fe539baa34
commit 6fb6ac2d6c
40 changed files with 1366 additions and 792 deletions
+65 -80
View File
@@ -1,97 +1,82 @@
package system
import (
"reflect"
"github.com/flipped-aurora/gin-vue-admin/server/global"
"github.com/flipped-aurora/gin-vue-admin/server/model/system"
"context"
sysModel "github.com/flipped-aurora/gin-vue-admin/server/model/system"
"github.com/flipped-aurora/gin-vue-admin/server/service/system"
"github.com/pkg/errors"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)
var AuthoritiesMenus = new(authoritiesMenus)
const initOrderMenuAuthority = initOrderMenu + initOrderAuthority
type authoritiesMenus struct{}
type initMenuAuthority struct{}
func (a *authoritiesMenus) TableName() string {
var entity AuthorityMenus
return entity.TableName()
// auto run
func init() {
system.RegisterInit(initOrderMenuAuthority, &initMenuAuthority{})
}
func (a *authoritiesMenus) Initialize() error {
entities := []AuthorityMenus{
{BaseMenuId: 1, AuthorityId: "888"},
{BaseMenuId: 2, AuthorityId: "888"},
{BaseMenuId: 3, AuthorityId: "888"},
{BaseMenuId: 4, AuthorityId: "888"},
{BaseMenuId: 5, AuthorityId: "888"},
{BaseMenuId: 6, AuthorityId: "888"},
{BaseMenuId: 7, AuthorityId: "888"},
{BaseMenuId: 8, AuthorityId: "888"},
{BaseMenuId: 9, AuthorityId: "888"},
{BaseMenuId: 10, AuthorityId: "888"},
{BaseMenuId: 11, AuthorityId: "888"},
{BaseMenuId: 12, AuthorityId: "888"},
{BaseMenuId: 13, AuthorityId: "888"},
{BaseMenuId: 14, AuthorityId: "888"},
{BaseMenuId: 15, AuthorityId: "888"},
{BaseMenuId: 16, AuthorityId: "888"},
{BaseMenuId: 17, AuthorityId: "888"},
{BaseMenuId: 18, AuthorityId: "888"},
{BaseMenuId: 19, AuthorityId: "888"},
{BaseMenuId: 20, AuthorityId: "888"},
{BaseMenuId: 22, AuthorityId: "888"},
{BaseMenuId: 23, AuthorityId: "888"},
{BaseMenuId: 24, AuthorityId: "888"},
{BaseMenuId: 25, AuthorityId: "888"},
{BaseMenuId: 26, AuthorityId: "888"},
{BaseMenuId: 1, AuthorityId: "8881"},
{BaseMenuId: 2, AuthorityId: "8881"},
{BaseMenuId: 8, AuthorityId: "8881"},
{BaseMenuId: 1, AuthorityId: "9528"},
{BaseMenuId: 2, AuthorityId: "9528"},
{BaseMenuId: 3, AuthorityId: "9528"},
{BaseMenuId: 4, AuthorityId: "9528"},
{BaseMenuId: 5, AuthorityId: "9528"},
{BaseMenuId: 6, AuthorityId: "9528"},
{BaseMenuId: 7, AuthorityId: "9528"},
{BaseMenuId: 8, AuthorityId: "9528"},
{BaseMenuId: 9, AuthorityId: "9528"},
{BaseMenuId: 10, AuthorityId: "9528"},
{BaseMenuId: 11, AuthorityId: "9528"},
{BaseMenuId: 12, AuthorityId: "9528"},
{BaseMenuId: 14, AuthorityId: "9528"},
{BaseMenuId: 15, AuthorityId: "9528"},
{BaseMenuId: 16, AuthorityId: "9528"},
{BaseMenuId: 17, AuthorityId: "9528"},
}
if err := global.GVA_DB.Create(&entities).Error; err != nil {
return errors.Wrap(err, a.TableName()+"表数据初始化失败!")
}
return nil
func (i *initMenuAuthority) MigrateTable(ctx context.Context) (context.Context, error) {
return ctx, nil // do nothing
}
func (a *authoritiesMenus) CheckDataExist() bool {
if errors.Is(global.GVA_DB.Where("sys_base_menu_id = ? AND sys_authority_authority_id = ?", 17, "9528").First(&AuthorityMenus{}).Error, gorm.ErrRecordNotFound) { // 判断是否存在数据
func (i *initMenuAuthority) TableCreated(ctx context.Context) bool {
return false // always replace
}
func (i initMenuAuthority) InitializerName() string {
return "sys_menu_authorities"
}
func (i *initMenuAuthority) InitializeData(ctx context.Context) (next context.Context, err error) {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return ctx, system.ErrMissingDBContext
}
authorities, ok := ctx.Value(initAuthority{}.InitializerName()).([]sysModel.SysAuthority)
if !ok {
return ctx, errors.Wrap(system.ErrMissingDependentContext, "创建 [菜单-权限] 关联失败, 未找到权限表初始化数据")
}
menus, ok := ctx.Value(initMenu{}.InitializerName()).([]sysModel.SysBaseMenu)
if !ok {
return next, errors.Wrap(errors.New(""), "创建 [菜单-权限] 关联失败, 未找到菜单表初始化数据")
}
next = ctx
// 888
if err = db.Model(&authorities[0]).Association("SysBaseMenus").Replace(menus[:20]); err != nil {
return next, err
}
if err = db.Model(&authorities[0]).Association("SysBaseMenus").Append(menus[21:]); err != nil {
return next, err
}
// 8881
menu8881 := menus[:2]
menu8881 = append(menu8881, menus[7])
if err = db.Model(&authorities[1]).Association("SysBaseMenus").Replace(menu8881); err != nil {
return next, err
}
// 9528
if err = db.Model(&authorities[2]).Association("SysBaseMenus").Replace(menus[:12]); err != nil {
return next, err
}
if err = db.Model(&authorities[2]).Association("SysBaseMenus").Append(menus[13:17]); err != nil {
return next, err
}
return next, nil
}
func (i *initMenuAuthority) DataInserted(ctx context.Context) bool {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return false
}
return true
}
type AuthorityMenus struct {
AuthorityId string `gorm:"column:sys_authority_authority_id"`
BaseMenuId uint `gorm:"column:sys_base_menu_id"`
}
func (a *AuthorityMenus) TableName() string {
var entity system.SysAuthority
types := reflect.TypeOf(entity)
if s, o := types.FieldByName("SysBaseMenus"); o {
m1 := schema.ParseTagSetting(s.Tag.Get("gorm"), ";")
return m1["MANY2MANY"]
var count int64
if err := db.Model(&sysModel.SysAuthority{}).
Where("authority_id = ?", "9528").Preload("SysBaseMenus").Count(&count); err != nil {
return count == 16
}
return ""
return false
}