Add action for create-user
This commit is contained in:
parent
e96d7ab624
commit
0009f671b7
|
@ -11,4 +11,8 @@ class Db {
|
|||
$stmt->execute($params);
|
||||
return $stmt;
|
||||
}
|
||||
|
||||
public function lastInsertId () {
|
||||
return $this->dbh->lastInsertId();
|
||||
}
|
||||
}
|
|
@ -32,12 +32,75 @@ class User {
|
|||
return false;
|
||||
}
|
||||
|
||||
public function hasMembership ($id) {
|
||||
public function create ($email, $password) {
|
||||
$insert_user_query = 'INSERT INTO users (email, password) VALUES (?, ?)';
|
||||
$password_hash = password_hash($password, PASSWORD_DEFAULT);
|
||||
|
||||
// Use a transaction to make sure all pieces are created successfully.
|
||||
$this->db->dbh->beginTransaction();
|
||||
|
||||
$insert_user = $this->db->query($insert_user_query, array($email, $password_hash));
|
||||
if ($insert_user === true) {
|
||||
$new_user_id = $this->db->lastInsertId();
|
||||
|
||||
$token = $this->createDictionary($new_user_id);
|
||||
|
||||
if ($token !== false) {
|
||||
if ($this->db->dbh->commit()) {
|
||||
return $token;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->db->dbh->rollBack();
|
||||
return false;
|
||||
}
|
||||
|
||||
public function createDictionary ($user) {
|
||||
$this->db->dbh->beginTransaction();
|
||||
|
||||
$insert_dictionary_query = "INSERT INTO dictionaries (user) VALUES ($user)";
|
||||
$insert_dictionary = $this->db->query($insert_dictionary_query);
|
||||
|
||||
if ($insert_dictionary === true) {
|
||||
$new_dictionary_id = $this->db->lastInsertId();
|
||||
|
||||
$insert_linguistics_query = "INSERT INTO dictionary_linguistics (dictionary) VALUES ($new_dictionary_id)";
|
||||
$insert_linguistics = $this->db->query($insert_dictionary_query);
|
||||
|
||||
if ($insert_linguistics === true) {
|
||||
if ($this->changeCurrentDictionary($user, $new_dictionary_id)) {
|
||||
if ($this->db->dbh->commit()) {
|
||||
$user_data = array(
|
||||
'id' => $user,
|
||||
'isMember' => $this->hasMembership($user['id']),
|
||||
'dictionary' => $new_dictionary_id,
|
||||
);
|
||||
return $this->token->encode($user_data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->db->dbh->rollBack();
|
||||
return false;
|
||||
}
|
||||
|
||||
public function changeCurrentDictionary ($user, $dictionary) {
|
||||
$update_query = 'UPDATE users SET current_dictionary=? WHERE id=?';
|
||||
$update = $this->db->query($update_query, array($user, $dictionary));
|
||||
if ($update->rowCount() > 0) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private function hasMembership ($id) {
|
||||
$current_membership = "SELECT * FROM memberships WHERE user=$id AND start_date>=CURRENT_TIMESTAMP AND CURRENT_TIMESTAMP<expire_date";
|
||||
$stmt = $this->db->query($current_membership)->rowCount() > 0;
|
||||
}
|
||||
|
||||
public function upgradePassword ($password) {
|
||||
private function upgradePassword ($password) {
|
||||
$new_password = password_hash($password, PASSWORD_DEFAULT);
|
||||
$update_query = 'UPDATE users SET old_password=NULL, password=? WHERE id=' . $user['id'];
|
||||
$stmt = $this->db->query($update_query, array($new_password));
|
||||
|
|
|
@ -26,4 +26,24 @@ switch ($action) {
|
|||
'error' => true,
|
||||
), 500);
|
||||
}
|
||||
case 'create-account': {
|
||||
if ($_POST['email'] && $_POST['password']) {
|
||||
$user = new User();
|
||||
$token = $user->create($_POST['email'], $_POST['password']);
|
||||
if ($token !== false) {
|
||||
return Response::out(array(
|
||||
'data' => $token,
|
||||
'error' => false,
|
||||
), 200);
|
||||
}
|
||||
return Response::out(array(
|
||||
'data' => 'Could not create account: incorrect data',
|
||||
'error' => true,
|
||||
), 400);
|
||||
}
|
||||
return Response::out(array(
|
||||
'data' => 'Could not create account: required information missing',
|
||||
'error' => true,
|
||||
), 500);
|
||||
}
|
||||
}
|
|
@ -10,7 +10,7 @@ SET time_zone = "+00:00";
|
|||
CREATE TABLE IF NOT EXISTS `dictionaries` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`user` int(11) NOT NULL,
|
||||
`name` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
|
||||
`name` varchar(50) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'New',
|
||||
`specification` varchar(50) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'Dictionary',
|
||||
`description` text COLLATE utf8_unicode_ci NOT NULL COMMENT 'Markdown',
|
||||
`allow_duplicates` tinyint(1) NOT NULL DEFAULT '0',
|
||||
|
@ -54,7 +54,7 @@ CREATE TABLE IF NOT EXISTS `users` (
|
|||
`password` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
|
||||
`public_name` varchar(50) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'Someone',
|
||||
`username` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
|
||||
`current_dictionary` int(11) NOT NULL,
|
||||
`current_dictionary` int(11) DEFAULT NULL,
|
||||
`allow_email` tinyint(1) NOT NULL DEFAULT '1',
|
||||
`last_login` timestamp NULL DEFAULT NULL,
|
||||
`password_reset_code` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
|
||||
|
|
Loading…
Reference in New Issue