casbin.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. "log-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: "/base/login", V2: "POST"},
  40. {PType: "p", V0: "888", V1: "/user/admin_register", V2: "POST"},
  41. {PType: "p", V0: "888", V1: "/api/createApi", V2: "POST"},
  42. {PType: "p", V0: "888", V1: "/api/getApiList", V2: "POST"},
  43. {PType: "p", V0: "888", V1: "/api/getApiById", V2: "POST"},
  44. {PType: "p", V0: "888", V1: "/api/deleteApi", V2: "POST"},
  45. {PType: "p", V0: "888", V1: "/api/updateApi", V2: "POST"},
  46. {PType: "p", V0: "888", V1: "/api/getAllApis", V2: "POST"},
  47. {PType: "p", V0: "888", V1: "/api/deleteApisByIds", V2: "DELETE"},
  48. {PType: "p", V0: "888", V1: "/authority/copyAuthority", V2: "POST"},
  49. {PType: "p", V0: "888", V1: "/authority/updateAuthority", V2: "PUT"},
  50. {PType: "p", V0: "888", V1: "/authority/createAuthority", V2: "POST"},
  51. {PType: "p", V0: "888", V1: "/authority/deleteAuthority", V2: "POST"},
  52. {PType: "p", V0: "888", V1: "/authority/getAuthorityList", V2: "POST"},
  53. {PType: "p", V0: "888", V1: "/authority/setDataAuthority", V2: "POST"},
  54. {PType: "p", V0: "888", V1: "/menu/getMenu", V2: "POST"},
  55. {PType: "p", V0: "888", V1: "/menu/getMenuList", V2: "POST"},
  56. {PType: "p", V0: "888", V1: "/menu/addBaseMenu", V2: "POST"},
  57. {PType: "p", V0: "888", V1: "/menu/getBaseMenuTree", V2: "POST"},
  58. {PType: "p", V0: "888", V1: "/menu/addMenuAuthority", V2: "POST"},
  59. {PType: "p", V0: "888", V1: "/menu/getMenuAuthority", V2: "POST"},
  60. {PType: "p", V0: "888", V1: "/menu/deleteBaseMenu", V2: "POST"},
  61. {PType: "p", V0: "888", V1: "/menu/updateBaseMenu", V2: "POST"},
  62. {PType: "p", V0: "888", V1: "/menu/getBaseMenuById", V2: "POST"},
  63. {PType: "p", V0: "888", V1: "/user/getUserInfo", V2: "GET"},
  64. {PType: "p", V0: "888", V1: "/user/setUserInfo", V2: "PUT"},
  65. {PType: "p", V0: "888", V1: "/user/setSelfInfo", V2: "PUT"},
  66. {PType: "p", V0: "888", V1: "/user/getUserList", V2: "POST"},
  67. {PType: "p", V0: "888", V1: "/user/deleteUser", V2: "DELETE"},
  68. {PType: "p", V0: "888", V1: "/user/changePassword", V2: "POST"},
  69. {PType: "p", V0: "888", V1: "/user/setUserAuthority", V2: "POST"},
  70. {PType: "p", V0: "888", V1: "/user/setUserAuthorities", V2: "POST"},
  71. {PType: "p", V0: "888", V1: "/user/resetPassword", V2: "POST"},
  72. {PType: "p", V0: "888", V1: "/fileUploadAndDownload/findFile", V2: "GET"},
  73. {PType: "p", V0: "888", V1: "/fileUploadAndDownload/breakpointContinueFinish", V2: "POST"},
  74. {PType: "p", V0: "888", V1: "/fileUploadAndDownload/breakpointContinue", V2: "POST"},
  75. {PType: "p", V0: "888", V1: "/fileUploadAndDownload/removeChunk", V2: "POST"},
  76. {PType: "p", V0: "888", V1: "/fileUploadAndDownload/upload", V2: "POST"},
  77. {PType: "p", V0: "888", V1: "/fileUploadAndDownload/deleteFile", V2: "POST"},
  78. {PType: "p", V0: "888", V1: "/fileUploadAndDownload/editFileName", V2: "POST"},
  79. {PType: "p", V0: "888", V1: "/fileUploadAndDownload/getFileList", V2: "POST"},
  80. {PType: "p", V0: "888", V1: "/casbin/updateCasbin", V2: "POST"},
  81. {PType: "p", V0: "888", V1: "/casbin/getPolicyPathByAuthorityId", V2: "POST"},
  82. {PType: "p", V0: "888", V1: "/jwt/jsonInBlacklist", V2: "POST"},
  83. {PType: "p", V0: "888", V1: "/system/getSystemConfig", V2: "POST"},
  84. {PType: "p", V0: "888", V1: "/system/setSystemConfig", V2: "POST"},
  85. {PType: "p", V0: "888", V1: "/system/getServerInfo", V2: "POST"},
  86. {PType: "p", V0: "888", V1: "/customer/customer", V2: "GET"},
  87. {PType: "p", V0: "888", V1: "/customer/customer", V2: "PUT"},
  88. {PType: "p", V0: "888", V1: "/customer/customer", V2: "POST"},
  89. {PType: "p", V0: "888", V1: "/customer/customer", V2: "DELETE"},
  90. {PType: "p", V0: "888", V1: "/customer/customerList", V2: "GET"},
  91. {PType: "p", V0: "888", V1: "/autoCode/getDB", V2: "GET"},
  92. {PType: "p", V0: "888", V1: "/autoCode/getMeta", V2: "POST"},
  93. {PType: "p", V0: "888", V1: "/autoCode/preview", V2: "POST"},
  94. {PType: "p", V0: "888", V1: "/autoCode/getTables", V2: "GET"},
  95. {PType: "p", V0: "888", V1: "/autoCode/getColumn", V2: "GET"},
  96. {PType: "p", V0: "888", V1: "/autoCode/rollback", V2: "POST"},
  97. {PType: "p", V0: "888", V1: "/autoCode/createTemp", V2: "POST"},
  98. {PType: "p", V0: "888", V1: "/autoCode/delSysHistory", V2: "POST"},
  99. {PType: "p", V0: "888", V1: "/autoCode/getSysHistory", V2: "POST"},
  100. {PType: "p", V0: "888", V1: "/autoCode/createPackage", V2: "POST"},
  101. {PType: "p", V0: "888", V1: "/autoCode/getPackage", V2: "POST"},
  102. {PType: "p", V0: "888", V1: "/autoCode/delPackage", V2: "POST"},
  103. {PType: "p", V0: "888", V1: "/autoCode/createPlug", V2: "POST"},
  104. {PType: "p", V0: "888", V1: "/sysDictionaryDetail/findSysDictionaryDetail", V2: "GET"},
  105. {PType: "p", V0: "888", V1: "/sysDictionaryDetail/updateSysDictionaryDetail", V2: "PUT"},
  106. {PType: "p", V0: "888", V1: "/sysDictionaryDetail/createSysDictionaryDetail", V2: "POST"},
  107. {PType: "p", V0: "888", V1: "/sysDictionaryDetail/getSysDictionaryDetailList", V2: "GET"},
  108. {PType: "p", V0: "888", V1: "/sysDictionaryDetail/deleteSysDictionaryDetail", V2: "DELETE"},
  109. {PType: "p", V0: "888", V1: "/sysDictionary/findSysDictionary", V2: "GET"},
  110. {PType: "p", V0: "888", V1: "/sysDictionary/updateSysDictionary", V2: "PUT"},
  111. {PType: "p", V0: "888", V1: "/sysDictionary/getSysDictionaryList", V2: "GET"},
  112. {PType: "p", V0: "888", V1: "/sysDictionary/createSysDictionary", V2: "POST"},
  113. {PType: "p", V0: "888", V1: "/sysDictionary/deleteSysDictionary", V2: "DELETE"},
  114. {PType: "p", V0: "888", V1: "/sysOperationRecord/findSysOperationRecord", V2: "GET"},
  115. {PType: "p", V0: "888", V1: "/sysOperationRecord/updateSysOperationRecord", V2: "PUT"},
  116. {PType: "p", V0: "888", V1: "/sysOperationRecord/createSysOperationRecord", V2: "POST"},
  117. {PType: "p", V0: "888", V1: "/sysOperationRecord/getSysOperationRecordList", V2: "GET"},
  118. {PType: "p", V0: "888", V1: "/sysOperationRecord/deleteSysOperationRecord", V2: "DELETE"},
  119. {PType: "p", V0: "888", V1: "/sysOperationRecord/deleteSysOperationRecordByIds", V2: "DELETE"},
  120. {PType: "p", V0: "888", V1: "/email/emailTest", V2: "POST"},
  121. {PType: "p", V0: "888", V1: "/simpleUploader/upload", V2: "POST"},
  122. {PType: "p", V0: "888", V1: "/simpleUploader/checkFileMd5", V2: "GET"},
  123. {PType: "p", V0: "888", V1: "/simpleUploader/mergeFileMd5", V2: "GET"},
  124. {PType: "p", V0: "888", V1: "/excel/importExcel", V2: "POST"},
  125. {PType: "p", V0: "888", V1: "/excel/loadExcel", V2: "GET"},
  126. {PType: "p", V0: "888", V1: "/excel/exportExcel", V2: "POST"},
  127. {PType: "p", V0: "888", V1: "/excel/downloadTemplate", V2: "GET"},
  128. {PType: "p", V0: "888", V1: "/authorityBtn/setAuthorityBtn", V2: "POST"},
  129. {PType: "p", V0: "888", V1: "/authorityBtn/getAuthorityBtn", V2: "POST"},
  130. {PType: "p", V0: "888", V1: "/authorityBtn/canRemoveAuthorityBtn", V2: "POST"},
  131. {PType: "p", V0: "8881", V1: "/base/login", V2: "POST"},
  132. {PType: "p", V0: "8881", V1: "/user/admin_register", V2: "POST"},
  133. {PType: "p", V0: "8881", V1: "/api/createApi", V2: "POST"},
  134. {PType: "p", V0: "8881", V1: "/api/getApiList", V2: "POST"},
  135. {PType: "p", V0: "8881", V1: "/api/getApiById", V2: "POST"},
  136. {PType: "p", V0: "8881", V1: "/api/deleteApi", V2: "POST"},
  137. {PType: "p", V0: "8881", V1: "/api/updateApi", V2: "POST"},
  138. {PType: "p", V0: "8881", V1: "/api/getAllApis", V2: "POST"},
  139. {PType: "p", V0: "8881", V1: "/authority/createAuthority", V2: "POST"},
  140. {PType: "p", V0: "8881", V1: "/authority/deleteAuthority", V2: "POST"},
  141. {PType: "p", V0: "8881", V1: "/authority/getAuthorityList", V2: "POST"},
  142. {PType: "p", V0: "8881", V1: "/authority/setDataAuthority", V2: "POST"},
  143. {PType: "p", V0: "8881", V1: "/menu/getMenu", V2: "POST"},
  144. {PType: "p", V0: "8881", V1: "/menu/getMenuList", V2: "POST"},
  145. {PType: "p", V0: "8881", V1: "/menu/addBaseMenu", V2: "POST"},
  146. {PType: "p", V0: "8881", V1: "/menu/getBaseMenuTree", V2: "POST"},
  147. {PType: "p", V0: "8881", V1: "/menu/addMenuAuthority", V2: "POST"},
  148. {PType: "p", V0: "8881", V1: "/menu/getMenuAuthority", V2: "POST"},
  149. {PType: "p", V0: "8881", V1: "/menu/deleteBaseMenu", V2: "POST"},
  150. {PType: "p", V0: "8881", V1: "/menu/updateBaseMenu", V2: "POST"},
  151. {PType: "p", V0: "8881", V1: "/menu/getBaseMenuById", V2: "POST"},
  152. {PType: "p", V0: "8881", V1: "/user/changePassword", V2: "POST"},
  153. {PType: "p", V0: "8881", V1: "/user/getUserList", V2: "POST"},
  154. {PType: "p", V0: "8881", V1: "/user/setUserAuthority", V2: "POST"},
  155. {PType: "p", V0: "8881", V1: "/fileUploadAndDownload/upload", V2: "POST"},
  156. {PType: "p", V0: "8881", V1: "/fileUploadAndDownload/getFileList", V2: "POST"},
  157. {PType: "p", V0: "8881", V1: "/fileUploadAndDownload/deleteFile", V2: "POST"},
  158. {PType: "p", V0: "8881", V1: "/fileUploadAndDownload/editFileName", V2: "POST"},
  159. {PType: "p", V0: "8881", V1: "/casbin/updateCasbin", V2: "POST"},
  160. {PType: "p", V0: "8881", V1: "/casbin/getPolicyPathByAuthorityId", V2: "POST"},
  161. {PType: "p", V0: "8881", V1: "/jwt/jsonInBlacklist", V2: "POST"},
  162. {PType: "p", V0: "8881", V1: "/system/getSystemConfig", V2: "POST"},
  163. {PType: "p", V0: "8881", V1: "/system/setSystemConfig", V2: "POST"},
  164. {PType: "p", V0: "8881", V1: "/customer/customer", V2: "POST"},
  165. {PType: "p", V0: "8881", V1: "/customer/customer", V2: "PUT"},
  166. {PType: "p", V0: "8881", V1: "/customer/customer", V2: "DELETE"},
  167. {PType: "p", V0: "8881", V1: "/customer/customer", V2: "GET"},
  168. {PType: "p", V0: "8881", V1: "/customer/customerList", V2: "GET"},
  169. {PType: "p", V0: "8881", V1: "/user/getUserInfo", V2: "GET"},
  170. {PType: "p", V0: "9528", V1: "/base/login", V2: "POST"},
  171. {PType: "p", V0: "9528", V1: "/user/admin_register", V2: "POST"},
  172. {PType: "p", V0: "9528", V1: "/api/createApi", V2: "POST"},
  173. {PType: "p", V0: "9528", V1: "/api/getApiList", V2: "POST"},
  174. {PType: "p", V0: "9528", V1: "/api/getApiById", V2: "POST"},
  175. {PType: "p", V0: "9528", V1: "/api/deleteApi", V2: "POST"},
  176. {PType: "p", V0: "9528", V1: "/api/updateApi", V2: "POST"},
  177. {PType: "p", V0: "9528", V1: "/api/getAllApis", V2: "POST"},
  178. {PType: "p", V0: "9528", V1: "/authority/createAuthority", V2: "POST"},
  179. {PType: "p", V0: "9528", V1: "/authority/deleteAuthority", V2: "POST"},
  180. {PType: "p", V0: "9528", V1: "/authority/getAuthorityList", V2: "POST"},
  181. {PType: "p", V0: "9528", V1: "/authority/setDataAuthority", V2: "POST"},
  182. {PType: "p", V0: "9528", V1: "/menu/getMenu", V2: "POST"},
  183. {PType: "p", V0: "9528", V1: "/menu/getMenuList", V2: "POST"},
  184. {PType: "p", V0: "9528", V1: "/menu/addBaseMenu", V2: "POST"},
  185. {PType: "p", V0: "9528", V1: "/menu/getBaseMenuTree", V2: "POST"},
  186. {PType: "p", V0: "9528", V1: "/menu/addMenuAuthority", V2: "POST"},
  187. {PType: "p", V0: "9528", V1: "/menu/getMenuAuthority", V2: "POST"},
  188. {PType: "p", V0: "9528", V1: "/menu/deleteBaseMenu", V2: "POST"},
  189. {PType: "p", V0: "9528", V1: "/menu/updateBaseMenu", V2: "POST"},
  190. {PType: "p", V0: "9528", V1: "/menu/getBaseMenuById", V2: "POST"},
  191. {PType: "p", V0: "9528", V1: "/user/changePassword", V2: "POST"},
  192. {PType: "p", V0: "9528", V1: "/user/getUserList", V2: "POST"},
  193. {PType: "p", V0: "9528", V1: "/user/setUserAuthority", V2: "POST"},
  194. {PType: "p", V0: "9528", V1: "/fileUploadAndDownload/upload", V2: "POST"},
  195. {PType: "p", V0: "9528", V1: "/fileUploadAndDownload/getFileList", V2: "POST"},
  196. {PType: "p", V0: "9528", V1: "/fileUploadAndDownload/deleteFile", V2: "POST"},
  197. {PType: "p", V0: "9528", V1: "/fileUploadAndDownload/editFileName", V2: "POST"},
  198. {PType: "p", V0: "9528", V1: "/casbin/updateCasbin", V2: "POST"},
  199. {PType: "p", V0: "9528", V1: "/casbin/getPolicyPathByAuthorityId", V2: "POST"},
  200. {PType: "p", V0: "9528", V1: "/jwt/jsonInBlacklist", V2: "POST"},
  201. {PType: "p", V0: "9528", V1: "/system/getSystemConfig", V2: "POST"},
  202. {PType: "p", V0: "9528", V1: "/system/setSystemConfig", V2: "POST"},
  203. {PType: "p", V0: "9528", V1: "/customer/customer", V2: "PUT"},
  204. {PType: "p", V0: "9528", V1: "/customer/customer", V2: "GET"},
  205. {PType: "p", V0: "9528", V1: "/customer/customer", V2: "POST"},
  206. {PType: "p", V0: "9528", V1: "/customer/customer", V2: "DELETE"},
  207. {PType: "p", V0: "9528", V1: "/customer/customerList", V2: "GET"},
  208. {PType: "p", V0: "9528", V1: "/autoCode/createTemp", V2: "POST"},
  209. {PType: "p", V0: "9528", V1: "/user/getUserInfo", V2: "GET"},
  210. }
  211. if err := db.Create(&entities).Error; err != nil {
  212. return ctx, errors.Wrap(err, "Casbin 表 ("+i.InitializerName()+") 数据初始化失败!")
  213. }
  214. next := context.WithValue(ctx, i.InitializerName(), entities)
  215. return next, nil
  216. }
  217. func (i *initCasbin) DataInserted(ctx context.Context) bool {
  218. db, ok := ctx.Value("db").(*gorm.DB)
  219. if !ok {
  220. return false
  221. }
  222. if errors.Is(db.Where(adapter.CasbinRule{PType: "p", V0: "9528", V1: "GET", V2: "/user/getUserInfo"}).
  223. First(&adapter.CasbinRule{}).Error, gorm.ErrRecordNotFound) { // 判断是否存在数据
  224. return false
  225. }
  226. return true
  227. }