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

Commit a16962b

Browse files
BaranekDvyskocilpavel
authored andcommitted
Added option to read warning attributes from config, file or url (#58)
* Changed config for warning, moved part of code from disco-tpl to Disco * Added option to read warning attributes from config, file or url * Added warning types: INFO, WARNING, ERROR
1 parent cbd3555 commit a16962b

File tree

9 files changed

+422
-59
lines changed

9 files changed

+422
-59
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ All notable changes to this project will be documented in this file.
1616
{ ... }
1717
]
1818
```
19+
- Added warning types: INFO, WARNING, ERROR
1920

2021
#### Changed
2122
- RpcConnector now stores cookie into file
2223
- Set CONNECTTIMEOUT and TIMEOUT in RpcConnector
2324
- Use new object perunFacility in LDAP to search information about facility
25+
- Configuration for warning on DS is now in module_perun.php
2426

2527
## [v3.2.1]
2628
#### Fixed

config-templates/module_perun.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,5 +159,51 @@
159159
''
160160
],
161161

162+
/**
163+
********************************************
164+
* Part of configuration for Warning on DS *
165+
********************************************
166+
*/
167+
168+
/**
169+
* Choose one of allowed sources: CONFIG/FILE/URL
170+
* If FILE or URL is chosen, please read the 'warning_file_or_url' file to see how it should look
171+
*/
172+
'disco.warning.source' => '',
173+
174+
/**
175+
* Specify the absolute path to configuration file
176+
* REQUIRED ONLY FOR TYPE FILE
177+
*/
178+
'disco.warning.file' => '/etc/simplesamlphp/cesnet/config/warning',
179+
180+
/**
181+
* Specify the url to configuration file
182+
* REQUIRED ONLY FOR TYPE URL
183+
*/
184+
'disco.warning.url' => 'url to configuration file',
162185

186+
/**
187+
* When true, the config file is switched on.
188+
* REQUIRED ONLY FOR TYPE CONFIG
189+
*/
190+
'disco.warning.isOn' => true,
191+
192+
/**
193+
* Choose one of allowed types: INFO/WARNING/ERROR.
194+
* REQUIRED ONLY FOR TYPE CONFIG
195+
*/
196+
'disco.warning.type' => 'INFO',
197+
198+
/**
199+
* Title of the warning. It is possible to use HTML.
200+
* REQUIRED ONLY FOR TYPE CONFIG
201+
*/
202+
'disco.warning.title' => '',
203+
204+
/**
205+
* Text of the warning. It is possible to use HTML.
206+
* REQUIRED ONLY FOR TYPE CONFIG
207+
*/
208+
'disco.warning.text' => '',
163209
];
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"disco.warning.isOn": true,
3+
"disco.warning.type": "INFO/WARNING/ERROR",
4+
"disco.warning.title": "Some warning title",
5+
"disco.warning.text": "Some warning text"
6+
}

lib/Disco.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,18 @@ public function handleRequest()
105105
HTTP::redirectTrustedURL($url);
106106
}
107107

108+
try {
109+
$warningInstance = WarningConfiguration::getInstance();
110+
$warningAttributes = $warningInstance->getWarningAttributes();
111+
} catch (Exception $ex) {
112+
$warningAttributes = [
113+
'warningIsOn' => false,
114+
'warningType' => '',
115+
'warningTitle' => '',
116+
'warningText' => ''
117+
];
118+
}
119+
108120
$t = new DiscoTemplate($this->config);
109121
$t->data['originalsp'] = $this->originalsp;
110122
$t->data['idplist'] = $this->idplistStructured($idpList);
@@ -113,6 +125,10 @@ public function handleRequest()
113125
$t->data['return'] = $this->returnURL;
114126
$t->data['returnIDParam'] = $this->returnIdParam;
115127
$t->data['AuthnContextClassRef'] = $this->authnContextClassRef;
128+
$t->data['warningIsOn'] = $warningAttributes['warningIsOn'];
129+
$t->data['warningType'] = $warningAttributes['warningType'];
130+
$t->data['warningTitle'] = $warningAttributes['warningTitle'];
131+
$t->data['warningText'] = $warningAttributes['warningText'];
116132
$t->show();
117133
}
118134

lib/WarningConfiguration.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace SimpleSAML\Module\perun;
4+
5+
use SimpleSAML\Error\Exception;
6+
use SimpleSAML\Logger;
7+
use SimpleSAML\Configuration;
8+
9+
10+
/**
11+
* Class WarningConfiguration provides an option to load warning in disco-tpl from different types of sources
12+
*
13+
* @package SimpleSAML\Module\perun
14+
* @author Dominik Baránek <[email protected]>
15+
*/
16+
abstract class WarningConfiguration
17+
{
18+
const CONFIG_FILE_NAME = 'module_perun.php';
19+
const WARNING_SOURCE = 'disco.warning.source';
20+
21+
const WARNING_FILE = 'disco.warning.file';
22+
const WARNING_URL = 'disco.warning.url';
23+
const WARNING_TYPE = 'disco.warning.type';
24+
const WARNING_IS_ON = 'disco.warning.isOn';
25+
const WARNING_TITLE = 'disco.warning.title';
26+
const WARNING_TEXT = 'disco.warning.text';
27+
28+
const WARNING_TYPE_INFO = 'INFO';
29+
const WARNING_TYPE_WARNING = 'WARNING';
30+
const WARNING_TYPE_ERROR = 'ERROR';
31+
32+
protected $warningIsOn = false;
33+
protected $warningType = '';
34+
protected $warningTitle = '';
35+
protected $warningText = '';
36+
37+
protected $allowedTypes = [self::WARNING_TYPE_INFO, self::WARNING_TYPE_WARNING, self::WARNING_TYPE_ERROR];
38+
39+
/**
40+
* Function returns the instance of WarningConfiguration
41+
* @return WarningConfigurationConfig|WarningConfigurationFile|WarningConfigurationUrl
42+
*/
43+
public static function getInstance()
44+
{
45+
$configuration = Configuration::getConfig(self::CONFIG_FILE_NAME);
46+
$source = $configuration->getString(self::WARNING_SOURCE);
47+
if ($source === 'CONFIG') {
48+
return new WarningConfigurationConfig();
49+
} elseif ($source === 'FILE') {
50+
return new WarningConfigurationFile();
51+
} elseif ($source === 'URL') {
52+
return new WarningConfigurationUrl();
53+
} else {
54+
Logger::warning("perun:WarningConfiguration: missing or invalid disco.warning.source in module_perun.php");
55+
throw new Exception(
56+
"perun:WarningConfiguration: missing or invalid disco.warning.source in module_perun.php"
57+
);
58+
}
59+
}
60+
61+
/**
62+
* @return string data with warning attributes
63+
*/
64+
abstract public function getSourceOfWarningAttributes();
65+
66+
/**
67+
* @return array with warning attributes
68+
*/
69+
abstract public function getWarningAttributes();
70+
}

lib/WarningConfigurationConfig.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace SimpleSAML\Module\perun;
4+
5+
use SimpleSAML\Error\Exception;
6+
use SimpleSAML\Logger;
7+
use SimpleSAML\Configuration;
8+
9+
/**
10+
* Implementation of WarningConfiguration using config file as the source of warning attributes
11+
* @package SimpleSAML\Module\perun
12+
* @author Dominik Baránek <[email protected]>
13+
*/
14+
class WarningConfigurationConfig extends WarningConfiguration
15+
{
16+
public function getSourceOfWarningAttributes()
17+
{
18+
$config = Configuration::getConfig(WarningConfiguration::CONFIG_FILE_NAME);
19+
return $config;
20+
}
21+
22+
public function getWarningAttributes()
23+
{
24+
$data = self::getSourceOfWarningAttributes();
25+
26+
if ($data !== null) {
27+
$this->warningIsOn = $data->getBoolean(WarningConfiguration::WARNING_IS_ON, false);
28+
}
29+
30+
if ($this->warningIsOn) {
31+
$this->warningType = $data->getString(WarningConfiguration::WARNING_TYPE, 'INFO');
32+
33+
if (!in_array($this->warningType, $this->allowedTypes)) {
34+
Logger::info('perun:warningConfigurationConfig: warningType has invalid value, value set to INFO');
35+
$this->warningType = 'INFO';
36+
}
37+
38+
try {
39+
$this->warningTitle = $data->getString(WarningConfiguration::WARNING_TITLE);
40+
$this->warningText = $data->getString(WarningConfiguration::WARNING_TEXT);
41+
if (empty($this->warningTitle) || empty($this->warningText)) {
42+
throw new Exception();
43+
}
44+
} catch (Exception $ex) {
45+
Logger::warning(
46+
"perun:WarningConfigurationConfig: " .
47+
"missing or invalid disco.warning.title or disco.warning.text in module_perun.php"
48+
);
49+
$this->warningIsOn = false;
50+
}
51+
}
52+
53+
return array(
54+
'warningIsOn' => $this->warningIsOn,
55+
'warningType' => $this->warningType,
56+
'warningTitle' => $this->warningTitle,
57+
'warningText' => $this->warningText
58+
);
59+
}
60+
}

lib/WarningConfigurationFile.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
namespace SimpleSAML\Module\perun;
4+
5+
use SimpleSAML\Error\Exception;
6+
use SimpleSAML\Logger;
7+
use SimpleSAML\Configuration;
8+
9+
/**
10+
* Implementation of WarningConfiguration using json file as the source of warning attributes
11+
* @package SimpleSAML\Module\perun
12+
* @author Dominik Baránek <[email protected]>
13+
*/
14+
class WarningConfigurationFile extends WarningConfiguration
15+
{
16+
public function getSourceOfWarningAttributes()
17+
{
18+
$file = null;
19+
$data = null;
20+
21+
$config = Configuration::getConfig(WarningConfiguration::CONFIG_FILE_NAME);
22+
23+
try {
24+
$file = $config->getString(WarningConfiguration::WARNING_FILE);
25+
26+
set_error_handler(function () {
27+
Logger::warning(
28+
"perun:WarningConfigurationFile: " .
29+
"missing or invalid disco.warning.file parameter in module_perun.php"
30+
);
31+
});
32+
33+
$json_data = file_get_contents($file);
34+
restore_error_handler();
35+
$data = json_decode($json_data, true);
36+
} catch (\Exception $ex) {
37+
Logger::warning(
38+
"perun:WarningConfigurationFile: missing or invalid disco.warning.file parameter in module_perun.php"
39+
);
40+
}
41+
42+
return $data;
43+
}
44+
45+
public function getWarningAttributes()
46+
{
47+
$data = self::getSourceOfWarningAttributes();
48+
49+
if ($data !== null) {
50+
if (isset($data[WarningConfiguration::WARNING_IS_ON])) {
51+
$this->warningIsOn = $data[WarningConfiguration::WARNING_IS_ON];
52+
} else {
53+
Logger::warning(
54+
"perun:warningConfigurationFile: " .
55+
"missing or invalid warningIsOn parameter in file with warning configuration"
56+
);
57+
}
58+
59+
if (isset($data[WarningConfiguration::WARNING_TYPE])) {
60+
$this->warningType = $data[WarningConfiguration::WARNING_TYPE];
61+
62+
if (!in_array($this->warningType, $this->allowedTypes)) {
63+
Logger::info('perun:warningConfigurationFile: warningType has invalid value, value set to INFO');
64+
$this->warningType = 'INFO';
65+
}
66+
} else {
67+
Logger::warning(
68+
"perun:warningConfigurationFile: " .
69+
"missing or invalid warningType parameter in file with warning configuration"
70+
);
71+
}
72+
73+
if (isset($data[WarningConfiguration::WARNING_TITLE])) {
74+
$this->warningTitle = $data[WarningConfiguration::WARNING_TITLE];
75+
} else {
76+
Logger::warning(
77+
"perun:warningConfigurationFile: " .
78+
"missing or invalid warningTitle parameter in file with warning configuration"
79+
);
80+
}
81+
82+
if (isset($data[WarningConfiguration::WARNING_TEXT])) {
83+
$this->warningText = $data[WarningConfiguration::WARNING_TEXT];
84+
} else {
85+
Logger::warning(
86+
"perun:warningConfigurationFile: " .
87+
"missing or invalid warningText parameter in file with warning configuration"
88+
);
89+
}
90+
}
91+
92+
return array(
93+
'warningIsOn' => $this->warningIsOn,
94+
'warningType' => $this->warningType,
95+
'warningTitle' => $this->warningTitle,
96+
'warningText' => $this->warningText
97+
);
98+
}
99+
}

0 commit comments

Comments
 (0)