MCPcopy
hub / github.com/apache/casbin / TestRBACModelWithCustomData

Function TestRBACModelWithCustomData

model_test.go:344–374  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

342}
343
344func TestRBACModelWithCustomData(t *testing.T) {
345 e, _ := NewEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv")
346
347 // You can add custom data to a grouping policy, Casbin will ignore it. It is only meaningful to the caller.
348 // This feature can be used to store information like whether "bob" is an end user (so no subject will inherit "bob")
349 // For Casbin, it is equivalent to: e.AddGroupingPolicy("bob", "data2_admin")
350 _, _ = e.AddGroupingPolicy("bob", "data2_admin", "custom_data")
351
352 testEnforce(t, e, "alice", "data1", "read", true)
353 testEnforce(t, e, "alice", "data1", "write", false)
354 testEnforce(t, e, "alice", "data2", "read", true)
355 testEnforce(t, e, "alice", "data2", "write", true)
356 testEnforce(t, e, "bob", "data1", "read", false)
357 testEnforce(t, e, "bob", "data1", "write", false)
358 testEnforce(t, e, "bob", "data2", "read", true)
359 testEnforce(t, e, "bob", "data2", "write", true)
360
361 // You should also take the custom data as a parameter when deleting a grouping policy.
362 // e.RemoveGroupingPolicy("bob", "data2_admin") won't work.
363 // Or you can remove it by using RemoveFilteredGroupingPolicy().
364 _, _ = e.RemoveGroupingPolicy("bob", "data2_admin", "custom_data")
365
366 testEnforce(t, e, "alice", "data1", "read", true)
367 testEnforce(t, e, "alice", "data1", "write", false)
368 testEnforce(t, e, "alice", "data2", "read", true)
369 testEnforce(t, e, "alice", "data2", "write", true)
370 testEnforce(t, e, "bob", "data1", "read", false)
371 testEnforce(t, e, "bob", "data1", "write", false)
372 testEnforce(t, e, "bob", "data2", "read", false)
373 testEnforce(t, e, "bob", "data2", "write", true)
374}
375
376func TestRBACModelWithPattern(t *testing.T) {
377 e, _ := NewEnforcer("examples/rbac_with_pattern_model.conf", "examples/rbac_with_pattern_policy.csv")

Callers

nothing calls this directly

Calls 4

AddGroupingPolicyMethod · 0.95
RemoveGroupingPolicyMethod · 0.95
NewEnforcerFunction · 0.85
testEnforceFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…