mirror of
https://github.com/aljazceru/CTFd.git
synced 2025-12-17 22:14:25 +01:00
Reimplement admin send mail to users (#903)
* Reimplement admin send mail to users as `/api/v1/users/<user_id>/email` * Update form and related Javascript * Write test for controller * Closes #897
This commit is contained in:
@@ -4,9 +4,12 @@ from CTFd.models import db, Users, Solves, Awards, Fails, Tracking, Unlocks, Sub
|
||||
from CTFd.utils.decorators import (
|
||||
authed_only,
|
||||
admins_only,
|
||||
authed
|
||||
authed,
|
||||
ratelimit
|
||||
)
|
||||
from CTFd.cache import cache, clear_standings
|
||||
from CTFd.utils.config import get_mail_provider
|
||||
from CTFd.utils.email import sendmail
|
||||
from CTFd.utils.user import get_current_user, is_admin
|
||||
from CTFd.utils.decorators.visibility import check_account_visibility, check_score_visibility
|
||||
|
||||
@@ -280,3 +283,44 @@ class UserAwards(Resource):
|
||||
'success': True,
|
||||
'data': response.data
|
||||
}
|
||||
|
||||
|
||||
@users_namespace.route('/<int:user_id>/email')
|
||||
@users_namespace.param('user_id', "User ID")
|
||||
class UserEmails(Resource):
|
||||
@admins_only
|
||||
@ratelimit(method="POST", limit=10, interval=60)
|
||||
def post(self, user_id):
|
||||
req = request.get_json()
|
||||
text = req.get('text', '').strip()
|
||||
user = Users.query.filter_by(id=user_id).first_or_404()
|
||||
|
||||
if get_mail_provider() is None:
|
||||
return {
|
||||
'success': False,
|
||||
'errors': {
|
||||
"": [
|
||||
"Email settings not configured"
|
||||
]
|
||||
}
|
||||
}, 400
|
||||
|
||||
if not text:
|
||||
return {
|
||||
'success': False,
|
||||
'errors': {
|
||||
"text": [
|
||||
"Email text cannot be empty"
|
||||
]
|
||||
}
|
||||
}, 400
|
||||
|
||||
result, response = sendmail(
|
||||
addr=user.email,
|
||||
text=text
|
||||
)
|
||||
|
||||
return {
|
||||
'success': result,
|
||||
'data': {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user