foca.security.access_control.foca_casbin_adapter package

Submodules

foca.security.access_control.foca_casbin_adapter.adapter module

Casbin rule adapter.

class foca.security.access_control.foca_casbin_adapter.adapter.Adapter(uri: str, dbname: str, collection: str | None = 'casbin_rule')

Bases: Adapter

Interface for casbin adapters. This is utilized for interacting casbin and mongodb.

Parameters:
  • uri – This should be the same requiement as pymongo Client’s ‘uri’ parameter. See https://pymongo.readthedocs.io/en/stable/api/pymong o/mongo_client.html#pymongo.mongo_client.MongoClient.

  • dbname – Database to store policy.

  • collection – Collection of the choosen database. Defaults to “casbin_rule”.

uri

This should be the same requiement as pymongo Client’s ‘uri’ parameter. See https://pymongo.readthedocs.io/en/stable/api/pymong o/mongo_client.html#pymongo.mongo_client.MongoClient.

dbname

Database to store policy.

collection

Collection of the choosen database. Defaults to “casbin_rule”.

add_policy(sec: str, ptype: str, rule: List[str]) bool

Add policy rules to mongodb

Parameters:
  • sec – Section corresponding which the rule will be added.

  • ptype – Policy type for which casbin rule will be added.

  • rule – Casbin rule list definition to be added.

Returns:

True if succeed else False.

load_policy(model: CasbinRule)

Implementing add Interface for casbin. Load all policy rules from mongodb

Parameters:

model – CasbinRule object.

remove_filtered_policy(sec: str, ptype: str, field_index: int, *field_values: List[str])
Remove policy rules that match the filter from the storage.

This is part of the Auto-Save feature.

Parameters:
  • sec – Section corresponding which the rule will be added.

  • ptype – Policy type for which casbin rule will be removed.

  • field_index – The policy index at which the field_values begin filtering. Its range is [0, 5]

  • field_values – A list of rules to filter policy.

Returns:

True if success.

remove_policy(sec: str, ptype: str, rule: List[str])

Remove policy rules from mongodb(duplicate rules are also removed).

Parameters:
  • sec – Section corresponding which the rule will be added.

  • ptype – Policy type for which casbin rule will be removed.

  • rule – Casbin rule list definition to be removed.

Returns:

Number of policies removed.

save_policy(model: Model) bool

Method to save a casbin model.

Parameters:

model – Casbin Model which loads from .conf file. For model description, cf. https://github.com/casbin/pycasbin/blob/72571 5fc04b3f37f26eb4be1ba7007ddf55d1e75/casbin/model/model.py#L23

Returns:

True if successfully created.

save_policy_line(ptype: str, rule: List[str])

Method to save a policy.

Parameters:
  • ptype – Policy type for the given rule based on the given conf file.

  • rule – List of policy attributes.

foca.security.access_control.foca_casbin_adapter.casbin_rule module

Casbin rule class.

class foca.security.access_control.foca_casbin_adapter.casbin_rule.CasbinRule(ptype: str | None = None, v0: str | None = None, v1: str | None = None, v2: str | None = None, v3: str | None = None, v4: str | None = None, v5: str | None = None)

Bases: object

This class defines the basic structuring of a casbin rule object.

Parameters:
  • ptype – Policy type for the given rule based on the given conf file.

  • v0 – Policy parameter.

  • v1 – Policy parameter.

  • v2 – Policy parameter.

  • v3 – Policy parameter.

  • v4 – Policy parameter.

  • v5 – Policy parameter.

ptype

Policy type for the given rule based on the given conf file.

v0

Policy parameter.

v1

Policy parameter.

v2

Policy parameter.

v3

Policy parameter.

v4

Policy parameter.

v5

Policy parameter.

dict() Dict

Method to convert params into casbin rule object.

Returns:

Casbin rule object.