Skip to content

Commit e122295

Browse files
committed
Enhance access URL management to integrate the login-only option - refs BT#22639
1 parent b7ea9af commit e122295

File tree

3 files changed

+80
-32
lines changed

3 files changed

+80
-32
lines changed

public/main/admin/access_url_edit.php

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
api_protect_global_admin_script();
1919

2020
$httpRequest = HttpRequest::createFromGlobals();
21+
$urlRepo = Container::getAccessUrlRepository();
2122

2223
$form = new FormValidator('add_url');
2324

@@ -30,26 +31,35 @@
3031
$form->addFile('url_image_1', get_lang('Image'));
3132
//$form->addElement('file', 'url_image_2', 'URL Image 2 (PNG)');
3233
//$form->addElement('file', 'url_image_3', 'URL Image 3 (PNG)');
34+
$form->addCheckBox('login_only', get_lang('Login only'), get_lang('Yes'));
3335

3436
$defaults['url'] = 'http://';
3537
$form->setDefaults($defaults);
3638
if ($httpRequest->query->has('url_id')) {
3739
$url_id = $httpRequest->query->getInt('url_id');
38-
$num_url_id = UrlManager::url_id_exist($url_id);
39-
if (1 != $num_url_id) {
40+
41+
/** @var AccessUrl $url_data */
42+
$url_data = $urlRepo->find($url_id);
43+
44+
if (!$url_data) {
4045
header('Location: access_urls.php');
4146
exit();
4247
}
43-
$url_data = UrlManager::get_url_data_from_id($url_id);
44-
$form->addElement('hidden', 'id', $url_data['id']);
48+
$form->addElement('hidden', 'id', $url_data->getId());
4549
// If we're still with localhost (should only happen at the very beginning)
4650
// offer the current URL by default. Once this has been saved, no more
4751
// magic will happen, ever.
48-
if ($url_data['id'] === 1 && $url_data['url'] === AccessUrl::DEFAULT_ACCESS_URL) {
52+
if ($url_data->getId() === 1 && $url_data->getUrl() === AccessUrl::DEFAULT_ACCESS_URL) {
4953
$https = api_is_https() ? 'https://' : 'http://';
50-
$url_data['url'] = $https.$_SERVER['HTTP_HOST'].'/';
54+
$url_data->setUrl($https.$_SERVER['HTTP_HOST'].'/');
5155
}
52-
$form->setDefaults($url_data);
56+
$form->setDefaults([
57+
'id' => $url_data->getId(),
58+
'url' => $url_data->getUrl(),
59+
'description' => $url_data->getDescription(),
60+
'active' => $url_data->getActive(),
61+
'login_only' => $url_data->isLoginOnly(),
62+
]);
5363
}
5464

5565
$form->addHidden(
@@ -71,18 +81,30 @@
7181
$description = Security::remove_XSS($url_array['description']);
7282
$active = isset($url_array['active']) ? (int) $url_array['active'] : 0;
7383
$url_id = isset($url_array['id']) ? (int) $url_array['id'] : 0;
84+
$isLoginOnly = isset($url_array['login_only']) && (bool) $url_array['login_only'];
7485
$url_to_go = 'access_urls.php';
7586
if (!empty($url_id)) {
7687
//we can't change the status of the url with id=1
7788
if (1 == $url_id) {
7889
$active = 1;
7990
}
8091
// Checking url
81-
if ('/' == substr($url, strlen($url) - 1, strlen($url))) {
82-
UrlManager::update($url_id, $url, $description, $active);
83-
} else {
84-
UrlManager::update($url_id, $url.'/', $description, $active);
92+
if ('/' != substr($url, strlen($url) - 1, strlen($url))) {
93+
$url .= '/';
8594
}
95+
96+
/** @var AccessUrl $accessUrl */
97+
$accessUrl = $urlRepo->find($url_id);
98+
99+
$accessUrl
100+
->setUrl($url)
101+
->setDescription($description)
102+
->setActive($active)
103+
->setCreatedBy(api_get_user_id())
104+
->setTms(api_get_utc_datetime())
105+
->setIsLoginOnly($isLoginOnly)
106+
;
107+
86108
$url_to_go = 'access_urls.php';
87109
$message = get_lang('The URL has been edited');
88110
} else {
@@ -91,19 +113,32 @@
91113
$message = get_lang('This URL already exists, please select another URL');
92114
if (0 === $num) {
93115
// checking url
94-
if ('/' == substr($url, strlen($url) - 1, strlen($url))) {
95-
$accessUrl = UrlManager::add($url, $description, $active);
96-
} else {
97-
//create
98-
$accessUrl = UrlManager::add($url.'/', $description, $active);
116+
if ('/' != substr($url, strlen($url) - 1, strlen($url))) {
117+
$url .= '/';
99118
}
100-
if (null !== $accessUrl) {
119+
120+
$accessUrl = $urlRepo->findOneBy(['url' => $url]);
121+
122+
if (!$accessUrl) {
123+
$accessUrl = new AccessUrl();
124+
$accessUrl
125+
->setDescription($description)
126+
->setActive($active)
127+
->setUrl($url)
128+
->setCreatedBy(api_get_user_id())
129+
->setIsLoginOnly($isLoginOnly)
130+
;
131+
132+
Database::getManager()->persist($accessUrl);
133+
101134
$message = get_lang('The URL has been added');
102135
$url_to_go = 'access_urls.php';
103136
}
104137
}
105138
}
106139

140+
Database::getManager()->flush();
141+
107142
Security::clear_token();
108143
$tok = Security::get_token();
109144
Display::addFlash(Display::return_message($message));

public/main/admin/access_urls.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
$httpRequest = HttpRequest::createFromGlobals();
2424

25+
$translator = Container::$container->get('translator');;
26+
2527
$interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('Administration')];
2628
$tool_name = get_lang('Multiple access URL / Branding');
2729
Display :: display_header($tool_name);
@@ -213,16 +215,24 @@
213215
'</a>';
214216
}
215217

216-
$rows[] = [$link, $desc, $status, $ts, $rowActions];
218+
$rows[] = [
219+
$link,
220+
$desc,
221+
$status,
222+
$u->isLoginOnly() ? $translator->trans('Yes') : $translator->trans('No'),
223+
$ts,
224+
$rowActions,
225+
];
217226
}
218227

219228
$table = new SortableTableFromArrayConfig($rows, 2, 50, 'urls');
220229
$table->set_additional_parameters($parameters);
221230
$table->set_header(0, 'URL');
222231
$table->set_header(1, get_lang('Description'));
223232
$table->set_header(2, get_lang('Active'));
224-
$table->set_header(3, get_lang('Created at'));
225-
$table->set_header(4, get_lang('Edit'), false);
233+
$table->set_header(3, get_lang('Is login only'));
234+
$table->set_header(4, get_lang('Created at'));
235+
$table->set_header(5, get_lang('Edit'), false);
226236
$table->display();
227237

228238
Display::display_footer();

public/main/inc/lib/urlmanager.lib.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class UrlManager
2121
* @param string $description The description of the site
2222
* @param int $active is active or not
2323
*/
24-
public static function add($url, $description, $active): ?AccessUrl
24+
public static function add($url, $description, $active, bool $isLoginOnly = false): ?AccessUrl
2525
{
2626
$repo = Container::getAccessUrlRepository();
2727

@@ -37,6 +37,7 @@ public static function add($url, $description, $active): ?AccessUrl
3737
->setActive($active)
3838
->setUrl($url)
3939
->setCreatedBy(api_get_user_id())
40+
->setIsLoginOnly($isLoginOnly)
4041
;
4142

4243
$repo->create($accessUrl);
@@ -56,23 +57,25 @@ public static function add($url, $description, $active): ?AccessUrl
5657
*
5758
* @return bool if success
5859
*/
59-
public static function update($urlId, $url, $description, $active)
60+
public static function update($urlId, $url, $description, $active, bool $isLoginOnly = false)
6061
{
6162
$urlId = (int) $urlId;
6263
$active = (int) $active;
6364

6465
$table = Database::get_main_table(TABLE_MAIN_ACCESS_URL);
65-
$sql = "UPDATE $table
66-
SET url = '".Database::escape_string($url)."',
67-
description = '".Database::escape_string($description)."',
68-
active = '".$active."',
69-
created_by = '".api_get_user_id()."',
70-
tms = '".api_get_utc_datetime()."'
71-
WHERE id = '$urlId'";
7266

73-
$result = Database::query($sql);
74-
75-
return $result;
67+
return Database::update(
68+
$table,
69+
[
70+
'url' => $url,
71+
'description' => $description,
72+
'active' => $active,
73+
'created_by' => api_get_user_id(),
74+
'tms' => api_get_utc_datetime(),
75+
'is_login_only' => $isLoginOnly,
76+
],
77+
['id = ?' => [$urlId]]
78+
);
7679
}
7780

7881
/**

0 commit comments

Comments
 (0)