Skip to content

Commit

Permalink
GH-224 Move login entry rendering into a component
Browse files Browse the repository at this point in the history
  • Loading branch information
mdziekon committed Jun 16, 2022
1 parent 0557864 commit a9768e6
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 31 deletions.
2 changes: 2 additions & 0 deletions modules/settings/_includes.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

$includePath = $_EnginePath . 'modules/settings/';

include($includePath . './components/LoginHistoryEntry/LoginHistoryEntry.component.php');

include($includePath . './utils/content/prepareChangeProcessEmails.content.php');

include($includePath . './utils/errorMappers/tryDeleteUserIgnoreEntries.errorMapper.php');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

namespace UniEngine\Engine\Modules\Settings\Components\LoginHistoryEntry;

use UniEngine\Engine\Includes\Helpers\Common\Collections;

/**
* @param object $props
* @param object $props['entryData']
* @param string $props['userLastIp']
* @param number $props['currentTimestamp']
*
* @return object $result
* @return string $result['componentHTML']
*/
function render($props) {
global $_Lang;

$entryData = $props['entryData'];
$userLastIp = $props['userLastIp'];
$currentTimestamp = $props['currentTimestamp'];

$localTemplateLoader = createLocalTemplateLoader(__DIR__);

$tplBodyCache = [
'body' => $localTemplateLoader('body'),
];

$isSuccessfulLogin = ($entryData['State'] === true);
$isSameIpAsLastSeen = ($entryData['IP'] == $userLastIp);

$dateColorClassNames = Collections\compact([
(
(
!$isSuccessfulLogin &&
!$isSameIpAsLastSeen
) ?
'red' :
null
)
]);
$ipColorClassNames = Collections\compact([
(
$isSuccessfulLogin ?
'lime' :
null
),
(
(
!$isSuccessfulLogin &&
!$isSameIpAsLastSeen
) ?
'red' :
null
)
]);
$stateColorClassNames = Collections\compact([
(
(
!$isSuccessfulLogin &&
!$isSameIpAsLastSeen
) ?
'red' :
null
),
(
(
!$isSuccessfulLogin &&
$isSameIpAsLastSeen
) ?
'orange' :
null
)
]);

$dateColorClassName = reset($dateColorClassNames);
$ipColorClassName = reset($ipColorClassNames);
$stateColorClassName = reset($stateColorClassNames);

$componentTplData = [
'inject_dateColorClass' => $dateColorClassName,
'inject_ipColorClass' => $ipColorClassName,
'inject_stateColorClass' => $stateColorClassName,
'data_loginTimeAbsolute' => prettyDate('d m Y, H:i:s', $entryData['Time'], 1),
'data_loginTimeRelative' => implode(
' ',
[
pretty_time($currentTimestamp - $entryData['Time'], true, 'D'),
$_Lang['Logons_ago']
]
),
'data_ip' => $entryData['IP'],
'data_stateLabel' => (
$isSuccessfulLogin ?
$_Lang['Logons_Success'] :
$_Lang['Logons_Failed']
),
];

$componentHTML = parsetemplate(
$tplBodyCache['body'],
$componentTplData
);

return [
'componentHTML' => $componentHTML
];
}

?>
14 changes: 14 additions & 0 deletions modules/settings/components/LoginHistoryEntry/body.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<tr class="logon">
<th class="{inject_dateColorClass}">
{data_loginTimeAbsolute}
</th>
<th class="{inject_dateColorClass}">
{data_loginTimeRelative}
</th>
<th class="{inject_ipColorClass}">
{data_ip}
</th>
<th class="{inject_stateColorClass}">
{data_stateLabel}
</th>
</tr>
5 changes: 5 additions & 0 deletions modules/settings/components/LoginHistoryEntry/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

header("Location: ../index.php");

?>
5 changes: 5 additions & 0 deletions modules/settings/components/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

header("Location: ../index.php");

?>
43 changes: 12 additions & 31 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -1126,44 +1126,25 @@

array_multisort($LogonList, SORT_DESC, $LogonTimes);
$LimitCounter = $LogonLIMIT;
foreach($LogonList as $LogonData)
{
if($LimitCounter <= 0)
{

$_Lang['ParseLogonsList'] = [];

foreach ($LogonList as $LogonData) {
if ($LimitCounter <= 0) {
break;
}
if($LogonData['IP'] == $_User['user_lastip'])
{
$LogonData['IPColor'] = 'lime';
}
if($LogonData['State'] === false)
{
if($LogonData['IP'] != $_User['user_lastip'])
{
$LogonData['DateColor'] = 'red';
$LogonData['IPColor'] = 'red';
$LogonData['StateColor'] = 'red';
}
else
{
$LogonData['StateColor'] = 'orange';
}
}

$_Lang['ParseLogonsList'][] = Settings\Components\LoginHistoryEntry\render([
'entryData' => $LogonData,
'userLastIp' => $_User['user_lastip'],
'currentTimestamp' => $Now,
])['componentHTML'];

$ThisRow = '<tr class="logon">';
$ThisRow .= '<th'.(!empty($LogonData['DateColor']) ? ' class="'.$LogonData['DateColor'].'"' : '').'>'.prettyDate('d m Y, H:i:s', $LogonData['Time'], 1).'</th>';
$ThisRow .= '<th'.(!empty($LogonData['DateColor']) ? ' class="'.$LogonData['DateColor'].'"' : '').'>'.pretty_time($Now - $LogonData['Time'], true, 'D').' '.$_Lang['Logons_ago'].'</th>';
$ThisRow .= '<th'.(!empty($LogonData['IPColor']) ? ' class="'.$LogonData['IPColor'].'"' : '').'>'.$LogonData['IP'].'</th>';
$ThisRow .= '<th'.(!empty($LogonData['StateColor']) ? ' class="'.$LogonData['StateColor'].'"' : '').'>'.($LogonData['State'] === true ? $_Lang['Logons_Success'] : $_Lang['Logons_Failed']).'</th>';
$ThisRow .= '</tr>';
$_Lang['ParseLogonsList'][] = $ThisRow;
$LimitCounter -= 1;
}

$_Lang['ParseLogonsList'] = implode('', $_Lang['ParseLogonsList']);
}
else
{
} else {
$_Lang['ParseLogonsList'] = '<tr><th colspan="4">'.$_Lang['Logons_ListEmpty'].'</th></tr>';
}

Expand Down

0 comments on commit a9768e6

Please sign in to comment.