Move apiRouter and prepare utils
This commit is contained in:
parent
6ae533c642
commit
7c8c565cba
|
@ -0,0 +1,100 @@
|
|||
<?php
|
||||
require_once 'utils.php';
|
||||
function apiRouter (FastRoute\RouteCollector $r) {
|
||||
$r->addRoute(
|
||||
'GET',
|
||||
'/healthcheck',
|
||||
function ($vars) {
|
||||
apiResponse(["state" => "SUCCESS", "description" => ""]);
|
||||
}
|
||||
);
|
||||
$r->addRoute(
|
||||
['GET', 'POST'],
|
||||
'/requestDebug',
|
||||
function ($vars) {
|
||||
apiResponse(["get" => $_GET, "post" => $_POST, "server" => $_SERVER]);
|
||||
}
|
||||
);
|
||||
|
||||
$r->addRoute(
|
||||
['GET'],
|
||||
'/list',
|
||||
function ($vars) {
|
||||
global $db;
|
||||
$response = $db->select("SELECT * FROM `".DB_PREFIX."_profiles` ORDER BY available DESC, chief DESC, services ASC, availability_minutes ASC, name ASC");
|
||||
apiResponse(
|
||||
!is_null($response) ? $response : []
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
$r->addRoute(
|
||||
['GET'],
|
||||
'/logs',
|
||||
function ($vars) {
|
||||
global $db;
|
||||
$response = $db->select("SELECT * FROM `".DB_PREFIX."_log` ORDER BY `timestamp` DESC");
|
||||
apiResponse(
|
||||
!is_null($response) ? $response : []
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
$r->addRoute(
|
||||
['GET'],
|
||||
'/services',
|
||||
function ($vars) {
|
||||
global $db;
|
||||
$response = $db->select("SELECT * FROM `".DB_PREFIX."_services` ORDER BY date DESC, beginning DESC");
|
||||
apiResponse(
|
||||
!is_null($response) ? $response : []
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
$r->addRoute(
|
||||
['GET'],
|
||||
'/trainings',
|
||||
function ($vars) {
|
||||
global $db;
|
||||
$response = $db->select("SELECT * FROM `".DB_PREFIX."_trainings` ORDER BY date DESC, beginning desc");
|
||||
apiResponse(
|
||||
!is_null($response) ? $response : []
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
$r->addRoute(
|
||||
['GET'],
|
||||
'/users',
|
||||
function ($vars) {
|
||||
global $users;
|
||||
apiResponse($users->get_users());
|
||||
}
|
||||
);
|
||||
$r->addRoute(
|
||||
['POST'],
|
||||
'/users',
|
||||
function ($vars) {
|
||||
global $users;
|
||||
apiResponse(["userId" => $users->add_user($_POST["email"], $_POST["name"], $_POST["username"], $_POST["password"], $_POST["phone_number"], $_POST["birthday"], $_POST["chief"], $_POST["driver"], $_POST["hidden"], $_POST["disabled"], "unknown")]);
|
||||
}
|
||||
);
|
||||
$r->addRoute(
|
||||
['GET'],
|
||||
'/users/{userId}',
|
||||
function ($vars) {
|
||||
global $users;
|
||||
apiResponse($users->get_user($vars["userId"]));
|
||||
}
|
||||
);
|
||||
$r->addRoute(
|
||||
['DELETE'],
|
||||
'/users/{userId}',
|
||||
function ($vars) {
|
||||
global $users;
|
||||
$users->remove_user($vars["userId"], "unknown");
|
||||
apiResponse(["status" => "success"]);
|
||||
}
|
||||
);
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
// ** Database settings ** //
|
||||
/* The name of the database for Allerta-vvf */
|
||||
define('DB_NAME', '@@db@@');
|
||||
|
||||
/* Database username */
|
||||
define('DB_USER', '@@user@@');
|
||||
|
||||
/* Database password */
|
||||
define('DB_PASSWORD', '@@password@@');
|
||||
|
||||
/* Database hostname */
|
||||
define('DB_HOST', '@@host@@');
|
||||
|
||||
/* Database hostname */
|
||||
define('DB_PREFIX', '@@prefix@@');
|
||||
|
||||
/* Telegram bot options */
|
||||
define('BOT_TELEGRAM_API_KEY', '');
|
||||
define('BOT_TELEGRAM_USERNAME', '');
|
||||
|
||||
/* Sentry options */
|
||||
define('SENTRY_CSP_REPORT_URI', '');
|
||||
define('SENTRY_ENABLED', false);
|
||||
define('SENTRY_DSN', '');
|
||||
define('SENTRY_ENV', 'prod');
|
|
@ -1,29 +1,16 @@
|
|||
<?php
|
||||
require 'vendor\autoload.php';
|
||||
require 'utils.php';
|
||||
require 'apiRouter.php';
|
||||
|
||||
use Spatie\ArrayToXml\ArrayToXml;
|
||||
$MIMEdetector = new League\MimeTypeDetection\ExtensionMimeTypeDetector();
|
||||
|
||||
$dispatcher = FastRoute\simpleDispatcher(
|
||||
function (FastRoute\RouteCollector $r) {
|
||||
$r->addGroup('/api', function (RouteCollector $r) {
|
||||
$r->addRoute(
|
||||
'GET',
|
||||
'/healthcheck',
|
||||
function ($vars) {
|
||||
apiResponse(["state" => "SUCCESS", "description" => ""]);
|
||||
}
|
||||
);
|
||||
$r->addRoute(
|
||||
['GET', 'POST'],
|
||||
'/requestDebug',
|
||||
function ($vars) {
|
||||
apiResponse(["get" => $_GET, "post" => $_POST, "server" => $_SERVER]);
|
||||
}
|
||||
);
|
||||
$r->addGroup('/api', function (FastRoute\RouteCollector $r) {
|
||||
apiRouter($r);
|
||||
});
|
||||
|
||||
|
||||
$r->addRoute(
|
||||
'GET',
|
||||
'/',
|
||||
|
|
|
@ -0,0 +1,117 @@
|
|||
<?php
|
||||
require_once("vendor/autoload.php");
|
||||
require("config.php");
|
||||
|
||||
$db = \Delight\Db\PdoDatabase::fromDsn(
|
||||
new \Delight\Db\PdoDsn(
|
||||
"mysql:host=".DB_HOST.";dbname=".DB_NAME,
|
||||
DB_USER,
|
||||
DB_PASSWORD
|
||||
)
|
||||
);
|
||||
|
||||
$auth = new \Delight\Auth\Auth($db, null, DB_PREFIX."_");
|
||||
|
||||
final class Role
|
||||
{
|
||||
//https://github.com/delight-im/PHP-Auth/blob/master/src/Role.php
|
||||
const GUEST = \Delight\Auth\Role::AUTHOR;
|
||||
const BASIC_VIEWER = \Delight\Auth\Role::COLLABORATOR;
|
||||
const FULL_VIEWER = \Delight\Auth\Role::CONSULTANT;
|
||||
const EDITOR = \Delight\Auth\Role::CONSUMER;
|
||||
const SUPER_EDITOR = \Delight\Auth\Role::CONTRIBUTOR;
|
||||
const DEVELOPER = \Delight\Auth\Role::DEVELOPER;
|
||||
const TESTER = \Delight\Auth\Role::CREATOR;
|
||||
const EXTERNAL_VIEWER = \Delight\Auth\Role::REVIEWER;
|
||||
const ADMIN = \Delight\Auth\Role::ADMIN;
|
||||
const SUPER_ADMIN = \Delight\Auth\Role::SUPER_ADMIN;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class Users
|
||||
{
|
||||
private $db = null;
|
||||
private $auth = null;
|
||||
private $profile_names = [];
|
||||
private $user_names = [];
|
||||
public $holidays = null;
|
||||
|
||||
public function __construct($db, $auth)
|
||||
{
|
||||
$this->db = $db;
|
||||
$this->auth = $auth;
|
||||
$this->profile_names = $this->db->select("SELECT `id`, `name` FROM `".DB_PREFIX."_profiles`");
|
||||
$this->user_names = $this->db->select("SELECT `id`, `username` FROM `".DB_PREFIX."_users`");
|
||||
//$this->holidays = Yasumi\Yasumi::create(get_option("holidays_provider") ?: "USA", date("Y"), get_option("holidays_language") ?: "en_US");
|
||||
}
|
||||
|
||||
public function add_user($email, $name, $username, $password, $phone_number, $birthday, $chief, $driver, $hidden, $disabled, $inserted_by)
|
||||
{
|
||||
//TODO: save birthday in db
|
||||
//$this->tools->profiler_start("Add user");
|
||||
$userId = $this->auth->admin()->createUserWithUniqueUsername($email, $password, $username);
|
||||
if($userId) {
|
||||
$hidden = $hidden ? 1 : 0;
|
||||
$disabled = $disabled ? 1 : 0;
|
||||
$chief = $chief ? 1 : 0;
|
||||
$driver = $driver ? 1 : 0;
|
||||
$this->db->insert(
|
||||
DB_PREFIX."_profiles",
|
||||
["hidden" => $hidden, "disabled" => $disabled, "name" => $name, "phone_number" => $phone_number, "chief" => $chief, "driver" => $driver]
|
||||
);
|
||||
if($chief == 1) {
|
||||
$this->auth->admin()->addRoleForUserById($userId, Role::FULL_VIEWER);
|
||||
}
|
||||
//$this->log("User added", $userId, $inserted_by);
|
||||
//$this->tools->profiler_stop();
|
||||
return $userId;
|
||||
} else {
|
||||
//$this->tools->profiler_stop();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function get_users()
|
||||
{
|
||||
return $this->db->select("SELECT * FROM `".DB_PREFIX."_profiles`");
|
||||
}
|
||||
|
||||
public function get_user($id)
|
||||
{
|
||||
return $this->db->selectRow("SELECT * FROM `".DB_PREFIX."_profiles` WHERE `id` = ?", [$id]);
|
||||
}
|
||||
|
||||
public function remove_user($id, $removed_by)
|
||||
{
|
||||
//$this->tools->profiler_start("Remove user");
|
||||
$this->db->delete(
|
||||
DB_PREFIX."_users",
|
||||
["id" => $id]
|
||||
);
|
||||
$this->db->delete(
|
||||
DB_PREFIX."_profiles",
|
||||
["id" => $id]
|
||||
);
|
||||
//$this->log("User removed", null, $removed_by);
|
||||
//$this->tools->profiler_stop();
|
||||
}
|
||||
|
||||
public function online_time_update($id=null){
|
||||
//$this->tools->profiler_start("Update online timestamp");
|
||||
if(is_null($id)) $id = $this->auth->getUserId();
|
||||
$time = time();
|
||||
$this->db->update(
|
||||
DB_PREFIX."_profiles",
|
||||
["online_time" => $time],
|
||||
["id" => $id]
|
||||
);
|
||||
//bdump(["id" => $id, "time" => $time]);
|
||||
//$this->tools->profiler_stop();
|
||||
}
|
||||
}
|
||||
|
||||
$users = new Users($db, $auth);
|
Loading…
Reference in New Issue