Skip to content

Commit

Permalink
PUSH
Browse files Browse the repository at this point in the history
-> Finishes #128
  • Loading branch information
NaysKutzu committed Dec 21, 2024
1 parent be72456 commit 1eb78c7
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 2 deletions.
18 changes: 18 additions & 0 deletions backend/app/Api/User/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use MythicalClient\Chat\Billing;
use MythicalClient\Chat\Session;
use MythicalClient\Chat\columns\UserColumns;
use MythicalClient\Chat\UserActivities;

$router->post('/api/user/session/info/update', function (): void {
App::init();
Expand Down Expand Up @@ -181,3 +182,20 @@
}

});


$router->get('/api/user/session/activities', function (): void {
App::init();
$appInstance = App::getInstance(true);
$config = $appInstance->getConfig();

$appInstance->allowOnlyGET();

$session = new Session($appInstance);

$accountToken = $session->SESSION_KEY;

$appInstance->OK('User activities', [
'activities' => UserActivities::get(User::getInfo($accountToken, UserColumns::UUID, false))
]);
});
4 changes: 3 additions & 1 deletion backend/app/Chat/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@

use Gravatar\Gravatar;
use MythicalClient\App;
use MythicalClient\Chat\interface\UserActivitiesTypes;
use MythicalClient\Mail\Mail;
use MythicalClient\Mail\templates\Verify;
use MythicalClient\Mail\templates\NewLogin;
use MythicalClient\Chat\columns\UserColumns;
use MythicalClient\Mail\templates\ResetPassword;
use MythicalClient\Chat\columns\EmailVerificationColumns;
use MythicalSystems\CloudFlare\CloudFlare;

class User extends Database
{
Expand Down Expand Up @@ -222,7 +224,7 @@ public static function login(string $login, string $password): string
App::getInstance(true)->getLogger()->error('Failed to send email: ' . $e->getMessage());
}
}

UserActivities::add($user['uuid'], UserActivitiesTypes::$login, CloudFlare::getRealUserIP());
return $user['token'];
}

Expand Down
52 changes: 52 additions & 0 deletions backend/app/Chat/UserActivities.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
namespace MythicalClient\Chat;

use MythicalClient\Chat\interface\UserActivitiesTypes;

class UserActivities {

/**
* Add user activity
*
* @param string $uuid User UUID
* @param string|\MythicalClient\Chat\interface\UserActivitiesTypes $type Activity type
* @param string $ipv4 IP address
*
* @return bool
*/
public static function add(string $uuid, string|UserActivitiesTypes $type, string $ipv4) : bool {
$dbConn = Database::getPdoConnection();

$stmt = $dbConn->prepare("INSERT INTO " . self::getTable() . " (user, action, ip_address) VALUES (:user, :action, :ip_address)");
return $stmt->execute([
':user' => $uuid,
':action' => $type,
':ip_address' => $ipv4
]);
}
/**
* Get user activities
*
* @param string $uuid User UUID
*
* @return array
*/
public static function get(string $uuid) : array {
$dbConn = Database::getPdoConnection();

$stmt = $dbConn->prepare("SELECT * FROM " . self::getTable() . " WHERE user = :user LIMIT 125");
$stmt->execute([
':user' => $uuid
]);
return $stmt->fetchAll(\PDO::FETCH_ASSOC);
}

/**
* Get table name
*
* @return string Table name
*/
public static function getTable() : string {
return "mythicalclient_users_activities";
}
}
20 changes: 20 additions & 0 deletions backend/app/Chat/interface/UserActivitiesTypes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace MythicalClient\Chat\interface;

class UserActivitiesTypes {
public static string $login = "auth:login";
public static string $register = "auth:register";

/**
* Get all types
*
* @return array All types
*/
public static function getTypes(): array {
return [
self::$login,
self::$register
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ CREATE TABLE
`mythicalclient_users_activities` (
`id` int (11) NOT NULL AUTO_INCREMENT,
`user` varchar(36) NOT NULL,
`description` text NOT NULL,
`action` text NOT NULL,
`ip_address` text NOT NULL,
`deleted` enum ('false', 'true') NOT NULL DEFAULT 'false',
Expand Down

0 comments on commit 1eb78c7

Please sign in to comment.