support for changing usernames

This commit is contained in:
Shish 2015-07-12 22:14:57 +01:00
parent 1907dc29bc
commit feecdd4d13
4 changed files with 54 additions and 1 deletions

View file

@ -202,6 +202,20 @@ class User {
log_info("core-user", 'Set class for '.$this->name.' to '.$class); log_info("core-user", 'Set class for '.$this->name.' to '.$class);
} }
/**
* @param string $name
*/
public function set_name(/*string*/ $name) {
global $database;
if(User::by_name($name)) {
throw new Exception("Desired username is already in use");
}
$old_name = $this->name;
$this->name = $name;
$database->Execute("UPDATE users SET name=:name WHERE id=:id", array("name"=>$this->name, "id"=>$this->id));
log_info("core-user", "Changed username for {$old_name} to {$this->name}");
}
/** /**
* @param string $password * @param string $password
*/ */

View file

@ -90,6 +90,7 @@ new UserClass("base", null, array(
"view_ip" => False, # view IP addresses associated with things "view_ip" => False, # view IP addresses associated with things
"ban_ip" => False, "ban_ip" => False,
"edit_user_name" => False,
"edit_user_password" => False, "edit_user_password" => False,
"edit_user_info" => False, # email address, etc "edit_user_info" => False, # email address, etc
"edit_user_class" => False, "edit_user_class" => False,
@ -155,6 +156,7 @@ new UserClass("admin", "base", array(
"edit_image_lock" => True, "edit_image_lock" => True,
"view_ip" => True, "view_ip" => True,
"ban_ip" => True, "ban_ip" => True,
"edit_user_name" => True,
"edit_user_password" => True, "edit_user_password" => True,
"edit_user_info" => True, "edit_user_info" => True,
"edit_user_class" => True, "edit_user_class" => True,

View file

@ -189,6 +189,16 @@ class UserPage extends Extension {
return; return;
} }
else if($event->get_arg(0) == "change_name") {
if(isset($_POST['id']) && isset($_POST['name'])) {
$duser = User::by_id($_POST['id']);
if ( ! $duser instanceof User) {
throw new NullUserException("Error: the user id does not exist!");
}
$name = $_POST['name'];
$this->change_name_wrapper($duser, $name);
}
}
else if($event->get_arg(0) == "change_pass") { else if($event->get_arg(0) == "change_pass") {
if(isset($_POST['id']) && isset($_POST['pass1']) && isset($_POST['pass2'])) { if(isset($_POST['id']) && isset($_POST['pass1']) && isset($_POST['pass2'])) {
$duser = User::by_id($_POST['id']); $duser = User::by_id($_POST['id']);
@ -527,6 +537,20 @@ class UserPage extends Extension {
} }
} }
private function change_name_wrapper(User $duser, $name) {
global $user;
if($user->can('edit_user_name') && $this->user_can_edit_user($user, $duser)) {
$duser->set_name($name);
flash_message("Username changed");
// TODO: set login cookie if user changed themselves
$this->redirect_to_user($duser);
}
else {
$this->theme->display_error(400, "Error", "Permission denied");
}
}
/** /**
* @param User $duser * @param User $duser
* @param string $pass1 * @param string $pass1

View file

@ -165,8 +165,21 @@ class UserPageTheme extends Themelet {
$html = ""; $html = "";
if($duser->id != $config->get_int('anon_id')){ //justa fool-admin protection so they dont mess around with anon users. if($duser->id != $config->get_int('anon_id')){ //justa fool-admin protection so they dont mess around with anon users.
if($user->can('edit_user_name')) {
$html .= "
<p>".make_form(make_link("user_admin/change_name"))."
<input type='hidden' name='id' value='{$duser->id}'>
<table class='form'>
<thead><tr><th colspan='2'>Change Name</th></tr></thead>
<tbody><tr><th>New name</th><td><input type='text' name='name' value='".html_escape($duser->name)."'></td></tr></tbody>
<tfoot><tr><td colspan='2'><input type='Submit' value='Set'></td></tr></tfoot>
</table>
</form>
";
}
$html .= " $html .= "
".make_form(make_link("user_admin/change_pass"))." <p>".make_form(make_link("user_admin/change_pass"))."
<input type='hidden' name='id' value='{$duser->id}'> <input type='hidden' name='id' value='{$duser->id}'>
<table class='form'> <table class='form'>
<thead> <thead>