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

[W.I.P]refactor: refactored the model section code #53

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
17 changes: 8 additions & 9 deletions lib/src/internal_enforcer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class InternalEnforcer extends CoreEnforcer {
if (sec == 'g' && res) {
var rules = <List<String>>[];
rules.add(rule);
buildIncrementalRoleLinks(PolicyOperations.PolicyAdd, ptype, rules);
buildIncrementalRoleLinks(PolicyOp.PolicyAdd, ptype, rules);
}

if (watcher != null && autoNotifyWatcher) {
Expand Down Expand Up @@ -82,7 +82,7 @@ class InternalEnforcer extends CoreEnforcer {
var res = model.addPolicies(sec, ptype, rules);

if (sec == 'g' && res) {
buildIncrementalRoleLinks(PolicyOperations.PolicyAdd, ptype, rules);
buildIncrementalRoleLinks(PolicyOp.PolicyAdd, ptype, rules);
}

if (watcher != null && autoNotifyWatcher) {
Expand All @@ -97,7 +97,7 @@ class InternalEnforcer extends CoreEnforcer {
/// [rules] the rules.

void buildIncrementalRoleLinks(
PolicyOperations op,
PolicyOp op,
String ptype,
List<List<String>> rules,
) {
Expand Down Expand Up @@ -148,8 +148,7 @@ class InternalEnforcer extends CoreEnforcer {
// remove the old rule
var oldRules = <List<String>>[];
oldRules.add(oldRule);
buildIncrementalRoleLinks(
PolicyOperations.PolicyRemove, ptype, oldRules);
buildIncrementalRoleLinks(PolicyOp.PolicyRemove, ptype, oldRules);
} catch (e) {
logger.logPrint('An exception occurred:' + e.toString());
return false;
Expand All @@ -159,7 +158,7 @@ class InternalEnforcer extends CoreEnforcer {
// add the new rule
var newRules = <List<String>>[];
newRules.add(newRule);
buildIncrementalRoleLinks(PolicyOperations.PolicyAdd, ptype, newRules);
buildIncrementalRoleLinks(PolicyOp.PolicyAdd, ptype, newRules);
} catch (e) {
logger.logPrint('An exception occurred:' + e.toString());
return false;
Expand Down Expand Up @@ -207,7 +206,7 @@ class InternalEnforcer extends CoreEnforcer {
if (sec == 'g') {
var rules = <List<String>>[];
rules.add(rule);
buildIncrementalRoleLinks(PolicyOperations.PolicyRemove, ptype, rules);
buildIncrementalRoleLinks(PolicyOp.PolicyRemove, ptype, rules);
}
if (watcher != null && autoNotifyWatcher) {
watcher!.update();
Expand Down Expand Up @@ -244,7 +243,7 @@ class InternalEnforcer extends CoreEnforcer {
}

if (sec == 'g') {
buildIncrementalRoleLinks(PolicyOperations.PolicyRemove, ptype, rules);
buildIncrementalRoleLinks(PolicyOp.PolicyRemove, ptype, rules);
}
if (watcher != null && autoNotifyWatcher) {
watcher!.update();
Expand Down Expand Up @@ -288,7 +287,7 @@ class InternalEnforcer extends CoreEnforcer {
}

if (sec == 'g') {
buildIncrementalRoleLinks(PolicyOperations.PolicyRemove, ptype, effects);
buildIncrementalRoleLinks(PolicyOp.PolicyRemove, ptype, effects);
}

if (watcher != null && autoNotifyWatcher) {
Expand Down
6 changes: 3 additions & 3 deletions lib/src/model/assertion.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class Assertion {

Copy link
Contributor

@MrUnfunny MrUnfunny Jul 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Rishabhmodii add a copywith to assertion class, similar to copy function of go version.

void buildIncrementalRoleLinks(
RoleManager rm,
PolicyOperations op,
PolicyOp op,
List<List<String>> rules,
) {
this.rm = rm;
Expand All @@ -91,10 +91,10 @@ class Assertion {
rule = rule.sublist(0, count);
}
switch (op) {
case PolicyOperations.PolicyAdd:
case PolicyOp.PolicyAdd:
rm.addLink(rule[0], rule[1], rule.sublist(2));
break;
case PolicyOperations.PolicyRemove:
case PolicyOp.PolicyRemove:
rm.deleteLink(rule[0], rule[1], rule.sublist(2));
break;
default:
Expand Down
12 changes: 6 additions & 6 deletions lib/src/model/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import '../rbac/role_manager.dart';
import 'assertion.dart';
import 'policy.dart';

enum PolicyOperations {
enum PolicyOp {
PolicyAdd,
PolicyRemove,
}
Expand All @@ -37,12 +37,12 @@ class Model extends Policy {
Model();

/// creates a model from a .CONF file.
Model.fromFile(String path) {
Model.newModelFromFile(String path) {
loadModel(path);
}

/// creates a model from a String.
Model.fromString(String text) {
Model.newModelFromString(String text) {
loadModelFromText(text);
}

Expand Down Expand Up @@ -130,7 +130,7 @@ class Model extends Policy {
}

/// Helper function for loadModel
void loadSections(Config cfg) {
void loadModelFromConfig(Config cfg) {
loadSection(this, cfg, 'r');
loadSection(this, cfg, 'p');
loadSection(this, cfg, 'e');
Expand All @@ -144,7 +144,7 @@ class Model extends Policy {
void loadModel(String path) {
final cfg = Config.newConfig(path);

loadSections(cfg);
loadModelFromConfig(cfg);
}

/// Loads the model from the text.
Expand All @@ -153,7 +153,7 @@ class Model extends Policy {
void loadModelFromText(String text) {
final cfg = Config.newConfigFromText(text);

loadSections(cfg);
loadModelFromConfig(cfg);
}

/// Saves the section to the text and returns the section text.
Expand Down
4 changes: 2 additions & 2 deletions lib/src/model/policy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,8 @@ class Policy {
return values;
}

void buildIncrementalRoleLinks(RoleManager rm, PolicyOperations op,
String sec, String ptype, List<List<String>> rules) {
void buildIncrementalRoleLinks(RoleManager rm, PolicyOp op, String sec,
String ptype, List<List<String>> rules) {
if (sec == 'g') {
model[sec]?[ptype]?.buildIncrementalRoleLinks(rm, op, rules);
}
Expand Down