Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: refactored the function_name #52

Merged
merged 1 commit into from
Jul 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions lib/src/enforcer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,26 @@ class Enforcer extends ManagementEnforcer {

Enforcer._();

factory Enforcer.fromModelPathAndPolicyFile(
factory Enforcer.initWithFile(
String modelPath,
String policyFile,
) {
final fileAdapter = FileAdapter(policyFile);
return Enforcer.fromModelPathAndAdapter(modelPath, fileAdapter);
return Enforcer.initWithAdapter(modelPath, fileAdapter);
}

factory Enforcer.fromModelPathAndAdapter(
factory Enforcer.initWithAdapter(
String modelPath,
Adapter adapter,
) {
final model = Model();
model.loadModel(modelPath);
final enf = Enforcer.fromModelAndAdapter(model, adapter);
final enf = Enforcer.initWithModelAndAdapter(model, adapter);
enf.modelPath = modelPath;
return enf;
}

factory Enforcer.fromModelAndAdapter(Model model, [Adapter? adapter]) {
factory Enforcer.initWithModelAndAdapter(Model model, [Adapter? adapter]) {
final _enforcer = Enforcer._();
_enforcer.model = model;
_enforcer.fm = FunctionMap.loadFunctionMap();
Expand Down
28 changes: 14 additions & 14 deletions test/enforcer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ void main() {

group('test enforcer initialization with adapter', () {
var adapter = FileAdapter('casbin_examples/basic_policy.csv');
var e = Enforcer.fromModelPathAndAdapter(
'casbin_examples/basic_model.conf', adapter);
var e =
Enforcer.initWithAdapter('casbin_examples/basic_model.conf', adapter);

testEnforce('test 1', e, 'alice', 'data1', 'read', true);
testEnforce('test 2', e, 'alice', 'data1', 'write', false);
Expand Down Expand Up @@ -145,7 +145,7 @@ void main() {
m.addDef('e', 'e', 'some(where (p.eft == allow))');
m.addDef('m', 'm', 'g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act');

var e = Enforcer.fromModelAndAdapter(m);
var e = Enforcer.initWithModelAndAdapter(m);

e.addPermissionForUser('alice', ['data1', 'invalid']);

Expand All @@ -160,7 +160,7 @@ void main() {
m.addDef('e', 'e', 'some(where (p.eft == allow))');
m.addDef('m', 'm', 'g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act');

var e = Enforcer.fromModelAndAdapter(m);
var e = Enforcer.initWithModelAndAdapter(m);

e.addPermissionForUser('alice', ['data1', 'read']);
e.addPermissionForUser('bob', ['data2', 'write']);
Expand Down Expand Up @@ -188,7 +188,7 @@ void main() {

var a = FileAdapter('casbin_examples/keymatch_policy.csv');

var e = Enforcer.fromModelAndAdapter(m, a);
var e = Enforcer.initWithModelAndAdapter(m, a);

testEnforce('test 1', e, 'alice', '/alice_data/resource1', 'GET', true);
testEnforce('test 2', e, 'alice', '/alice_data/resource1', 'POST', true);
Expand Down Expand Up @@ -223,7 +223,7 @@ void main() {

var a = FileAdapter('casbin_examples/keymatch_policy.csv');

var e = Enforcer.fromModelAndAdapter(m, a);
var e = Enforcer.initWithModelAndAdapter(m, a);

testEnforce('test 1', e, 'alice', '/alice_data/resource2', 'POST', true);
});
Expand Down Expand Up @@ -257,7 +257,7 @@ void main() {
m.addDef('e', 'e', 'some(where (p.eft == allow))');
m.addDef('m', 'm', 'g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act');

final e = Enforcer.fromModelAndAdapter(m);
final e = Enforcer.initWithModelAndAdapter(m);

e.addPermissionForUser('alice', ['data1', 'read']);
e.addPermissionForUser('bob', ['data2', 'write']);
Expand All @@ -282,7 +282,7 @@ void main() {
m.addDef('e', 'e', 'some(where (p.eft == allow))');
m.addDef('m', 'm', 'g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act');

final e = Enforcer.fromModelAndAdapter(m);
final e = Enforcer.initWithModelAndAdapter(m);

e.addPermissionForUser('alice', ['data1', 'read']);
e.addPermissionForUser('bob', ['data2', 'write']);
Expand Down Expand Up @@ -313,7 +313,7 @@ void main() {
m.addDef('e', 'e', 'some(where (p.eft == allow))');
m.addDef('m', 'm', 'g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act');

final e = Enforcer.fromModelAndAdapter(m);
final e = Enforcer.initWithModelAndAdapter(m);

e.addPermissionForUser('alice', ['data1', 'read']);
e.addPermissionForUser('bob', ['data2', 'write']);
Expand Down Expand Up @@ -345,7 +345,7 @@ void main() {
m.addDef('e', 'e', 'some(where (p.eft == allow))');
m.addDef('m', 'm', 'g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act');

final e = Enforcer.fromModelAndAdapter(m);
final e = Enforcer.initWithModelAndAdapter(m);

e.addPermissionForUser('alice', ['data1', 'read']);
e.addPermissionForUser('bob', ['data2', 'write']);
Expand Down Expand Up @@ -379,7 +379,7 @@ void main() {
m.addDef('e', 'e', 'some(where (p.eft == allow))');
m.addDef('m', 'm', 'g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act');

final e = Enforcer.fromModelAndAdapter(m);
final e = Enforcer.initWithModelAndAdapter(m);

e.addPermissionForUser('alice', ['data1', 'read']);
e.addPermissionForUser('bob', ['data2', 'write']);
Expand Down Expand Up @@ -415,7 +415,7 @@ void main() {
m.addDef('e', 'e', 'some(where (p.eft == allow))');
m.addDef('m', 'm', 'g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act');

final e = Enforcer.fromModelAndAdapter(m);
final e = Enforcer.initWithModelAndAdapter(m);

e.addPermissionForUser('alice', ['data1', 'read']);
e.addPermissionForUser('bob', ['data2', 'write']);
Expand Down Expand Up @@ -465,7 +465,7 @@ m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act
final model = Model();
model.loadModelFromText(text);

final e = Enforcer.fromModelAndAdapter(model);
final e = Enforcer.initWithModelAndAdapter(model);

e.addPermissionForUser('alice', ['data1', 'read']);
e.addPermissionForUser('bob', ['data2', 'write']);
Expand All @@ -492,7 +492,7 @@ m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act
model.addDef(
'm', 'm', 'g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act');

final e = Enforcer.fromModelAndAdapter(model);
final e = Enforcer.initWithModelAndAdapter(model);

e.addPermissionForUser('alice', ['data1', 'read']);
e.addPermissionForUser('bob', ['data2', 'write']);
Expand Down
4 changes: 2 additions & 2 deletions test/logger/logger_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void main() {
'm.m: r_sub == p_sub && keyMatch(r_obj, p_obj) && regexMatch(r_act, p_act)');
});
group('test logPolicy', () {
final enf = Enforcer.fromModelPathAndPolicyFile(
final enf = Enforcer.initWithFile(
'casbin_examples/basic_model.conf',
'casbin_examples/basic_policy.csv',
);
Expand All @@ -79,7 +79,7 @@ void main() {
});

group('test logRole', () {
final enf = Enforcer.fromModelPathAndPolicyFile(
final enf = Enforcer.initWithFile(
'casbin_examples/rbac_with_domains_model.conf',
'casbin_examples/rbac_with_domains_policy.csv',
);
Expand Down
3 changes: 1 addition & 2 deletions test/model/model_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ void main() {
final rbacWithNotDenyModel = 'casbin_examples/rbac_with_not_deny_model.conf';

group('Test enforcing with basic model', () {
final e =
Enforcer.fromModelPathAndPolicyFile(basicModelPath, basicPolicyFile);
final e = Enforcer.initWithFile(basicModelPath, basicPolicyFile);

testEnforce('test 1', e, 'alice', 'data1', 'read', true);
testEnforce('test 2', e, 'alice', 'data1', 'write', false);
Expand Down