Skip to content

Commit f0795fa

Browse files
authored
cleanup panel/account.php (#201)
* cleanup code * Custom not custom
1 parent c749241 commit f0795fa

File tree

2 files changed

+42
-57
lines changed

2 files changed

+42
-57
lines changed

test/functional/LoginShellSetTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function testSetLoginShellCustom(string $shell): void
3939
// FIXME shell is not validated
4040
post(
4141
__DIR__ . "/../../webroot/panel/account.php",
42-
["form_type" => "loginshell", "shellSelect" => "custom", "shell" => $shell]
42+
["form_type" => "loginshell", "shellSelect" => "Custom", "shell" => $shell]
4343
);
4444
$this->assertEquals($shell, $USER->getLoginShell());
4545
}

webroot/panel/account.php

Lines changed: 41 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
$USER->setSSHKeys($keys, $OPERATOR); // Update user keys
7070
break;
7171
case "loginshell":
72-
if ($_POST["shellSelect"] == "custom") {
72+
if ($_POST["shellSelect"] == "Custom") {
7373
$USER->setLoginShell($_POST["shell"], $OPERATOR);
7474
} else {
7575
$USER->setLoginShell($_POST["shellSelect"], $OPERATOR);
@@ -186,49 +186,28 @@
186186
<hr>
187187

188188
<form action="" method="POST">
189-
190-
<input type="hidden" name="form_type" value="loginshell">
191-
192-
<select id="loginSelector" name= "shellSelect">
193-
194-
<option value="" disabled hidden>Select Login Shell...</option>
195-
196-
<?php
197-
$cur_shell = $USER->getLoginShell();
198-
$found_selector = false;
199-
foreach ($CONFIG["loginshell"]["shell"] as $shell) {
200-
if ($cur_shell == $shell) {
201-
echo "<option selected>$shell</option>";
202-
$found_selector = true;
203-
} else {
204-
echo "<option>$shell</option>";
205-
}
206-
}
207-
208-
if ($found_selector) {
209-
echo "<option value='custom'>Custom</option>";
210-
} else {
211-
echo "<option value='custom' selected>Custom</option>";
212-
}
213-
?>
214-
</select>
215-
216-
<?php
217-
218-
if ($found_selector) {
219-
echo "<input id='customLoginBox' type='text'
220-
placeholder='Enter login shell path (ie. /bin/bash)' name='shell'>";
221-
} else {
222-
echo "<input id='customLoginBox' type='text'
223-
placeholder='Enter login shell path (ie. /bin/bash)' name='shell' value='$cur_shell'>";
224-
}
225-
226-
?>
227-
<br>
228-
<input type='submit' value='Set Login Shell'>
229-
189+
<input type="hidden" name="form_type" value="loginshell">
190+
<select id="loginSelector" name="shellSelect">
191+
<?php
192+
foreach ($CONFIG["loginshell"]["shell"] as $shell) {
193+
echo "<option>$shell</option>";
194+
}
195+
echo "<option id='customLoginSelectorOption'>Custom</option>";
196+
?>
197+
</select>
198+
<?php
199+
echo "
200+
<input
201+
id='customLoginBox'
202+
type='text'
203+
placeholder='Enter login shell path (ie. /bin/bash)'
204+
name='shell'
205+
>
206+
";
207+
?>
208+
<br>
209+
<input id='submitLoginShell' type='submit' value='Set Login Shell'>
230210
</form>
231-
232211
<hr>
233212

234213
<h5>Account Deletion</h5>
@@ -257,30 +236,36 @@
257236

258237

259238
<script>
239+
const sitePrefix = '<?php echo $CONFIG["site"]["prefix"]; ?>';
240+
const ldapLoginShell = '<?php echo $USER->getLoginShell(); ?>';
241+
260242
$("button.btnAddKey").click(function() {
261-
openModal("Add New Key", "<?php echo $CONFIG["site"]["prefix"]; ?>/panel/modal/new_key.php");
243+
openModal("Add New Key", `${sitePrefix}/panel/modal/new_key.php`);
262244
});
263245

264-
var customLoginBox = $("#customLoginBox");
265-
if (customLoginBox.val() == "") {
266-
// login box is empty, so we hide it by default
267-
// if the login box had a value, that means it would be a custom shell
268-
// and should not hide by default
269-
customLoginBox.hide();
246+
var defaultShellSelected = false;
247+
$("#loginSelector option").each(function(i, e) {
248+
if ($(this).val() == ldapLoginShell) {
249+
$(this).prop("selected", true);
250+
defaultShellSelected = true;
251+
}
252+
});
253+
if (!defaultShellSelected) {
254+
$("#customLoginBox").val(ldapLoginShell);
255+
$("#customLoginSelectorOption").prop("selected", true);
270256
}
271257

272-
$("#loginSelector").change(function() {
258+
function showOrHideCustomLoginBox() {
273259
var customBox = $("#customLoginBox");
274-
if($(this).val() == "custom") {
260+
if($("#loginSelector").val() == "Custom") {
275261
customBox.show();
276262
} else {
277263
customBox.hide();
278264
}
279-
});
280-
281-
if ($("#loginSelector").val() == "custom") {
282-
$("#customLoginBox").show();
283265
}
266+
$("#loginSelector").change(showOrHideCustomLoginBox);
267+
showOrHideCustomLoginBox();
268+
284269
</script>
285270

286271
<style>

0 commit comments

Comments
 (0)