Skip to content

Commit 0a242e2

Browse files
Merge pull request #32 from OnePlc/1.0.25-dev
1.0.25 dev
2 parents f52ec3c + 7f3db23 commit 0a242e2

File tree

11 files changed

+346
-36
lines changed

11 files changed

+346
-36
lines changed

CHANGELOG.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
# Changelog
22

3-
All notable changes to this project will be documented in this file, in reverse chronological order by release.
3+
All notable changes to oneplace-event will be documented in this file.
44

5-
## 1.0.0 - TBD
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
67

7-
### Added
8+
## [1.0.25] -
89

9-
- Its a composer package now
10+
### Added
11+
- User Interface for Application Firewall (pre-alpha)
12+
- Module can now be updated
13+
- Firewall Whitelist for Logged-in users
1014

1115
### Changed
1216

13-
- 100% of 0.9.x Codebase
14-
15-
### Deprecated
17+
## [1.0.24] - 2021-03-31
1618

17-
- 100% of 0.9.x Codebase
18-
19-
### Removed
19+
### Added
2020

21-
- 100% of 0.9.x Codebase
21+
- Theme and Language Fields in User Form
2222

23-
### Fixed
23+
### Changed
2424

25-
- Removed dependencies
25+
- New Login Route to /home instead of /

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "onePlace User Module",
44
"type": "oneplace-module",
55
"license": "BSD-3-Clause",
6-
"version": "1.0.24",
6+
"version": "1.0.25",
77
"keywords": [
88
"laminas",
99
"mvc",

config/module.config.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@
5252
],
5353
],
5454
],
55+
'user-update' => [
56+
'type' => Literal::class,
57+
'options' => [
58+
'route' => '/user/update',
59+
'defaults' => [
60+
'controller' => Controller\UserController::class,
61+
'action' => 'update',
62+
],
63+
],
64+
],
5565
'signup' => [
5666
'type' => Segment::class,
5767
'options' => [
@@ -131,6 +141,20 @@
131141
],
132142
],
133143
],
144+
'user-firewall' => [
145+
'type' => Segment::class,
146+
'options' => [
147+
'route' => '/firewall[/:action[/:id]]',
148+
'constraints' => [
149+
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
150+
'id' => '[0-9]+',
151+
],
152+
'defaults' => [
153+
'controller' => Controller\FirewallController::class,
154+
'action' => 'index',
155+
],
156+
],
157+
],
134158
],
135159
],
136160

data/data.sql

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ INSERT INTO `core_index_table` (`table_name`, `form`, `label`) VALUES
6060
INSERT INTO `permission` (`permission_key`, `module`, `label`, `nav_label`, `nav_href`, `show_in_menu`, `needs_globaladmin`) VALUES
6161
('add', 'OnePlace\\User\\Controller\\UserController', 'Add', '', '', 0, 0),
6262
('edit', 'OnePlace\\User\\Controller\\UserController', 'Edit', '', '', 0, 0),
63+
('update', 'OnePlace\\User\\Controller\\UserController', 'Update Module', '', '', 0, 0),
6364
('index', 'OnePlace\\User\\Controller\\UserController', 'Index', 'Users', '/user', 1, 0),
6465
('updateindexcolumnsort', 'OnePlace\\User\\Controller\\UserController', 'Update Column Index', '', '', 0, 0),
6566
('settheme', 'OnePlace\\User\\Controller\\UserController', 'Set own Theme', '', '', 0, 0),
@@ -72,7 +73,8 @@ INSERT INTO `permission` (`permission_key`, `module`, `label`, `nav_label`, `nav
7273
('add', 'OnePlace\\User\\Controller\\ApiController', 'Create API Key', '', '', 0, 1),
7374
('globaladmin', 'OnePlace\\Core', 'Super Admin', '', '', 0, 1),
7475
('updatesetting', 'OnePlace\\User\\Controller\\UserController', 'Update Personal Setting', '', '', 0, 0),
75-
('languages', 'OnePlace\\User\\Controller\\UserController', 'Language Selection', '', '', 0, 0);
76+
('languages', 'OnePlace\\User\\Controller\\UserController', 'Language Selection', '', '', 0, 0),
77+
('index', 'OnePlace\\User\\Controller\\FirewallController', 'Firewall Index', '', '', 0, 0);
7678

7779
--
7880
-- Default Leveling Settings
@@ -193,4 +195,5 @@ INSERT INTO `settings` (`settings_key`, `settings_value`) VALUES ('user-icon', '
193195
--
194196
-- basic whitelist
195197
--
196-
INSERT INTO `settings` (`settings_key`, `settings_value`) VALUES ('firewall-whitelist', '[\"setup\",\"login\",\"reset-pw\",\"forgot-pw\",\"home\"]');
198+
INSERT INTO `settings` (`settings_key`, `settings_value`) VALUES ('firewall-whitelist', '[\"setup\",\"login\",\"reset-pw\",\"forgot-pw\",\"home\"]');
199+
INSERT INTO `settings` (`settings_key`, `settings_value`) VALUES ('firewall-user-whitelist', '[\"login\",\"reset-pw\",\"forgot-pw\",\"home\",\"app-home\",\"route\"]');

data/update_1.0.24-1.0.25.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
INSERT INTO `settings` (`settings_key`, `settings_value`) VALUES ('firewall-user-whitelist', '[\"login\",\"reset-pw\",\"forgot-pw\",\"home\",\"app-home\",\"route\"]');
2+
3+
INSERT INTO `permission` (`permission_key`, `module`, `label`, `nav_label`, `nav_href`, `show_in_menu`, `needs_globaladmin`) VALUES
4+
('index', 'OnePlace\\User\\Controller\\FirewallController', 'Firewall Index', '', '', 0, 0);
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* FirewallController.php - Firewall Controller
4+
*
5+
* Main Controller for Application Firewall Management
6+
*
7+
* @category Controller
8+
* @package User
9+
* @author Verein onePlace
10+
* @copyright (C) 2021 Verein onePlace <[email protected]>
11+
* @license https://opensource.org/licenses/BSD-3-Clause
12+
* @version 1.0.0
13+
* @since 1.0.24
14+
*/
15+
16+
declare(strict_types=1);
17+
18+
namespace OnePlace\User\Controller;
19+
20+
use Application\Controller\CoreController;
21+
use OnePlace\User\Model\Apikey;
22+
use OnePlace\User\Model\ApikeyTable;
23+
use OnePlace\User\Model\UserTable;
24+
use Laminas\View\Model\ViewModel;
25+
use Laminas\Db\Adapter\AdapterInterface;
26+
use Laminas\Db\TableGateway\TableGateway;
27+
use Laminas\Paginator\Paginator;
28+
use Laminas\Paginator\Adapter\DbSelect;
29+
use Laminas\Db\Sql\Select;
30+
use Laminas\Db\Sql\Where;
31+
use Laminas\Math\Rand;
32+
33+
class FirewallController extends CoreController
34+
{
35+
/**
36+
* Skeleton Table Object
37+
*
38+
* @since 1.0.0
39+
*/
40+
private $oTableGateway;
41+
42+
/**
43+
* ApiController constructor.
44+
*
45+
* @param AdapterInterface $oDbAdapter
46+
* @param UserTable $oTableGateway
47+
* @since 1.0.0
48+
*/
49+
public function __construct(AdapterInterface $oDbAdapter, UserTable $oTableGateway, $oServiceManager)
50+
{
51+
parent::__construct($oDbAdapter, $oTableGateway, $oServiceManager);
52+
$this->oTableGateway = $oTableGateway;
53+
$this->sSingleForm = 'firewall-single';
54+
}
55+
56+
/**
57+
* Firewall Home - Main Index
58+
*
59+
* @return bool - no View File
60+
* @since 1.0.0
61+
*/
62+
public function indexAction()
63+
{
64+
$this->setThemeBasedLayout('firewall');
65+
66+
return new ViewModel([]);
67+
}
68+
}

src/Controller/UserController.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,4 +803,69 @@ public function copyAction()
803803
return $this->redirect()->toRoute('user', ['action' => 'view','id' => $iNewUserID]);
804804
}
805805
}
806+
807+
/**
808+
* Update Module to the latest version
809+
*
810+
* @return ViewModel
811+
* @since 1.0.25
812+
*/
813+
public function updateAction()
814+
{
815+
# Set Layout based on users theme
816+
$this->setThemeBasedLayout('user');
817+
818+
$oModTbl = new TableGateway('core_module', CoreController::$oDbAdapter);
819+
$oCurrentMod = $oModTbl->select([
820+
'module_key'=>'oneplace-user',
821+
])->current();
822+
823+
$oRequest = $this->getRequest();
824+
if(! $oRequest->isPost()) {
825+
return new ViewModel([
826+
'oCurrentModule' => $oCurrentMod,
827+
]);
828+
}
829+
830+
$sNewVer = \OnePlace\User\Module::VERSION;
831+
832+
$sUpdateSQL = \OnePlace\User\Module::getModuleDir().'data/update_'.$oCurrentMod->version.'-'.$sNewVer.'.sql';
833+
if(file_exists($sUpdateSQL)) {
834+
$this->parseSQLInstallFile($sUpdateSQL,CoreController::$oDbAdapter);
835+
}
836+
$oModTbl->update([
837+
'version' => $sNewVer,
838+
],[
839+
'module_key'=>'oneplace-user',
840+
]);
841+
842+
$this->flashMessenger()->addSuccessMessage('User Module successfully updated to version '.$sNewVer);
843+
844+
return $this->redirect()->toRoute('application', ['action' => 'checkforupdates']);
845+
}
846+
847+
/**
848+
* Parse SQL File from Installer and save to database
849+
*
850+
* @param string $sFile location of sql file
851+
* @param AdapterInterface $oAdapter database connection
852+
* @since 1.0.2.1
853+
*/
854+
private function parseSQLInstallFile($sFile,$oAdapter) {
855+
$templine = '';
856+
$lines = file($sFile);
857+
// Loop through each line
858+
foreach ($lines as $line) {
859+
if (substr($line, 0, 2) == '--' || $line == '')
860+
continue;
861+
// Add this line to the current segment
862+
$templine .= $line;
863+
// If it has a semicolon at the end, it's the end of the query
864+
if (substr(trim($line), -1, 1) == ';')
865+
{
866+
$results = $oAdapter->query($templine, $oAdapter::QUERY_MODE_EXECUTE);
867+
$templine = '';
868+
}
869+
}
870+
}
806871
}

src/Module.php

Lines changed: 62 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Module
3434
*
3535
* @since 1.0.0
3636
*/
37-
const VERSION = '1.0.24';
37+
const VERSION = '1.0.25';
3838

3939
/**
4040
* Load module config file
@@ -47,6 +47,17 @@ public function getConfig() : array
4747
return include __DIR__ . '/../config/module.config.php';
4848
}
4949

50+
/**
51+
* Get Modules File Directory
52+
*
53+
* @return string
54+
* @since 1.0.25
55+
*/
56+
public static function getModuleDir() : string
57+
{
58+
return __DIR__.'/../';
59+
}
60+
5061
/**
5162
* On Bootstrap - is executed on every page request
5263
*
@@ -71,6 +82,8 @@ function ($e) {
7182
$app = $e->getApplication();
7283
$routeMatch = $e->getRouteMatch();
7384
$sm = $app->getServiceManager();
85+
$sRouteName = $routeMatch->getMatchedRouteName();
86+
$aRouteInfo = $routeMatch->getParams();
7487

7588
$oDbAdapter = $sm->get(AdapterInterface::class);
7689

@@ -103,11 +116,19 @@ function ($e) {
103116
$manager = new SessionManager($config);
104117
**/
105118

106-
$sRouteName = $routeMatch->getMatchedRouteName();
107-
$aRouteInfo = $routeMatch->getParams();
108-
109119
$app->getMvcEvent()->getViewModel()->setVariables(['sRouteName' => $sRouteName]);
110120

121+
/**
122+
* preparign for firewall access log
123+
124+
$log = "User: ".$_SERVER['REMOTE_ADDR'].' - '.date("F j, Y, g:i a").PHP_EOL.
125+
"URL: ".$sRouteName.PHP_EOL.
126+
"Attempt: ".('Success').PHP_EOL.
127+
"-------------------------".PHP_EOL;
128+
//Save string to log, use FILE_APPEND to append.
129+
file_put_contents('./log_'.date("Y-m-d").'.log', $log, FILE_APPEND);
130+
* */
131+
111132
# get session
112133
$container = new Container('plcauth');
113134
$bLoggedIn = false;
@@ -125,18 +146,27 @@ function ($e) {
125146

126147
$bIsSetupController = stripos($aRouteInfo['controller'], 'InstallController');
127148
if ($bIsSetupController === false) {
128-
if (! $container->oUser->hasPermission($aRouteInfo['action'], $aRouteInfo['controller'])
129-
&& $sRouteName != 'denied') {
130-
$response = $e->getResponse();
131-
$response->getHeaders()->addHeaderLine(
132-
'Location',
133-
$e->getRouter()->assemble(
134-
['permission' => $aRouteInfo['action'].'-'.str_replace(['\\'],['-'],$aRouteInfo['controller'])],
135-
['name' => 'denied']
136-
)
137-
);
138-
$response->setStatusCode(302);
139-
return $response;
149+
$aWhiteListedRoutes = [];
150+
$aWhiteListedRoutesDB = json_decode(CoreController::$aGlobalSettings['firewall-user-whitelist']);
151+
if(is_array($aWhiteListedRoutesDB)) {
152+
foreach($aWhiteListedRoutesDB as $sWhiteRoute) {
153+
$aWhiteListedRoutes[$sWhiteRoute] = [];
154+
}
155+
}
156+
if(!array_key_exists($sRouteName, $aWhiteListedRoutes)) {
157+
if (! $container->oUser->hasPermission($aRouteInfo['action'], $aRouteInfo['controller'])
158+
&& $sRouteName != 'denied') {
159+
$response = $e->getResponse();
160+
$response->getHeaders()->addHeaderLine(
161+
'Location',
162+
$e->getRouter()->assemble(
163+
['permission' => $aRouteInfo['action'].'-'.str_replace(['\\'],['-'],$aRouteInfo['controller'])],
164+
['name' => 'denied']
165+
)
166+
);
167+
$response->setStatusCode(302);
168+
return $response;
169+
}
140170
}
141171
} else {
142172
# let user install module
@@ -165,13 +195,17 @@ function ($e) {
165195

166196
# Whitelisted routes that need no authentication
167197
$aWhiteListedRoutes = [
168-
'tokenlogin' => [],
169198
'setup' => [],
170199
'login' => [],
171-
'reset-pw' => [],
172-
'forgot-pw' => [],
173-
'register' => [],
174200
];
201+
if(isset(CoreController::$aGlobalSettings['firewall-whitelist'])) {
202+
$aWhiteListedRoutesDB = json_decode(CoreController::$aGlobalSettings['firewall-whitelist']);
203+
if(is_array($aWhiteListedRoutesDB)) {
204+
foreach($aWhiteListedRoutesDB as $sWhiteRoute) {
205+
$aWhiteListedRoutes[$sWhiteRoute] = [];
206+
}
207+
}
208+
}
175209

176210
/**
177211
* Redirect to Login Page if not logged in
@@ -278,6 +312,14 @@ public function getControllerConfig() : array
278312
$container
279313
);
280314
},
315+
Controller\FirewallController::class => function ($container) {
316+
$oDbAdapter = $container->get(AdapterInterface::class);
317+
return new Controller\FirewallController(
318+
$oDbAdapter,
319+
$container->get(Model\UserTable::class),
320+
$container
321+
);
322+
},
281323
],
282324
];
283325
}

0 commit comments

Comments
 (0)