1
0
Fork 0
mirror of https://github.com/Alamantus/Lexiconga.git synced 2025-04-02 01:30:30 +02:00

Add get-all-dictionaries action using token

This commit is contained in:
Robbie Antenesse 2017-12-24 15:14:34 -07:00
parent e5ced20d27
commit adf3e3fc18
2 changed files with 42 additions and 3 deletions

View file

@ -14,7 +14,7 @@ class User {
$query = 'SELECT * FROM users WHERE email=?';
$user = $this->db->query($query, array($email))->fetch();
if ($user) {
if ($user['old_password'] !== 'NULL') {
if ($user['old_password'] !== null) {
if ($user['old_password'] === crypt($password, $email)) {
if ($this->upgradePassword($password)) {
return $this->logIn($email, $password);
@ -90,9 +90,28 @@ class User {
return false;
}
public function getAllDictionaries ($token) {
$user_data = $this->token->decode($token);
if ($user_data !== false) {
$id = $user_data->id;
$query = "SELECT id, name FROM dictionaries WHERE user=$id";
$results = $this->db->query($query)->fetchAll();
if ($results) {
return array_map(function($result) {
return array(
'id' => $this->token->hash($result['id']),
'name' => $result['name'],
);
}, $results);
}
return array();
}
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;
return $this->db->query($current_membership)->rowCount() > 0;
}
private function upgradePassword ($password) {

View file

@ -6,7 +6,7 @@ $inputJSON = file_get_contents('php://input');
$request= json_decode($inputJSON, true);
$action = isset($request['action']) ? $request['action'] : '';
$token = isset($request['token']) ? $request['token'] : '';
$token = isset($request['token']) ? $request['token'] : false;
switch ($action) {
case 'login': {
@ -55,6 +55,26 @@ switch ($action) {
'error' => true,
), 400);
}
case 'get-all-dictionaries': {
if ($token !== false) {
$user = new User();
$all_dictionaries = $user->getAllDictionaries($token);
if ($all_dictionaries !== false) {
return Response::json(array(
'data' => $all_dictionaries,
'error' => false,
), 200);
}
return Response::json(array(
'data' => 'Could not get dictionaries: invalid token',
'error' => true,
), 400);
}
return Response::json(array(
'data' => 'Could not get dictionaries: no token provided',
'error' => true,
), 403);
}
default: {
return Response::html('Hi!');