async def delete_user(request, next_id): """Delete a specific user by next_id.""" log_request(request) env = Env() if not env.int("ENABLE_NEXT_BASE_USE"): raise ApiDisabled("Not a valid action. Source not enabled.") txn_list = [] txn_key, _ = await get_transactor_key(request) txn_list = await create_del_ownr_by_user_txns(txn_key, next_id, txn_list) txn_list = await create_del_admin_by_user_txns(txn_key, next_id, txn_list) txn_list = await create_del_mmbr_by_user_txns(txn_key, next_id, txn_list) txn_list = create_delete_user_txns(txn_key, next_id, txn_list) if txn_list: batch = batcher.make_batch_from_txns(transactions=txn_list, signer_keypair=txn_key) batch_list = batcher.batch_to_list(batch=batch) await send(request.app.config.VAL_CONN, batch_list, request.app.config.TIMEOUT) await reject_users_proposals(next_id, request) return json({ "message": "User {} successfully deleted".format(next_id), "deleted": 1 })
async def delete_user(request, next_id): """Delete a specific user by next_id.""" txn_list = [] txn_key, _ = await utils.get_transactor_key(request) txn_list = await create_delete_role_owner_txns(txn_key, next_id, txn_list) txn_list = await create_delete_role_admin_txns(txn_key, next_id, txn_list) txn_list = create_delete_user_txns(txn_key, next_id, txn_list) if txn_list: batch = batcher.make_batch_from_txns(transactions=txn_list, signer_keypair=txn_key) batch_list = batcher.batch_to_list(batch=batch) await utils.send(request.app.config.VAL_CONN, batch_list, request.app.config.TIMEOUT) await reject_users_proposals(next_id, request) conn = await create_connection() await roles_query.delete_role_member_by_next_id(conn, next_id) conn.close() return json({ "message": "User {} successfully deleted".format(next_id), "deleted": 1 })
def process(rec, database): """ Process inbound queue records """ try: if "batch" not in rec or not rec["batch"]: database.run_query( database.get_table("inbound_queue").get(rec["id"]).delete()) rec["sync_direction"] = "inbound" database.run_query(database.get_table("sync_errors").insert(rec)) return batch = batch_pb2.Batch() batch.ParseFromString(rec["batch"]) batch_list = batcher.batch_to_list(batch=batch) status = ClientSync().send_batches_get_status(batch_list=batch_list) if status[0]["status"] == "COMMITTED": if "metadata" in rec and rec["metadata"]: data = { "address": rec["address"], "object_type": rec["object_type"], "object_id": rec["object_id"], "provider_id": rec["provider_id"], "created_at": r.now(), "updated_at": r.now(), **rec["metadata"], } query = ( database.get_table("metadata").get( rec["address"]).replace(lambda doc: r.branch( # pylint: disable=singleton-comparison (doc == None), # noqa r.expr(data), doc.merge({ "metadata": rec["metadata"], "updated_at": r.now() }), ))) result = database.run_query(query) if (not result["inserted"] and not result["replaced"]) or result["errors"] > 0: LOGGER.warning("error updating metadata record:\n%s\n%s", result, query) rec["sync_direction"] = "inbound" database.run_query(database.get_table("changelog").insert(rec)) database.run_query( database.get_table("inbound_queue").get(rec["id"]).delete()) else: rec["error"] = get_status_error(status) rec["sync_direction"] = "inbound" database.run_query(database.get_table("sync_errors").insert(rec)) database.run_query( database.get_table("inbound_queue").get(rec["id"]).delete()) except Exception as err: # pylint: disable=broad-except LOGGER.exception("%s exception processing inbound record:\n%s", type(err).__name__, rec) LOGGER.exception(err)
def test_batch_to_list(self): """Test the make batch to list batch function""" payload, signer = self.get_test_payload() transaction = batcher.make_transaction(payload=payload, signer_keypair=signer) batch = batcher.make_batch(transaction=transaction) batch_list = batcher.batch_to_list(batch) self.assertValidBatchList( batch_list=batch_list, payload=payload, signer_public_key=signer.public_key, batcher_public_key=BATCHER_KEY_PAIR.public_key, )
def process(rec, conn): """ Process inbound queue records """ try: # Changes members from distinguished name to next_id for roles if "members" in rec["data"]: rec = translate_field_to_next(rec, "members") if "owners" in rec["data"]: rec = translate_field_to_next(rec, "owners") add_transaction(rec) if "batch" not in rec or not rec["batch"]: r.table("inbound_queue").get(rec["id"]).delete().run(conn) rec["sync_direction"] = "inbound" r.table("sync_errors").insert(rec).run(conn) return batch = batch_pb2.Batch() batch.ParseFromString(rec["batch"]) batch_list = batch_to_list(batch=batch) client = ClientSync() status = client.send_batches_get_status(batch_list=batch_list) while status[0]["status"] == "PENDING": LOGGER.info("Batch status is %s", status) status = client.status_recheck(batch_list) if status[0]["status"] == "COMMITTED": if rec["data_type"] == "user": insert_to_user_mapping(rec) if "metadata" in rec and rec["metadata"]: data = { "address": rec["address"], "object_type": rec["object_type"], "object_id": rec["object_id"], "provider_id": rec["provider_id"], "created_at": r.now(), "updated_at": r.now(), **rec["metadata"], } query = ( r.table("metadata").get( rec["address"]).replace(lambda doc: r.branch( # pylint: disable=singleton-comparison (doc == None), # noqa r.expr(data), doc.merge({ "metadata": rec["metadata"], "updated_at": r.now() }), ))) result = query.run(conn) if (not result["inserted"] and not result["replaced"]) or result["errors"] > 0: LOGGER.warning("error updating metadata record:\n%s\n%s", result, query) rec["sync_direction"] = "inbound" r.table("changelog").insert(rec).run(conn) r.table("inbound_queue").get(rec["id"]).delete().run(conn) else: rec["error"] = get_status_error(status) rec["sync_direction"] = "inbound" r.table("sync_errors").insert(rec).run(conn) r.table("inbound_queue").get(rec["id"]).delete().run(conn) except Exception as err: # pylint: disable=broad-except LOGGER.exception("%s exception processing inbound record:\n%s", type(err).__name__, rec) LOGGER.exception(err)
async def delete_role(request, role_id): """Delete a role by it's next_id. Args: role_id: str: the role_id field of the targeted role Returns: json: dict: { message: str: the status of the role delete operation deleted: int: count of the number of roles that were deleted } Raises: ApiForbidden: The user is not a system admin or owner of the targeted role. ApiNotFound: The role does not exist in RethinkDB. ApiInternalError: There was an error compiling blockchain transactions. """ txn_key, txn_user_id = await utils.get_transactor_key(request) # does the role exist? if not await roles_query.does_role_exist(request.app.config.DB_CONN, role_id): LOGGER.warning( "Nonexistent Role – User %s is attempting to delete the nonexistent role %s", txn_user_id, role_id, ) return await handle_not_found( request, ApiNotFound("The targeted role does not exist.") ) is_role_owner = await check_role_owner_status(txn_user_id, role_id) if not is_role_owner: is_admin = await check_admin_status(txn_user_id) if not is_admin: LOGGER.warning( "Permission Denied – User %s does not have sufficient privilege to delete role %s.", txn_user_id, role_id, ) return await handle_errors( request, ApiForbidden("You do not have permission to delete this role.") ) txn_list = [] txn_list = await create_rjct_ppsls_role_txns( txn_key, role_id, txn_user_id, txn_list ) txn_list = await create_del_admin_by_role_txns(txn_key, role_id, txn_list) txn_list = await create_del_mmbr_by_role_txns(txn_key, role_id, txn_list) txn_list = await create_del_ownr_by_role_txns(txn_key, role_id, txn_list) txn_list = create_del_role_txns(txn_key, role_id, txn_list) # validate transaction list if not txn_list: LOGGER.warning( "txn_list is empty. There was an error processing the delete role transactions. Transaction list: %s", txn_list, ) return await handle_errors( request, ApiInternalError( "An error occurred while creating the blockchain transactions to delete the role." ), ) batch = batcher.make_batch_from_txns(transactions=txn_list, signer_keypair=txn_key) batch_list = batcher.batch_to_list(batch=batch) await utils.send( request.app.config.VAL_CONN, batch_list, request.app.config.TIMEOUT ) return json( {"message": "Role {} successfully deleted".format(role_id), "deleted": 1} )