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

Commit 5c399f7

Browse files
committed
Added new proccess filter to redirect some users to selected page.
1 parent 41c1a7d commit 5c399f7

File tree

5 files changed

+207
-0
lines changed

5 files changed

+207
-0
lines changed

dictionaries/perun.definition.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,5 +94,17 @@
9494
"unauthorized-access_redirect_to_registration": {
9595
"en": "Now you will be redirected to registration to Perun system.",
9696
"cs": "Nyní budete přesměrování na registraci do systému Perun."
97+
},
98+
"redirect_some_users-header": {
99+
"en": "Your activity is necessary to access the service",
100+
"cs": "Pro přístup ke službě je vyžadována vaše aktivita"
101+
},
102+
"redirect_some_users-text": {
103+
"en": "Text",
104+
"cs": "Text"
105+
},
106+
"continue_to_service": {
107+
"en": "You can continue to the service",
108+
"cs": "Na službu můžete pokračovat"
97109
}
98110
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
4+
namespace SimpleSAML\Module\perun\Auth\Process;
5+
6+
7+
use SimpleSAML\Auth\ProcessingFilter;
8+
use SimpleSAML\Auth\State;
9+
use SimpleSAML\Error\Exception;
10+
use SimpleSAML\Logger;
11+
use SimpleSAML\Module;
12+
use SimpleSAML\Utils\HTTP;
13+
14+
class RedirectSomeUsers extends ProcessingFilter
15+
{
16+
17+
const ATTRIBUTE_IDENTIFIER = 'attributeIdentifier';
18+
const URL_WITH_LOGINS = 'urlWithLogins';
19+
const ALLOWED_CONTINUE = 'allowedContinue';
20+
const REDIRECT_URL = 'redirectURL';
21+
const PAGE_TEXT = 'pageText';
22+
23+
private $attributeIdentifier;
24+
private $URLWtithLogins;
25+
private $allowedContinue = true;
26+
private $redirectURL;
27+
private $pageText;
28+
29+
public function __construct($config, $reserved)
30+
{
31+
parent::__construct($config, $reserved);
32+
33+
if (!isset($config[self::ATTRIBUTE_IDENTIFIER])) {
34+
throw new Exception(
35+
'perun:RedirectSomeUsers - missing mandatory configuration option \'' .
36+
self::ATTRIBUTE_IDENTIFIER . '\'.'
37+
);
38+
}
39+
if (!isset($config[self::URL_WITH_LOGINS])) {
40+
throw new Exception(
41+
'perun:RedirectSomeUsers - missing mandatory configuration option \'' . self::URL_WITH_LOGINS . '\'.'
42+
);
43+
}
44+
if (!isset($config[self::REDIRECT_URL])) {
45+
throw new Exception(
46+
'perun:RedirectSomeUsers - missing mandatory configuration option \'' . self::REDIRECT_URL . '\'.'
47+
);
48+
}
49+
if (!isset($config[self::PAGE_TEXT]['en'])) {
50+
throw new Exception(
51+
'perun:RedirectSomeUsers - missing mandatory configuration option \'' . self::REDIRECT_URL . '\'.'
52+
);
53+
}
54+
55+
$this->attributeIdentifier = (string)$config[self::ATTRIBUTE_IDENTIFIER];
56+
$this->URLWtithLogins = (string)$config[self::URL_WITH_LOGINS];
57+
if (isset($config[self::ALLOWED_CONTINUE])) {
58+
$this->allowedContinue = (boolean)$config[self::ALLOWED_CONTINUE];
59+
}
60+
$this->redirectURL = (string)$config[self::REDIRECT_URL];
61+
$this->pageText = $config[self::PAGE_TEXT];
62+
}
63+
64+
public function process(&$request)
65+
{
66+
$listOfLoginsToRedirect = file_get_contents($this->URLWtithLogins);
67+
if (empty($listOfLoginsToRedirect)) {
68+
Logger::debug('perun:RedirectSomeUsers - List of logins is empty!');
69+
}
70+
71+
$listOfLoginsToRedirect = explode("\n", $listOfLoginsToRedirect);
72+
73+
if (!isset($request['Attributes'][$this->attributeIdentifier])) {
74+
Logger::debug('perun:RedirectSomeUsers - User has not an attribute with identifier \''.
75+
$this->attributeIdentifier . ' \'!');
76+
}
77+
$userLogins = $request['Attributes'][$this->attributeIdentifier];
78+
79+
$redirectUser = false;
80+
81+
foreach ($userLogins as $userLogin) {
82+
if (in_array($userLogin, $listOfLoginsToRedirect)) {
83+
$redirectUser = true;
84+
continue;
85+
}
86+
}
87+
88+
if (!$redirectUser) {
89+
Logger::debug('perun:RedirectSomeUsers - Redirect is not required. Skipping to another process filter.');
90+
return;
91+
}
92+
93+
94+
$id = State::saveState($request, 'perun:redirectSomeUsers');
95+
$url = Module::getModuleURL('perun/redirect_some_users.php');
96+
$attributes = [
97+
'StateId' => $id,
98+
'allowedContinue' => $this->allowedContinue,
99+
'redirectURL' => $this->redirectURL,
100+
'pageText' => $this->pageText
101+
];
102+
HTTP::redirectTrustedURL($url, $attributes);
103+
}
104+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
use SimpleSAML\Module;
4+
use SimpleSAML\XHTML\Template;
5+
6+
/**
7+
* Template for warn user that he/she is accessing test SP
8+
*
9+
* Allow type hinting in IDE
10+
* @var Template $this
11+
*/
12+
13+
$this->data['header'] = '';
14+
$allowedContinue = $this->data['allowedContinue'];
15+
$redirectURL = $this->data['redirectURL'];
16+
$pageText = $this->data['pageText'];
17+
$this->includeAtTemplateBase('includes/header.php');
18+
19+
?>
20+
21+
<form method="post" action="<?php echo Module::getModuleURL('perun/redirect_some_users_continue.php'); ?>">
22+
23+
<input type="hidden" name="StateId" value="<?php echo $_REQUEST['StateId'] ?>">
24+
<h3> <?php echo $this->t('{perun:perun:redirect_some_users-header}') ?> </h3>
25+
</hr>
26+
</br>
27+
28+
<div> <?php echo $pageText ?> </div>
29+
30+
</hr>
31+
</br>
32+
33+
<?php
34+
if ($allowedContinue) {
35+
echo '<a class="btn btn-lg btn-block btn-primary" style="color:#FFF" target="_blank" href="' .
36+
$redirectURL . '">' . $this->t('{perun:perun:continue}') . '</a>';
37+
38+
39+
echo "</br>";
40+
echo '<div class="form-group">'. $this->t('{perun:perun:continue_to_service}') . '
41+
<input type="submit" value="' . $this->t('{perun:perun:here}') . '"
42+
class="btn btn-sm btn-link">
43+
</div>';
44+
} else {
45+
echo '<a class="btn btn-lg btn-block btn-primary "style="color:#FFF" href="' . $redirectURL . '">' .
46+
$this->t('{perun:perun:continue}') . '</a>';
47+
}
48+
?>
49+
50+
</form>
51+
52+
<?php
53+
54+
$this->includeAtTemplateBase('includes/footer.php');

www/redirect_some_users.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
use SimpleSAML\Auth\State;
4+
use SimpleSAML\Configuration;
5+
use SimpleSAML\XHTML\Template;
6+
use SimpleSAML\Locale\Language;
7+
8+
$id = $_REQUEST['StateId'];
9+
$state = State::loadState($id, 'perun:redirectSomeUsers');
10+
11+
$config = Configuration::getInstance();
12+
13+
$language = (new Language($config))->getLanguage();
14+
15+
$t = new Template($config, 'perun:redirect_some_users-tpl.php');
16+
$t->data['allowedContinue'] = $_REQUEST['allowedContinue'];
17+
$t->data['redirectURL'] = $_REQUEST['redirectURL'];
18+
$t->data['language'] = $language;
19+
20+
if (isset($_REQUEST['pageText'][$language])) {
21+
$t->data['pageText'] = $_REQUEST['pageText'][$language];
22+
} else {
23+
$t->data['pageText'] = $_REQUEST['pageText']['en'];
24+
}
25+
26+
27+
28+
$t->show();
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
use SimpleSAML\Auth\State;
4+
use SimpleSAML\Auth\ProcessingChain;
5+
6+
$id = $_REQUEST['StateId'];
7+
$state = State::loadState($id, 'perun:redirectSomeUsers');
8+
9+
ProcessingChain::resumeProcessing($state);

0 commit comments

Comments
 (0)