Struct oso::Oso

source ·
pub struct Oso { /* private fields */ }
Expand description

Oso is the main struct you interact with. It is an instance of the Oso authorization library and contains the polar language knowledge base and query engine.

Implementations§

source§

impl Oso

source

pub fn new() -> Self

Create a new instance of Oso. Each instance is separate and can have different rules and classes loaded into it.

source

pub fn is_allowed<Actor, Action, Resource>( &self, actor: Actor, action: Action, resource: Resource ) -> Result<bool>
where Actor: ToPolar, Action: ToPolar, Resource: ToPolar,

High level interface for authorization decisions. Makes an allow query with the given actor, action and resource and returns true or false.

source

pub fn get_allowed_actions<Actor, Resource, T>( &self, actor: Actor, resource: Resource ) -> Result<HashSet<T>>
where Actor: ToPolar, Resource: ToPolar, T: FromPolar + Eq + Hash,

Get the actions actor is allowed to take on resource. Returns a std::collections::HashSet of actions, typed according the return value.

Examples
oso.load_str(r#"allow(actor: Actor{name: "sally"}, action, resource: Widget{id: 1}) if
              action in ["CREATE", "READ"];"#);

// get a HashSet of oso::Actions
let actions: HashSet<Action> = oso.get_allowed_actions(actor, resource)?;

// or Strings
let actions: HashSet<String> = oso.get_allowed_actions(actor, resource)?;
source

pub fn clear_rules(&mut self) -> Result<()>

Clear out all files and rules that have been loaded.

source

pub fn load_file<P: AsRef<Path>>(&mut self, filename: P) -> Result<()>

👎Deprecated since 0.20.1: Oso::load_file has been deprecated in favor of Oso::load_files as of the 0.20 release.Please see changelog for migration instructions: https://docs.osohq.com/project/changelogs/2021-09-15.html

Load a file containing Polar rules. All Polar files must end in .polar.

source

pub fn load_files<P: AsRef<Path>>(&mut self, filenames: Vec<P>) -> Result<()>

Load files containing Polar rules. All Polar files must end in .polar.

source

pub fn load_str(&mut self, src: &str) -> Result<()>

Load a string of polar source directly.

Examples
oso.load_str("allow(a, b, c) if true;");
source

pub fn query(&self, s: &str) -> Result<Query>

Query the knowledge base. This can be an allow query or any other polar expression.

Examples
oso.query("x = 1 or x = 2");
source

pub fn query_rule(&self, name: &str, args: impl ToPolarList) -> Result<Query>

Query the knowledge base but with a rule name and argument list. This allows you to pass in rust values.

Examples
oso.query_rule("is_admin", vec![User{name: "steve"}]);
source

pub fn register_class(&mut self, class: Class) -> Result<()>

Register a rust type as a Polar class. See [oso::Class] docs.

source

pub fn register_constant<V: ToPolar + Send + Sync>( &mut self, value: V, name: &str ) -> Result<()>

Register a rust type as a Polar constant. See [oso::Class] docs.

Trait Implementations§

source§

impl Clone for Oso

source§

fn clone(&self) -> Oso

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Default for Oso

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Oso

§

impl Send for Oso

§

impl Sync for Oso

§

impl Unpin for Oso

§

impl !UnwindSafe for Oso

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more