Skip to content

Commit 7cd5fd2

Browse files
read only now shows text of challenges
1 parent e4c4685 commit 7cd5fd2

21 files changed

+316
-56
lines changed

CTFd/plugins/userchallenge/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from CTFd.plugins.userchallenge.api_calls import challenges, comments, attempts, files, flags, hints, tags, topics
2+
from CTFd.utils.logging import log
23
from flask import render_template,request,Blueprint, url_for, abort
34
from CTFd.plugins.challenges import CHALLENGE_CLASSES, get_chal_class
45
from CTFd.models import Challenges, Solves, Flags, db, Configs,Flags
@@ -155,12 +156,17 @@ def updateChallenge(challenge_id):
155156
)
156157

157158
update_j2 = render_template(
158-
challenge_class.templates["update"].lstrip("/admin/challenges/"), challenge=challenge
159+
challenge_class.templates["update"].lstrip("admin/challenges/"), challenge=challenge
159160
)
160161

162+
if isReadOnly():
163+
if type(update_j2) == str:
164+
update_j2 = update_j2.replace(" <div>\n\t\t<button class=\"btn btn-success btn-outlined float-right\" type=\"submit\">\n\t\t\tUpdate\n\t\t</button>\n\t</div>","")
165+
161166
update_script = url_for(
162167
"views.static_html", route=challenge_class.scripts["update"].lstrip("/admin/challenges/")
163168
)
169+
164170
return render_template(
165171
"editUserChallenge.html",
166172
update_template=update_j2,
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import "./main";
2+
import $ from "jquery";
3+
import "./compat/json";
4+
import "bootstrap/js/dist/tab";
5+
import CTFd from "./compat/CTFd";;
6+
import { ezQuery, ezAlert, ezToast } from "./compat/ezq";
7+
import { default as helpers } from "./compat/helpers";
8+
import { bindMarkdownEditors } from "./styles";
9+
import Vue from "vue";
10+
import CommentBox from "./components/comments/CommentBox.vue";
11+
12+
13+
function loadChalTemplate(challenge) {
14+
CTFd._internal.challenge = {};
15+
$.getScript(CTFd.config.urlRoot + challenge.scripts.view, function () {
16+
let template_data = challenge.create;
17+
$("#create-chal-entry-div").html(template_data);
18+
bindMarkdownEditors();
19+
20+
$.getScript(CTFd.config.urlRoot + challenge.scripts.create, function () {
21+
$("#create-chal-entry-div form").submit(function (event) {
22+
event.preventDefault();
23+
const params = $("#create-chal-entry-div form").serializeJSON();
24+
CTFd.fetch("/userchallenge/api/challenges/", {
25+
method: "POST",
26+
credentials: "same-origin",
27+
headers: {
28+
Accept: "application/json",
29+
"Content-Type": "application/json",
30+
},
31+
body: JSON.stringify(params),
32+
})
33+
.then(function (response) {
34+
return response.json();
35+
})
36+
.then(function (response) {
37+
if (response.success) {
38+
$("#challenge-create-options #challenge_id").val(
39+
response.data.id,
40+
);
41+
$("#challenge-create-options").modal();
42+
} else {
43+
let body = "";
44+
for (const k in response.errors) {
45+
body += response.errors[k].join("\n");
46+
body += "\n";
47+
}
48+
49+
ezAlert({
50+
title: "Error",
51+
body: body,
52+
button: "OK",
53+
});
54+
}
55+
});
56+
});
57+
});
58+
});
59+
}
60+
61+
$(() => {
62+
$(".preview-challenge").click(function (_e) {
63+
let url = `/userchallenge/challenges/preview/${window.CHALLENGE_ID}`;
64+
$("#challenge-window").html(
65+
`<iframe src="${url}" height="100%" width="100%" frameBorder=0></iframe>`,
66+
);
67+
$("#challenge-modal").modal();
68+
});
69+
70+
$(".comments-challenge").click(function (_event) {
71+
$("#challenge-comments-window").modal();
72+
});
73+
});
74+
75+
// Because this JS is shared by a few pages,
76+
// we should only insert the CommentBox if it's actually in use
77+
if (document.querySelector("#comment-box")) {
78+
// Insert CommentBox element
79+
const commentBox = Vue.extend(CommentBox);
80+
let vueContainer = document.createElement("div");
81+
document.querySelector("#comment-box").appendChild(vueContainer);
82+
new commentBox({
83+
propsData: { type: "challenge", id: window.CHALLENGE_ID },
84+
}).$mount(vueContainer);
85+
}
86+
87+
$.get("/userchallenge/api/challenges/types", function (res) {
88+
const data = res.data;
89+
loadChalTemplate(data["standard"]);
90+
91+
$("#create-chals-select input[name=type]").change(function () {
92+
let challenge = data[this.value];
93+
loadChalTemplate(challenge);
94+
});
95+
});

CTFd/plugins/userchallenge/package-lock.json

Lines changed: 130 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CTFd/plugins/userchallenge/staticAssets/assets/CommentBox-D18HLkQY.css

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)