This commit is contained in:
Stefan Puraner
2016-04-11 09:11:37 +02:00
691 changed files with 64471 additions and 509 deletions
@@ -14,22 +14,23 @@
// ------------------------------------------------------------------------
defined('BASEPATH') OR exit('No direct script access allowed');
if (! defined('BASEPATH'))
exit('No direct script access allowed');
// This can be removed if you use __autoload() in config.php OR use Modular Extensions
require APPPATH . '/libraries/REST_Controller.php';
//require APPPATH . '/libraries/REST_Controller.php';
/**
* Handles user authentication and registration process
*/
class AuthAPI extends REST_Controller {
class AuthAPI extends APIv1_Controller
{
/**
* Userauth-Controller constructor.
* A more elaborate description of the constructor.
* {@inheritdoc}
*/
function __construct()
public function __construct()
{
// Construct the parent class
parent::__construct();
@@ -39,16 +40,18 @@ class AuthAPI extends REST_Controller {
$this->methods['login_get']['limit'] = 500; // 500 requests per hour per user/key
// Load helper
$this->load->helper('fhcauth');
//$this->load->helper('fhcauth');
$this->load->library('session');
$this->load->library('FHC_Auth');
}
/**
* Checks user credentials and creates a new session
* @return string JSON that indicates success/failure of login
*
* @example normal account: http://wsp.fortyseeds.at/backend/api/userauth/login/username/foo%40bar.at/password/secret/device_id/abcdef123
* @example OAuth Google: http://wsp.fortyseeds.at/backend/api/userauth/login/username/foo%40bar.at/device_id/abcdef123/google_token/qwert321
* @example OAuth Facebook: http://wsp.fortyseeds.at/backend/api/userauth/login/username/foo%40bar.at/device_id/abcdef123/fb_token/qwert321
* @return void JSON that indicates success/failure of login.
*/
public function login_get()
{
@@ -57,8 +60,8 @@ class AuthAPI extends REST_Controller {
$httpstatus = null;
$username = urldecode($this->get('username'));
$password = urldecode($this->get('password'));
$account = auth($username,$password);
$account = $this->FHCAuth->auth($username, $password);
// perform login checks
if (!$account)
@@ -92,8 +95,9 @@ class AuthAPI extends REST_Controller {
/**
* Logs out user by destroying session
* @return string JSON that indicates success/failure of logout
*
* @example http://wsp.fortyseeds.at/backend/api/userauth/logout/username/foo%40bar.at/session_id/55afab8ba6f1b/device_id/abcdef123
* @return void JSON that indicates success/failure of logout
*/
public function logout_get()
{
@@ -125,6 +129,4 @@ class AuthAPI extends REST_Controller {
// Set the response and exit
$this->response($payload, $httpstatus);
}
}
@@ -1,9 +1,10 @@
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
if (! defined('BASEPATH'))
exit('No direct script access allowed');
// This can be removed if you use __autoload() in config.php OR use Modular Extensions
require APPPATH . '/libraries/REST_Controller.php';
require APPPATH.'/libraries/REST_Controller.php';
/**
* Keys Controller
@@ -16,8 +17,8 @@ require APPPATH . '/libraries/REST_Controller.php';
* @license MIT
* @link https://github.com/chriskacerguis/codeigniter-restserver
*/
class Key extends REST_Controller {
class APIKey extends REST_Controller
{
protected $methods = [
'index_put' => ['level' => 10, 'limit' => 10],
'index_delete' => ['level' => 10],
@@ -34,24 +35,24 @@ class Key extends REST_Controller {
public function index_put()
{
// Build a new key
$key = $this->_generate_key();
$key = $this->__generateKey();
// If no key level provided, provide a generic key
$level = $this->put('level') ? $this->put('level') : 1;
$ignore_limits = ctype_digit($this->put('ignore_limits')) ? (int) $this->put('ignore_limits') : 1;
$ignore_limits = ctype_digit($this->put('ignore_limits')) ? (int)$this->put('ignore_limits') : 1;
// Insert the new key
if ($this->_insert_key($key, ['level' => $level, 'ignore_limits' => $ignore_limits]))
{
$this->response([
'status' => TRUE,
'status' => true,
'key' => $key
], REST_Controller::HTTP_CREATED); // CREATED (201) being the HTTP response code
}
else
{
$this->response([
'status' => FALSE,
'status' => false,
'message' => 'Could not save the key'
], REST_Controller::HTTP_INTERNAL_SERVER_ERROR); // INTERNAL_SERVER_ERROR (500) being the HTTP response code
}
@@ -72,7 +73,7 @@ class Key extends REST_Controller {
{
// It doesn't appear the key exists
$this->response([
'status' => FALSE,
'status' => false,
'message' => 'Invalid API key'
], REST_Controller::HTTP_BAD_REQUEST); // BAD_REQUEST (400) being the HTTP response code
}
@@ -82,7 +83,7 @@ class Key extends REST_Controller {
// Respond that the key was destroyed
$this->response([
'status' => TRUE,
'status' => true,
'message' => 'API key was deleted'
], REST_Controller::HTTP_NO_CONTENT); // NO_CONTENT (204) being the HTTP response code
}
@@ -103,7 +104,7 @@ class Key extends REST_Controller {
{
// It doesn't appear the key exists
$this->response([
'status' => FALSE,
'status' => false,
'message' => 'Invalid API key'
], REST_Controller::HTTP_BAD_REQUEST); // BAD_REQUEST (400) being the HTTP response code
}
@@ -112,14 +113,14 @@ class Key extends REST_Controller {
if ($this->_update_key($key, ['level' => $new_level]))
{
$this->response([
'status' => TRUE,
'status' => true,
'message' => 'API key was updated'
], REST_Controller::HTTP_OK); // OK (200) being the HTTP response code
}
else
{
$this->response([
'status' => FALSE,
'status' => false,
'message' => 'Could not update the key level'
], REST_Controller::HTTP_INTERNAL_SERVER_ERROR); // INTERNAL_SERVER_ERROR (500) being the HTTP response code
}
@@ -140,7 +141,7 @@ class Key extends REST_Controller {
{
// It doesn't appear the key exists
$this->response([
'status' => FALSE,
'status' => false,
'message' => 'Invalid API key'
], REST_Controller::HTTP_BAD_REQUEST); // BAD_REQUEST (400) being the HTTP response code
}
@@ -149,14 +150,14 @@ class Key extends REST_Controller {
if ($this->_update_key($key, ['level' => 0]))
{
$this->response([
'status' => TRUE,
'status' => true,
'message' => 'Key was suspended'
], REST_Controller::HTTP_OK); // OK (200) being the HTTP response code
}
else
{
$this->response([
'status' => FALSE,
'status' => false,
'message' => 'Could not suspend the user'
], REST_Controller::HTTP_INTERNAL_SERVER_ERROR); // INTERNAL_SERVER_ERROR (500) being the HTTP response code
}
@@ -171,20 +172,20 @@ class Key extends REST_Controller {
public function regenerate_post()
{
$old_key = $this->post('key');
$key_details = $this->_get_key($old_key);
$key_details = $this->__getKey($old_key);
// Does this key exist?
if (!$key_details)
{
// It doesn't appear the key exists
$this->response([
'status' => FALSE,
'status' => false,
'message' => 'Invalid API key'
], REST_Controller::HTTP_BAD_REQUEST); // BAD_REQUEST (400) being the HTTP response code
}
// Build a new key
$new_key = $this->_generate_key();
$new_key = $this->__generateKey();
// Insert the new key
if ($this->_insert_key($new_key, ['level' => $key_details->level, 'ignore_limits' => $key_details->ignore_limits]))
@@ -193,14 +194,14 @@ class Key extends REST_Controller {
$this->_update_key($old_key, ['level' => 0]);
$this->response([
'status' => TRUE,
'status' => true,
'key' => $new_key
], REST_Controller::HTTP_CREATED); // CREATED (201) being the HTTP response code
}
else
{
$this->response([
'status' => FALSE,
'status' => false,
'message' => 'Could not save the key'
], REST_Controller::HTTP_INTERNAL_SERVER_ERROR); // INTERNAL_SERVER_ERROR (500) being the HTTP response code
}
@@ -208,7 +209,13 @@ class Key extends REST_Controller {
/* Helper Methods */
private function _generate_key()
/**
* Generate a key
*
* @access private
* @return void
*/
private function __generateKey()
{
do
{
@@ -216,9 +223,9 @@ class Key extends REST_Controller {
$salt = base_convert(bin2hex($this->security->get_random_bytes(64)), 16, 36);
// If an error occurred, then fall back to the previous method
if ($salt === FALSE)
if ($salt === false)
{
$salt = hash('sha256', time() . mt_rand());
$salt = hash('sha256', time().mt_rand());
}
$new_key = substr($salt, 0, config_item('rest_key_length'));
@@ -230,7 +237,14 @@ class Key extends REST_Controller {
/* Private Data Methods */
private function _get_key($key)
/**
* Get a key
*
* @access private
* @param string $key The API-Key.
* @return array
*/
private function __getKey($key)
{
return $this->db
->where(config_item('rest_key_column'), $key)
@@ -238,6 +252,13 @@ class Key extends REST_Controller {
->row();
}
/**
* Check if key exists
*
* @access private
* @param string $key The API-Key.
* @return bool
*/
private function _key_exists($key)
{
return $this->db
@@ -245,6 +266,14 @@ class Key extends REST_Controller {
->count_all_results(config_item('rest_keys_table')) > 0;
}
/**
* Insert a key
*
* @access private
* @param string $key The API-Key.
* @param array $data The API-Key-Data.
* @return bool
*/
private function _insert_key($key, $data)
{
$data[config_item('rest_key_column')] = $key;
@@ -255,6 +284,14 @@ class Key extends REST_Controller {
->insert(config_item('rest_keys_table'));
}
/**
* Update a key
*
* @access private
* @param string $key The API-Key.
* @param array $data The API-Key-Data.
* @return bool
*/
private function _update_key($key, $data)
{
return $this->db
@@ -262,6 +299,13 @@ class Key extends REST_Controller {
->update(config_item('rest_keys_table'), $data);
}
/**
* Delete a key
*
* @access private
* @param string $key The API-Key.
* @return bool
*/
private function _delete_key($key)
{
return $this->db
+29 -19
View File
@@ -11,17 +11,18 @@
* @license MIT
* @link https://github.com/chriskacerguis/codeigniter-restserver
*/
defined('BASEPATH') OR exit('No direct script access allowed');
if (! defined('BASEPATH'))
exit('No direct script access allowed');
// This can be removed if you use __autoload() in config.php OR use Modular Extensions
require APPPATH . '/libraries/REST_Controller.php';
require APPPATH.'/libraries/REST_Controller.php';
class Example extends REST_Controller {
/**
* @copydoc REST_Controller::__construct()
*/
function __construct()
class Example extends REST_Controller
{
/**
* @copydoc REST_Controller::__construct()
*/
public function __construct()
{
// Construct the parent class
parent::__construct();
@@ -33,6 +34,9 @@ class Example extends REST_Controller {
$this->methods['user_delete']['limit'] = 50; // 50 requests per hour per user/key
}
/**
* @return void
*/
public function users_get()
{
// Users from a data store e.g. database
@@ -46,9 +50,9 @@ class Example extends REST_Controller {
// If the id parameter doesn't exist return all the users
if ($id === NULL)
if ($id === null)
{
// Check if the users data store contains users (in case the database result returns NULL)
// Check if the users data store contains users (in case the database result returns null)
if ($users)
{
// Set the response and exit
@@ -58,7 +62,7 @@ class Example extends REST_Controller {
{
// Set the response and exit
$this->response([
'status' => FALSE,
'status' => false,
'message' => 'No users were found'
], REST_Controller::HTTP_NOT_FOUND); // NOT_FOUND (404) being the HTTP response code
}
@@ -66,19 +70,19 @@ class Example extends REST_Controller {
// Find and return a single record for a particular user.
$id = (int) $id;
$id = (int)$id;
// Validate the id.
if ($id <= 0)
{
// Invalid id, set the response and exit.
$this->response(NULL, REST_Controller::HTTP_BAD_REQUEST); // BAD_REQUEST (400) being the HTTP response code
$this->response(null, REST_Controller::HTTP_BAD_REQUEST); // BAD_REQUEST (400) being the HTTP response code
}
// Get the user from the array, using the id as key for retreival.
// Usually a model is to be used for this.
$user = NULL;
$user = null;
if (!empty($users))
{
@@ -98,12 +102,15 @@ class Example extends REST_Controller {
else
{
$this->set_response([
'status' => FALSE,
'status' => false,
'message' => 'User could not be found'
], REST_Controller::HTTP_NOT_FOUND); // NOT_FOUND (404) being the HTTP response code
}
}
/**
* @return void
*/
public function users_post()
{
// $this->some_model->update_user( ... );
@@ -117,15 +124,18 @@ class Example extends REST_Controller {
$this->set_response($message, REST_Controller::HTTP_CREATED); // CREATED (201) being the HTTP response code
}
/**
* @return void
*/
public function users_delete()
{
$id = (int) $this->get('id');
$id = (int)$this->get('id');
// Validate the id.
if ($id <= 0)
{
// Set the response and exit
$this->response(NULL, REST_Controller::HTTP_BAD_REQUEST); // BAD_REQUEST (400) being the HTTP response code
$this->response(null, REST_Controller::HTTP_BAD_REQUEST); // BAD_REQUEST (400) being the HTTP response code
}
// $this->some_model->delete_something($id);
@@ -134,7 +144,7 @@ class Example extends REST_Controller {
'message' => 'Deleted the resource'
];
$this->set_response($message, REST_Controller::HTTP_NO_CONTENT); // NO_CONTENT (204) being the HTTP response code
// NO_CONTENT (204) being the HTTP response code
$this->set_response($message, REST_Controller::HTTP_NO_CONTENT);
}
}
@@ -0,0 +1,188 @@
<?php
/**
* FH-Complete
*
* @package FHC-API
* @author FHC-Team
* @copyright Copyright (c) 2016, fhcomplete.org
* @license GPLv3
* @link http://fhcomplete.org
* @since Version 1.0
* @filesource
*/
// ------------------------------------------------------------------------
if (! defined('BASEPATH'))
exit('No direct script access allowed');
class Person extends APIv1_Controller
{
/**
* Person API constructor.
*/
public function __construct()
{
parent::__construct();
$this->load->model('person/person_model');
}
/**
* @return void
*/
public function person_get()
{
//if(!$this->session_model->validate($this->get('session_id'), $this->get('device_id')))
// $this->response(array(['success' => false, 'message' => 'access denied']), REST_Controller::HTTP_UNAUTHORIZED);
$code = $this->get('code');
$person_id = $this->get('person_id');
if (! is_null($code))
{
$result = $this->person_model->getPersonByCode($code);
}
elseif (! is_null($person_id))
{
$result = $this->person_model->getPerson($person_id);
}
else
{
$result = $this->person_model->getPerson();
}
if ($result['err'])
{
$payload = [
'success' => false,
'message' => $result['msg'].': '.$result['retval']
];
$httpstatus = REST_Controller::HTTP_OK;
}
else
{
// return all available persons
$payload = [
'success' => true,
'message' => 'Persons found'
];
$payload['data'] = $result;
$httpstatus = REST_Controller::HTTP_OK;
}
if (empty($result))
{
$payload = [
'success' => false,
'message' => 'Person not found'
];
$httpstatus = REST_Controller::HTTP_OK;
}
else
{
// return all available persons
$payload = [
'success' => true,
'message' => 'Persons found'
];
$payload['data'] = $result;
$httpstatus = REST_Controller::HTTP_OK;
}
// Set the response and exit
$this->response($payload, $httpstatus);
}
/**
* @return void
*/
public function person_post()
{
$result = $this->person_model->savePerson($this->post());
if ($result != false)
{
$httpstatus = REST_Controller::HTTP_OK;
$payload = [
'success' => true,
'message' => 'Person saved.'
];
$payload['data'] = $result;
}
else
{
$payload = [
'success' => false,
'message' => 'Could not save person.'
];
$httpstatus = REST_Controller::HTTP_OK;
}
$this->response($payload, $httpstatus);
}
/**
* @return void
*/
public function personUpdate_post()
{
$result = $this->person_model->updatePerson($this->post());
if ($result != false)
{
$httpstatus = REST_Controller::HTTP_OK;
$payload = [
'success' => true,
'message' => 'Person updated.'
];
$payload['data'] = $result;
}
else
{
$payload = [
'success' => false,
'message' => 'Could not update person.'
];
$httpstatus = REST_Controller::HTTP_OK;
}
$this->response($payload, $httpstatus);
}
/**
* @return void
*/
public function checkBewerbung_get()
{
$result = $this->person_model->checkBewerbung($this->get("email"), $this->get("studiensemester_kurzbz"));
$httpstatus = REST_Controller::HTTP_OK;
$payload = [
'success' => true,
'message' => 'Bewerbung exists.'
];
$payload['data'] = $result;
$this->response($payload, $httpstatus);
}
/**
* @return void
*/
public function checkZugangscodePerson_get()
{
$result = $this->person_model->checkZugangscodePerson($this->get("code"));
$httpstatus = REST_Controller::HTTP_OK;
if (!empty($result))
{
$payload = [
'success' => true,
'message' => 'Zugangscode exists.'
];
$payload['data'] = $result;
}
else
{
$payload = [
'success' => false,
'message' => 'Zugangscode does not exist.'
];
$httpstatus = REST_Controller::HTTP_OK;
}
$this->response($payload, $httpstatus);
}
}