diff --git a/public/api/Dictionary.php b/public/api/Dictionary.php index c0f8075..c3ea484 100644 --- a/public/api/Dictionary.php +++ b/public/api/Dictionary.php @@ -26,22 +26,33 @@ class Dictionary { ); } + private function checkIfIdExists ($id) { + $query = "SELECT id FROM dictionaries WHERE id=?"; + $results = $this->db->query($query, array($id))->fetchAll(); + return count($results) > 0; + } + public function create ($user) { - $insert_dictionary_query = "INSERT INTO dictionaries (user, created_on) VALUES (?, ?)"; - $insert_dictionary = $this->db->execute($insert_dictionary_query, array($user, time())); + $new_id = mt_rand(1000, 999999999); + $id_exists = $this->checkIfIdExists($new_id); + while ($id_exists) { + $new_id = mt_rand(1000, 999999999); + $id_exists = $this->checkIfIdExists($new_id); + } + + $insert_dictionary_query = "INSERT INTO dictionaries (id, user, created_on) VALUES (?, ?, ?)"; + $insert_dictionary = $this->db->execute($insert_dictionary_query, array($new_id, $user, time())); if ($insert_dictionary === true) { - $new_dictionary_id = $this->db->lastInsertId(); - $insert_linguistics_query = "INSERT INTO dictionary_linguistics (dictionary, parts_of_speech, phonology) -VALUES ($new_dictionary_id, ?, ?)"; +VALUES ($new_id, ?, ?)"; $insert_linguistics = $this->db->execute($insert_linguistics_query, array( json_encode($this->defaults['partsOfSpeech']), json_encode($this->defaults['phonology']), )); if ($insert_linguistics === true) { - return $this->changeCurrent($user, $new_dictionary_id); + return $this->changeCurrent($user, $new_id); } else { return array( 'error' => '"INSERT INTO dictionary_linguistics" failed: ' . $this->db->last_error_info[2], @@ -69,7 +80,7 @@ VALUES ($new_dictionary_id, ?, ?)"; if ($results) { return array_map(function($result) { return array( - 'id' => $this->token->hash($result['id']), + 'id' => $result['id'], 'name' => $result['name'] . ' ' . $result['specification'], ); }, $results); @@ -86,7 +97,7 @@ VALUES ($new_dictionary_id, ?, ?)"; $phonology = $result['phonology'] !== '' ? json_decode($result['phonology']) : $this->defaults['phonology']; return array( - 'id' => $this->token->hash($result['id']), + 'id' => $result['id'], 'name' => $result['name'], 'specification' => $result['specification'], 'description' => $result['description'], diff --git a/public/api/User.php b/public/api/User.php index 34ebb39..7acc1fc 100644 --- a/public/api/User.php +++ b/public/api/User.php @@ -115,9 +115,10 @@ VALUES (?, ?, ?, ?, ?)'; $id = $user_data->id; $new_dictionary = $this->dictionary->create($id); if (!isset($new_dictionary['error'])) { + $new_token = $this->generateUserToken($id, $new_dictionary); return array( - 'token' => $this->generateUserToken($id, $new_dictionary), - 'dictionary' => $this->getCurrentDictionary($token), + 'token' => $new_token, + 'dictionary' => $this->getCurrentDictionary($new_token), ); } else { return $new_dictionary; diff --git a/src/components/management/AccountManager/MyAccount.jsx b/src/components/management/AccountManager/MyAccount.jsx index 6397176..c3f8b69 100644 --- a/src/components/management/AccountManager/MyAccount.jsx +++ b/src/components/management/AccountManager/MyAccount.jsx @@ -11,6 +11,7 @@ export class MyAccount extends Component { publicName: PropTypes.string.isRequired, allowEmails: PropTypes.bool.isRequired, userDictionaries: PropTypes.array.isRequired, + dictionary: PropTypes.object, sendUserData: PropTypes.func, changeDictionary: PropTypes.func, }, props, 'prop', 'MyAccount'); @@ -136,15 +137,23 @@ export class MyAccount extends Component {