summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--INSTALL8
-rw-r--r--application/controllers/user.php67
2 files changed, 68 insertions, 7 deletions
diff --git a/INSTALL b/INSTALL
index ada32f7cb..84eb91314 100644
--- a/INSTALL
+++ b/INSTALL
@@ -17,10 +17,4 @@
* Copy ./data/local/examples/contact-info.php to ./data/local/ and edit it
-# Generate the first user
-
-* Go to http://yourdomain.com/some/where/index.php/user/hash_password to generate a password hash
-
-* Run the following SQL command in your database:
- insert into users (username, password, email, referrer)
- values ("your_name", "hash_from_above", "you@foo.com", NULL);
+* run `php index.php user add_user` to add your first user
diff --git a/application/controllers/user.php b/application/controllers/user.php
index be4359404..a702b63c7 100644
--- a/application/controllers/user.php
+++ b/application/controllers/user.php
@@ -509,4 +509,71 @@ class User extends MY_Controller {
$this->db->where('date <', $oldest_time)
->delete('actions');
}
+
+ private function _get_line_cli($message, $verification_func = NULL)
+ {
+ echo "$message: ";
+
+ while ($line = fgets(STDIN)) {
+ $line = trim($line);
+ if ($verification_func === NULL) {
+ return $line;
+ }
+
+ if ($verification_func($line)) {
+ return $line;
+ } else {
+ echo "$message: ";
+ }
+ }
+ }
+
+ function add_user()
+ {
+ if (!$this->input->is_cli_request()) return;
+ $this->duser->require_implemented("can_register_new_users");
+
+ $error = array();
+
+ // FIXME: deduplicate username/email verification with register()
+ $username = $this->_get_line_cli("Username", function($username) {
+ if (!$username || strlen($username) > 32 || !preg_match("/^[a-z0-9]+$/", $username)) {
+ echo "Invalid username (only up to 32 chars of a-z0-9 are allowed).\n";
+ return false;
+ } else {
+ if (get_instance()->muser->username_exists($username)) {
+ echo "Username already exists.\n";
+ return false;
+ }
+ }
+ return true;
+ });
+
+ $this->load->helper("email");
+ $email = $this->_get_line_cli("Email", function($email) {
+ if (!valid_email($email)) {
+ echo "Invalid email.\n";
+ return false;
+ }
+ return true;
+ });
+
+ $password = $this->_get_line_cli("Password", function($password) {
+ if (!$password || $password === "") {
+ echo "No password supplied.\n";
+ return false;
+ }
+ return true;
+ });
+
+ $this->db->set(array(
+ 'username' => $username,
+ 'password' => $this->muser->hash_password($password),
+ 'email' => $email,
+ 'referrer' => NULL
+ ))
+ ->insert('users');
+
+ echo "User added\n";
+ }
}