Work on register.php, look at TODOs
This commit is contained in:
1 parent
5d7db34397
commit
b561dc0531
7 files changed
+126
-40
No files matched your search
@@ -34,6 +34,8 @@ class User {
|
||||
|
||||
var $conn;
|
||||
|
||||
var $fromSnappOrder;
|
||||
|
||||
function User($id = NULL, $data = NULL) {
|
||||
$this->__construct($id, $data);
|
||||
}
|
||||
|
||||
@@ -314,7 +314,11 @@ function handle_error($errno, $errstr, $errfile, $errline, $errcontext) {
|
||||
}
|
||||
|
||||
function error($msg) {
|
||||
Errors :: addError($msg);
|
||||
if(class_exists('Errors')) {
|
||||
Errors :: addError($msg);
|
||||
} else {
|
||||
var_dump($msg);
|
||||
}
|
||||
}
|
||||
|
||||
function notify($msg) {
|
||||
|
||||
@@ -36,7 +36,7 @@ if(!isset($_GET['phone'])) {
|
||||
die();
|
||||
}
|
||||
$phone = $_GET['phone'];
|
||||
if(!is_numeric($phone) || strlen($phone) !== 8 ) {
|
||||
if( !valid_phonenumber($phone) ) {
|
||||
set_response_code(400);
|
||||
echo json_encode(array('error' => 'Not a phone number', 'query' => $_GET));
|
||||
die();
|
||||
@@ -68,42 +68,13 @@ if( $res->numRows() == 0) {
|
||||
die();
|
||||
}
|
||||
$res->fetchInto($row);
|
||||
$user_id = $row['user_id'];
|
||||
|
||||
/* Get user object with group ids and membership status */
|
||||
$cols = array('id', 'firstname', 'lastname', 'email', 'expires', 'cardno');
|
||||
$sql = "SELECT ".implode($cols, ",").",GROUP_CONCAT(group_id) AS group_ids,expires > NOW() OR expires IS NULL AS is_member FROM din_user AS u, din_usergrouprelationship AS ug WHERE u.id=$user_id AND u.id=ug.user_id GROUP BY user_id";
|
||||
$res = $conn->query($sql);
|
||||
if( DB::isError($res) ) {
|
||||
echo json_encode( array('error' => 'db_error', 'error_message' => $res->toString() ) );
|
||||
die();
|
||||
}
|
||||
$res->fetchInto($user);
|
||||
/* Get and format user */
|
||||
$user = get_user($row['user_id']);
|
||||
|
||||
/* Membership status according to spec.
|
||||
* Status codes:
|
||||
* 0 - Registered
|
||||
* 1 - Member
|
||||
* 2 - Active member
|
||||
*/
|
||||
$groups = explode(",", $user['group_ids']);
|
||||
$user['membership_status'] = 0;
|
||||
|
||||
if($user['is_member'] !== "0") {
|
||||
// Group id mappings 1:dns-alle, 2: dns-aktiv, 3:administrator, 4+:orgunits/special groups
|
||||
if(in_array("2", $groups)) {
|
||||
$user['membership_status'] = 2;
|
||||
} else {
|
||||
$user['membership_status'] = 1;
|
||||
}
|
||||
}
|
||||
/* Add back phone number from query */
|
||||
$user['phone'] = $phone;
|
||||
|
||||
/* Clean up user object */
|
||||
unset($user['is_member']);
|
||||
unset($user['group_ids']);
|
||||
|
||||
/* Return encrypted user object */
|
||||
echo json_encode($user);
|
||||
//echo $crypt->encrypt(json_encode($user));
|
||||
|
||||
@@ -1,6 +1,20 @@
|
||||
<?php
|
||||
|
||||
/* Pull in Inside */
|
||||
set_include_path("../../inside/");
|
||||
require_once("functions.php");
|
||||
require_once("User.php");
|
||||
set_include_path("../../includes/");
|
||||
require_once("DB.php");
|
||||
|
||||
/* Our own */
|
||||
require_once("../lib/CryptoHelper.php");
|
||||
require_once("../lib/functions.php");
|
||||
require_once("../config.php");
|
||||
|
||||
|
||||
/*
|
||||
* Registers a temporary user in the temp user table
|
||||
* Registers a user in the user table
|
||||
*
|
||||
* Request:
|
||||
*
|
||||
@@ -25,13 +39,56 @@
|
||||
* }
|
||||
*
|
||||
* TODO:
|
||||
* - Decode encrypted http body
|
||||
* - validate http body
|
||||
* - register user in temp user db
|
||||
* - register user with fromSnappOrder flag
|
||||
* - return result
|
||||
*/
|
||||
/* Decrypt body */
|
||||
$crypt = new CryptoHelper(SNAP_IV, SNAP_KEY);
|
||||
var_dump($crypt->decrypt($crypt->encrypt("lol")));
|
||||
|
||||
/* Validate request */
|
||||
$body = file_get_contents('php://input');
|
||||
if(strlen($body) === 0) {
|
||||
set_response_code(400);
|
||||
echo json_encode(array('error' => 'Invalid request'));
|
||||
die();
|
||||
}
|
||||
|
||||
/* Decrypt body */
|
||||
// TODO enable
|
||||
//$crypt = new CryptoHelper(SNAP_IV, SNAP_KEY);
|
||||
//$body = $crypt->decrypt($body);
|
||||
$data = (array) json_decode($body);
|
||||
if($data === NULL) {
|
||||
set_response_code(400);
|
||||
echo json_encode(array('error' => 'Can\'t decode body'));
|
||||
die();
|
||||
}
|
||||
|
||||
/* Validate supplied data */
|
||||
$required_keys = array('firstname', 'lastname', 'phone', 'email');
|
||||
|
||||
foreach($required_keys as $key) {
|
||||
if(!array_key_exists($key, $data)) {
|
||||
set_response_code(400);
|
||||
echo json_encode(array('error' => 'missing required field \''.$key.'\''));
|
||||
die();
|
||||
}
|
||||
}
|
||||
// TODO validate phonenumber
|
||||
// TODO validate email
|
||||
// TODO look at getUseridFromPhone in inside/functions.php
|
||||
// TODO when $fromSnappOrder is sent to User-constructor...
|
||||
// ...then work around requirements for username, password, birthdate, address
|
||||
|
||||
/* Create user */
|
||||
//$new_user = new User(NULL, $data);
|
||||
|
||||
/* Get and format user */
|
||||
//$user = get_user($new_user->id);
|
||||
|
||||
/* Add back phone number from query */
|
||||
//$user['phone'] = $phone;
|
||||
|
||||
/* Return encrypted user object */
|
||||
//echo json_encode($user);
|
||||
//echo $crypt->encrypt(json_encode($user));
|
||||
|
||||
?>
|
||||
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
/* Set HTTP response code */
|
||||
function set_response_code($code) {
|
||||
if(!is_int($code)) {
|
||||
return false;
|
||||
@@ -6,4 +7,44 @@ function set_response_code($code) {
|
||||
header('X-Ignore-This: something', true, $code);
|
||||
}
|
||||
|
||||
/* Get user object with group ids and membership status */
|
||||
function get_user($user_id) {
|
||||
global $conn;
|
||||
|
||||
$cols = array('id', 'firstname', 'lastname', 'email', 'expires', 'cardno');
|
||||
$sql = "SELECT ".implode($cols, ",").",GROUP_CONCAT(group_id) AS group_ids,expires > NOW() OR expires IS NULL AS is_member FROM din_user AS u, din_usergrouprelationship AS ug WHERE u.id=$user_id AND u.id=ug.user_id GROUP BY user_id";
|
||||
$res = $conn->query($sql);
|
||||
if( DB::isError($res) ) {
|
||||
echo json_encode( array('error' => 'db_error', 'error_message' => $res->toString() ) );
|
||||
die();
|
||||
}
|
||||
$res->fetchInto($user);
|
||||
|
||||
/* Membership status according to spec.
|
||||
* Status codes:
|
||||
* 0 - Registered
|
||||
* 1 - Member
|
||||
* 2 - Active member
|
||||
*/
|
||||
$groups = explode(",", $user['group_ids']);
|
||||
$user['membership_status'] = 0;
|
||||
|
||||
if($user['is_member'] !== "0") {
|
||||
// Group id mappings 1:dns-alle, 2: dns-aktiv, 3:administrator, 4+:orgunits/special groups
|
||||
if(in_array("2", $groups)) {
|
||||
$user['membership_status'] = 2;
|
||||
} else {
|
||||
$user['membership_status'] = 1;
|
||||
}
|
||||
}
|
||||
/* Clean up user object */
|
||||
unset($user['is_member']);
|
||||
unset($user['group_ids']);
|
||||
|
||||
return $user;
|
||||
}
|
||||
function valid_phonenumber($phone) {
|
||||
return is_numeric($phone) && strlen($phone) === 8;
|
||||
}
|
||||
|
||||
?>
|
||||
Executable
+5
@@ -0,0 +1,5 @@
|
||||
#!/bin/sh
|
||||
HOST="http://inside.dev"
|
||||
curl -XPOST -H"Content-type: application/json" --data-binary @testuser.json $HOST/snapporder/api/register.php
|
||||
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"firstname": "Test",
|
||||
"lastname": "Testesen",
|
||||
"email": "asdf@asdf.com",
|
||||
"phone": "48105886"
|
||||
}
|
||||
Reference in new issue
Block a user