296fd6ea825709974e5fa8626effec07a5557c25.svn-base 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package inject
  2. import (
  3. "github.com/casbin/casbin"
  4. "github.com/facebookgo/inject"
  5. "../../pkg/setting"
  6. "../../service/bll"
  7. "runtime"
  8. )
  9. // Object 注入对象
  10. type Object struct {
  11. Common *bll.Common
  12. Enforcer *casbin.Enforcer
  13. }
  14. var Obj *Object
  15. // Init 初始化依赖注入
  16. func Init() {
  17. g := new(inject.Graph)
  18. // 注入casbin
  19. osType := runtime.GOOS
  20. var path string
  21. if osType == "windows" {
  22. path = setting.CurrentPath+"conf\\rbac_model.conf"
  23. } else if osType == "linux" {
  24. path = setting.CurrentPath+"conf/rbac_model.conf"
  25. }
  26. enforcer := casbin.NewEnforcer(path, false)
  27. _ = g.Provide(&inject.Object{Value: enforcer})
  28. Common := new(bll.Common)
  29. _ = g.Provide(&inject.Object{Value: Common})
  30. if err := g.Populate(); err != nil {
  31. panic("初始化依赖注入发生错误:" + err.Error())
  32. }
  33. Obj = &Object{
  34. Enforcer: enforcer,
  35. Common: Common,
  36. }
  37. return
  38. }
  39. //var instance *Object
  40. //var once sync.Once
  41. //
  42. //func GetInstance() *Object {
  43. // once.Do(func() {
  44. // instance = Init()
  45. // })
  46. // return instance
  47. //}
  48. // 加载casbin策略数据,包括角色权限数据、用户角色数据
  49. func LoadCasbinPolicyData() error {
  50. c := Obj.Common
  51. err := c.RoleAPI.LoadAllPolicy()
  52. if err != nil {
  53. return err
  54. }
  55. err = c.UserAPI.LoadAllPolicy()
  56. if err != nil {
  57. return err
  58. }
  59. return nil
  60. }