Ejemplo n.º 1
0
def validate_task_rel_proposal(header, propose, rel_address, state):
    """Validates that the User exists, the Task exists, and the User is not
    in the Task's relationship specified by rel_address.

    Args:
        header (TransactionHeader): The transaction header.
        propose (ProposeAddTask_____): The Task relationship proposal.
        rel_address (str): The Task relationship address produced by the Task
            and the User.
        state (sawtooth_sdk.Context): The way to communicate to the validator
            the state gets and sets.

    Returns:
        (dict of addresses)
    """

    user_address = addresser.make_user_address(propose.user_id)
    task_address = addresser.make_task_attributes_address(propose.task_id)
    proposal_address = addresser.make_proposal_address(
        object_id=propose.task_id,
        related_id=propose.user_id)

    state_entries = get_state(state, [user_address,
                                      task_address,
                                      proposal_address,
                                      rel_address])
    validate_identifier_is_user(state_entries,
                                identifier=propose.user_id,
                                address=user_address)
    user_entry = get_state_entry(state_entries, user_address)
    user = get_user_from_container(
        return_user_container(user_entry),
        propose.user_id)

    if header.signer_public_key not in [user.user_id, user.manager_id]:
        raise InvalidTransaction(
            "Txn signer {} is not the user or the user's "
            "manager {}".format(header.signer_public_key,
                                [user.user_id, user.manager_id]))

    validate_identifier_is_task(state_entries,
                                identifier=propose.task_id,
                                address=task_address)

    try:
        task_admins_entry = get_state_entry(state_entries, rel_address)
        task_rel_container = return_task_rel_container(task_admins_entry)
        if is_in_task_rel_container(
                task_rel_container,
                propose.task_id,
                propose.user_id):
            raise InvalidTransaction(
                "User {} is already in the Role {} "
                "relationship".format(propose.user_id,
                                      propose.task_id))
    except KeyError:
        # The task rel container doesn't exist so no task relationship exists
        pass

    return state_entries
def validate_task_rel_del_proposal(header, propose, rel_address, state):
    """Validates that the User exists, the Task exists, and the User is in
    the Tasks's relationship specified by the rel_address.

    Args:
        header (TransactionHeader): The transaction header.
        propose (ProposeRemoveTask____): The Task Remove relationship proposal
        rel_address (str): The task relationship address.
        state (Context:: The way to communicate to the validator State gets
            and sets.

    Returns:
        (list of StateEntry)
    """

    user_address = addresser.make_user_address(propose.user_id)
    task_address = addresser.make_task_attributes_address(propose.task_id)

    proposal_address = addresser.make_proposal_address(
        object_id=propose.task_id, related_id=propose.user_id)

    state_entries = get_state(
        state, [user_address, task_address, proposal_address, rel_address])

    validate_identifier_is_user(state_entries,
                                identifier=propose.user_id,
                                address=user_address)

    user_entry = get_state_entry(state_entries, user_address)

    user = get_user_from_container(return_user_container(user_entry),
                                   propose.user_id)

    if header.signer_pubkey not in [user.user_id, user.manager_id]:
        raise InvalidTransaction(
            "Txn signer {} is not the user {} or the user's manager {}".format(
                header.signer_pubkey, user.user_id, user.manager_id))

    validate_identifier_is_task(state_entries,
                                identifier=propose.task_id,
                                address=task_address)

    try:
        task_rel_entry = get_state_entry(state_entries, rel_address)
        task_rel_container = return_task_rel_container(task_rel_entry)
        if not is_in_task_rel_container(task_rel_container, propose.task_id,
                                        propose.user_id):
            raise InvalidTransaction("User {} isn't in the Task {} "
                                     "relationship".format(
                                         propose.user_id, propose.task_id))
    except KeyError:
        raise InvalidTransaction(
            "User {} isn't in the Task {} relationship, "
            "since there isn't a container at the address".format(
                propose.user_id, propose.task_id))

    return state_entries
Ejemplo n.º 3
0
def _validate_state_and_return_user(header, user_proposal, state):
    """Validate that 1. There is no other open proposal for the manager change
    2. The user is a User 3. the manager is a User 4. The manager is the
    signer of the transaction.

    Args:
        header (TransactionHeader): The transaction header.
        user_proposal (ProposeUpdateUserManager): The transaction that makes
            the proposal to update the user's manager.
        state (Context): The way to set and get values from state.

    """

    prop_state_entries = _validate_unique_proposal(
        header,
        user_proposal,
        state)

    user_address = addresser.make_user_address(
        user_id=user_proposal.user_id)
    user_state_entries = get_state(
        state,
        [user_address])
    validate_identifier_is_user(
        state_entries=user_state_entries,
        identifier=user_proposal.user_id,
        address=user_address)

    manager_address = addresser.make_user_address(
        user_id=user_proposal.new_manager_id)

    manager_state_entries = get_state(
        state,
        [manager_address])

    validate_identifier_is_user(
        state_entries=manager_state_entries,
        identifier=user_proposal.new_manager_id,
        address=manager_address)

    user_state_entry = get_state_entry(
        user_state_entries,
        user_address)

    user_container = return_user_container(user_state_entry)

    _validate_manager_is_signer(header, user_container, user_proposal.user_id)

    return prop_state_entries