Skip to content

Commit

Permalink
PUSH
Browse files Browse the repository at this point in the history
-> Password reset form
-> User account update
  • Loading branch information
NaysKutzu committed Sep 19, 2024
1 parent 214af7d commit 5a0cfe6
Show file tree
Hide file tree
Showing 12 changed files with 271 additions and 390 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
* along with this program. If not, see <https://opensource.org/licenses/MIT>.
*/

namespace MythicalSystemsFramework\Notification;
namespace MythicalSystemsFramework\User\Notification;

use MythicalSystemsFramework\Database\MySQL;
use MythicalSystemsFramework\Kernel\Logger;
use MythicalSystemsFramework\Kernel\LoggerLevels;
use MythicalSystemsFramework\Kernel\LoggerTypes;

class Notification
class Notifications
{
/**
* Create a new notification.
Expand Down Expand Up @@ -98,100 +101,6 @@ public static function deleteAll(): void
}
}

/**
* Get a single notification by ID.
*
* @param int $id the id of the notification to retrieve
*
* @return array|null the notification data or null if not found
*/
public static function getOne(int $id): ?array
{
try {
if (!self::exists($id)) {
return [];
}
$mysqli = new MySQL();
$conn = $mysqli->connectMYSQLI();
$stmt = $conn->prepare('SELECT * FROM framework_users_notifications WHERE id = ?');
$stmt->bind_param('i', $id);
$stmt->execute();
$result = $stmt->get_result();
$notification = $result->fetch_assoc();
$stmt->close();

return $notification;
} catch (\Exception $e) {
return [];
}
}

/**
* Get all framework_users_notifications.
*
* @return array all framework_users_notifications
*/
public static function getAll(): array
{
try {
$mysqli = new MySQL();
$conn = $mysqli->connectMYSQLI();
$result = $conn->query('SELECT * FROM framework_users_notifications');
$framework_users_notifications = [];
while ($notification = $result->fetch_assoc()) {
$framework_users_notifications[] = $notification;
}

return $framework_users_notifications;
} catch (\Exception $e) {
return [];
}
}

/**
* Get all framework_users_notifications sorted by ID in descending order.
*
* @return array sorted framework_users_notifications
*/
public static function getAllSortedById(): array
{
try {
$mysqli = new MySQL();
$conn = $mysqli->connectMYSQLI();
$result = $conn->query('SELECT * FROM framework_users_notifications ORDER BY id DESC');
$framework_users_notifications = [];
while ($notification = $result->fetch_assoc()) {
$framework_users_notifications[] = $notification;
}

return $framework_users_notifications;
} catch (\Exception $e) {
return [];
}
}

/**
* Get all framework_users_notifications sorted by date in descending order.
*
* @return array sorted framework_users_notifications
*/
public static function getAllSortedByDate(): array
{
try {
$mysqli = new MySQL();
$conn = $mysqli->connectMYSQLI();
$result = $conn->query('SELECT * FROM framework_users_notifications ORDER BY date DESC');
$framework_users_notifications = [];
while ($notification = $result->fetch_assoc()) {
$framework_users_notifications[] = $notification;
}

return $framework_users_notifications;
} catch (\Exception $e) {
return [];
}
}

/**
* Get framework_users_notifications filtered by user ID.
*
Expand Down Expand Up @@ -243,51 +152,49 @@ public static function exists(string $id): bool
return false;
}
}

/**
* Mark a notification as read.
*
* @param string $notification_id the id of the notification to mark as read
* @param string $user_uuid the user uuid
*
* @throws \Exception
*
* Check if a user owns a notification.
*
* @param string $user_uuid The user uuid
* @param int $notification_id The notification id
*
* @return bool
*/
public static function markAsRead(string $notification_id, string $user_uuid): void
public static function doesUserOwnThisNotification(string $user_uuid, int $notification_id): bool
{
try {
if (!self::exists($notification_id)) {
return false;
}
$mysqli = new MySQL();
$conn = $mysqli->connectMYSQLI();
$stmt = $conn->prepare('INSERT INTO framework_users_notifications_read (notification_id, user_uuid, date) VALUES (?, ?, NOW())');
$stmt->bind_param('ss', $notification_id, $user_uuid);
$stmt = $conn->prepare('SELECT * FROM framework_users_notifications WHERE user_id = ? AND id = ?');
$stmt->bind_param('si', $user_uuid, $notification_id);
$stmt->execute();
$result = $stmt->get_result();
$stmt->close();
return $result->num_rows > 0;
} catch (\Exception $e) {
throw new \Exception('' . $e->getMessage());
Logger::log(LoggerLevels::CRITICAL, LoggerTypes::DATABASE, '(App/User/Notification/Notifications.php) Failed to check if user owns notification: ' . $e->getMessage());
return false;
}
}

/**
* Check if a notification was already read!
* Mark a notification as read.
*
* @param string $notification_id The id of the notification
* @param string $user_uuid The uuid of the user
* @param string $notification_id the id of the notification to mark as read
* @param string $user_uuid the user uuid
*
* @throws \Exception
*/
public static function hasAlreadyRead(string $notification_id, string $user_uuid): bool
public static function markAsRead(string $notification_id, string $user_uuid): void
{
try {
$mysqli = new MySQL();
$conn = $mysqli->connectMYSQLI();
$stmt = $conn->prepare('SELECT * FROM framework_users_notifications_read WHERE notification_id = ? AND user_uuid = ?');
$stmt->bind_param('ss', $notification_id, $user_uuid);
$stmt->execute();
$result = $stmt->get_result();
$stmt->close();

return $result->num_rows > 0;
self::delete($notification_id);
} catch (\Exception $e) {
throw new \Exception('' . $e->getMessage());
Logger::log(LoggerLevels::CRITICAL, LoggerTypes::DATABASE,''. $e->getMessage());
}
}
}
8 changes: 7 additions & 1 deletion app/User/UserHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
use MythicalSystemsFramework\CloudFlare\CloudFlare;
use MythicalSystemsFramework\Roles\RolesDataHandler;
use MythicalSystemsFramework\Roles\RolesPermissionDataHandler;
use MythicalSystemsFramework\User\Notification\Notifications;

class UserHelper extends UserDataHandler
{
private string $account_token;

public function __construct(string $token)
public function __construct(string $token,\Twig\Environment $renderer)
{
$this->account_token = $token;
$isBanned = self::isUserBanned($token);
Expand All @@ -49,6 +50,11 @@ public function __construct(string $token)
} else {
$this->killSession();
}
$uuid = UserDataHandler::getSpecificUserData($token, 'uuid', false);

$notifications = Notifications::getByUserId($uuid);
$renderer->addGlobal('notifications', $notifications);

}

/**
Expand Down
13 changes: 7 additions & 6 deletions app/Web/Routes/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@
if (isset($_GET['token']) && !$_GET['token'] == '') {
if (MailVerification::isValid($_GET['token'])) {
$token = UserDataHandler::getTokenUUID(MailVerification::getUserUUID($_GET['token']));
$user = new UserHelper($token);
$user = new UserHelper($token,$renderer);
$user->verifyUser();
setcookie('token', $token, time() + 3600 * 24 * 365 * 5, '/');
MailVerification::remove($_GET['token']);
Expand Down Expand Up @@ -270,7 +270,7 @@
$router->add('/auth/2fa/disable', function (): void {
global $renderer;

$user = new UserHelper($_COOKIE['token']);
$user = new UserHelper($_COOKIE['token'],$renderer);
UserDataHandler::requireAuthorization($renderer, $_COOKIE['token'], true);

$user2fa = new TwoFactor($_COOKIE['token']);
Expand All @@ -293,7 +293,7 @@
global $renderer;
$template_name = 'auth/2fa/login.twig';

$user = new UserHelper($_COOKIE['token']);
$user = new UserHelper($_COOKIE['token'],$renderer);
UserDataHandler::requireAuthorization($renderer, $_COOKIE['token'], true);

$user2fa = new TwoFactor($_COOKIE['token']);
Expand Down Expand Up @@ -352,7 +352,7 @@
global $renderer;
$template_name = 'auth/2fa/setup.twig';

$user = new UserHelper($_COOKIE['token']);
$user = new UserHelper($_COOKIE['token'],$renderer);
UserDataHandler::requireAuthorization($renderer, $_COOKIE['token']);

$user2fa = new TwoFactor($_COOKIE['token']);
Expand Down Expand Up @@ -473,7 +473,7 @@
header('Location: /auth/login?s=mail_verify');
exit;
}
$user = new UserHelper($user_check);
$user = new UserHelper($user_check,$renderer);
$user->verifyUser();
setcookie('token', $user_check, time() + 3600 * 24 * 365 * 5, '/');
header('Location: /auth/login?s=register');
Expand All @@ -496,7 +496,8 @@
* @return void
*/
$router->add('/auth/logout', function (): void {
$user = new UserHelper($_COOKIE['token']);
global $renderer;
$user = new UserHelper($_COOKIE['token'],$renderer);
$user->killSession();
header('Location: /');
exit;
Expand Down
Loading

0 comments on commit 5a0cfe6

Please sign in to comment.