Skip to content

Commit

Permalink
Add Reset CTF button (#639)
Browse files Browse the repository at this point in the history
  • Loading branch information
ColdHeat committed Nov 9, 2018
1 parent 9e7d694 commit 95a33f9
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 12 deletions.
32 changes: 28 additions & 4 deletions CTFd/admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,34 @@

from CTFd.utils.decorators import admins_only
from CTFd.utils.user import is_admin
from CTFd.utils.security.auth import logout_user
from CTFd.utils.config import is_setup
from CTFd.utils import (
config as ctf_config,
validators,
uploads,
user as current_user,
get_config,
get_app_config,
set_config
set_config,
)
from CTFd.cache import cache, clear_config
from CTFd.utils.exports import (
export_ctf as export_ctf_util,
import_ctf as import_ctf_util
)
from CTFd.models import db, Configs, get_class_by_tablename

from CTFd.models import (
db,
get_class_by_tablename,
Users,
Teams,
Configs,
Submissions,
Solves,
Awards,
Unlocks,
Tracking
)
import datetime
import os
import six
Expand Down Expand Up @@ -162,5 +174,17 @@ def config():
def reset():
if request.method == 'POST':
# Truncate Users, Teams, Submissions, Solves, Notifications, Awards, Unlocks, Tracking
pass
Users.query.delete()
Teams.query.delete()
Submissions.query.delete()
Solves.query.delete()
Awards.query.delete()
Unlocks.query.delete()
set_config('setup', False)
db.session.commit()
cache.clear()
logout_user()
db.session.close()
return redirect(url_for('views.setup'))

return render_template('admin/reset.html')
54 changes: 54 additions & 0 deletions CTFd/themes/admin/templates/reset.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{% extends "admin/base.html" %}

{% block stylesheets %}
{% endblock %}

{% block content %}
<div class="jumbotron">
<div class="container">
<h1>Reset</h1>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-md-6 offset-md-3">
<form method="POST" id="reset-ctf-form">
<div class="alert alert-danger" role="alert">
<p>
Resetting your CTF will delete all user and team data. Think carefully before resetting because
no automated backups are made and all non-challenge data is lost.
</p>

<span>
<strong>
Create backups of all data you need by <a href="{{ url_for('admin.config', _anchor='backup') }}">creating a CTFd Export</a>
or by copying the database and CTFd source code folder.
</strong>
</span>
</div>

<input id="nonce" type="hidden" name="nonce" value="{{ nonce }}">

<button class="btn btn-warning btn-lg btn-block">
Reset CTF
</button>
</form>
</div>
</div>
</div>
{% endblock %}

{% block scripts %}
<script>
$('#reset-ctf-form').submit(function(e){
e.preventDefault();
ezq({
title: "Reset CTF?",
body: "Are you sure you want to reset your CTFd instance?",
success: function () {
$('#reset-ctf-form').off('submit').submit();
}
});
});
</script>
{% endblock %}
6 changes: 1 addition & 5 deletions CTFd/utils/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ def hide_scores():

@cache.memoize()
def is_setup():
setup = Configs.query.filter_by(key='setup').first()
if setup:
return setup.value
else:
return False
return get_config('setup')


@cache.memoize()
Expand Down
15 changes: 12 additions & 3 deletions CTFd/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from CTFd.utils import user as current_user
from CTFd.utils.dates import ctf_started, ctftime
from CTFd.utils.decorators import authed_only
from sqlalchemy.exc import IntegrityError
import os


Expand Down Expand Up @@ -97,9 +98,17 @@ def setup():

setup = set_config('setup', True)

db.session.add(page)
db.session.add(admin)
db.session.commit()
try:
db.session.add(admin)
db.session.commit()
except IntegrityError:
db.session.rollback()

try:
db.session.add(page)
db.session.commit()
except IntegrityError:
db.session.rollback()

login_user(admin)

Expand Down

0 comments on commit 95a33f9

Please sign in to comment.