diff --git a/backend/app/Api/User/Session.php b/backend/app/Api/User/Session.php index b7dbca8..3dbbf99 100755 --- a/backend/app/Api/User/Session.php +++ b/backend/app/Api/User/Session.php @@ -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(); @@ -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)) + ]); +}); \ No newline at end of file diff --git a/backend/app/Chat/User.php b/backend/app/Chat/User.php index 84db168..ec4086d 100755 --- a/backend/app/Chat/User.php +++ b/backend/app/Chat/User.php @@ -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 { @@ -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']; } diff --git a/backend/app/Chat/UserActivities.php b/backend/app/Chat/UserActivities.php new file mode 100755 index 0000000..4f28e06 --- /dev/null +++ b/backend/app/Chat/UserActivities.php @@ -0,0 +1,52 @@ +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"; + } +} \ No newline at end of file diff --git a/backend/app/Chat/interface/UserActivitiesTypes.php b/backend/app/Chat/interface/UserActivitiesTypes.php new file mode 100755 index 0000000..c228571 --- /dev/null +++ b/backend/app/Chat/interface/UserActivitiesTypes.php @@ -0,0 +1,20 @@ +