Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 23 additions & 22 deletions src/Admin/src/Service/AdminLoginService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
use Dot\DependencyInjection\Attribute\Inject;
use Dot\GeoIP\Service\LocationService;
use Exception;
use stdClass;

use function get_browser;
use function in_array;
use function ini_get;

class AdminLoginService implements AdminLoginServiceInterface
{
Expand Down Expand Up @@ -49,16 +52,12 @@ public function getAdminLogins(array $params): array
'login.continent',
'login.organization',
'login.deviceType',
'login.deviceBrand',
'login.deviceModel',
'login.isMobile',
'login.osName',
'login.osVersion',
'login.osPlatform',
'login.clientType',
'login.clientName',
'login.clientEngine',
'login.clientVersion',
'login.isCrawler',
'login.loginStatus',
'login.identity',
'login.created',
Expand Down Expand Up @@ -99,35 +98,37 @@ public function logSuccessfulLogin(array $serverParams, string $name): AdminLogi
*/
private function logAdminVisit(array $serverParams, string $name, SuccessFailureEnum $status): AdminLogin
{
/**
* For device information
*
* @see https://github.com/dotkernel/dot-user-agent-sniffer
*/

$ipAddress = IpService::getUserIp($serverParams);

$country = $this->locationService->getCountry($ipAddress)->getName();
$continent = $this->locationService->getContinent($ipAddress)->getName();
$organization = $this->locationService->getOrganization($ipAddress)->getName();

/**
* For browscap information
*
* @see https://www.php.net/manual/en/function.get-browser.php
*/
$browser = new stdClass();
if (ini_get('browscap')) {
$browser = get_browser($_SERVER['HTTP_USER_AGENT']);
}

$adminLogin = (new AdminLogin())
->setAdminIp($this->locationService->obfuscateIpAddress($ipAddress))
->setContinent($continent)
->setCountry($country)
->setOrganization($organization)
->setDeviceType(null)
->setDeviceBrand(null)
->setDeviceModel(null)
->setIsMobile(YesNoEnum::No)
->setOsName(null)
->setOsVersion(null)
->setOsPlatform(null)
->setClientType(null)
->setClientName(null)
->setClientEngine(null)
->setClientVersion(null)
->setDeviceType(! empty($browser->device_type) ? $browser->device_type : null)
->setIsMobile(
! empty($browser->ismobiledevice) ? YesNoEnum::Yes : YesNoEnum::No
)
->setOsName(! empty($browser->platform) ? $browser->platform : null)
->setOsVersion(! empty($browser->platform_version) ? $browser->platform_version : null)
->setClientType(! empty($browser->browser_type) ? $browser->browser_type : null)
->setClientName(! empty($browser->browser) ? $browser->browser : null)
->setLoginStatus($status)
->setIsCrawler(! empty($browser->crawler) ? YesNoEnum::Yes : YesNoEnum::No)
->setIdentity($name);

$this->adminLoginRepository->saveResource($adminLogin);
Expand Down
28 changes: 9 additions & 19 deletions src/Admin/templates/admin/list-admin-login.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,6 @@
<th class="column-login-deviceType">
{{ sortableColumn('admin::list-admin-login', {}, pagination.queryParams, 'login.deviceType', 'Device Type') }}
</th>
<th class="column-login-deviceBrand">
{{ sortableColumn('admin::list-admin-login', {}, pagination.queryParams, 'login.deviceBrand', 'Device Brand') }}
</th>
<th class="column-login-deviceModel">
{{ sortableColumn('admin::list-admin-login', {}, pagination.queryParams, 'login.deviceModel', 'Device Model') }}
</th>
<th class="column-login-isMobile">
{{ sortableColumn('admin::list-admin-login', {}, pagination.queryParams, 'login.isMobile', 'Is Mobile') }}
</th>
Expand All @@ -91,20 +85,14 @@
<th class="column-login-osVersion">
{{ sortableColumn('admin::list-admin-login', {}, pagination.queryParams, 'login.osVersion', 'Os Version') }}
</th>
<th class="column-login-osPlatform">
{{ sortableColumn('admin::list-admin-login', {}, pagination.queryParams, 'login.osPlatform', 'Os Platform') }}
</th>
<th class="column-login-clientType">
{{ sortableColumn('admin::list-admin-login', {}, pagination.queryParams, 'login.clientType', 'Client Type') }}
</th>
<th class="column-login-clientName">
{{ sortableColumn('admin::list-admin-login', {}, pagination.queryParams, 'login.clientName', 'Client Name') }}
</th>
<th class="column-login-clientEngine">
{{ sortableColumn('admin::list-admin-login', {}, pagination.queryParams, 'login.clientEngine', 'Client Engine') }}
</th>
<th class="column-login-clientVersion">
{{ sortableColumn('admin::list-admin-login', {}, pagination.queryParams, 'login.clientVersion', 'Client Version') }}
<th class="column-login-isCrawler">
{{ sortableColumn('admin::list-admin-login', {}, pagination.queryParams, 'login.isCrawler', 'Is Crawler') }}
</th>
<th class="column-login-created">
{{ sortableColumn('admin::list-admin-login', {}, pagination.queryParams, 'login.created', 'Created') }}
Expand All @@ -127,8 +115,6 @@
<td class="column-login-continent">{{ login.continent }}</td>
<td class="column-login-organization">{{ login.organization }}</td>
<td class="column-login-deviceType">{{ login.deviceType }}</td>
<td class="column-login-deviceBrand">{{ login.deviceBrand }}</td>
<td class="column-login-deviceModel">{{ login.deviceModel }}</td>
<td class="column-login-isMobile">
{% if login.isMobile.value == 'yes' %}
<span class="badge text-bg-primary">Yes</span>
Expand All @@ -138,11 +124,15 @@
</td>
<td class="column-login-osName">{{ login.osName }}</td>
<td class="column-login-osVersion">{{ login.osVersion }}</td>
<td class="column-login-osPlatform">{{ login.osPlatform }}</td>
<td class="column-login-clientType">{{ login.clientType }}</td>
<td class="column-login-clientName">{{ login.clientName }}</td>
<td class="column-login-clientEngine">{{ login.clientEngine }}</td>
<td class="column-login-clientVersion">{{ login.clientVersion }}</td>
<td class="column-login-isCrawler">
{% if login.isCrawler.value == 'yes' %}
<span class="badge text-bg-primary">Yes</span>
{% else %}
<span class="badge text-bg-secondary">No</span>
{% endif %}
</td>
<td class="column-login-created">{{ login.getCreated()|date('Y-m-d H:i:s') }}</td>
</tr>
{% endfor %}
Expand Down
116 changes: 24 additions & 92 deletions src/Core/src/Admin/src/Entity/AdminLogin.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@ class AdminLogin extends AbstractEntity
#[ORM\Column(name: 'deviceType', type: 'string', length: 191, nullable: true)]
protected ?string $deviceType = null;

#[ORM\Column(name: 'deviceBrand', type: 'string', length: 191, nullable: true)]
protected ?string $deviceBrand = null;

#[ORM\Column(name: 'deviceModel', type: 'string', length: 40, nullable: true)]
protected ?string $deviceModel = null;

#[ORM\Column(type: 'yes_no_enum', nullable: true, enumType: YesNoEnum::class)]
protected YesNoEnum $isMobile = YesNoEnum::No;

Expand All @@ -54,20 +48,14 @@ class AdminLogin extends AbstractEntity
#[ORM\Column(name: 'osVersion', type: 'string', length: 191, nullable: true)]
protected ?string $osVersion = null;

#[ORM\Column(name: 'osPlatform', type: 'string', length: 191, nullable: true)]
protected ?string $osPlatform = null;

#[ORM\Column(name: 'clientType', type: 'string', length: 191, nullable: true)]
protected ?string $clientType = null;

#[ORM\Column(name: 'clientName', type: 'string', length: 191, nullable: true)]
protected ?string $clientName = null;

#[ORM\Column(name: 'clientEngine', type: 'string', length: 191, nullable: true)]
protected ?string $clientEngine = null;

#[ORM\Column(name: 'clientVersion', type: 'string', length: 191, nullable: true)]
protected ?string $clientVersion = null;
#[ORM\Column(type: 'yes_no_enum', nullable: true, enumType: YesNoEnum::class)]
protected YesNoEnum $isCrawler = YesNoEnum::No;

#[ORM\Column(type: 'success_failure_enum', nullable: true, enumType: SuccessFailureEnum::class)]
protected SuccessFailureEnum $loginStatus = SuccessFailureEnum::Fail;
Expand Down Expand Up @@ -144,30 +132,6 @@ public function setDeviceType(?string $deviceType): self
return $this;
}

public function getDeviceBrand(): ?string
{
return $this->deviceBrand;
}

public function setDeviceBrand(?string $deviceBrand): self
{
$this->deviceBrand = $deviceBrand;

return $this;
}

public function getDeviceModel(): ?string
{
return $this->deviceModel;
}

public function setDeviceModel(?string $deviceModel): self
{
$this->deviceModel = $deviceModel;

return $this;
}

public function getIsMobile(): ?YesNoEnum
{
return $this->isMobile;
Expand Down Expand Up @@ -204,18 +168,6 @@ public function setOsVersion(?string $osVersion): self
return $this;
}

public function getOsPlatform(): ?string
{
return $this->osPlatform;
}

public function setOsPlatform(?string $osPlatform): self
{
$this->osPlatform = $osPlatform;

return $this;
}

public function getClientType(): ?string
{
return $this->clientType;
Expand All @@ -240,26 +192,14 @@ public function setClientName(?string $clientName): self
return $this;
}

public function getClientEngine(): ?string
{
return $this->clientEngine;
}

public function setClientEngine(?string $clientEngine): self
{
$this->clientEngine = $clientEngine;

return $this;
}

public function getClientVersion(): ?string
public function getIsCrawler(): ?YesNoEnum
{
return $this->clientVersion;
return $this->isCrawler;
}

public function setClientVersion(?string $clientVersion): self
public function setIsCrawler(YesNoEnum $isCrawler): self
{
$this->clientVersion = $clientVersion;
$this->isCrawler = $isCrawler;

return $this;
}
Expand All @@ -285,16 +225,12 @@ public function setLoginStatus(SuccessFailureEnum $loginStatus): self
* continent: string|null,
* organization: string|null,
* deviceType: string|null,
* deviceBrand: string|null,
* deviceModel: string|null,
* isMobile: string,
* isMobile: 'no'|'yes',
* osName: string|null,
* osVersion: string|null,
* osPlatform: string|null,
* clientType: string|null,
* clientName: string|null,
* clientEngine: string|null,
* clientVersion: string|null,
* isCrawler: 'no'|'yes',
* loginStatus: string,
* created: DateTimeImmutable|null,
* updated: DateTimeImmutable|null,
Expand All @@ -303,26 +239,22 @@ public function setLoginStatus(SuccessFailureEnum $loginStatus): self
public function getArrayCopy(): array
{
return [
'id' => $this->id->toString(),
'identity' => $this->identity,
'adminIp' => $this->adminIp,
'country' => $this->country,
'continent' => $this->continent,
'organization' => $this->organization,
'deviceType' => $this->deviceType,
'deviceBrand' => $this->deviceBrand,
'deviceModel' => $this->deviceModel,
'isMobile' => $this->isMobile->value,
'osName' => $this->osName,
'osVersion' => $this->osVersion,
'osPlatform' => $this->osPlatform,
'clientType' => $this->clientType,
'clientName' => $this->clientName,
'clientEngine' => $this->clientEngine,
'clientVersion' => $this->clientVersion,
'loginStatus' => $this->loginStatus->value,
'created' => $this->created,
'updated' => $this->updated,
'id' => $this->id->toString(),
'identity' => $this->identity,
'adminIp' => $this->adminIp,
'country' => $this->country,
'continent' => $this->continent,
'organization' => $this->organization,
'deviceType' => $this->deviceType,
'isMobile' => $this->isMobile->value,
'osName' => $this->osName,
'osVersion' => $this->osVersion,
'clientType' => $this->clientType,
'clientName' => $this->clientName,
'isCrawler' => $this->isCrawler->value,
'loginStatus' => $this->loginStatus->value,
'created' => $this->created,
'updated' => $this->updated,
];
}
}
27 changes: 4 additions & 23 deletions test/Unit/Admin/Entity/AdminLoginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,6 @@ public function testAccessors(): void
$this->assertSame(AdminLogin::class, $adminLogin::class);
$this->assertSame('test', $adminLogin->getDeviceType());

$this->assertNull($adminLogin->getDeviceBrand());
$adminLogin = $adminLogin->setDeviceBrand('test');
$this->assertSame(AdminLogin::class, $adminLogin::class);
$this->assertSame('test', $adminLogin->getDeviceBrand());

$this->assertNull($adminLogin->getDeviceModel());
$adminLogin = $adminLogin->setDeviceModel('test');
$this->assertSame(AdminLogin::class, $adminLogin::class);
$this->assertSame('test', $adminLogin->getDeviceModel());

$this->assertSame(YesNoEnum::No, $adminLogin->getIsMobile());
$adminLogin = $adminLogin->setIsMobile(YesNoEnum::Yes);
$this->assertSame(AdminLogin::class, $adminLogin::class);
Expand All @@ -93,11 +83,6 @@ public function testAccessors(): void
$this->assertSame(AdminLogin::class, $adminLogin::class);
$this->assertSame('test', $adminLogin->getOsVersion());

$this->assertNull($adminLogin->getOsPlatform());
$adminLogin = $adminLogin->setOsPlatform('test');
$this->assertSame(AdminLogin::class, $adminLogin::class);
$this->assertSame('test', $adminLogin->getOsPlatform());

$this->assertNull($adminLogin->getClientType());
$adminLogin = $adminLogin->setClientType('test');
$this->assertSame(AdminLogin::class, $adminLogin::class);
Expand All @@ -108,15 +93,11 @@ public function testAccessors(): void
$this->assertSame(AdminLogin::class, $adminLogin::class);
$this->assertSame('test', $adminLogin->getClientName());

$this->assertNull($adminLogin->getClientEngine());
$adminLogin = $adminLogin->setClientEngine('test');
$this->assertSame(AdminLogin::class, $adminLogin::class);
$this->assertSame('test', $adminLogin->getClientEngine());

$this->assertNull($adminLogin->getClientVersion());
$adminLogin = $adminLogin->setClientVersion('test');
$this->assertSame(YesNoEnum::No, $adminLogin->getIsCrawler());
$adminLogin = $adminLogin->setIsCrawler(YesNoEnum::Yes);
$this->assertSame(AdminLogin::class, $adminLogin::class);
$this->assertSame('test', $adminLogin->getClientVersion());
$this->assertNotNull($adminLogin->getIsCrawler());
$this->assertSame('yes', $adminLogin->getIsCrawler()->value);

$this->assertSame(SuccessFailureEnum::Fail, $adminLogin->getLoginStatus());
$adminLogin = $adminLogin->setLoginStatus(SuccessFailureEnum::Success);
Expand Down
Loading