---
# System prepended metadata

title: Roles

---

# Roles

### Cliff notes
Kizuna role need
- pin message
- add member
- update group name
- update group avatar
- remove member
- send message
- react message
- reply to message

## role creation general flow
![](https://i.imgur.com/cTib13a.png)



## Entry Structure
```rust=
pub struct RoleImage(SerializedBytes)
pub struct Role {
     role_name: String,
     image: EntryHash,
     role_color: String,
}
pub struct Permission {
    zome_name: ZomeName,
    zome_id: ZomeId,
    function_name: FunctionName,
}
```

## ERD
```mermaid
graph TD
  subgraph group_zome
    subgraph path
        all_roles_path
        all_permissions_path
        all_permissions_path-->group_zome_path
        all_permissions_path-->p2p_zome_path
    end
    subgraph role_entry
        all_roles_path-->admin_role
        all_roles_path-->guest_role
        all_roles_path-->everyone_role
    end
    subgraph role_image_entry
        admin_role.->admin_role_image
        guest_role.->guest_role_image
        admin_role_image-->admin_role_image_v2
        admin_role_image-->admin_role_image_v3
    end
    subgraph permissions
        group_zome_path-->add_member_permission
        group_zome_path-->create_role_permission
        p2p_zome_path-->pin_message_permission
        admin_role.->add_member_permission
        admin_role.->create_role_permission
    end
    subgraph agents
      alice-->|has_role|admin_role
      bobby-->|has_role|guest_role
      diego-->|has_role|everyone_role
      admin_role-->|holder|alice
      guest_role-->|holder|bobby
      everyone_role-->|holder|diego
    end
    
  end
```

## Zome Fn
### create_role
```rust
struct CreateRoleInput {
    role_name: String,
    image: String, // EntryHash of RoleImage
    role_color: String,
    permissions: BTreeMap<String, Permission>
}
pub fn create_role(input: CreateRoleInput) -> ExternResult<Role>
```
```mermaid
sequenceDiagram

participant AUI as Alice_UI
participant ACD as Alice_Cell
participant ADHT as DHT

	AUI-->>ACD: input
    ACD-->>ADHT: get permissions to make sure they exist
    ACD-->>ADHT: create role image entry
	ACD-->>ADHT: create role entry
	ACD-->>AUI: return Role
```
### update_role
```rust
struct UpdateRoleInput {
    role_name: Option<String>,
    image: Option<String>,
    role_color: Option<String>,
    permissions: BTreeMap<String, Permission>
}
pub fn update_role(input: UpdateRoleInput) -> ExternResult<Role>
```
```mermaid
sequenceDiagram

participant AUI as Alice_UI
participant ACD as Alice_Cell
participant ADHT as DHT

	AUI-->>ACD: update input
    opt if image is Some
        ACD-->>ADHT: update role image entry
	    ACD-->>ADHT: update role entry
    end
    ACD-->>ADHT: get permissions to make sure they exist
	ACD-->>ADHT: update role entry
	ACD-->>AUI: return Role
```
### assign_role
```rust
struct AssignRoleInput {
    role_id: String // EntryHash of Role
    assignee: Vec<AgentPubKey>
}
pub fn assign_role(input: AssignRoleInput) -> ExternResult<bool> 
```
```mermaid
sequenceDiagram

participant AUI as Alice_UI
participant ACD as Alice_Cell
participant ADHT as DHT

	AUI-->>ACD: input
    ACD-->>ADHT: get the role to make sure they exist
    loop for all agents
    ACD-->>ADHT: create link base agent and target role
    ACD-->>ADHT: create link base role and target agent
    end    
	ACD-->>AUI: return true
```
### deassign_role
```rust
struct DeassignRoleInput {
    role_id: String // EntryHash of Role
    assignee: Vec<AgentPubKey>
}
pub fn deassign_role(input: DeassignRoleInput) -> ExternResult<bool> 
```
```mermaid
sequenceDiagram

participant AUI as Alice_UI
participant ACD as Alice_Cell
participant ADHT as DHT

	AUI-->>ACD: input
    ACD-->>ADHT: get the role to make sure they exist
    loop for all agents
    ACD-->>ADHT: get links base agent and target role
    ACD-->>ADHT: remove link base role and target agent
    ACD-->>ADHT: remove link base agent and target role
    end    
	ACD-->>AUI: return true
```
### delete_role(?)
```rust
struct DeleteRoleInput {
    role_id: String, // EntryHash of Role
}
pub fn delete_role(input: DeleteRoleInput) -> ExternResult<bool>
```
```mermaid
sequenceDiagram

participant AUI as Alice_UI
participant ACD as Alice_Cell
participant ADHT as DHT

	AUI-->>ACD: input
    ACD-->>ADHT: get the role to make sure they exist
    ACD-->>ADHT: get links "has_role"
    loop for all links
    ACD-->>ADHT: remove links base agent and target role
    ACD-->>ADHT: remove link base role and target agent
    end
    ACD-->>ADHT: remove path link
    ACD-->>ADHT: remove entry role
	ACD-->>AUI: return true
```
### get_all_roles
```rust
pub fn list_all_roles(_: ()) -> ExternResult<Vec<Role>>
```
```mermaid
sequenceDiagram

participant AUI as Alice_UI
participant ACD as Alice_Cell
participant ADHT as DHT

	AUI-->>ACD: input
    ACD-->>ADHT: get links from all roles path
    loop for all links
        ACD-->>ADHT: get role entry
    end
	ACD-->>AUI: return Roles
```
### get_all_member_roles
```rust
struct RoleWithMembers {
    role_name: String,
    image: EntryHash,
    role_color: String,
    members: Vec<AgentPubKey>
}
pub fn list_all_member_roles(_: ()) -> ExternResult<HashMap<String, RoleWithMembers>>
```
```mermaid
sequenceDiagram

participant AUI as Alice_UI
participant ACD as Alice_Cell
participant ADHT as DHT

    ACD-->>ADHT: get links from all roles path
    loop for all links
        ACD-->>ADHT: get role entry
    end
    loop for each Role
	    ACD-->>ADHT: get links "holder"
    end
    ACD-->>ACD: construct return value
    ACD-->>AUI: return HashMap
```
### get_my_roles
```rust
pub fn get_my_roles(_: ()) -> ExternResult<Vec<Role>>
```
```mermaid
sequenceDiagram

participant AUI as Alice_UI
participant ACD as Alice_Cell
participant ADHT as DHT

    ACD-->>ADHT: get links base AgentPubKey "has_role"
    loop for all links
    ACD-->>ADHT: get Role entry
    end
    ACD-->>ACD: construct return value
    ACD-->>AUI: return Roles
```
### create_permissions
```rust
pub struct Permission {
    zome_name: String,
    function_name: String,
}
pub fn create_permission(input: Vec<Permission>) -> ExternResult<Vec<Permission>>
```
```mermaid
sequenceDiagram

participant AUI as Alice_UI
participant ACD as Alice_Cell
participant ADHT as DHT

    AUI-->>ACD: input
    ACD-->>ADHT: create all permissions path
    ACD-->>ADHT: create all zome permission path as children
    loop for all permissions input
        ACD-->>ADHT: create Permission entry
        ACD-->>ADHT: create link Permission entry zome path
    end
    ACD-->>AUI: return vector Permission
```
### list_all_permissions
```rust
struct PermissionQueryInput {
    zome_id: Option<ZomeId>
}
pub fn list_all_permissions(input: PermissionQueryInput -> ExternResult<Vec<Permission>>
```
```mermaid
sequenceDiagram

participant AUI as Alice_UI
participant ACD as Alice_Cell
participant ADHT as DHT

    ACD-->>ADHT: get links from all permissions path
    alt zome_id in input is None
        loop for each zome path
            ACD-->>ADHT: get all links
            loop for each link
                ACD-->>ADHT: get Permission entry
            end
        end
    else zome_id in input is !None
        ACD-->>ADHT: get all links for the zome id given
        loop for each link
            ACD-->>ADHT: get Permission entry
        end
    end
    ACD-->>ADHT: construct return value
    ACD-->>AUI: return vector Permission
```