';
61 | echo '
';
62 | echo "### BEGIN ###\n";
63 | $signon_provider->getResourceOwner();
64 | echo "### END ###";
65 | echo '
';
66 | Html::nullFooter();
67 | exit();
68 | }
69 |
70 | $user_id = Session::getLoginUserID();
71 |
72 | $REDIRECT = "";
73 |
74 | if ($user_id || $signon_provider->login()) {
75 |
76 | $user_id = $user_id ?: Session::getLoginUserID();
77 |
78 | if ($user_id) {
79 | $signon_provider->linkUser($user_id);
80 | }
81 |
82 | $params = PluginSinglesignonToolbox::getCallbackParameters('q');
83 |
84 | if (isset($params['redirect'])) {
85 | $REDIRECT = '?redirect=' . $params['redirect'];
86 | } else if (isset($_GET['state']) && is_integer(strpos($_GET['state'], ";redirect="))) {
87 | $REDIRECT = '?' . substr($_GET['state'], strpos($_GET['state'], ";redirect=") + 1);
88 | }
89 |
90 | $url_redirect = '';
91 |
92 | if ($_SESSION["glpiactiveprofile"]["interface"] == "helpdesk") {
93 | if ($_SESSION['glpiactiveprofile']['create_ticket_on_login'] && empty($REDIRECT)) {
94 | $url_redirect = PluginSinglesignonToolbox::getBaseURL() . "/front/helpdesk.public.php?create_ticket=1";
95 | } else {
96 | $url_redirect = PluginSinglesignonToolbox::getBaseURL() . "/front/helpdesk.public.php$REDIRECT";
97 | }
98 | } else {
99 | if ($_SESSION['glpiactiveprofile']['create_ticket_on_login'] && empty($REDIRECT)) {
100 | $url_redirect = PluginSinglesignonToolbox::getBaseURL() . "/front/ticket.form.php";
101 | } else {
102 | $url_redirect = PluginSinglesignonToolbox::getBaseURL() . "/front/central.php$REDIRECT";
103 | }
104 | }
105 |
106 | Html::nullHeader("Login", PluginSinglesignonToolbox::getBaseURL() . '/index.php');
107 | echo '
';
117 | Html::nullFooter();
118 | exit();
119 |
120 | // Auth::redirectIfAuthenticated();
121 |
122 | }
123 |
124 | // we have done at least a good login? No, we exit.
125 | Html::nullHeader("Login", PluginSinglesignonToolbox::getBaseURL() . '/index.php');
126 | echo '
' . __('User not authorized to connect in GLPI') . '
';
127 | // Logout whit noAUto to manage auto_login with errors
128 | echo '
' . __('Log in again') . '';
130 | echo '';
140 | Html::nullFooter();
141 | exit();
142 |
--------------------------------------------------------------------------------
/front/picture.send.php:
--------------------------------------------------------------------------------
1 | .
21 | * ---------------------------------------------------------------------
22 | * @copyright Copyright © 2021 - 2022 Edgard
23 | * @license http://www.gnu.org/licenses/gpl.txt GPLv3+
24 | * @link https://github.com/edgardmessias/glpi-singlesignon/
25 | * ---------------------------------------------------------------------
26 | */
27 |
28 | include('../../../inc/includes.php');
29 |
30 | $provider = new PluginSinglesignonProvider();
31 | $path = false;
32 |
33 | if (isset($_GET['id'])) { // docid for document
34 | if (!$provider->getFromDB($_GET['id'])) {
35 | Html::displayErrorAndDie(__('Unknown file'), true);
36 | }
37 |
38 | $path = $provider->fields['picture'];
39 | } else if (isset($_GET['path'])) {
40 | $path = $_GET['path'];
41 | } else {
42 | Html::displayErrorAndDie(__('Invalid filename'), true);
43 | }
44 |
45 | $path = GLPI_PLUGIN_DOC_DIR . "/singlesignon/" . $path;
46 |
47 | if (!file_exists($path)) {
48 | Html::displayErrorAndDie(__('File not found'), true); // Not found
49 | }
50 |
51 | Toolbox::sendFile($path, "logo.png", null, true);
52 |
--------------------------------------------------------------------------------
/front/preference.form.php:
--------------------------------------------------------------------------------
1 | .
21 | * ---------------------------------------------------------------------
22 | * @copyright Copyright © 2021 - 2022 Edgard
23 | * @license http://www.gnu.org/licenses/gpl.txt GPLv3+
24 | * @link https://github.com/edgardmessias/glpi-singlesignon/
25 | * ---------------------------------------------------------------------
26 | */
27 |
28 | include ('../../../inc/includes.php');
29 |
30 | Session::checkLoginUser();
31 |
32 | if (isset($_POST["update"])) {
33 |
34 | $prefer = new PluginSinglesignonPreference(Session::getLoginUserID());
35 | $prefer->loadProviders();
36 |
37 | $prefer->update($_POST);
38 |
39 | Html::back();
40 | } else {
41 | Html::back();
42 | }
43 |
--------------------------------------------------------------------------------
/front/provider.form.php:
--------------------------------------------------------------------------------
1 | .
21 | * ---------------------------------------------------------------------
22 | * @copyright Copyright © 2021 - 2022 Edgard
23 | * @license http://www.gnu.org/licenses/gpl.txt GPLv3+
24 | * @link https://github.com/edgardmessias/glpi-singlesignon/
25 | * ---------------------------------------------------------------------
26 | */
27 |
28 | use Glpi\Event;
29 |
30 | include ('../../../inc/includes.php');
31 |
32 | Session::checkRight("config", UPDATE);
33 |
34 | if (!isset($_GET["id"])) {
35 | $_GET["id"] = "";
36 | }
37 |
38 | $provider = new PluginSinglesignonProvider();
39 |
40 | if (isset($_POST["add"])) {
41 | $provider->check(-1, CREATE, $_POST);
42 |
43 | if ($newID = $provider->add($_POST)) {
44 | Event::log($newID, "singlesignon", 4, "provider",
45 | sprintf(__('%1$s adds the item %2$s'), $_SESSION["glpiname"], $_POST["name"]));
46 | if ($_SESSION['glpibackcreated']) {
47 | Html::redirect($provider->getLinkURL());
48 | }
49 | }
50 | Html::back();
51 | } else if (isset($_POST["delete"])) {
52 | $provider->check($_POST["id"], DELETE);
53 | $provider->delete($_POST);
54 |
55 | Event::log($_POST["id"], "singlesignon", 4, "provider",
56 | //TRANS: %s is the user login
57 | sprintf(__('%s deletes an item'), $_SESSION["glpiname"]));
58 |
59 | $provider->redirectToList();
60 | } else if (isset($_POST["restore"])) {
61 | $provider->check($_POST["id"], DELETE);
62 |
63 | $provider->restore($_POST);
64 | Event::log($_POST["id"], "singlesignon", 4, "provider",
65 | //TRANS: %s is the user login
66 | sprintf(__('%s restores an item'), $_SESSION["glpiname"]));
67 | $provider->redirectToList();
68 | } else if (isset($_POST["purge"])) {
69 | $provider->check($_POST["id"], PURGE);
70 |
71 | $provider->delete($_POST, 1);
72 | Event::log($_POST["id"], "singlesignon", 4, "provider",
73 | //TRANS: %s is the user login
74 | sprintf(__('%s purges an item'), $_SESSION["glpiname"]));
75 | $provider->redirectToList();
76 | } else if (isset($_POST["update"])) {
77 | $provider->check($_POST["id"], UPDATE);
78 |
79 | $provider->update($_POST);
80 | Event::log($_POST["id"], "singlesignon", 4, "provider",
81 | //TRANS: %s is the user login
82 | sprintf(__('%s updates an item'), $_SESSION["glpiname"]));
83 | Html::back();
84 | } else {
85 | if ($_SESSION["glpiactiveprofile"]["interface"] == "central") {
86 | Html::header(__sso('Single Sign-on'), $_SERVER['PHP_SELF'], "config", "pluginsinglesignonprovider", "");
87 | } else {
88 | Html::helpHeader(__sso('Single Sign-on'), $_SERVER['PHP_SELF']);
89 | }
90 |
91 | $provider->display($_GET);
92 | }
93 |
94 |
95 | Html::footer();
96 |
--------------------------------------------------------------------------------
/front/provider.php:
--------------------------------------------------------------------------------
1 | .
21 | * ---------------------------------------------------------------------
22 | * @copyright Copyright © 2021 - 2022 Edgard
23 | * @license http://www.gnu.org/licenses/gpl.txt GPLv3+
24 | * @link https://github.com/edgardmessias/glpi-singlesignon/
25 | * ---------------------------------------------------------------------
26 | */
27 |
28 | include ('../../../inc/includes.php');
29 |
30 | Session::checkRight("config", UPDATE);
31 |
32 | if ($_SESSION["glpiactiveprofile"]["interface"] == "central") {
33 | Html::header(__sso('Single Sign-on'), $_SERVER['PHP_SELF'], "config", "pluginsinglesignonprovider", "");
34 | } else {
35 | Html::helpHeader(__sso('Single Sign-on'), $_SERVER['PHP_SELF']);
36 | }
37 |
38 |
39 | //checkTypeRight('PluginExampleExample',"r");
40 |
41 | Search::show('PluginSinglesignonProvider');
42 |
43 | Html::footer();
44 |
--------------------------------------------------------------------------------
/front/user.form.php:
--------------------------------------------------------------------------------
1 | .
21 | * ---------------------------------------------------------------------
22 | * @copyright Copyright © 2021 - 2022 Edgard
23 | * @license http://www.gnu.org/licenses/gpl.txt GPLv3+
24 | * @link https://github.com/edgardmessias/glpi-singlesignon/
25 | * ---------------------------------------------------------------------
26 | */
27 |
28 | include('../../../inc/includes.php');
29 |
30 | Session::checkRight(User::$rightname, UPDATE);
31 |
32 | if (isset($_POST["update"]) && isset($_POST["user_id"])) {
33 |
34 | $prefer = new PluginSinglesignonPreference((int) $_POST["user_id"]);
35 | $prefer->loadProviders();
36 |
37 | $prefer->update($_POST);
38 |
39 | Html::back();
40 | } else {
41 | Html::back();
42 | }
43 |
--------------------------------------------------------------------------------
/hook.php:
--------------------------------------------------------------------------------
1 | .
21 | * ---------------------------------------------------------------------
22 | * @copyright Copyright © 2021 - 2022 Edgard
23 | * @license http://www.gnu.org/licenses/gpl.txt GPLv3+
24 | * @link https://github.com/edgardmessias/glpi-singlesignon/
25 | * ---------------------------------------------------------------------
26 | */
27 |
28 | function plugin_singlesignon_display_login() {
29 | global $CFG_GLPI;
30 |
31 | $signon_provider = new PluginSinglesignonProvider();
32 |
33 | $condition = '`is_active` = 1';
34 | if (version_compare(GLPI_VERSION, '9.4', '>=')) {
35 | $condition = [$condition];
36 | }
37 | $rows = $signon_provider->find($condition);
38 |
39 | $html = [];
40 |
41 | foreach ($rows as $row) {
42 | $query = [];
43 |
44 | if (isset($_REQUEST['redirect'])) {
45 | $query['redirect'] = $_REQUEST['redirect'];
46 | }
47 |
48 | $url = PluginSinglesignonToolbox::getCallbackUrl($row['id'], $query);
49 | $isDefault = PluginSinglesignonToolbox::isDefault($row);
50 | if ($isDefault && !isset($_GET["noAUTO"])) {
51 | Html::redirect($url);
52 | return;
53 | }
54 | $html[] = PluginSinglesignonToolbox::renderButton($url, $row);
55 | }
56 |
57 | if (!empty($html)) {
58 | echo '
';
59 | echo implode(" \n", $html);
60 | echo PluginSinglesignonToolbox::renderButton('#', ['name' => __('GLPI')], 'vsubmit old-login');
61 | echo '
';
62 | ?>
63 |
91 |
191 | $value) {
210 | if (!isset($current[$key])) {
211 | $current[$key] = $value;
212 | }
213 | }
214 |
215 | Config::setConfigurationValues('singlesignon', $current);
216 |
217 | if (!sso_TableExists("glpi_plugin_singlesignon_providers")) {
218 | $query = "CREATE TABLE `glpi_plugin_singlesignon_providers` (
219 | `id` int(11) NOT NULL auto_increment,
220 | `is_default` tinyint(1) NOT NULL DEFAULT '0',
221 | `popup` tinyint(1) NOT NULL DEFAULT '0',
222 | `split_domain` tinyint(1) NOT NULL DEFAULT '0',
223 | `authorized_domains` varchar(255) COLLATE utf8_unicode_ci NULL,
224 | `type` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
225 | `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
226 | `client_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
227 | `client_secret` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
228 | `scope` varchar(255) COLLATE utf8_unicode_ci NULL,
229 | `extra_options` varchar(255) COLLATE utf8_unicode_ci NULL,
230 | `url_authorize` varchar(255) COLLATE utf8_unicode_ci NULL,
231 | `url_access_token` varchar(255) COLLATE utf8_unicode_ci NULL,
232 | `url_resource_owner_details` varchar(255) COLLATE utf8_unicode_ci NULL,
233 | `is_active` tinyint(1) NOT NULL DEFAULT '0',
234 | `use_email_for_login` tinyint(1) NOT NULL DEFAULT '0',
235 | `split_name` tinyint(1) NOT NULL DEFAULT '0',
236 | `is_deleted` tinyint(1) NOT NULL default '0',
237 | `comment` text COLLATE utf8_unicode_ci,
238 | `date_mod` timestamp NULL DEFAULT NULL,
239 | `date_creation` timestamp NULL DEFAULT NULL,
240 | PRIMARY KEY (`id`),
241 | KEY `date_mod` (`date_mod`),
242 | KEY `date_creation` (`date_creation`)
243 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
244 |
245 | $DB->query($query) or die("error creating glpi_plugin_singlesignon_providers " . $DB->error());
246 | } else {
247 | $query = "SHOW COLUMNS FROM glpi_plugin_singlesignon_providers LIKE 'is_default'";
248 | $result = $DB->query($query) or die($DB->error());
249 | if ($DB->numrows($result) != 1) {
250 | $DB->query("ALTER TABLE glpi_plugin_singlesignon_providers ADD is_default tinyint(1) NOT NULL DEFAULT '0'") or die($DB->error());
251 | }
252 |
253 | $query = "SHOW COLUMNS FROM glpi_plugin_singlesignon_providers LIKE 'popup'";
254 | $result = $DB->query($query) or die($DB->error());
255 | if ($DB->numrows($result) != 1) {
256 | $DB->query("ALTER TABLE glpi_plugin_singlesignon_providers ADD popup tinyint(1) NOT NULL DEFAULT '0'") or die($DB->error());
257 | }
258 | $query = "SHOW COLUMNS FROM glpi_plugin_singlesignon_providers LIKE 'split_domain'";
259 | $result = $DB->query($query) or die($DB->error());
260 | if ($DB->numrows($result) != 1) {
261 | $DB->query("ALTER TABLE glpi_plugin_singlesignon_providers ADD split_domain tinyint(1) NOT NULL DEFAULT '0'") or die($DB->error());
262 | }
263 | $query = "SHOW COLUMNS FROM glpi_plugin_singlesignon_providers LIKE 'authorized_domains'";
264 | $result = $DB->query($query) or die($DB->error());
265 | if ($DB->numrows($result) != 1) {
266 | $DB->query("ALTER TABLE glpi_plugin_singlesignon_providers ADD authorized_domains varchar(255) COLLATE utf8_unicode_ci NULL") or die($DB->error());
267 | }
268 | $query = "SHOW COLUMNS FROM glpi_plugin_singlesignon_providers LIKE 'use_email_for_login'";
269 | $result = $DB->query($query) or die($DB->error());
270 | if ($DB->numrows($result) != 1) {
271 | $DB->query("ALTER TABLE glpi_plugin_singlesignon_providers ADD use_email_for_login tinyint(1) NOT NULL DEFAULT '0'") or die($DB->error());
272 | }
273 | $query = "SHOW COLUMNS FROM glpi_plugin_singlesignon_providers LIKE 'split_name'";
274 | $result = $DB->query($query) or die($DB->error());
275 | if ($DB->numrows($result) != 1) {
276 | $DB->query("ALTER TABLE glpi_plugin_singlesignon_providers ADD split_name tinyint(1) NOT NULL DEFAULT '0'") or die($DB->error());
277 | }
278 | }
279 |
280 | // add display preferences
281 | $query_display_pref = "SELECT id
282 | FROM glpi_displaypreferences
283 | WHERE itemtype = 'PluginSinglesignonProvider'";
284 | $res_display_pref = $DB->query($query_display_pref);
285 | if ($DB->numrows($res_display_pref) == 0) {
286 | $DB->query("INSERT INTO `glpi_displaypreferences` VALUES (NULL,'PluginSinglesignonProvider','2','1','0');");
287 | $DB->query("INSERT INTO `glpi_displaypreferences` VALUES (NULL,'PluginSinglesignonProvider','3','2','0');");
288 | $DB->query("INSERT INTO `glpi_displaypreferences` VALUES (NULL,'PluginSinglesignonProvider','5','4','0');");
289 | $DB->query("INSERT INTO `glpi_displaypreferences` VALUES (NULL,'PluginSinglesignonProvider','6','5','0');");
290 | $DB->query("INSERT INTO `glpi_displaypreferences` VALUES (NULL,'PluginSinglesignonProvider','10','6','0');");
291 | }
292 |
293 | if (!sso_TableExists("glpi_plugin_singlesignon_providers_users") && version_compare($currentVersion, "1.2.0", '<')) {
294 | $query = "ALTER TABLE `glpi_plugin_singlesignon_providers`
295 | ADD `picture` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
296 | ADD `bgcolor` varchar(7) DEFAULT NULL,
297 | ADD `color` varchar(7) DEFAULT NULL";
298 | $DB->query($query) or die("error adding picture column " . $DB->error());
299 | }
300 | if (!sso_TableExists("glpi_plugin_singlesignon_providers_users") && version_compare($currentVersion, "1.3.0", '<')) {
301 | $query = "CREATE TABLE `glpi_plugin_singlesignon_providers_users` (
302 | `id` int(11) NOT NULL AUTO_INCREMENT,
303 | `plugin_singlesignon_providers_id` int(11) NOT NULL DEFAULT '0',
304 | `users_id` int(11) NOT NULL DEFAULT '0',
305 | `remote_id` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
306 | PRIMARY KEY (`id`),
307 | UNIQUE KEY `unicity` (`plugin_singlesignon_providers_id`,`users_id`),
308 | UNIQUE KEY `unicity_remote` (`plugin_singlesignon_providers_id`,`remote_id`)
309 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;";
310 | $DB->query($query) or die("error creating glpi_plugin_singlesignon_providers_users " . $DB->error());
311 | }
312 |
313 | Config::setConfigurationValues('singlesignon', [
314 | 'version' => PLUGIN_SINGLESIGNON_VERSION,
315 | ]);
316 | return true;
317 | }
318 |
319 | function plugin_singlesignon_uninstall() {
320 | global $DB;
321 |
322 | $config = new Config();
323 | $condition = "`context` LIKE 'singlesignon%'";
324 | if (version_compare(GLPI_VERSION, '9.4', '>=')) {
325 | $condition = [$condition];
326 | }
327 | $rows = $config->find($condition);
328 |
329 | foreach ($rows as $id => $row) {
330 | $config->delete(['id' => $id]);
331 | }
332 |
333 | // Old version tables
334 | if (sso_TableExists("glpi_plugin_singlesignon_providers")) {
335 | $query = "DROP TABLE `glpi_plugin_singlesignon_providers`";
336 | $DB->query($query) or die("error deleting glpi_plugin_singlesignon_providers");
337 | }
338 |
339 | return true;
340 | }
341 |
--------------------------------------------------------------------------------
/inc/preference.class.php:
--------------------------------------------------------------------------------
1 | .
21 | * ---------------------------------------------------------------------
22 | * @copyright Copyright © 2021 - 2022 Edgard
23 | * @license http://www.gnu.org/licenses/gpl.txt GPLv3+
24 | * @link https://github.com/edgardmessias/glpi-singlesignon/
25 | * ---------------------------------------------------------------------
26 | */
27 |
28 | class PluginSinglesignonPreference extends CommonDBTM {
29 |
30 | static protected $notable = true;
31 | static $rightname = '';
32 |
33 | // Provider data
34 | public $user_id = null;
35 | public $providers = [];
36 | public $providers_users = [];
37 |
38 | public function __construct($user_id = null) {
39 | parent::__construct();
40 |
41 | $this->user_id = $user_id;
42 | }
43 |
44 | public function loadProviders() {
45 | $signon_provider = new PluginSinglesignonProvider();
46 |
47 | $condition = '`is_active` = 1';
48 | if (version_compare(GLPI_VERSION, '9.4', '>=')) {
49 | $condition = [$condition];
50 | }
51 | $this->providers = $signon_provider->find($condition);
52 |
53 | $provider_user = new PluginSinglesignonProvider_User();
54 |
55 | $condition = "`users_id` = {$this->user_id}";
56 | if (version_compare(GLPI_VERSION, '9.4', '>=')) {
57 | $condition = [$condition];
58 | }
59 | $this->providers_users = $provider_user->find($condition);
60 | }
61 |
62 | public function update(array $input, $history = 1, $options = []) {
63 | if (!isset($input['_remove_sso']) || !is_array($input['_remove_sso'])) {
64 | return false;
65 | }
66 |
67 | $ids = $input['_remove_sso'];
68 | if (empty($ids)) {
69 | return false;
70 | }
71 |
72 | $provider_user = new PluginSinglesignonProvider_User();
73 | $condition = "`users_id` = {$this->user_id} AND `id` IN (" . implode(',', $ids) . ")";
74 | if (version_compare(GLPI_VERSION, '9.4', '>=')) {
75 | $condition = [$condition];
76 | }
77 |
78 | $providers_users = $provider_user->find($condition);
79 |
80 | foreach ($providers_users as $pu) {
81 | $provider_user->delete($pu);
82 | }
83 | }
84 |
85 | function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) {
86 | switch (get_class($item)) {
87 | case 'Preference':
88 | case 'User':
89 | return [1 => __sso('Single Sign-on')];
90 | default:
91 | return '';
92 | }
93 | }
94 |
95 | static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0) {
96 | switch (get_class($item)) {
97 | case 'User':
98 | $prefer = new self($item->fields['id']);
99 | $prefer->loadProviders();
100 | $prefer->showFormUser($item);
101 | break;
102 | case 'Preference':
103 | $prefer = new self(Session::getLoginUserID());
104 | $prefer->loadProviders();
105 | $prefer->showFormPreference($item);
106 | break;
107 | }
108 | return true;
109 | }
110 |
111 | function showFormUser(CommonGLPI $item) {
112 | global $CFG_GLPI;
113 |
114 | if (!User::canView()) {
115 | return false;
116 | }
117 | $canedit = Session::haveRight(User::$rightname, UPDATE);
118 | if ($canedit) {
119 | echo "