Nav apraksta

manage_attributes_routes.py 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # IRIS Source Code
  2. # Copyright (C) 2021 - Airbus CyberSecurity (SAS)
  3. # ir@cyberactionlab.net
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU Lesser General Public
  7. # License as published by the Free Software Foundation; either
  8. # version 3 of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. # Lesser General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Lesser General Public License
  16. # along with this program; if not, write to the Free Software Foundation,
  17. # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. import json
  19. from flask import Blueprint
  20. from flask import redirect
  21. from flask import render_template
  22. from flask import request
  23. from flask import url_for
  24. from app.forms import AddAssetForm
  25. from app.forms import AttributeForm
  26. from app.models.authorization import Permissions
  27. from app.models.models import CustomAttribute
  28. from app.blueprints.access_controls import ac_requires
  29. from app.blueprints.responses import response_error
  30. manage_attributes_blueprint = Blueprint('manage_attributes', __name__, template_folder='templates')
  31. @manage_attributes_blueprint.route('/manage/attributes')
  32. @ac_requires(Permissions.server_administrator, no_cid_required=True)
  33. def manage_attributes(caseid, url_redir):
  34. if url_redir:
  35. return redirect(url_for('manage_attributes.manage_attributes', cid=caseid))
  36. form = AddAssetForm()
  37. return render_template('manage_attributes.html', form=form)
  38. @manage_attributes_blueprint.route('/manage/attributes/<int:cur_id>/modal', methods=['GET'])
  39. @ac_requires(Permissions.server_administrator, no_cid_required=True)
  40. def attributes_modal(cur_id, caseid, url_redir):
  41. if url_redir:
  42. return redirect(url_for('manage_attributes.manage_attributes', cid=caseid))
  43. form = AttributeForm()
  44. attribute = CustomAttribute.query.filter(CustomAttribute.attribute_id == cur_id).first()
  45. if not attribute:
  46. return response_error(f'Invalid Attribute ID {cur_id}')
  47. form.attribute_content.data = attribute.attribute_content
  48. return render_template('modal_add_attribute.html', form=form, attribute=attribute)
  49. # TODO this endpoint should probably be a GET
  50. @manage_attributes_blueprint.route('/manage/attributes/preview', methods=['POST'])
  51. @ac_requires(Permissions.server_administrator, no_cid_required=True)
  52. def attributes_preview(caseid, url_redir):
  53. if url_redir:
  54. return redirect(url_for('manage_attributes.manage_attributes', cid=caseid))
  55. data = request.get_json()
  56. if not data:
  57. return response_error('Invalid request')
  58. attribute = data.get('attribute_content')
  59. if not attribute:
  60. return response_error('Invalid request')
  61. try:
  62. attribute = json.loads(attribute)
  63. except Exception as e:
  64. return response_error('Invalid JSON', data=str(e))
  65. return render_template('modal_preview_attribute.html', attributes=attribute)