Skip to content

Commit 4236629

Browse files
xh3n1Sam Tuke
authored andcommitted
Option to add password on the interface when you create a new admin (#399)
* option to add password on the interface Signed-off-by: Xheni Myrtaj <[email protected]> * add confirm password input Signed-off-by: Xheni Myrtaj <[email protected]> * add js to compare passwords and password length Signed-off-by: Xheni Myrtaj <[email protected]> * Apply changes to make the strings translatable Signed-off-by: Xheni Myrtaj <[email protected]> * Add missing label Signed-off-by: Xheni Myrtaj <[email protected]> * Add both options, apply js changes Signed-off-by: Xheni Myrtaj <[email protected]>
1 parent 616e8df commit 4236629

File tree

2 files changed

+118
-30
lines changed

2 files changed

+118
-30
lines changed

public_html/lists/admin/admin.php

Lines changed: 74 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
require_once dirname(__FILE__).'/accesscheck.php';
44

55
$start = sprintf('%d', !empty($_GET['start']) ? $_GET['start'] : 0);
6-
echo PageLinkActionButton('admins', $GLOBALS['I18N']->get('List of Administrators'), "start=$start");
6+
echo PageLinkActionButton('admins', s('List of Administrators'), "start=$start");
77

88
require dirname(__FILE__).'/structure.php';
99

@@ -28,14 +28,14 @@
2828
$noaccess = 1;
2929
}
3030
if ($noaccess) {
31-
echo Error($GLOBALS['I18N']->get('No Access'));
31+
echo Error(s('No Access'));
3232

3333
return;
3434
}
3535

3636
if (!empty($_POST['change'])) {
3737
if (!verifyToken()) { //# csrf check, should be added in more places
38-
echo Error($GLOBALS['I18N']->get('No Access'));
38+
echo Error(s('No Access'));
3939

4040
return;
4141
}
@@ -50,9 +50,15 @@
5050
$totalres = Sql_fetch_Row($result);
5151
$total = $totalres[0];
5252
if (!$total) {
53+
54+
if (isset($_REQUEST['adminpassword'])) {
55+
$adminpass = $_REQUEST['adminpassword'];
56+
} else {
57+
$adminpass = random_bytes(32);
58+
}
5359
Sql_Query(sprintf('insert into %s (loginname,namelc,password,email,created) values("%s","%s","%s","%s",now())',
5460
$tables['admin'], strtolower(normalize($_POST['loginname'])),
55-
strtolower(normalize($_POST['loginname'])), encryptPass(random_bytes(32)),
61+
strtolower(normalize($_POST['loginname'])), encryptPass($adminpass),
5662
sql_escape($_POST['email'])));
5763
$id = Sql_Insert_Id($tables['admin'], 'id');
5864
} else {
@@ -95,9 +101,7 @@
95101
if (!empty($_POST['updatepassword'])) {
96102
//Send token email.
97103
echo sendAdminPasswordToken($id).'<br/>';
98-
//# check for password changes
99-
} elseif (isset($_POST['password'])) {
100-
// Sql_Query("update {$tables["admin"]} set password = \"".sql_escape($_POST['password'])."\" where id = $id");
104+
101105
}
102106
if (isset($_POST['attribute']) && is_array($_POST['attribute'])) {
103107
foreach ($_POST['attribute'] as $key => $val) {
@@ -115,31 +119,32 @@
115119
$GLOBALS['tables']['admin'], adminName($_SESSION['logindetails']['id']), sql_escape(serialize($privs)),
116120
$id));
117121

118-
echo $GLOBALS['I18N']->get('Changes saved');
122+
echo s('Changes saved');
119123
echo '</div>';
120124
} else {
121-
Error($GLOBALS['I18N']->get('Error adding new admin, login name and/or email not inserted, email not valid or admin already exists'));
125+
Error(s('Error adding new admin, login name and/or email not inserted, email not valid or admin already exists'));
122126
}
123127
}
124128

125129
if (!empty($_GET['delete'])) {
126130
$delete = sprintf('%d', $_GET['delete']);
127131
// delete the index in delete
128-
echo $GLOBALS['I18N']->get('Deleting')." $delete ..\n";
132+
echo s('Deleting')." $delete ..\n";
129133
if ($delete != $_SESSION['logindetails']['id']) {
130134
Sql_query(sprintf('delete from %s where id = %d', $GLOBALS['tables']['admin'], $delete));
131135
Sql_query(sprintf('delete from %s where adminid = %d', $GLOBALS['tables']['admin_attribute'], $delete));
132-
echo '..'.$GLOBALS['I18N']->get('Done');
136+
echo '..'.s('Done');
133137
} else {
134-
echo '..'.$GLOBALS['I18N']->get('Failed, you cannot delete yourself');
138+
echo '..'.s('Failed, you cannot delete yourself');
135139
}
136140
echo "<br /><hr/><br />\n";
137141
}
138142

139143
echo '<div class="panel">';
140144

141145
if ($id) {
142-
echo '<h3>'.$GLOBALS['I18N']->get('Edit Administrator').': ';
146+
$addAdmin = false;
147+
echo '<h3>'.s('Edit Administrator').': ';
143148
$result = Sql_query("SELECT * FROM {$tables['admin']} where id = $id");
144149
$data = sql_fetch_assoc($result);
145150
echo $data['loginname'].'</h3>';
@@ -148,15 +153,16 @@
148153
$data['loginname']);
149154
}
150155
} else {
156+
$addAdmin = true;
151157
$data = array();
152-
echo '<h3>'.$GLOBALS['I18N']->get('Add a new Administrator').'</h3>';
158+
echo '<h3>'.s('Add a new Administrator').'</h3>';
153159
}
154160

155161
echo '<div class="content">';
156162
//var_dump($data);
157163

158164
echo formStart(' class="adminAdd"');
159-
printf('<input type="hidden" name="id" value="%d" /><table class="adminDetails" border="1">', $id);
165+
printf('<input type="hidden" name="id" value="%d" /><table class="adminDetails" border="1">', $id);
160166

161167
if (isset($data['privileges'])) {
162168
$privileges = unserialize($data['privileges']);
@@ -179,45 +185,81 @@
179185
//If key is 'password' and the passwords are encrypted, locate two radio buttons to allow an update.
180186
if ($b == 'Password') {
181187
$changeAdminPass = !empty($_SESSION['firstinstall']);
188+
if ($addAdmin===true){
189+
190+
echo ' <tr>
191+
<td>'.s('Choose how to set password').'</td>
192+
<td>
193+
<input type="radio" id="passwordoption1" name="passwordoption" value="1" checked="checked">'.s('Send email').'
194+
<input type="radio" id= "passwordoption0" name="passwordoption" value="0" >'.s('Create password').'
195+
</td>
196+
</tr>
197+
198+
<tr id="passrow">
199+
<td>
200+
<label for="adminpassword">'.s('Create password').'</label>
201+
</td>
202+
<td>
203+
<input type="password" name="adminpassword" id="adminpassword" value="" >
204+
<span id= "shortpassword">'.s('Password must be at least 8 characters').'</span>
205+
</td>
206+
</tr>
207+
208+
<tr id="confirmrow">
209+
<td>
210+
<label for="confirmpassword">'.s('Confirm password').'</label>
211+
</td>
212+
<td>
213+
<input type="password" name="confirmpassword" id="confirmpassword" value="">
214+
<span id= "notmatching">'.s('Not matching').'</span>
215+
</td>
216+
</tr>';
217+
218+
219+
}
220+
182221
if ($changeAdminPass) {
183222
$checkNo = '';
184223
$checkYes = 'checked="checked"';
185224
} else {
186225
$checkYes = '';
187226
$checkNo = 'checked="checked"';
188227
}
189-
190-
printf('<tr><td>%s (%s)</td><td>%s<input type="radio" name="updatepassword" value="0" %s>%s</input>
228+
if ($addAdmin===false) {
229+
printf('<tr><td>%s (%s)</td><td>%s<input type="radio" name="updatepassword" value="0" %s>%s</input>
191230
<input type="radio" name="updatepassword" value="1" %s>%s</input></td></tr>
192231
',
193-
$GLOBALS['I18N']->get('Password'), $GLOBALS['I18N']->get('hidden'),
194-
$GLOBALS['I18N']->get('Update it?') ,
195-
$checkNo,
196-
$GLOBALS['I18N']->get('No'), $checkYes, $GLOBALS['I18N']->get('Yes'));
232+
s('Password'), s('hidden'),
233+
s('Update it?'),
234+
$checkYes, s('Yes'), $checkNo, s('No'));
235+
}
197236
} else {
198237
if ($b != 'Password') {
199-
printf('<tr><td>%s</td><td>%s</td></tr>', $GLOBALS['I18N']->get($b), $data[$key]);
238+
if ($addAdmin !==true) {
239+
printf('<tr><td>%s</td><td>%s</td></tr>', s($b), $data[$key]);
240+
}
200241
} else {
201242
printf('<tr><td>%s</td><td><input type="text" name="%s" value="%s" size="30" /></td></tr>'."\n",
202-
$GLOBALS['I18N']->get('Password'), $key, stripslashes($data[$key]));
243+
s('Password'), $key, stripslashes($data[$key]));
203244
}
204245
}
205246
}
206247
} elseif ($key == 'loginname' && $data[$key] == 'admin') {
207-
printf('<tr><td>'.$GLOBALS['I18N']->get('Login Name').'</td><td>admin</td>');
248+
printf('<tr><td>'.s('Login Name').'</td><td>admin</td>');
208249
echo '<td><input type="hidden" name="loginname" value="admin" /></td></tr>';
209250
} elseif ($key == 'superuser' || $key == 'disabled') {
210251
if ($accesslevel == 'all') {
211252
//If key is 'superuser' or 'disable' locate a boolean combo box.
212-
printf('<tr><td>%s</td><td>', $GLOBALS['I18N']->get($val[1]));
253+
printf('<tr><td>%s</td><td>', s($val[1]));
213254
printf('<select name="%s" size="1">', $key);
214-
echo '<option value="1" '.(!empty($data[$key]) ? ' selected="selected"' : '').'>'.$GLOBALS['I18N']->get('Yes').'</option>';
215-
echo '<option value="0" '.(empty($data[$key]) ? ' selected="selected"' : '').'>'.$GLOBALS['I18N']->get('No').'</option></select>';
255+
echo '<option value="1" '.(!empty($data[$key]) ? ' selected="selected"' : '').'>'.s('Yes').'</option>';
256+
echo '<option value="0" '.(empty($data[$key]) ? ' selected="selected"' : '').'>'.s('No').'</option></select>';
216257
echo '</td></tr>'."\n";
217258
}
218259
} elseif (!empty($val[1]) && !strpos($key, '_')) {
260+
219261
printf('<tr><td>%s</td><td><input type="text" name="%s" value="%s" size="30" /></td></tr>'."\n",
220-
$GLOBALS['I18N']->get($val[1]), $key, htmlspecialchars(stripslashes($data[$key])));
262+
s($val[1]), $key, htmlspecialchars(stripslashes($data[$key])));
221263
}
222264
}
223265
$res = Sql_Query("select
@@ -268,8 +310,10 @@
268310
<label for="settings"><input type="checkbox" name="settings" ' .$checked['settings'].'/>'.s('Change Settings').'</label>
269311
</div>';
270312
echo '</td></tr>';
271-
272-
echo '<tr><td colspan="2"><input class="submit" type="submit" name="change" value="'.$GLOBALS['I18N']->get('Save Changes').'" /></td></tr></table>';
313+
if (!empty($_POST['passwordoption'])) {
314+
echo sendAdminPasswordToken($id).'<br/>';
315+
}
316+
echo '<tr><td colspan="2"><input class="submit" type="submit" name="change" id ="savechanges" value="' . s('Save Changes') . '" /></td></tr></table>';
273317

274318
echo '</div>'; // content
275319
echo '</div>'; // panel

public_html/lists/admin/js/phplistapp.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,8 @@ $(document).ready(function () {
296296
}
297297
});
298298

299+
300+
299301
$("a.savechanges").click(function () {
300302
if (changed) {
301303
document.sendmessageform.followupto.value = this.href;
@@ -332,13 +334,55 @@ $(document).ready(function () {
332334
}
333335
});
334336

337+
$("#passrow").hide();
338+
$("#confirmrow").hide();
339+
340+
$('input[type=radio][name=passwordoption]').change(function() {
341+
if($("#passwordoption1").is(':checked'))
342+
{
343+
$("#passrow").hide();
344+
$("#confirmrow").hide();
345+
} else if($("#passwordoption0").is(':checked'))
346+
{
347+
$("#passrow").show();
348+
$("#confirmrow").show();
349+
$('#shortpassword').prop('required',true);
350+
$('#confirmpassword').prop('required',true);
351+
}
352+
});
353+
$(document).ready(function () {
354+
355+
356+
$('#notmatching, #shortpassword').hide();
357+
$('#adminpassword, #confirmpassword').on('keyup', function () {
358+
if ($('#adminpassword').val().length < 8){
359+
$('#shortpassword').show();
360+
$('#shortpassword').css('color', 'red');
361+
$("#savechanges").attr('disabled', 'disabled');
362+
}else if ($('#adminpassword').val().length >= 8){
363+
$('#shortpassword').hide();
364+
if ($('#adminpassword').val() === $('#confirmpassword').val()) {
365+
$('#notmatching').hide();
366+
$("#savechanges").removeAttr('disabled');
367+
} else {
368+
$('#notmatching').show();
369+
$('#notmatching').css('color', 'red');
370+
$("#savechanges").attr('disabled', 'disabled');
371+
}
372+
373+
374+
}
375+
});
376+
});
377+
335378
$("#initialadminpassword").keyup(function () {
336379
if (this.value.length >= 8) {
337380
$("#initialisecontinue").removeAttr('disabled');
338381
} else if (this.value.length < 8) {
339382
$("#initialisecontinue").attr('disabled', 'disabled');
340383
}
341384
});
385+
342386
$("#initialiseform").submit(function () {
343387
$("#dialog").dialog({
344388
minHeight: 400,

0 commit comments

Comments
 (0)