Skip to content

Commit fb20cca

Browse files
committed
Add login page
1 parent daba1a7 commit fb20cca

19 files changed

+413
-128
lines changed

config.php

Lines changed: 0 additions & 36 deletions
This file was deleted.

docs/README.md

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Free MongoDB GUI powered by PHP
22

33
Visually administrate your MongoDB database. Create, read, update and delete your documents.<br>
4-
Autocompletion is available for collection fields and MongoDB keywords via `Ctrl` + `Space` keys.
4+
Autocompletion is available for collection fields and MongoDB keywords via `Ctrl` + `Space` keys.<br>
5+
You can also create and drop indexes.
56

67
Screenshots
78
-----------
@@ -13,16 +14,10 @@ Screenshots
1314
Installation
1415
------------
1516

16-
1. `git clone` current repository somewhere in the cloud or on your local machine.<br>
17-
**Warning: If you choose cloud option. Be sure to secure folder with a *.htpasswd*.**
17+
1. `git clone` current repository somewhere in the cloud or on your local machine.
1818
2. Be sure to have PHP >= 7 with [MongoDB extension](https://www.php.net/manual/en/mongodb.installation.php) enabled in this environment.
1919
3. Run `composer install` at project's root directory to install all PHP dependencies.
2020

21-
Configuration
22-
-------------
23-
24-
Open *config.php* file located at project's root directory. Edit `MPG_MONGODB*` constants.
25-
2621
Thanks
2722
------
2823

index.php

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
use Limber\Application;
44
use Capsule\Factory\ServerRequestFactory;
5+
use Limber\Exceptions\NotFoundHttpException;
56

6-
require __DIR__ . '/autoload.php';
7-
require __DIR__ . '/config.php';
8-
require __DIR__ . '/routes.php';
7+
session_start();
98

109
/**
1110
* Application name.
@@ -19,7 +18,7 @@
1918
*
2019
* @var string
2120
*/
22-
define('MPG_APP_VERSION', '0.9.9');
21+
define('MPG_APP_VERSION', '1.0.0');
2322

2423
/**
2524
* Development mode?
@@ -35,7 +34,37 @@
3534
*/
3635
define('MPG_VIEWS_PATH', __DIR__ . '/views');
3736

37+
$baseUrl = ( isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) ) ? 'https' : 'http';
38+
$baseUrl .= '://' . $_SERVER['HTTP_HOST'];
39+
$serverPath = str_replace('\\', '/', dirname($_SERVER['REQUEST_URI']));
40+
$serverPath = ( $serverPath === '/' ) ? '' : $serverPath;
41+
$baseUrl .= $serverPath;
42+
43+
/**
44+
* Server path. XXX Without trailing slash.
45+
*
46+
* @var string
47+
*/
48+
define('MPG_SERVER_PATH', $serverPath);
49+
50+
/**
51+
* Base URL. XXX Without trailing slash.
52+
*
53+
* @var string
54+
*/
55+
define('MPG_BASE_URL', $baseUrl);
56+
57+
require __DIR__ . '/autoload.php';
58+
require __DIR__ . '/routes.php';
59+
3860
$application = new Application($router);
3961
$serverRequest = ServerRequestFactory::createFromGlobals();
40-
$response = $application->dispatch($serverRequest);
62+
63+
// XXX This hack makes index to work in sub-folder case.
64+
try {
65+
$response = $application->dispatch($serverRequest);
66+
} catch (NotFoundHttpException $e) {
67+
header('Location: ' . $_SERVER['REQUEST_URI'] . '/index');
68+
}
69+
4170
$application->send($response);

routes.php

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,111 @@
11
<?php
22

33
use Limber\Router\Router;
4+
use Controllers\LoginController;
45
use Controllers\DatabaseController;
56
use Controllers\CollectionController;
7+
use Controllers\Controller;
68

79
$router = new Router();
810

911
$router->get('/', function() {
10-
header('Location: /queryDatabase');
11-
exit;
12+
13+
LoginController::ensureUserIsLogged();
14+
15+
Controller::redirectTo('/queryDatabase');
16+
17+
});
18+
19+
// XXX This hack makes index to work in sub-folder case.
20+
$router->get(MPG_SERVER_PATH . '/index', function() {
21+
22+
LoginController::ensureUserIsLogged();
23+
24+
Controller::redirectTo('/queryDatabase');
25+
1226
});
1327

1428
$router->get(
15-
'/createDatabase',
29+
MPG_SERVER_PATH . '/login',
30+
LoginController::class . '@renderViewAction'
31+
);
32+
33+
$router->post(
34+
MPG_SERVER_PATH . '/login',
35+
LoginController::class . '@renderViewAction'
36+
);
37+
38+
$router->get(
39+
MPG_SERVER_PATH . '/createDatabase',
1640
DatabaseController::class . '@renderCreateViewAction'
1741
);
1842

1943
$router->get(
20-
'/queryDatabase',
44+
MPG_SERVER_PATH . '/queryDatabase',
2145
DatabaseController::class . '@renderQueryViewAction'
2246
);
2347

2448
$router->post(
25-
'/ajax/database/listCollections',
49+
MPG_SERVER_PATH . '/ajaxDatabaseListCollections',
2650
DatabaseController::class . '@listCollectionsAction'
2751
);
2852

2953
$router->post(
30-
'/ajax/database/createCollection',
54+
MPG_SERVER_PATH . '/ajaxDatabaseCreateCollection',
3155
DatabaseController::class . '@createCollectionAction'
3256
);
3357

3458
$router->post(
35-
'/ajax/collection/insertOne',
59+
MPG_SERVER_PATH . '/ajaxCollectionInsertOne',
3660
CollectionController::class . '@insertOneAction'
3761
);
3862

3963
$router->post(
40-
'/ajax/collection/count',
64+
MPG_SERVER_PATH . '/ajaxCollectionCount',
4165
CollectionController::class . '@countAction'
4266
);
4367

4468
$router->post(
45-
'/ajax/collection/deleteOne',
69+
MPG_SERVER_PATH . '/ajaxCollectionDeleteOne',
4670
CollectionController::class . '@deleteOneAction'
4771
);
4872

4973
$router->post(
50-
'/ajax/collection/find',
74+
MPG_SERVER_PATH . '/ajaxCollectionFind',
5175
CollectionController::class . '@findAction'
5276
);
5377

5478
$router->post(
55-
'/ajax/collection/updateOne',
79+
MPG_SERVER_PATH . '/ajaxCollectionUpdateOne',
5680
CollectionController::class . '@updateOneAction'
5781
);
5882

5983
$router->post(
60-
'/ajax/collection/enumFields',
84+
MPG_SERVER_PATH . '/ajaxCollectionEnumFields',
6185
CollectionController::class . '@enumFieldsAction'
6286
);
6387

6488
$router->get(
65-
'/manageIndexes',
89+
MPG_SERVER_PATH . '/manageIndexes',
6690
CollectionController::class . '@renderIndexesViewAction'
6791
);
6892

6993
$router->post(
70-
'/ajax/collection/createIndex',
94+
MPG_SERVER_PATH . '/ajaxCollectionCreateIndex',
7195
CollectionController::class . '@createIndexAction'
7296
);
7397

7498
$router->post(
75-
'/ajax/collection/listIndexes',
99+
MPG_SERVER_PATH . '/ajaxCollectionListIndexes',
76100
CollectionController::class . '@listIndexesAction'
77101
);
78102

79103
$router->post(
80-
'/ajax/collection/dropIndex',
104+
MPG_SERVER_PATH . '/ajaxCollectionDropIndex',
81105
CollectionController::class . '@dropIndexAction'
82106
);
107+
108+
$router->get(
109+
MPG_SERVER_PATH . '/logout',
110+
LoginController::class . '@logoutAction'
111+
);

src/Controllers/CollectionController.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ class CollectionController extends Controller {
1111

1212
public function renderIndexesViewAction() : Response {
1313

14+
LoginController::ensureUserIsLogged();
15+
1416
return new Response(200, $this->renderView('collection.indexes', [
1517
'databaseNames' => DatabaseController::getDatabaseNames()
1618
]));

src/Controllers/Controller.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@
77

88
class Controller {
99

10+
/**
11+
* Redirects to a route.
12+
*
13+
* @param string $route
14+
*/
15+
public static function redirectTo(string $route) {
16+
17+
header('Location: ' . MPG_BASE_URL . $route); exit;
18+
19+
}
20+
1021
/**
1122
* If it exists: returns request body.
1223
*

src/Controllers/DatabaseController.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public static function getDatabaseNames() : array {
1313

1414
$databaseNames = [];
1515

16-
if ( !empty(MPG_MONGODB_DATABASE) ) {
17-
$databaseNames[] = MPG_MONGODB_DATABASE;
16+
if ( isset($_SESSION['mpg']['mongodb_database']) ) {
17+
$databaseNames[] = $_SESSION['mpg']['mongodb_database'];
1818
} else {
1919

2020
try {
@@ -33,11 +33,15 @@ public static function getDatabaseNames() : array {
3333

3434
public function renderCreateViewAction() : Response {
3535

36+
LoginController::ensureUserIsLogged();
37+
3638
return new Response(200, $this->renderView('database.create'));
3739

3840
}
3941

4042
public function renderQueryViewAction() : Response {
43+
44+
LoginController::ensureUserIsLogged();
4145

4246
return new Response(200, $this->renderView('database.query', [
4347
'databaseNames' => self::getDatabaseNames()
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace Controllers;
4+
5+
use Capsule\Response;
6+
7+
class LoginController extends Controller {
8+
9+
public static function ensureUserIsLogged() {
10+
11+
if ( !isset($_SESSION['mpg']['user_is_logged']) ) {
12+
13+
Controller::redirectTo('/login#');
14+
15+
}
16+
17+
}
18+
19+
public function processFormData() : array {
20+
21+
$errors = [];
22+
23+
$_SESSION['mpg'] = [];
24+
25+
if ( isset($_POST['user']) && !empty($_POST['user']) ) {
26+
$_SESSION['mpg']['mongodb_user'] = $_POST['user'];
27+
}
28+
29+
if ( isset($_POST['password']) && !empty($_POST['password']) ) {
30+
$_SESSION['mpg']['mongodb_password'] = $_POST['password'];
31+
}
32+
33+
if ( isset($_POST['host']) && !empty($_POST['host']) ) {
34+
$_SESSION['mpg']['mongodb_host'] = $_POST['host'];
35+
} else {
36+
$errors[] = 'Host';
37+
}
38+
39+
if ( isset($_POST['port']) && !empty($_POST['port']) ) {
40+
$_SESSION['mpg']['mongodb_port'] = $_POST['port'];
41+
} else {
42+
$errors[] = 'Port';
43+
}
44+
45+
if ( isset($_POST['database']) && !empty($_POST['database']) ) {
46+
$_SESSION['mpg']['mongodb_database'] = $_POST['database'];
47+
}
48+
49+
return $errors;
50+
51+
}
52+
53+
public function renderViewAction() : Response {
54+
55+
if ( isset($_POST['login']) ) {
56+
57+
$errors = $this->processFormData();
58+
59+
if ( count($errors) >= 1 ) {
60+
61+
return new Response(200, $this->renderView('login', [
62+
'errors' => $errors
63+
]));
64+
65+
} else {
66+
67+
$_SESSION['mpg']['user_is_logged'] = true;
68+
Controller::redirectTo('/index');
69+
70+
}
71+
72+
} else {
73+
return new Response(200, $this->renderView('login'));
74+
}
75+
76+
}
77+
78+
public function logoutAction() {
79+
80+
$_SESSION['mpg'] = [];
81+
82+
Controller::redirectTo('/login');
83+
84+
}
85+
86+
}

0 commit comments

Comments
 (0)