Ejemplo n.º 1
0
def _role_already_exists(state_return, role_id):
    if not state_return:
        return False

    role_attr_container = role_state_pb2.RoleAttributesContainer()
    role_attr_container.ParseFromString(state_return[0].data)

    return is_in_role_attributes_container(container=role_attr_container,
                                           identifier=role_id)
Ejemplo n.º 2
0
def _handle_role_state_set(create_role, state):
    role_container = role_state_pb2.RoleAttributesContainer()
    role = role_container.role_attributes.add()
    role.role_id = create_role.role_id
    role.name = create_role.name
    role.metadata = create_role.metadata

    entries_to_set = [
        StateEntry(address=addresser.make_role_attributes_address(
            create_role.role_id),
                   data=role_container.SerializeToString())
    ]

    pubkeys_by_address = {}

    for admin in list(create_role.admins):
        admin_address = addresser.make_role_admins_address(
            role_id=create_role.role_id, user_id=admin)

        if admin_address in pubkeys_by_address:
            pubkeys_by_address[admin_address].append(admin)
        else:
            pubkeys_by_address[admin_address] = [admin]

    for owner in list(create_role.owners):
        owner_address = addresser.make_role_owners_address(
            role_id=create_role.role_id, user_id=owner)

        if owner_address in pubkeys_by_address:
            pubkeys_by_address[owner_address].append(owner)
        else:
            pubkeys_by_address[owner_address] = [owner]

    state_returns = get_state(state, [
        addresser.make_role_admins_address(role_id=create_role.role_id,
                                           user_id=a)
        for a in create_role.admins
    ] + [
        addresser.make_role_owners_address(role_id=create_role.role_id,
                                           user_id=o)
        for o in create_role.owners
    ])

    for addr, pubkeys in pubkeys_by_address.items():
        try:
            state_entry = get_state_entry(state_returns, addr)
            container = role_state_pb2.RoleRelationshipContainer()
            container.ParseFromString(state_entry.data)
        except KeyError:
            container = role_state_pb2.RoleRelationshipContainer()

        _add_role_rel_to_container(container, create_role.role_id, pubkeys)

        entries_to_set.append(
            StateEntry(address=addr, data=container.SerializeToString()))
    set_state(state, entries_to_set)
Ejemplo n.º 3
0
def return_role_container(entry):
    role_container = role_state_pb2.RoleAttributesContainer()
    role_container.ParseFromString(entry.data)
    return role_container