authorization

Authorization is a role-based-access-control (RBAC) library, to find permission for the logged-user on a resource.

A resource can be a check box, button, or textarea in html page.

Permissions can be used to enable/disable/view/hide components such as checkbox, input text, buttons, ... in html pages.

mappings of user - roles - permissions

Following two mapping files are used; they are external; they can be edited in any text editor:
* user to roles mapping file (refer sampleuser-roles.json)
* for each resources, a role to permissions mapping is added. (refer sample
resource_permissions.json)

permissions

A resource can have one or more of the following permission type:
* C reate (also known as Add)
* R ead (aka View)
* U pdate (aka Edit)
* D elete (aka Remove)

explanations

{ "resource": "cb_enable", "description": "enable or disable this checkbox", "role2permissions": [ {"role": "viewer", "permission": "R", "condition": "{{resource_owner_id}} == {{session_user_id}}" }, {"role": "editor", "permission": "RU", "condition": "{{resource_owner_id}} == {{session_user_id}}" }, {"role": "admin", "permission": "CRUD" }] }

Note:
a. In the above JSON notation, for a cb_enable resource, three roles to permissions mapping are made.

b. First role is a viewer role, for which, permission is assigned as R, that means READ-ONLY permission, on a condition that only owner of the resource, can view this check box.

how to use it

Refer sample code below: ``` let mut authzn = Authorization::load("./sampleresourcepermissions.json", "./sampleuserroles.json");

let user_id = "1002";
authzn.set_permissions_for(user_id);

let resource = "cb_enable";
let owner_id = "1001";
let mut data = Map::new();

data.insert("resource_owner_id".to_string(), to_json( &owner_id ) );
data.insert("session_user_id".to_string(), to_json( &user_id) );    

let mut permitted = authzn.allows_add(&user_id, &resource, &data);
if permitted { println!("CREATE PERMITTED"); } else { println!("Create NOT Permitted");}

permitted = authzn.allows_view(&user_id, &resource, &data);
if permitted { println!("VIEW PERMITTED"); } else { println!("View NOT Permitted");}

permitted = authzn.allows_edit(&user_id, &resource, &data);
if permitted { println!("EDIT PERMITTED"); } else { println!("Edit NOT Permitted"); }

permitted = authzn.allows_delete(&user_id, &resource, &data);
if permitted { println!("DELETE PERMITTED"); } else { println!("Delete NOT Permitted"); }

```