Giter Club home page Giter Club logo

Comments (5)

casbin-bot avatar casbin-bot commented on June 3, 2024

@smrpn @hackerchai @PsiACE @GopherJ

from casbin-rs.

hsluoyz avatar hsluoyz commented on June 3, 2024

@adeelfareed can you give a detailed example?

from casbin-rs.

adeelfareed avatar adeelfareed commented on June 3, 2024

@hsluoyz Please take a look, I have added an example.

from casbin-rs.

valkum avatar valkum commented on June 3, 2024

We have a

pub trait RbacApiEx: RbacApi {
    /// Gets implicit users for a role
    fn get_implicit_users_for_role(&mut self, role: &str, domain: Option<&str>) -> Vec<String> {
        let mut res: HashSet<String> = HashSet::new();
        let mut q: Vec<String> = vec![role.to_owned()];
        while !q.is_empty() {
            let name = q.swap_remove(0);
            let users = self.get_role_manager().write().get_users(&name, domain);
            for r in users.into_iter() {
                if res.insert(r.to_owned()) {
                    q.push(r);
                }
            }
        }
        res.into_iter().collect()
    }

    /// Gets implicit resources for a user
    fn get_implicit_resources_for_user(
        &mut self,
        user: &str,
        domain: Option<&str>,
    ) -> Vec<Vec<String>> {
        let permissions = self.get_implicit_permissions_for_user(user, domain);
        let mut result = Vec::new();
        for permission in permissions {
            // This resource is directly accessible by the user.
            if permission[0] == user {
                result.push(permission.clone());
                continue;
            }

            // Now handle the implicit permissions
            // First collect all rules that are implicitly accessible (v0) by the role (v1)
            // The result is a Vec of Vecs which contain all v0 entries that are accessible by v1.
            // The target v1 can be in v1 to v5 of the direct permissions
            let t = permission
                .iter()
                .skip(1)
                .map(|token| {
                    let mut tokens = self.get_implicit_users_for_role(token, domain);
                    tokens.push(token.clone());
                    tokens
                })
                .collect::<Vec<_>>();

            // Extend each rule in result_local.
            let mut result_local = vec![vec![user.to_string()]];
            let tokens_length = permission.len();
            for i in 0..tokens_length - 1 {
                let mut n = Vec::new();
                for tokens in &t[i] {
                    for policy in &result_local {
                        let mut t = policy.clone();
                        t.push(tokens.clone());
                        n.push(t);
                    }
                }
                result_local = n;
            }
            result.extend(result_local.into_iter());
        }
        result
    }
}
impl<T: RbacApi> RbacApiEx for T {}

from casbin-rs.

hsluoyz avatar hsluoyz commented on June 3, 2024

@PsiACE @hackerchai

from casbin-rs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.