async def add_role_member(request, role_id):
    """Add a member to a role."""
    required_fields = ["id"]
    utils.validate_fields(required_fields, request.json)
    txn_key, txn_user_id = await utils.get_transactor_key(request)
    proposal_id = str(uuid4())
    batch_list = Role().member.propose.batch_list(
        signer_keypair=txn_key,
        signer_user_id=txn_user_id,
        proposal_id=proposal_id,
        role_id=role_id,
        pack_id=request.json.get("pack_id"),
        next_id=request.json.get("id"),
        reason=request.json.get("reason"),
        metadata=request.json.get("metadata"),
    )
    batch_status = await utils.send(
        request.app.config.VAL_CONN,
        batch_list,
        request.app.config.TIMEOUT,
        request.json.get("tracker") and True,
    )
    if request.json.get("tracker"):
        return utils.create_tracker_response("batch_status", batch_status)
    return json({"proposal_id": proposal_id})
예제 #2
0
async def add_role_member(request, role_id):
    """Add a member to a role."""
    required_fields = ["id"]
    utils.validate_fields(required_fields, request.json)
    txn_key, txn_user_id = await utils.get_transactor_key(request)
    proposal_id = str(uuid4())
    conn = await create_connection()
    approver = await fetch_relationships("role_owners", "role_id", role_id).run(conn)
    conn.close()
    batch_list = Role().member.propose.batch_list(
        signer_keypair=txn_key,
        signer_user_id=txn_user_id,
        proposal_id=proposal_id,
        role_id=role_id,
        pack_id=request.json.get("pack_id"),
        next_id=request.json.get("id"),
        reason=request.json.get("reason"),
        metadata=request.json.get("metadata"),
        assigned_approver=approver,
    )
    batch_status = await utils.send(
        request.app.config.VAL_CONN,
        batch_list,
        request.app.config.TIMEOUT,
        request.json.get("tracker") and True,
    )
    conn = await create_connection()
    role_resource = await roles_query.fetch_role_resource(conn, role_id)
    owners = role_resource.get("owners")
    conn.close()
    requester_id = request.json.get("id")
    if requester_id in owners:
        request.json["status"] = "APPROVED"
        request.json["reason"] = "I am the owner of this role"
        await proposals.update_proposal(request, proposal_id)
        return json(
            {
                "message": "Owner is the requester. Proposal is autoapproved",
                "proposal_id": proposal_id,
            }
        )
    if request.json.get("tracker"):
        return utils.create_tracker_response("batch_status", batch_status)
    return json({"proposal_id": proposal_id})
예제 #3
0
async def add_role_member(request, role_id):
    """Add a member to a role."""
    required_fields = ["id"]
    utils.validate_fields(required_fields, request.json)
    txn_key, txn_user_id = await utils.get_transactor_key(request)
    proposal_id = str(uuid4())
    approver = await fetch_relationships("role_owners", "role_id", role_id).run(
        request.app.config.DB_CONN
    )
    batch_list = Role().member.propose.batch_list(
        signer_keypair=txn_key,
        signer_user_id=txn_user_id,
        proposal_id=proposal_id,
        role_id=role_id,
        pack_id=request.json.get("pack_id"),
        next_id=request.json.get("id"),
        reason=request.json.get("reason"),
        metadata=request.json.get("metadata"),
        assigned_approver=approver,
    )
    batch_status = await utils.send(
        request.app.config.VAL_CONN,
        batch_list,
        request.app.config.TIMEOUT,
        request.json.get("tracker") and True,
    )
    role_resource = await roles_query.fetch_role_resource(
        request.app.config.DB_CONN, role_id
    )
    owners = role_resource.get("owners")
    requester_id = request.json.get("id")
    if requester_id in owners:
        is_proposal_ready = await wait_for_resource_in_db(
            "proposals", "proposal_id", proposal_id, max_attempts=30
        )
        if not is_proposal_ready:
            LOGGER.warning(
                "Max attempts exceeded. Proposal %s not found in RethinkDB.",
                proposal_id,
            )
            return await handle_errors(
                request,
                ApiInternalError(
                    "Max attempts exceeded. Proposal %s not found in RethinkDB."
                    % proposal_id
                ),
            )
        request.json["status"] = "APPROVED"
        request.json["reason"] = "I am the owner of this role"
        await proposals.update_proposal(request, proposal_id)
        if request.json.get("tracker"):
            events = {"batch_status": batch_status, "member_status": "MEMBER"}
            return utils.create_tracker_response(events)
        return json(
            {
                "message": "Owner is the requester. Proposal is autoapproved",
                "proposal_id": proposal_id,
            }
        )
    if request.json.get("tracker"):
        events = {"batch_status": batch_status}
        if batch_status == 1:
            events["member_status"] = "PENDING"
        return utils.create_tracker_response(events)
    return json({"proposal_id": proposal_id})