Skip to content

Builds perfectly on windows (Visual studio code) and added Makefile(make). #4

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

Closed
wants to merge 8 commits into from
Closed
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ StyleCopReport.xml
*.tmp
*.tmp_proj
*_wpftmp.csproj
*.log
# *.log
*.vspscc
*.vssscc
.builds
Expand Down
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
ifdef OS
RM = del
else
ifeq ($(shell uname), Linux)
RM = rm -f
endif
endif

CC = g++
CFLAGS = -std=c++11

all: src/effect/DefaultEffector.h src/effect/Effect.h src/effect/Effector.h src/error/Error.h src/exception/IllegalArgumentException.h src/exception/UnsupportedOperationException.h src/log/DefaultLogger.h src/log/Logger.h src/log/LogUtil.h src/model/Assertion.h src/rbac/DefaultRoleManager.h src/rbac/GroupRoleManager.h src/rbac/RoleManager.h
$(CC) $(CFLAGS) $?

clean:
$(RM) src\effect\*.gch src\error\*.gch src\exception\*.gch src\log\*.gch src\model\*.gch src\rbac\*.gch
Binary file added casbin-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
203 changes: 203 additions & 0 deletions src/config/Config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
// Copyright 2017 The casbin Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// package org.casbin.jcasbin.config;

// import org.casbin.jcasbin.exception.CasbinConfigException;

// import java.io.*;
// import java.util.HashMap;
// import java.util.Map;
// import java.util.concurrent.locks.ReentrantLock;

#include <string>
#include <unordered_map>

using namespace std;

class Config {
private:
static const string DEFAULT_SECTION = "default";
static const string DEFAULT_COMMENT = "#";
static const string DEFAULT_COMMENT_SEM = ";";
// private ReentrantLock lock = new ReentrantLock();

// Section:key=value
unordered_map < string, unordered_map <string, string> > data;

/**
* Config represents the configuration parser.
*/
private Config() {
data = new HashMap<>();
}

/**
* newConfig create an empty configuration representation from file.
*
* @param confName the path of the model file.
* @return the constructor of Config.
*/
public static Config newConfig(String confName) {
Config c = new Config();
c.parse(confName);
return c;
}

/**
* newConfigFromText create an empty configuration representation from text.
*
* @param text the model text.
* @return the constructor of Config.
*/
public static Config newConfigFromText(String text) {
Config c = new Config();
try {
c.parseBuffer(new BufferedReader(new StringReader(text)));
} catch (IOException e) {
throw new CasbinConfigException(e.getMessage(), e.getCause());
}
return c;
}

/**
* addConfig adds a new section->key:value to the configuration.
*/
private boolean addConfig(String section, String option, String value) {
if (section.equals("")) {
section = DEFAULT_SECTION;
}

if (!data.containsKey(section)) {
data.put(section, new HashMap<>());
}

boolean ok = data.get(section).containsKey(option);
data.get(section).put(option, value);
return !ok;
}

private void parse(String fname) {
lock.lock();
try (FileInputStream fis = new FileInputStream(fname)) {
BufferedReader buf = new BufferedReader(new InputStreamReader(fis));
parseBuffer(buf);
} catch (IOException e) {
throw new CasbinConfigException(e.getMessage(), e.getCause());
} finally {
lock.unlock();
}
}

private void parseBuffer(BufferedReader buf) throws IOException {
String section = "";
int lineNum = 0;
String line;

while (true) {
lineNum++;

if ((line = buf.readLine()) != null) {
if ("".equals(line)) {
continue;
}
} else {
break;
}


line = line.trim();
if (line.startsWith(DEFAULT_COMMENT)) {
continue;
} else if (line.startsWith(DEFAULT_COMMENT_SEM)) {
continue;
} else if (line.startsWith("[") && line.endsWith("]")) {
section = line.substring(1, line.length() - 1);
} else {
String[] optionVal = line.split("=", 2);
if (optionVal.length != 2) {
throw new IllegalArgumentException(String.format("parse the content error : line %d , %s = ? ", lineNum, optionVal[0]));
}
String option = optionVal[0].trim();
String value = optionVal[1].trim();
addConfig(section, option, value);
}
}
}

public boolean getBool(String key) {
return Boolean.parseBoolean(get(key));
}

public int getInt(String key) {
return Integer.parseInt(get(key));
}

public float getFloat(String key) {
return Float.parseFloat(get(key));
}

public String getString(String key) {
return get(key);
}

public String[] getStrings(String key) {
String v = get(key);
if (v.equals("")) {
return null;
}
return v.split(",");
}

public void set(String key, String value) {
lock.lock();
if (key.length() == 0) {
throw new IllegalArgumentException("key is empty");
}

String section = "";
String option;

String[] keys = key.toLowerCase().split("::");
if (keys.length >= 2) {
section = keys[0];
option = keys[1];
} else {
option = keys[0];
}

addConfig(section, option, value);
}

public String get(String key) {
String section;
String option;

String[] keys = key.toLowerCase().split("::");
if (keys.length >= 2) {
section = keys[0];
option = keys[1];
} else {
section = DEFAULT_SECTION;
option = keys[0];
}

boolean ok = data.containsKey(section) && data.get(section).containsKey(option);
if (ok) {
return data.get(section).get(option);
} else {
return "";
}
}
}
61 changes: 61 additions & 0 deletions src/effect/DefaultEffector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include "Effector.h"
#include "../exception/UnsupportedOperationException.h"

/**
* DefaultEffector is default effector for Casbin.
*/
class DefaultEffector : public Effector{
public:
/**
* mergeEffects merges all matching results collected by the enforcer into a single decision.
*/
bool mergeEffects(string expr, Effect effects[], float results[]) {
bool result;

unsigned int number_of_effects = sizeof(effects)/sizeof(effects[0]);

if (!expr.compare("some(where (p_eft == allow))")) {
result = false;
for(unsigned int index = 0 ; index < number_of_effects ; index++){
if (effects[index] == Effect::Allow) {
result = true;
break;
}
}
} else if (!expr.compare("!some(where (p_eft == deny))")) {
result = true;
for(unsigned int index = 0 ; index < number_of_effects ; index++){
if (effects[index] == Effect::Deny) {
result = false;
break;
}
}
} else if (!expr.compare("some(where (p_eft == allow)) && !some(where (p_eft == deny))")) {
result = false;
for(unsigned int index = 0 ; index < number_of_effects ; index++){
if (effects[index] == Effect::Allow) {
result = true;
} else if (effects[index] == Effect::Deny) {
result = false;
break;
}
}
} else if (!expr.compare("priority(p_eft) || deny")) {
result = false;
for(unsigned int index = 0 ; index < number_of_effects ; index++){
if (effects[index] != Effect::Indeterminate) {
if (effects[index] == Effect::Allow) {
result = true;
} else {
result = false;
}
break;
}
}
} else {
throw new UnsupportedOperationException("unsupported effect");
}

return result;
}
};
5 changes: 5 additions & 0 deletions src/effect/Effect.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
enum class Effect{
Allow, Indeterminate, Deny
};

typedef enum Effect Effect;
21 changes: 21 additions & 0 deletions src/effect/Effector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <string>

#include "Effect.h"

using namespace std;

/**
* Effector is the abstract class for Casbin effectors.
*/
class Effector{
public:
/**
* mergeEffects merges all matching results collected by the enforcer into a single decision.
*
* @param expr the expression of [policy_effect].
* @param effects the effects of all matched rules.
* @param results the matcher results of all matched rules.
* @return the final effect.
*/
virtual bool mergeEffects(string expr, Effect effects[], float results[]) = 0;
};
19 changes: 19 additions & 0 deletions src/error/Error.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <string>

using namespace std;

class Error{
public:
static Error NIL;
string err;

Error(string error_message){
err = error_message;
}

string toString(){
return err;
}
};

Error Error::NIL = Error("nil");
12 changes: 12 additions & 0 deletions src/exception/IllegalArgumentException.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <string>

using namespace std;

// Exception class for unsupported operations.
class IllegalArgumentException{
string error_message;
public:
IllegalArgumentException(string error_message){
this->error_message = error_message;
}
};
12 changes: 12 additions & 0 deletions src/exception/UnsupportedOperationException.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <string>

using namespace std;

// Exception class for unsupported operations.
class UnsupportedOperationException{
string error_message;
public:
UnsupportedOperationException(string error_message){
this->error_message = error_message;
}
};
Loading