demo_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package test
  2. import (
  3. "fmt"
  4. "gfast/library/utils"
  5. "github.com/casbin/casbin/v2"
  6. "github.com/casbin/casbin/v2/util"
  7. "github.com/gogf/gf/os/glog"
  8. "testing"
  9. )
  10. func TestDemo(t *testing.T) {
  11. //t.Run("demo1" ,Demo1)
  12. t.Run("Adapters_test", Adapters)
  13. }
  14. func Demo1(t *testing.T) {
  15. e, err := casbin.NewEnforcer("casbin_conf/model.conf", "casbin_conf/policy.csv")
  16. if err != nil {
  17. panic(err)
  18. }
  19. sub := "alice" // the user that wants to access a resource. 对象
  20. obj := "data1" // the resource that is going to be accessed. 资源
  21. act := "write" // the operation that the user performs on the resource. 操作
  22. ok, err := e.Enforce(sub, obj, act)
  23. if err != nil {
  24. fmt.Println("验证失败", err)
  25. }
  26. if ok == true {
  27. fmt.Println("权限通过")
  28. } else {
  29. fmt.Println("没有权限")
  30. }
  31. }
  32. func Adapters(t *testing.T) {
  33. a := initAdapter(t, "mysql", "root:123456@tcp(127.0.0.1:3306)/test2")
  34. testAutoSave(t, a)
  35. testSaveLoad(t, a)
  36. a = initAdapterFormOptions(t, &utils.Adapter{
  37. DriverName: "mysql",
  38. DataSourceName: "root:123456@tcp(127.0.0.1:3306)/test2",
  39. })
  40. testAutoSave(t, a)
  41. testSaveLoad(t, a)
  42. }
  43. func initAdapterFormOptions(t *testing.T, adapter *utils.Adapter) *utils.Adapter {
  44. // Create an adapter
  45. a, _ := utils.NewAdapterFromOptions(adapter)
  46. // Initialize some policy in DB.
  47. initPolicy(t, a)
  48. // Now the DB has policy, so we can provide a normal use case.
  49. // Note: you don't need to look at the above code
  50. // if you already have a working DB with policy inside.
  51. return a
  52. }
  53. func initPolicy(t *testing.T, a *utils.Adapter) {
  54. // Because the DB is empty at first,
  55. // so we need to load the policy from the file adapter (.CSV) first.
  56. e, err := casbin.NewEnforcer("casbin_conf/rbac_model.conf", "casbin_conf/rbac_policy.csv")
  57. if err != nil {
  58. panic(err)
  59. }
  60. // This is a trick to save the current policy to the DB.
  61. // We can't call e.SavePolicy() because the adapter in the enforcer is still the file adapter.
  62. // The current policy means the policy in the Casbin enforcer (aka in memory).
  63. err = a.SavePolicy(e.GetModel())
  64. if err != nil {
  65. panic(err)
  66. }
  67. // Clear the current policy.
  68. e.ClearPolicy()
  69. testGetPolicy(t, e, [][]string{})
  70. // Load the policy from DB.
  71. err = a.LoadPolicy(e.GetModel())
  72. if err != nil {
  73. panic(err)
  74. }
  75. testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}})
  76. }
  77. func testGetPolicy(t *testing.T, e *casbin.Enforcer, res [][]string) {
  78. myRes := e.GetPolicy()
  79. glog.Info("Policy: ", myRes)
  80. if !util.Array2DEquals(res, myRes) {
  81. t.Error("Policy: ", myRes, ", supposed to be ", res)
  82. }
  83. }
  84. func initAdapter(t *testing.T, driverName string, dataSourceName string) *utils.Adapter {
  85. // Create an adapter
  86. a, err := utils.NewAdapter(driverName, dataSourceName)
  87. if err != nil {
  88. panic(err)
  89. }
  90. // Initialize some policy in DB.
  91. initPolicy(t, a)
  92. // Now the DB has policy, so we can provide a normal use case.
  93. // Note: you don't need to look at the above code
  94. // if you already have a working DB with policy inside.
  95. return a
  96. }
  97. func testAutoSave(t *testing.T, a *utils.Adapter) {
  98. // NewEnforcer() will load the policy automatically.
  99. e, err := casbin.NewEnforcer("casbin_conf/rbac_model.conf", a)
  100. if err != nil {
  101. panic(err)
  102. }
  103. // AutoSave is enabled by default.
  104. // Now we disable it.
  105. e.EnableAutoSave(false)
  106. // Because AutoSave is disabled, the policy change only affects the policy in Casbin enforcer,
  107. // it doesn't affect the policy in the storage.
  108. e.AddPolicy("alice", "data1", "write")
  109. // Reload the policy from the storage to see the effect.
  110. e.LoadPolicy()
  111. // This is still the original policy.
  112. testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}})
  113. // Now we enable the AutoSave.
  114. e.EnableAutoSave(true)
  115. // Because AutoSave is enabled, the policy change not only affects the policy in Casbin enforcer,
  116. // but also affects the policy in the storage.
  117. e.AddPolicy("alice", "data1", "write")
  118. // Reload the policy from the storage to see the effect.
  119. e.LoadPolicy()
  120. // The policy has a new rule: {"alice", "data1", "write"}.
  121. testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}, {"alice", "data1", "write"}})
  122. // Remove the added rule.
  123. e.RemovePolicy("alice", "data1", "write")
  124. e.LoadPolicy()
  125. testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}})
  126. // Remove "data2_admin" related policy rules via a filter.
  127. // Two rules: {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"} are deleted.
  128. e.RemoveFilteredPolicy(0, "data2_admin")
  129. e.LoadPolicy()
  130. testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}})
  131. }
  132. func testSaveLoad(t *testing.T, a *utils.Adapter) {
  133. // Initialize some policy in DB.
  134. initPolicy(t, a)
  135. // Note: you don't need to look at the above code
  136. // if you already have a working DB with policy inside.
  137. // Now the DB has policy, so we can provide a normal use case.
  138. // Create an adapter and an enforcer.
  139. // NewEnforcer() will load the policy automatically.
  140. e, _ := casbin.NewEnforcer("casbin_conf/rbac_model.conf", a)
  141. testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}})
  142. }