summaryrefslogtreecommitdiffstats
path: root/application/controllers
diff options
context:
space:
mode:
authorFlorian Pritz <bluewind@xinu.at>2014-10-29 15:26:30 +0100
committerFlorian Pritz <bluewind@xinu.at>2014-10-29 15:26:30 +0100
commit9fcab419ba819273ed95fdfa76b08c87d72cb3e8 (patch)
treeace8c62e8a6d23d44b7835f925189c60f4627ca5 /application/controllers
parent5b985a997f50e3476a606f40e2ced32b36cbef03 (diff)
Simplify creation of initial user
Signed-off-by: Florian Pritz <bluewind@xinu.at>
Diffstat (limited to 'application/controllers')
-rw-r--r--application/controllers/user.php67
1 files changed, 67 insertions, 0 deletions
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";
+ }
}