forked from RealQuestions/apiengine-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslimuser.php
151 lines (117 loc) · 4.39 KB
/
slimuser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
require('./config.inc'); # Neo4j, Slim & config
$app = new \Slim\Slim();
$app->get('/hello/', function () use ($app){
global $client;
/**
*/
$name = $app->request()->get('name');
$queryString = "MATCH (n:SiteUsers) Where n.name = '{$name}' Return n";
$query = new Everyman\Neo4j\Cypher\Query($client, $queryString, array('name' => $name));
$result = $query->getResultSet();
foreach ($result as $key=>$row) {
$id = $row['n']->getID();
$userarray[$id]['name'] = $row['n']->getProperty('name');
$userarray[$id]['email'] = $row['n']->getProperty('email');
// print_r ($row['n']);
}
if (isset($userarray)) {
echo json_encode($userarray);
} else {
echo $name . ' not registered';
}
});
$app->post('/user/register', function () use ($app) {
try {
global $client;
// get and decode JSON request body
$request = $app->request();
$body = $request->getBody();
$input = json_decode($body);
$passwordHash = password_hash($input->password, PASSWORD_DEFAULT);
// {"name":"username","email":"[email protected]", "password":"password"}
$useraccount = $client->makeNode();
$useraccount->setProperty('name', $input->name)
->setProperty('email', $input->email)
->setProperty('password', $passwordHash)
->save();
$userlabel = $client->makeLabel('SiteUsers');
$node = $client->getNode($useraccount->getID());
$labels = $node->addLabels(array($userlabel));
// return JSON-encoded response body
$app->response()->header('Content-Type', 'application/json');
echo json_encode($input);
echo json_encode($useraccount->getID);
} catch (Exception $e) {
$app->response()->status(400);
$app->response()->header('X-Status-Reason', $e->getMessage());
}
});
$app->put('/user/:email', function ($email) use ($app) {
try {
global $client;
// get and decode JSON request body
$request = $app->request();
$body = $request->getBody();
$input = json_decode($body);
$passwordHash = password_hash($input->password, PASSWORD_DEFAULT);
// {"name":"username","email":"[email protected]", "password":"password"}
$cypherStr = "MATCH (n:SiteUsers) where n.email ='{$email}' Return n";
$cypher = New Everyman\Neo4j\Cypher\Query($client, $cypherStr, array('email' => $email));
$result = $cypher->getResultSet();
foreach ($result as $key=>$row) {
$id = $row['n']->getID();
$useraccount = $client->getNode($id);
}
if (isset($useraccount)) {
$useraccount->setProperty('name', $input->name)
->setProperty('email', $input->email)
->setProperty('password', $passwordHash)
->save();
$useraccount = array("message", "Record updated");
} else {
$useraccount = array("message", "No user with that email exists");
}
// return JSON-encoded response body
$app->response()->header('Content-Type', 'application/json');
echo json_encode($useraccount);
} catch (Exception $e) {
$app->response()->status(400);
$app->response()->header('X-Status-Reason', $e->getMessage());
}
});
$app->post('/user/', function () use ($app) {
try {
global $client;
// get and decode JSON request body
$request = $app->request();
$body = $request->getBody();
$input = json_decode($body);
$email = $input->email;
$cypherStr = "MATCH (n:SiteUsers) where n.email ='{$email}' Return n";
$cypher = New Everyman\Neo4j\Cypher\Query($client, $cypherStr, array('email' => $email));
$result = $cypher->getResultSet();
foreach ($result as $key=>$row) {
$id = $row['n']->getID();
$useraccount = $client->getNode($id);
$passwordhash = $row['n']->getProperty('password');
}
$token = bin2hex(openssl_random_pseudo_bytes(16));
if (password_verify($input->password, $passwordhash)) {
$res['status'] = 'OK'; // Success!
$res['accessToken'] = $token; // Success!
}
else {
$res['status'] = 'Failed'; // Success!
// Invalid credentials
}
// {"name":"username","email":"[email protected]", "password":"password"}
// return JSON-encoded response body
$app->response()->header('Content-Type', 'application/json');
echo json_encode($res);
} catch (Exception $e) {
$app->response()->status(400);
$app->response()->header('X-Status-Reason', $e->getMessage());
}
});
$app->run();