diff --git a/webadmin/fitcrackAPI/src/src/api/fitcrack/endpoints/hashlists/functions.py b/webadmin/fitcrackAPI/src/src/api/fitcrack/endpoints/hashlists/functions.py index 2aee3852c..e3ca14dff 100644 --- a/webadmin/fitcrackAPI/src/src/api/fitcrack/endpoints/hashlists/functions.py +++ b/webadmin/fitcrackAPI/src/src/api/fitcrack/endpoints/hashlists/functions.py @@ -159,7 +159,7 @@ def build_hash_query(args:reqparse.ParseResult,hash_list_id:int): return hash_query -def acquire_hash_list(id:str,name:Optional[str]) -> FcHashList: +def acquire_hash_list(id:str,name:Optional[str]=None) -> FcHashList: """ Common function for all "add hashes to hash list" endpoints. Given an id, returns an FcHashList object. diff --git a/webadmin/fitcrackAPI/src/src/api/fitcrack/endpoints/hashlists/hashlists.py b/webadmin/fitcrackAPI/src/src/api/fitcrack/endpoints/hashlists/hashlists.py index 2bb17fa1b..70b2bc938 100644 --- a/webadmin/fitcrackAPI/src/src/api/fitcrack/endpoints/hashlists/hashlists.py +++ b/webadmin/fitcrackAPI/src/src/api/fitcrack/endpoints/hashlists/hashlists.py @@ -8,12 +8,14 @@ from flask import request, send_file from flask_restx import Resource, abort +from flask_login import current_user from src.api.apiConfig import api from src.api.fitcrack.endpoints.hashlists.argumentsParser import make_empty_hash_list_parser, hash_list_parser, hash_list_add_hash_list_parser, hash_list_add_hash_file_parser, hash_list_hashes_parser_paginated, hash_list_hashes_parser, hash_list_add_protected_file_parser from src.api.fitcrack.endpoints.hashlists.responseModels import empty_hash_list_created_model, page_of_hash_lists_model, hash_addition_result_model, page_of_hashes_model, hash_list_model_long from src.api.fitcrack.endpoints.hashlists.functions import upload_hash_list, build_hash_query, acquire_hash_list from src.api.fitcrack.endpoints.protectedFile.functions import addProtectedFile +from src.api.fitcrack.endpoints.job.functions import editable_jobs_ids, kill_job from src.database import db from src.database.models import FcHashList, FcHash @@ -107,6 +109,7 @@ def delete(self, id): db.session.commit() return None, 204 + @ns.route('//details') class HashListHashes(Resource): @api.expect(hash_list_hashes_parser_paginated) @@ -129,6 +132,7 @@ def get(self,id:int): return hash_page + @ns.route('//download') class exportHashes(Resource): @api.expect(hash_list_hashes_parser) @@ -159,6 +163,7 @@ def get(self, id:int): filename = hash_list.name + ".txt" return send_file(hash_list_file, attachment_filename=filename, as_attachment=True, mimetype="text/plain") + @ns.route('//hashes') class hashListUploadList(Resource): @@ -230,3 +235,35 @@ def post(self,id:str): result = addProtectedFile(args.file) return upload_hash_list([result['hash']],hash_list,int(result['hash_type']),'fail_invalid',False) + + +@ns.route('//purge') +class hashListPurge(Resource): + @api.response(200, 'Hash list purged.') + @api.response(403, 'Hash list contains jobs that you do not have rights to; cannot perform purge.') + def post(self,id:int): + """ + Removes all cracked hashes from the hash list; it will be as if the hash list was created anew. + This also kills all jobs that are attached to the hash list. + This endpoint does check that the user has rights to all jobs that are part of the hash list. + """ + hash_list = acquire_hash_list(id) + jobs = hash_list.jobs + if not current_user.role.EDIT_ALL_JOBS: #Logic taken from job/multiJobOperation endpoint. + editable = editable_jobs_ids() + if not {job.id for job in jobs} <= set(editable): + abort(403, 'Hash list contains jobs that you do not have rights to; cannot perform purge.') + + for job in jobs: + kill_job(job,db) + + for job_hash in hash_list.hashes: + job_hash.result = None + job_hash.time_cracked = None + + try: + db.session.commit() + except: + return 'Something went wrong.', 500 + + return 'Hash list purged', 200 diff --git a/webadmin/fitcrackAPI/src/src/api/fitcrack/endpoints/job/functions.py b/webadmin/fitcrackAPI/src/src/api/fitcrack/endpoints/job/functions.py index 683f1be10..67c29a20e 100644 --- a/webadmin/fitcrackAPI/src/src/api/fitcrack/endpoints/job/functions.py +++ b/webadmin/fitcrackAPI/src/src/api/fitcrack/endpoints/job/functions.py @@ -33,7 +33,7 @@ def stop_job(job): job.status = status_to_code['finishing'] -def kill_job(job, db): +def kill_job(job:FcJob, db): id = job.id # Job is stopped in Generator after sending BOINC commands if (int(job.status) != status_to_code['running']) and (int(job.status) != status_to_code['finishing']): @@ -70,9 +70,6 @@ def kill_job(job, db): for item in graphData: db.session.delete(item) - for job_hash in job.hash_list.hashes: - job_hash.result = None - job_hash.time_cracked = None def start_job(job, db): hosts = [ a[0] for a in db.session.query(Host.id).all() ]