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

use Rc<RefCell<Role>> to store roles #9

Merged
merged 3 commits into from
Aug 18, 2019

Conversation

xcaptain
Copy link
Contributor

@xcaptain xcaptain commented Aug 17, 2019

Fix: #7

to make shared mutability
@codecov
Copy link

codecov bot commented Aug 17, 2019

Codecov Report

Merging #9 into master will increase coverage by 14.82%.
The diff coverage is 82.9%.

Impacted file tree graph

@@             Coverage Diff             @@
##           master       #9       +/-   ##
===========================================
+ Coverage   61.63%   76.45%   +14.82%     
===========================================
  Files           8        9        +1     
  Lines         735     1015      +280     
===========================================
+ Hits          453      776      +323     
+ Misses        282      239       -43
Impacted Files Coverage Δ
src/rbac/role_manager.rs 100% <100%> (ø)
src/enforcer.rs 72.42% <100%> (+12.2%) ⬆️
src/rbac/default_role_manager.rs 82.68% <82.68%> (ø)
src/config.rs 87.68% <0%> (+6.83%) ⬆️
src/model.rs 85.45% <0%> (+29.04%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9119c89...c92b3e3. Read the comment docs.

src/rbac.rs Outdated Show resolved Hide resolved
@xcaptain
Copy link
Contributor Author

@hsluoyz please take a look, I think most functions in role_manager.go have been ported to rbac.rs now, this branch is ready to get merged.

@xcaptain
Copy link
Contributor Author

I'm using a Option<&str> in has_link, get_roles, get_users and using Vec<&str> in add_link and delete_link
https://github.com/casbin/casbin/blob/master/model/assertion.go#L57

https://github.com/casbin/casbin/blob/master/rbac/default-role-manager/role_manager.go#L91

Is that a bug in casbin, because when passing 2 string as domain to AddLink, the default role manager will throw a error.

I'm suggesting using Option<&str> because domain can only be at most 1 string, but it's easy to change to Vec<&str>

@hsluoyz
Copy link
Member

hsluoyz commented Aug 18, 2019

Why not use the role_manager.rs name? I think it's better to use the same file name.

@hsluoyz
Copy link
Member

hsluoyz commented Aug 18, 2019

I have forgotten why this commit is necessary: casbin/casbin@9a33e2d

Can anybody remind me why?

@xcaptain
Copy link
Contributor Author

I have separated the DefaultRoleManager to a different file. But I think module visibility is another thing that we can improve in the later commits.

@xcaptain xcaptain mentioned this pull request Aug 18, 2019
24 tasks
@hsluoyz hsluoyz merged commit d23ffad into casbin:master Aug 18, 2019
@xcaptain xcaptain deleted the feature/add-links-rc branch August 18, 2019 12:50
Copy link

@lopopolo lopopolo left a comment

Choose a reason for hiding this comment

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

Hey I came across this project from the What’s Everyone Working On? thread on r/rust. I hope the feedback I’ve left on this PR is welcome. 🙂

src/rbac/default_role_manager.rs Show resolved Hide resolved
.clone()
}

fn has_role(&self, name: &str) -> bool {

Choose a reason for hiding this comment

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

This method implementation can be replaced with a call to contains_key on the underlying HashMap.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, contains_key is better here, I will fix this later.

src/rbac/default_role_manager.rs Show resolved Hide resolved

fn add_link(&mut self, name1: &str, name2: &str, domain: Vec<&str>) {
let mut name1 = name1.to_owned();
let mut name2 = name2.to_owned();

Choose a reason for hiding this comment

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

It appears unnecessary to create owned references to these strings.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm using to_owned here to convert &str to String, because format! need String as parameters. Do you suggest using concat! here?

src/rbac/default_role_manager.rs Show resolved Hide resolved
src/rbac/default_role_manager.rs Show resolved Hide resolved
src/rbac/default_role_manager.rs Show resolved Hide resolved
src/rbac/default_role_manager.rs Show resolved Hide resolved
}

fn has_direct_role(&self, name: &str) -> bool {
for role in self.roles.iter() {

Choose a reason for hiding this comment

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

iter + any works here too

@@ -0,0 +1,16 @@
pub trait RoleManager {
fn clone_box(&self) -> Box<dyn RoleManager>;

Choose a reason for hiding this comment

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

Rather than require this method, you can add a constraint on the self type of this trait to require implementors are Clone.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I learned this technical from https://users.rust-lang.org/t/how-to-clone-a-boxed-closure/31035 I don't know how to add clone constraint to self, can you be more specific?

Copy link
Contributor

Choose a reason for hiding this comment

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

If you use dyn Rolemanager any where else you can not change this because your trait becomes object unsafe and you then can not refer to it as dyn Rolemanager.
If you don't need that ability it would look like this

// this means that every type that impl's RoleManager must be Clone
pub trait RoleManager: Clone {
//  No more clone_box
    fn clear(&mut self);
    fn print_roles(&self);
}

#[derive(Clone)]
struct Try(Option<fn()>);
impl RoleManager for Try {
    fn clear(&mut self) {}
    fn print_roles(&self) {}
}

try it here

Copy link
Contributor Author

@xcaptain xcaptain Aug 25, 2019

Choose a reason for hiding this comment

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

I tried to use RoleManager: Clone to replace boxed trait with clone, but gave up in the end, because this approach introduces so many trait constraints. I have a tracking task about it at #5 Can you help make a pull request on this? @DevinR528

Copy link
Contributor

@DevinR528 DevinR528 Aug 25, 2019

Choose a reason for hiding this comment

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

You do use RoleManager as a trait object constraints that require knowing the Size of the trait object is what makes it unsafe. I should have looked around a bit more 🤦‍♂️.

pub fn generate_g_function(rm: Box<dyn RoleManager>) -> Box<dyn MatchFnClone> 

So it is probably not worth changing just to remove one method that is perfectly fine. Although if you wanted to this an Rc may work I can try if you want or using dtonlay's crate or even easier objekt but it probably isn't worth it I don't think clone_box is all that much more expensive.

@xcaptain
Copy link
Contributor Author

Most of the code review suggestions have been fixed in bc1c6e2
@lopopolo please take a look 😊️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Role inheritance problem in rbac.rs
4 participants