Skip to content
This repository was archived by the owner on Sep 19, 2022. It is now read-only.

Commit 6c85240

Browse files
authored
Changed the way how to access database (#158)
1 parent 45d3add commit 6c85240

13 files changed

+392
-481
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file.
77

88
#### Changed
99
- Use translation for privacy policy document block on consent screen from module Perun
10+
- Connection to the database obtained through the SimpleSAML Database class
1011

1112
#### Fixed
1213
- Fixed bad check in NagiosStatusConnector.php

hooks/hook_cron.php

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
<?php
22

33
use SimpleSAML\Logger;
4-
use SimpleSAML\Module\perun\DatabaseConnector;
5-
6-
const TABLE_NAME = 'scriptChallenges';
7-
const DATE_COLUMN = 'date';
4+
use SimpleSAML\Module\perun\databaseCommand\ChallengesDbCmd;
85

96
/**
107
* Hook to run a cron job.
@@ -18,34 +15,11 @@ function challenges_hook_cron(&$croninfo)
1815
Logger::debug('cron [perun]: Skipping cron in cron tag [' . $croninfo['tag'] . '] ');
1916
return;
2017
}
21-
2218
Logger::info('cron [perun]: Running cron in cron tag [' . $croninfo['tag'] . '] ');
2319

24-
try {
25-
$databaseConnector = new DatabaseConnector();
26-
$conn = $databaseConnector->getConnection();
27-
28-
if ($conn !== null) {
29-
$stmt = $conn->prepare(
30-
'DELETE FROM ' . TABLE_NAME . ' WHERE ' . DATE_COLUMN . ' < (NOW() - INTERVAL 5 MINUTE)'
31-
);
32-
33-
if (!$stmt) {
34-
$conn->close();
35-
Logger::error('cron [perun]: Error during preparing statement');
36-
return;
37-
}
38-
39-
$ex = $stmt->execute();
40-
41-
if ($ex === false) {
42-
Logger::error('cron [perun]: Error while deleting old challenges from the database.');
43-
}
20+
$challengesDbCmd = new ChallengesDbCmd();
4421

45-
$stmt->close();
46-
$conn->close();
47-
}
48-
} catch (\Exception $e) {
49-
$croninfo['summary'][] = 'Error while deleting old challenges from the database: ' . $e->getMessage();
22+
if (!$challengesDbCmd->deleteOldChallenges()) {
23+
Logger::error('cron [perun]: Error while deleting old challenges from the database.');
5024
}
5125
}

lib/ChallengeManager.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace SimpleSAML\Module\perun;
4+
5+
use SimpleSAML\Logger;
6+
use SimpleSAML\Module\perun\databaseCommand\ChallengesDbCmd;
7+
8+
class ChallengeManager
9+
{
10+
const LOG_PREFIX = 'Perun:ChallengeManager: ';
11+
private $challengeDbCmd;
12+
13+
public function __construct()
14+
{
15+
$this->challengeDbCmd = new ChallengesDbCmd();
16+
}
17+
18+
public function insertChallenge($challenge, $id, $scriptName): bool
19+
{
20+
if (empty($challenge) ||
21+
empty($id) ||
22+
empty($scriptName) ||
23+
!$this->challengeDbCmd->insertChallenge($challenge, $id, $scriptName)) {
24+
Logger::error(self::LOG_PREFIX . 'Error while creating a challenge');
25+
http_response_code(500);
26+
return false;
27+
}
28+
29+
return true;
30+
}
31+
32+
public function readChallengeFromDb($id)
33+
{
34+
if (empty($id)) {
35+
http_response_code(400);
36+
return null;
37+
}
38+
39+
$result = $this->challengeDbCmd->readChallenge($id);
40+
41+
if ($result === null) {
42+
http_response_code(500);
43+
}
44+
45+
return $result;
46+
}
47+
48+
public static function checkAccess($challenge, $challengeDb): bool
49+
{
50+
if (empty($challenge) || empty($challengeDb)) {
51+
http_response_code(400);
52+
return false;
53+
}
54+
55+
if (!hash_equals($challengeDb, $challenge)) {
56+
Logger::error(self::LOG_PREFIX . 'Hashes are not equal.');
57+
http_response_code(401);
58+
return false;
59+
}
60+
61+
return true;
62+
}
63+
64+
public function deleteChallengeFromDb($id): bool
65+
{
66+
if (empty($id)) {
67+
http_response_code(400);
68+
return false;
69+
}
70+
71+
if (!$this->challengeDbCmd->deleteChallenge($id)) {
72+
Logger::error(self::LOG_PREFIX . 'Error while deleting challenge from the database.');
73+
http_response_code(500);
74+
return false;
75+
}
76+
77+
return true;
78+
}
79+
}

lib/DatabaseCommand.php

Lines changed: 0 additions & 183 deletions
This file was deleted.

0 commit comments

Comments
 (0)