123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- package system
- import (
- "context"
- adapter "github.com/casbin/gorm-adapter/v3"
- "github.com/pkg/errors"
- "gorm.io/gorm"
- "server/service/system"
- )
- const initOrderCasbin = initOrderApi + 1
- type initCasbin struct{}
- // auto run
- func init() {
- system.RegisterInit(initOrderCasbin, &initCasbin{})
- }
- func (i *initCasbin) MigrateTable(ctx context.Context) (context.Context, error) {
- db, ok := ctx.Value("db").(*gorm.DB)
- if !ok {
- return ctx, system.ErrMissingDBContext
- }
- return ctx, db.AutoMigrate(&adapter.CasbinRule{})
- }
- func (i *initCasbin) TableCreated(ctx context.Context) bool {
- db, ok := ctx.Value("db").(*gorm.DB)
- if !ok {
- return false
- }
- return db.Migrator().HasTable(&adapter.CasbinRule{})
- }
- func (i initCasbin) InitializerName() string {
- var entity adapter.CasbinRule
- return entity.TableName()
- }
- func (i *initCasbin) InitializeData(ctx context.Context) (context.Context, error) {
- db, ok := ctx.Value("db").(*gorm.DB)
- if !ok {
- return ctx, system.ErrMissingDBContext
- }
- entities := []adapter.CasbinRule{
- {Ptype: "p", V0: "888", V1: "/user/admin_register", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/api/createApi", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/api/getApiList", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/api/getApiById", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/api/deleteApi", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/api/updateApi", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/api/getAllApis", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/api/deleteApisByIds", V2: "DELETE"},
- {Ptype: "p", V0: "888", V1: "/authority/copyAuthority", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/authority/updateAuthority", V2: "PUT"},
- {Ptype: "p", V0: "888", V1: "/authority/createAuthority", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/authority/deleteAuthority", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/authority/getAuthorityList", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/authority/setDataAuthority", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/menu/getMenu", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/menu/getMenuList", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/menu/addBaseMenu", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/menu/getBaseMenuTree", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/menu/addMenuAuthority", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/menu/getMenuAuthority", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/menu/deleteBaseMenu", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/menu/updateBaseMenu", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/menu/getBaseMenuById", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/user/getUserInfo", V2: "GET"},
- {Ptype: "p", V0: "888", V1: "/user/setUserInfo", V2: "PUT"},
- {Ptype: "p", V0: "888", V1: "/user/setSelfInfo", V2: "PUT"},
- {Ptype: "p", V0: "888", V1: "/user/getUserList", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/user/deleteUser", V2: "DELETE"},
- {Ptype: "p", V0: "888", V1: "/user/changePassword", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/user/setUserAuthority", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/user/setUserAuthorities", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/user/resetPassword", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/deptUploadAndDownload/findFile", V2: "GET"},
- {Ptype: "p", V0: "888", V1: "/deptUploadAndDownload/breakpointContinueFinish", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/deptUploadAndDownload/breakpointContinue", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/deptUploadAndDownload/removeChunk", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/deptUploadAndDownload/upload", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/deptUploadAndDownload/deleteFile", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/deptUploadAndDownload/editFileName", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/deptUploadAndDownload/getFileList", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/casbin/updateCasbin", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/casbin/getPolicyPathByAuthorityId", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/jwt/jsonInBlacklist", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/system/getSystemConfig", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/system/setSystemConfig", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/system/getServerInfo", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/customer/customer", V2: "GET"},
- {Ptype: "p", V0: "888", V1: "/customer/customer", V2: "PUT"},
- {Ptype: "p", V0: "888", V1: "/customer/customer", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/customer/customer", V2: "DELETE"},
- {Ptype: "p", V0: "888", V1: "/customer/customerList", V2: "GET"},
- {Ptype: "p", V0: "888", V1: "/autoCode/getDB", V2: "GET"},
- {Ptype: "p", V0: "888", V1: "/autoCode/getMeta", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/autoCode/preview", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/autoCode/getTables", V2: "GET"},
- {Ptype: "p", V0: "888", V1: "/autoCode/getColumn", V2: "GET"},
- {Ptype: "p", V0: "888", V1: "/autoCode/rollback", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/autoCode/createTemp", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/autoCode/delSysHistory", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/autoCode/getSysHistory", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/autoCode/createPackage", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/autoCode/getPackage", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/autoCode/delPackage", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/autoCode/createPlug", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/autoCode/installPlugin", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/autoCode/pubPlug", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/sysDictionaryDetail/findSysDictionaryDetail", V2: "GET"},
- {Ptype: "p", V0: "888", V1: "/sysDictionaryDetail/updateSysDictionaryDetail", V2: "PUT"},
- {Ptype: "p", V0: "888", V1: "/sysDictionaryDetail/createSysDictionaryDetail", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/sysDictionaryDetail/getSysDictionaryDetailList", V2: "GET"},
- {Ptype: "p", V0: "888", V1: "/sysDictionaryDetail/deleteSysDictionaryDetail", V2: "DELETE"},
- {Ptype: "p", V0: "888", V1: "/sysDictionary/findSysDictionary", V2: "GET"},
- {Ptype: "p", V0: "888", V1: "/sysDictionary/updateSysDictionary", V2: "PUT"},
- {Ptype: "p", V0: "888", V1: "/sysDictionary/getSysDictionaryList", V2: "GET"},
- {Ptype: "p", V0: "888", V1: "/sysDictionary/createSysDictionary", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/sysDictionary/deleteSysDictionary", V2: "DELETE"},
- {Ptype: "p", V0: "888", V1: "/sysOperationRecord/findSysOperationRecord", V2: "GET"},
- {Ptype: "p", V0: "888", V1: "/sysOperationRecord/updateSysOperationRecord", V2: "PUT"},
- {Ptype: "p", V0: "888", V1: "/sysOperationRecord/createSysOperationRecord", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/sysOperationRecord/getSysOperationRecordList", V2: "GET"},
- {Ptype: "p", V0: "888", V1: "/sysOperationRecord/deleteSysOperationRecord", V2: "DELETE"},
- {Ptype: "p", V0: "888", V1: "/sysOperationRecord/deleteSysOperationRecordByIds", V2: "DELETE"},
- {Ptype: "p", V0: "888", V1: "/email/emailTest", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/simpleUploader/upload", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/simpleUploader/checkFileMd5", V2: "GET"},
- {Ptype: "p", V0: "888", V1: "/simpleUploader/mergeFileMd5", V2: "GET"},
- {Ptype: "p", V0: "888", V1: "/authorityBtn/setAuthorityBtn", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/authorityBtn/getAuthorityBtn", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/authorityBtn/canRemoveAuthorityBtn", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/sysExportTemplate/createSysExportTemplate", V2: "POST"},
- {Ptype: "p", V0: "888", V1: "/sysExportTemplate/deleteSysExportTemplate", V2: "DELETE"},
- {Ptype: "p", V0: "888", V1: "/sysExportTemplate/deleteSysExportTemplateByIds", V2: "DELETE"},
- {Ptype: "p", V0: "888", V1: "/sysExportTemplate/updateSysExportTemplate", V2: "PUT"},
- {Ptype: "p", V0: "888", V1: "/sysExportTemplate/findSysExportTemplate", V2: "GET"},
- {Ptype: "p", V0: "888", V1: "/sysExportTemplate/getSysExportTemplateList", V2: "GET"},
- {Ptype: "p", V0: "888", V1: "/sysExportTemplate/exportExcel", V2: "GET"},
- {Ptype: "p", V0: "888", V1: "/sysExportTemplate/exportTemplate", V2: "GET"},
- {Ptype: "p", V0: "888", V1: "/sysExportTemplate/importExcel", V2: "POST"},
- {Ptype: "p", V0: "8881", V1: "/user/admin_register", V2: "POST"},
- {Ptype: "p", V0: "8881", V1: "/api/createApi", V2: "POST"},
- {Ptype: "p", V0: "8881", V1: "/api/getApiList", V2: "POST"},
- {Ptype: "p", V0: "8881", V1: "/api/getApiById", V2: "POST"},
- {Ptype: "p", V0: "8881", V1: "/api/deleteApi", V2: "POST"},
- {Ptype: "p", V0: "8881", V1: "/api/updateApi", V2: "POST"},
- {Ptype: "p", V0: "8881", V1: "/api/getAllApis", V2: "POST"},
- {Ptype: "p", V0: "8881", V1: "/authority/createAuthority", V2: "POST"},
- {Ptype: "p", V0: "8881", V1: "/authority/deleteAuthority", V2: "POST"},
- {Ptype: "p", V0: "8881", V1: "/authority/getAuthorityList", V2: "POST"},
- {Ptype: "p", V0: "8881", V1: "/authority/setDataAuthority", V2: "POST"},
- {Ptype: "p", V0: "8881", V1: "/menu/getMenu", V2: "POST"},
- {Ptype: "p", V0: "8881", V1: "/menu/getMenuList", V2: "POST"},
- {Ptype: "p", V0: "8881", V1: "/menu/addBaseMenu", V2: "POST"},
- {Ptype: "p", V0: "8881", V1: "/menu/getBaseMenuTree", V2: "POST"},
- {Ptype: "p", V0: "8881", V1: "/menu/addMenuAuthority", V2: "POST"},
- {Ptype: "p", V0: "8881", V1: "/menu/getMenuAuthority", V2: "POST"},
- {Ptype: "p", V0: "8881", V1: "/menu/deleteBaseMenu", V2: "POST"},
- {Ptype: "p", V0: "8881", V1: "/menu/updateBaseMenu", V2: "POST"},
- {Ptype: "p", V0: "8881", V1: "/menu/getBaseMenuById", V2: "POST"},
- {Ptype: "p", V0: "8881", V1: "/user/changePassword", V2: "POST"},
- {Ptype: "p", V0: "8881", V1: "/user/getUserList", V2: "POST"},
- {Ptype: "p", V0: "8881", V1: "/user/setUserAuthority", V2: "POST"},
- {Ptype: "p", V0: "8881", V1: "/deptUploadAndDownload/upload", V2: "POST"},
- {Ptype: "p", V0: "8881", V1: "/deptUploadAndDownload/getFileList", V2: "POST"},
- {Ptype: "p", V0: "8881", V1: "/deptUploadAndDownload/deleteFile", V2: "POST"},
- {Ptype: "p", V0: "8881", V1: "/deptUploadAndDownload/editFileName", V2: "POST"},
- {Ptype: "p", V0: "8881", V1: "/casbin/updateCasbin", V2: "POST"},
- {Ptype: "p", V0: "8881", V1: "/casbin/getPolicyPathByAuthorityId", V2: "POST"},
- {Ptype: "p", V0: "8881", V1: "/jwt/jsonInBlacklist", V2: "POST"},
- {Ptype: "p", V0: "8881", V1: "/system/getSystemConfig", V2: "POST"},
- {Ptype: "p", V0: "8881", V1: "/system/setSystemConfig", V2: "POST"},
- {Ptype: "p", V0: "8881", V1: "/customer/customer", V2: "POST"},
- {Ptype: "p", V0: "8881", V1: "/customer/customer", V2: "PUT"},
- {Ptype: "p", V0: "8881", V1: "/customer/customer", V2: "DELETE"},
- {Ptype: "p", V0: "8881", V1: "/customer/customer", V2: "GET"},
- {Ptype: "p", V0: "8881", V1: "/customer/customerList", V2: "GET"},
- {Ptype: "p", V0: "8881", V1: "/user/getUserInfo", V2: "GET"},
- {Ptype: "p", V0: "9528", V1: "/user/admin_register", V2: "POST"},
- {Ptype: "p", V0: "9528", V1: "/api/createApi", V2: "POST"},
- {Ptype: "p", V0: "9528", V1: "/api/getApiList", V2: "POST"},
- {Ptype: "p", V0: "9528", V1: "/api/getApiById", V2: "POST"},
- {Ptype: "p", V0: "9528", V1: "/api/deleteApi", V2: "POST"},
- {Ptype: "p", V0: "9528", V1: "/api/updateApi", V2: "POST"},
- {Ptype: "p", V0: "9528", V1: "/api/getAllApis", V2: "POST"},
- {Ptype: "p", V0: "9528", V1: "/authority/createAuthority", V2: "POST"},
- {Ptype: "p", V0: "9528", V1: "/authority/deleteAuthority", V2: "POST"},
- {Ptype: "p", V0: "9528", V1: "/authority/getAuthorityList", V2: "POST"},
- {Ptype: "p", V0: "9528", V1: "/authority/setDataAuthority", V2: "POST"},
- {Ptype: "p", V0: "9528", V1: "/menu/getMenu", V2: "POST"},
- {Ptype: "p", V0: "9528", V1: "/menu/getMenuList", V2: "POST"},
- {Ptype: "p", V0: "9528", V1: "/menu/addBaseMenu", V2: "POST"},
- {Ptype: "p", V0: "9528", V1: "/menu/getBaseMenuTree", V2: "POST"},
- {Ptype: "p", V0: "9528", V1: "/menu/addMenuAuthority", V2: "POST"},
- {Ptype: "p", V0: "9528", V1: "/menu/getMenuAuthority", V2: "POST"},
- {Ptype: "p", V0: "9528", V1: "/menu/deleteBaseMenu", V2: "POST"},
- {Ptype: "p", V0: "9528", V1: "/menu/updateBaseMenu", V2: "POST"},
- {Ptype: "p", V0: "9528", V1: "/menu/getBaseMenuById", V2: "POST"},
- {Ptype: "p", V0: "9528", V1: "/user/changePassword", V2: "POST"},
- {Ptype: "p", V0: "9528", V1: "/user/getUserList", V2: "POST"},
- {Ptype: "p", V0: "9528", V1: "/user/setUserAuthority", V2: "POST"},
- {Ptype: "p", V0: "9528", V1: "/deptUploadAndDownload/upload", V2: "POST"},
- {Ptype: "p", V0: "9528", V1: "/deptUploadAndDownload/getFileList", V2: "POST"},
- {Ptype: "p", V0: "9528", V1: "/deptUploadAndDownload/deleteFile", V2: "POST"},
- {Ptype: "p", V0: "9528", V1: "/deptUploadAndDownload/editFileName", V2: "POST"},
- {Ptype: "p", V0: "9528", V1: "/casbin/updateCasbin", V2: "POST"},
- {Ptype: "p", V0: "9528", V1: "/casbin/getPolicyPathByAuthorityId", V2: "POST"},
- {Ptype: "p", V0: "9528", V1: "/jwt/jsonInBlacklist", V2: "POST"},
- {Ptype: "p", V0: "9528", V1: "/system/getSystemConfig", V2: "POST"},
- {Ptype: "p", V0: "9528", V1: "/system/setSystemConfig", V2: "POST"},
- {Ptype: "p", V0: "9528", V1: "/customer/customer", V2: "PUT"},
- {Ptype: "p", V0: "9528", V1: "/customer/customer", V2: "GET"},
- {Ptype: "p", V0: "9528", V1: "/customer/customer", V2: "POST"},
- {Ptype: "p", V0: "9528", V1: "/customer/customer", V2: "DELETE"},
- {Ptype: "p", V0: "9528", V1: "/customer/customerList", V2: "GET"},
- {Ptype: "p", V0: "9528", V1: "/autoCode/createTemp", V2: "POST"},
- {Ptype: "p", V0: "9528", V1: "/user/getUserInfo", V2: "GET"},
- }
- if err := db.Create(&entities).Error; err != nil {
- return ctx, errors.Wrap(err, "Casbin 表 ("+i.InitializerName()+") 数据初始化失败!")
- }
- next := context.WithValue(ctx, i.InitializerName(), entities)
- return next, nil
- }
- func (i *initCasbin) DataInserted(ctx context.Context) bool {
- db, ok := ctx.Value("db").(*gorm.DB)
- if !ok {
- return false
- }
- if errors.Is(db.Where(adapter.CasbinRule{Ptype: "p", V0: "9528", V1: "/user/getUserInfo", V2: "GET"}).
- First(&adapter.CasbinRule{}).Error, gorm.ErrRecordNotFound) { // 判断是否存在数据
- return false
- }
- return true
- }
|