casbin.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. package system
  2. import (
  3. "context"
  4. adapter "github.com/casbin/gorm-adapter/v3"
  5. "github.com/pkg/errors"
  6. "gorm.io/gorm"
  7. "server/service/system"
  8. )
  9. const initOrderCasbin = initOrderApi + 1
  10. type initCasbin struct{}
  11. // auto run
  12. func init() {
  13. system.RegisterInit(initOrderCasbin, &initCasbin{})
  14. }
  15. func (i *initCasbin) MigrateTable(ctx context.Context) (context.Context, error) {
  16. db, ok := ctx.Value("db").(*gorm.DB)
  17. if !ok {
  18. return ctx, system.ErrMissingDBContext
  19. }
  20. return ctx, db.AutoMigrate(&adapter.CasbinRule{})
  21. }
  22. func (i *initCasbin) TableCreated(ctx context.Context) bool {
  23. db, ok := ctx.Value("db").(*gorm.DB)
  24. if !ok {
  25. return false
  26. }
  27. return db.Migrator().HasTable(&adapter.CasbinRule{})
  28. }
  29. func (i initCasbin) InitializerName() string {
  30. var entity adapter.CasbinRule
  31. return entity.TableName()
  32. }
  33. func (i *initCasbin) InitializeData(ctx context.Context) (context.Context, error) {
  34. db, ok := ctx.Value("db").(*gorm.DB)
  35. if !ok {
  36. return ctx, system.ErrMissingDBContext
  37. }
  38. entities := []adapter.CasbinRule{
  39. {Ptype: "p", V0: "888", V1: "/user/admin_register", V2: "POST"},
  40. {Ptype: "p", V0: "888", V1: "/api/createApi", V2: "POST"},
  41. {Ptype: "p", V0: "888", V1: "/api/getApiList", V2: "POST"},
  42. {Ptype: "p", V0: "888", V1: "/api/getApiById", V2: "POST"},
  43. {Ptype: "p", V0: "888", V1: "/api/deleteApi", V2: "POST"},
  44. {Ptype: "p", V0: "888", V1: "/api/updateApi", V2: "POST"},
  45. {Ptype: "p", V0: "888", V1: "/api/getAllApis", V2: "POST"},
  46. {Ptype: "p", V0: "888", V1: "/api/deleteApisByIds", V2: "DELETE"},
  47. {Ptype: "p", V0: "888", V1: "/authority/copyAuthority", V2: "POST"},
  48. {Ptype: "p", V0: "888", V1: "/authority/updateAuthority", V2: "PUT"},
  49. {Ptype: "p", V0: "888", V1: "/authority/createAuthority", V2: "POST"},
  50. {Ptype: "p", V0: "888", V1: "/authority/deleteAuthority", V2: "POST"},
  51. {Ptype: "p", V0: "888", V1: "/authority/getAuthorityList", V2: "POST"},
  52. {Ptype: "p", V0: "888", V1: "/authority/setDataAuthority", V2: "POST"},
  53. {Ptype: "p", V0: "888", V1: "/menu/getMenu", V2: "POST"},
  54. {Ptype: "p", V0: "888", V1: "/menu/getMenuList", V2: "POST"},
  55. {Ptype: "p", V0: "888", V1: "/menu/addBaseMenu", V2: "POST"},
  56. {Ptype: "p", V0: "888", V1: "/menu/getBaseMenuTree", V2: "POST"},
  57. {Ptype: "p", V0: "888", V1: "/menu/addMenuAuthority", V2: "POST"},
  58. {Ptype: "p", V0: "888", V1: "/menu/getMenuAuthority", V2: "POST"},
  59. {Ptype: "p", V0: "888", V1: "/menu/deleteBaseMenu", V2: "POST"},
  60. {Ptype: "p", V0: "888", V1: "/menu/updateBaseMenu", V2: "POST"},
  61. {Ptype: "p", V0: "888", V1: "/menu/getBaseMenuById", V2: "POST"},
  62. {Ptype: "p", V0: "888", V1: "/user/getUserInfo", V2: "GET"},
  63. {Ptype: "p", V0: "888", V1: "/user/setUserInfo", V2: "PUT"},
  64. {Ptype: "p", V0: "888", V1: "/user/setSelfInfo", V2: "PUT"},
  65. {Ptype: "p", V0: "888", V1: "/user/getUserList", V2: "POST"},
  66. {Ptype: "p", V0: "888", V1: "/user/deleteUser", V2: "DELETE"},
  67. {Ptype: "p", V0: "888", V1: "/user/changePassword", V2: "POST"},
  68. {Ptype: "p", V0: "888", V1: "/user/setUserAuthority", V2: "POST"},
  69. {Ptype: "p", V0: "888", V1: "/user/setUserAuthorities", V2: "POST"},
  70. {Ptype: "p", V0: "888", V1: "/user/resetPassword", V2: "POST"},
  71. {Ptype: "p", V0: "888", V1: "/deptUploadAndDownload/findFile", V2: "GET"},
  72. {Ptype: "p", V0: "888", V1: "/deptUploadAndDownload/breakpointContinueFinish", V2: "POST"},
  73. {Ptype: "p", V0: "888", V1: "/deptUploadAndDownload/breakpointContinue", V2: "POST"},
  74. {Ptype: "p", V0: "888", V1: "/deptUploadAndDownload/removeChunk", V2: "POST"},
  75. {Ptype: "p", V0: "888", V1: "/deptUploadAndDownload/upload", V2: "POST"},
  76. {Ptype: "p", V0: "888", V1: "/deptUploadAndDownload/deleteFile", V2: "POST"},
  77. {Ptype: "p", V0: "888", V1: "/deptUploadAndDownload/editFileName", V2: "POST"},
  78. {Ptype: "p", V0: "888", V1: "/deptUploadAndDownload/getFileList", V2: "POST"},
  79. {Ptype: "p", V0: "888", V1: "/casbin/updateCasbin", V2: "POST"},
  80. {Ptype: "p", V0: "888", V1: "/casbin/getPolicyPathByAuthorityId", V2: "POST"},
  81. {Ptype: "p", V0: "888", V1: "/jwt/jsonInBlacklist", V2: "POST"},
  82. {Ptype: "p", V0: "888", V1: "/system/getSystemConfig", V2: "POST"},
  83. {Ptype: "p", V0: "888", V1: "/system/setSystemConfig", V2: "POST"},
  84. {Ptype: "p", V0: "888", V1: "/system/getServerInfo", V2: "POST"},
  85. {Ptype: "p", V0: "888", V1: "/customer/customer", V2: "GET"},
  86. {Ptype: "p", V0: "888", V1: "/customer/customer", V2: "PUT"},
  87. {Ptype: "p", V0: "888", V1: "/customer/customer", V2: "POST"},
  88. {Ptype: "p", V0: "888", V1: "/customer/customer", V2: "DELETE"},
  89. {Ptype: "p", V0: "888", V1: "/customer/customerList", V2: "GET"},
  90. {Ptype: "p", V0: "888", V1: "/autoCode/getDB", V2: "GET"},
  91. {Ptype: "p", V0: "888", V1: "/autoCode/getMeta", V2: "POST"},
  92. {Ptype: "p", V0: "888", V1: "/autoCode/preview", V2: "POST"},
  93. {Ptype: "p", V0: "888", V1: "/autoCode/getTables", V2: "GET"},
  94. {Ptype: "p", V0: "888", V1: "/autoCode/getColumn", V2: "GET"},
  95. {Ptype: "p", V0: "888", V1: "/autoCode/rollback", V2: "POST"},
  96. {Ptype: "p", V0: "888", V1: "/autoCode/createTemp", V2: "POST"},
  97. {Ptype: "p", V0: "888", V1: "/autoCode/delSysHistory", V2: "POST"},
  98. {Ptype: "p", V0: "888", V1: "/autoCode/getSysHistory", V2: "POST"},
  99. {Ptype: "p", V0: "888", V1: "/autoCode/createPackage", V2: "POST"},
  100. {Ptype: "p", V0: "888", V1: "/autoCode/getPackage", V2: "POST"},
  101. {Ptype: "p", V0: "888", V1: "/autoCode/delPackage", V2: "POST"},
  102. {Ptype: "p", V0: "888", V1: "/autoCode/createPlug", V2: "POST"},
  103. {Ptype: "p", V0: "888", V1: "/autoCode/installPlugin", V2: "POST"},
  104. {Ptype: "p", V0: "888", V1: "/autoCode/pubPlug", V2: "POST"},
  105. {Ptype: "p", V0: "888", V1: "/sysDictionaryDetail/findSysDictionaryDetail", V2: "GET"},
  106. {Ptype: "p", V0: "888", V1: "/sysDictionaryDetail/updateSysDictionaryDetail", V2: "PUT"},
  107. {Ptype: "p", V0: "888", V1: "/sysDictionaryDetail/createSysDictionaryDetail", V2: "POST"},
  108. {Ptype: "p", V0: "888", V1: "/sysDictionaryDetail/getSysDictionaryDetailList", V2: "GET"},
  109. {Ptype: "p", V0: "888", V1: "/sysDictionaryDetail/deleteSysDictionaryDetail", V2: "DELETE"},
  110. {Ptype: "p", V0: "888", V1: "/sysDictionary/findSysDictionary", V2: "GET"},
  111. {Ptype: "p", V0: "888", V1: "/sysDictionary/updateSysDictionary", V2: "PUT"},
  112. {Ptype: "p", V0: "888", V1: "/sysDictionary/getSysDictionaryList", V2: "GET"},
  113. {Ptype: "p", V0: "888", V1: "/sysDictionary/createSysDictionary", V2: "POST"},
  114. {Ptype: "p", V0: "888", V1: "/sysDictionary/deleteSysDictionary", V2: "DELETE"},
  115. {Ptype: "p", V0: "888", V1: "/sysOperationRecord/findSysOperationRecord", V2: "GET"},
  116. {Ptype: "p", V0: "888", V1: "/sysOperationRecord/updateSysOperationRecord", V2: "PUT"},
  117. {Ptype: "p", V0: "888", V1: "/sysOperationRecord/createSysOperationRecord", V2: "POST"},
  118. {Ptype: "p", V0: "888", V1: "/sysOperationRecord/getSysOperationRecordList", V2: "GET"},
  119. {Ptype: "p", V0: "888", V1: "/sysOperationRecord/deleteSysOperationRecord", V2: "DELETE"},
  120. {Ptype: "p", V0: "888", V1: "/sysOperationRecord/deleteSysOperationRecordByIds", V2: "DELETE"},
  121. {Ptype: "p", V0: "888", V1: "/email/emailTest", V2: "POST"},
  122. {Ptype: "p", V0: "888", V1: "/simpleUploader/upload", V2: "POST"},
  123. {Ptype: "p", V0: "888", V1: "/simpleUploader/checkFileMd5", V2: "GET"},
  124. {Ptype: "p", V0: "888", V1: "/simpleUploader/mergeFileMd5", V2: "GET"},
  125. {Ptype: "p", V0: "888", V1: "/authorityBtn/setAuthorityBtn", V2: "POST"},
  126. {Ptype: "p", V0: "888", V1: "/authorityBtn/getAuthorityBtn", V2: "POST"},
  127. {Ptype: "p", V0: "888", V1: "/authorityBtn/canRemoveAuthorityBtn", V2: "POST"},
  128. {Ptype: "p", V0: "888", V1: "/sysExportTemplate/createSysExportTemplate", V2: "POST"},
  129. {Ptype: "p", V0: "888", V1: "/sysExportTemplate/deleteSysExportTemplate", V2: "DELETE"},
  130. {Ptype: "p", V0: "888", V1: "/sysExportTemplate/deleteSysExportTemplateByIds", V2: "DELETE"},
  131. {Ptype: "p", V0: "888", V1: "/sysExportTemplate/updateSysExportTemplate", V2: "PUT"},
  132. {Ptype: "p", V0: "888", V1: "/sysExportTemplate/findSysExportTemplate", V2: "GET"},
  133. {Ptype: "p", V0: "888", V1: "/sysExportTemplate/getSysExportTemplateList", V2: "GET"},
  134. {Ptype: "p", V0: "888", V1: "/sysExportTemplate/exportExcel", V2: "GET"},
  135. {Ptype: "p", V0: "888", V1: "/sysExportTemplate/exportTemplate", V2: "GET"},
  136. {Ptype: "p", V0: "888", V1: "/sysExportTemplate/importExcel", V2: "POST"},
  137. {Ptype: "p", V0: "8881", V1: "/user/admin_register", V2: "POST"},
  138. {Ptype: "p", V0: "8881", V1: "/api/createApi", V2: "POST"},
  139. {Ptype: "p", V0: "8881", V1: "/api/getApiList", V2: "POST"},
  140. {Ptype: "p", V0: "8881", V1: "/api/getApiById", V2: "POST"},
  141. {Ptype: "p", V0: "8881", V1: "/api/deleteApi", V2: "POST"},
  142. {Ptype: "p", V0: "8881", V1: "/api/updateApi", V2: "POST"},
  143. {Ptype: "p", V0: "8881", V1: "/api/getAllApis", V2: "POST"},
  144. {Ptype: "p", V0: "8881", V1: "/authority/createAuthority", V2: "POST"},
  145. {Ptype: "p", V0: "8881", V1: "/authority/deleteAuthority", V2: "POST"},
  146. {Ptype: "p", V0: "8881", V1: "/authority/getAuthorityList", V2: "POST"},
  147. {Ptype: "p", V0: "8881", V1: "/authority/setDataAuthority", V2: "POST"},
  148. {Ptype: "p", V0: "8881", V1: "/menu/getMenu", V2: "POST"},
  149. {Ptype: "p", V0: "8881", V1: "/menu/getMenuList", V2: "POST"},
  150. {Ptype: "p", V0: "8881", V1: "/menu/addBaseMenu", V2: "POST"},
  151. {Ptype: "p", V0: "8881", V1: "/menu/getBaseMenuTree", V2: "POST"},
  152. {Ptype: "p", V0: "8881", V1: "/menu/addMenuAuthority", V2: "POST"},
  153. {Ptype: "p", V0: "8881", V1: "/menu/getMenuAuthority", V2: "POST"},
  154. {Ptype: "p", V0: "8881", V1: "/menu/deleteBaseMenu", V2: "POST"},
  155. {Ptype: "p", V0: "8881", V1: "/menu/updateBaseMenu", V2: "POST"},
  156. {Ptype: "p", V0: "8881", V1: "/menu/getBaseMenuById", V2: "POST"},
  157. {Ptype: "p", V0: "8881", V1: "/user/changePassword", V2: "POST"},
  158. {Ptype: "p", V0: "8881", V1: "/user/getUserList", V2: "POST"},
  159. {Ptype: "p", V0: "8881", V1: "/user/setUserAuthority", V2: "POST"},
  160. {Ptype: "p", V0: "8881", V1: "/deptUploadAndDownload/upload", V2: "POST"},
  161. {Ptype: "p", V0: "8881", V1: "/deptUploadAndDownload/getFileList", V2: "POST"},
  162. {Ptype: "p", V0: "8881", V1: "/deptUploadAndDownload/deleteFile", V2: "POST"},
  163. {Ptype: "p", V0: "8881", V1: "/deptUploadAndDownload/editFileName", V2: "POST"},
  164. {Ptype: "p", V0: "8881", V1: "/casbin/updateCasbin", V2: "POST"},
  165. {Ptype: "p", V0: "8881", V1: "/casbin/getPolicyPathByAuthorityId", V2: "POST"},
  166. {Ptype: "p", V0: "8881", V1: "/jwt/jsonInBlacklist", V2: "POST"},
  167. {Ptype: "p", V0: "8881", V1: "/system/getSystemConfig", V2: "POST"},
  168. {Ptype: "p", V0: "8881", V1: "/system/setSystemConfig", V2: "POST"},
  169. {Ptype: "p", V0: "8881", V1: "/customer/customer", V2: "POST"},
  170. {Ptype: "p", V0: "8881", V1: "/customer/customer", V2: "PUT"},
  171. {Ptype: "p", V0: "8881", V1: "/customer/customer", V2: "DELETE"},
  172. {Ptype: "p", V0: "8881", V1: "/customer/customer", V2: "GET"},
  173. {Ptype: "p", V0: "8881", V1: "/customer/customerList", V2: "GET"},
  174. {Ptype: "p", V0: "8881", V1: "/user/getUserInfo", V2: "GET"},
  175. {Ptype: "p", V0: "9528", V1: "/user/admin_register", V2: "POST"},
  176. {Ptype: "p", V0: "9528", V1: "/api/createApi", V2: "POST"},
  177. {Ptype: "p", V0: "9528", V1: "/api/getApiList", V2: "POST"},
  178. {Ptype: "p", V0: "9528", V1: "/api/getApiById", V2: "POST"},
  179. {Ptype: "p", V0: "9528", V1: "/api/deleteApi", V2: "POST"},
  180. {Ptype: "p", V0: "9528", V1: "/api/updateApi", V2: "POST"},
  181. {Ptype: "p", V0: "9528", V1: "/api/getAllApis", V2: "POST"},
  182. {Ptype: "p", V0: "9528", V1: "/authority/createAuthority", V2: "POST"},
  183. {Ptype: "p", V0: "9528", V1: "/authority/deleteAuthority", V2: "POST"},
  184. {Ptype: "p", V0: "9528", V1: "/authority/getAuthorityList", V2: "POST"},
  185. {Ptype: "p", V0: "9528", V1: "/authority/setDataAuthority", V2: "POST"},
  186. {Ptype: "p", V0: "9528", V1: "/menu/getMenu", V2: "POST"},
  187. {Ptype: "p", V0: "9528", V1: "/menu/getMenuList", V2: "POST"},
  188. {Ptype: "p", V0: "9528", V1: "/menu/addBaseMenu", V2: "POST"},
  189. {Ptype: "p", V0: "9528", V1: "/menu/getBaseMenuTree", V2: "POST"},
  190. {Ptype: "p", V0: "9528", V1: "/menu/addMenuAuthority", V2: "POST"},
  191. {Ptype: "p", V0: "9528", V1: "/menu/getMenuAuthority", V2: "POST"},
  192. {Ptype: "p", V0: "9528", V1: "/menu/deleteBaseMenu", V2: "POST"},
  193. {Ptype: "p", V0: "9528", V1: "/menu/updateBaseMenu", V2: "POST"},
  194. {Ptype: "p", V0: "9528", V1: "/menu/getBaseMenuById", V2: "POST"},
  195. {Ptype: "p", V0: "9528", V1: "/user/changePassword", V2: "POST"},
  196. {Ptype: "p", V0: "9528", V1: "/user/getUserList", V2: "POST"},
  197. {Ptype: "p", V0: "9528", V1: "/user/setUserAuthority", V2: "POST"},
  198. {Ptype: "p", V0: "9528", V1: "/deptUploadAndDownload/upload", V2: "POST"},
  199. {Ptype: "p", V0: "9528", V1: "/deptUploadAndDownload/getFileList", V2: "POST"},
  200. {Ptype: "p", V0: "9528", V1: "/deptUploadAndDownload/deleteFile", V2: "POST"},
  201. {Ptype: "p", V0: "9528", V1: "/deptUploadAndDownload/editFileName", V2: "POST"},
  202. {Ptype: "p", V0: "9528", V1: "/casbin/updateCasbin", V2: "POST"},
  203. {Ptype: "p", V0: "9528", V1: "/casbin/getPolicyPathByAuthorityId", V2: "POST"},
  204. {Ptype: "p", V0: "9528", V1: "/jwt/jsonInBlacklist", V2: "POST"},
  205. {Ptype: "p", V0: "9528", V1: "/system/getSystemConfig", V2: "POST"},
  206. {Ptype: "p", V0: "9528", V1: "/system/setSystemConfig", V2: "POST"},
  207. {Ptype: "p", V0: "9528", V1: "/customer/customer", V2: "PUT"},
  208. {Ptype: "p", V0: "9528", V1: "/customer/customer", V2: "GET"},
  209. {Ptype: "p", V0: "9528", V1: "/customer/customer", V2: "POST"},
  210. {Ptype: "p", V0: "9528", V1: "/customer/customer", V2: "DELETE"},
  211. {Ptype: "p", V0: "9528", V1: "/customer/customerList", V2: "GET"},
  212. {Ptype: "p", V0: "9528", V1: "/autoCode/createTemp", V2: "POST"},
  213. {Ptype: "p", V0: "9528", V1: "/user/getUserInfo", V2: "GET"},
  214. }
  215. if err := db.Create(&entities).Error; err != nil {
  216. return ctx, errors.Wrap(err, "Casbin 表 ("+i.InitializerName()+") 数据初始化失败!")
  217. }
  218. next := context.WithValue(ctx, i.InitializerName(), entities)
  219. return next, nil
  220. }
  221. func (i *initCasbin) DataInserted(ctx context.Context) bool {
  222. db, ok := ctx.Value("db").(*gorm.DB)
  223. if !ok {
  224. return false
  225. }
  226. if errors.Is(db.Where(adapter.CasbinRule{Ptype: "p", V0: "9528", V1: "/user/getUserInfo", V2: "GET"}).
  227. First(&adapter.CasbinRule{}).Error, gorm.ErrRecordNotFound) { // 判断是否存在数据
  228. return false
  229. }
  230. return true
  231. }