admin.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import os
  2. from functools import wraps
  3. from flask import request
  4. from flask_restful import Resource, reqparse
  5. from werkzeug.exceptions import NotFound, Unauthorized
  6. from constants.languages import supported_language
  7. from controllers.console import api
  8. from controllers.console.wraps import only_edition_cloud
  9. from extensions.ext_database import db
  10. from models.model import App, InstalledApp, RecommendedApp
  11. def admin_required(view):
  12. @wraps(view)
  13. def decorated(*args, **kwargs):
  14. if not os.getenv('ADMIN_API_KEY'):
  15. raise Unauthorized('API key is invalid.')
  16. auth_header = request.headers.get('Authorization')
  17. if auth_header is None:
  18. raise Unauthorized('Authorization header is missing.')
  19. if ' ' not in auth_header:
  20. raise Unauthorized('Invalid Authorization header format. Expected \'Bearer <api-key>\' format.')
  21. auth_scheme, auth_token = auth_header.split(None, 1)
  22. auth_scheme = auth_scheme.lower()
  23. if auth_scheme != 'bearer':
  24. raise Unauthorized('Invalid Authorization header format. Expected \'Bearer <api-key>\' format.')
  25. if os.getenv('ADMIN_API_KEY') != auth_token:
  26. raise Unauthorized('API key is invalid.')
  27. return view(*args, **kwargs)
  28. return decorated
  29. class InsertExploreAppListApi(Resource):
  30. @only_edition_cloud
  31. @admin_required
  32. def post(self):
  33. parser = reqparse.RequestParser()
  34. parser.add_argument('app_id', type=str, required=True, nullable=False, location='json')
  35. parser.add_argument('desc', type=str, location='json')
  36. parser.add_argument('copyright', type=str, location='json')
  37. parser.add_argument('privacy_policy', type=str, location='json')
  38. parser.add_argument('language', type=supported_language, required=True, nullable=False, location='json')
  39. parser.add_argument('category', type=str, required=True, nullable=False, location='json')
  40. parser.add_argument('position', type=int, required=True, nullable=False, location='json')
  41. args = parser.parse_args()
  42. app = App.query.filter(App.id == args['app_id']).first()
  43. if not app:
  44. raise NotFound(f'App \'{args["app_id"]}\' is not found')
  45. site = app.site
  46. if not site:
  47. desc = args['desc'] if args['desc'] else ''
  48. copy_right = args['copyright'] if args['copyright'] else ''
  49. privacy_policy = args['privacy_policy'] if args['privacy_policy'] else ''
  50. else:
  51. desc = site.description if site.description else \
  52. args['desc'] if args['desc'] else ''
  53. copy_right = site.copyright if site.copyright else \
  54. args['copyright'] if args['copyright'] else ''
  55. privacy_policy = site.privacy_policy if site.privacy_policy else \
  56. args['privacy_policy'] if args['privacy_policy'] else ''
  57. recommended_app = RecommendedApp.query.filter(RecommendedApp.app_id == args['app_id']).first()
  58. if not recommended_app:
  59. recommended_app = RecommendedApp(
  60. app_id=app.id,
  61. description=desc,
  62. copyright=copy_right,
  63. privacy_policy=privacy_policy,
  64. language=args['language'],
  65. category=args['category'],
  66. position=args['position']
  67. )
  68. db.session.add(recommended_app)
  69. app.is_public = True
  70. db.session.commit()
  71. return {'result': 'success'}, 201
  72. else:
  73. recommended_app.description = desc
  74. recommended_app.copyright = copy_right
  75. recommended_app.privacy_policy = privacy_policy
  76. recommended_app.language = args['language']
  77. recommended_app.category = args['category']
  78. recommended_app.position = args['position']
  79. app.is_public = True
  80. db.session.commit()
  81. return {'result': 'success'}, 200
  82. class InsertExploreAppApi(Resource):
  83. @only_edition_cloud
  84. @admin_required
  85. def delete(self, app_id):
  86. recommended_app = RecommendedApp.query.filter(RecommendedApp.app_id == str(app_id)).first()
  87. if not recommended_app:
  88. return {'result': 'success'}, 204
  89. app = App.query.filter(App.id == recommended_app.app_id).first()
  90. if app:
  91. app.is_public = False
  92. installed_apps = InstalledApp.query.filter(
  93. InstalledApp.app_id == recommended_app.app_id,
  94. InstalledApp.tenant_id != InstalledApp.app_owner_tenant_id
  95. ).all()
  96. for installed_app in installed_apps:
  97. db.session.delete(installed_app)
  98. db.session.delete(recommended_app)
  99. db.session.commit()
  100. return {'result': 'success'}, 204
  101. api.add_resource(InsertExploreAppListApi, '/admin/insert-explore-apps')
  102. api.add_resource(InsertExploreAppApi, '/admin/insert-explore-apps/<uuid:app_id>')