hook.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. function plugin_singlesignon_display_login() {
  3. global $CFG_GLPI;
  4. $signon_provider = new PluginSinglesignonProvider();
  5. $condition = '`is_active` = 1';
  6. if (version_compare(GLPI_VERSION, '9.4', '>=')) {
  7. $condition = [$condition];
  8. }
  9. $rows = $signon_provider->find($condition);
  10. $html = [];
  11. foreach ($rows as $row) {
  12. $query = [];
  13. if (isset($_REQUEST['redirect'])) {
  14. $query['redirect'] = $_REQUEST['redirect'];
  15. }
  16. $url = PluginSinglesignonToolbox::getCallbackUrl($row, $query);
  17. $isDefault = PluginSinglesignonToolbox::isDefault($row);
  18. if ($isDefault && !isset($_GET["noAUTO"])) {
  19. Html::redirect($url);
  20. return;
  21. }
  22. $html[] = PluginSinglesignonToolbox::renderButton($url, $row);
  23. }
  24. if (!empty($html)) {
  25. echo '<div class="singlesignon-box">';
  26. echo implode(" \n", $html);
  27. echo PluginSinglesignonToolbox::renderButton('#', ['name' => __('GLPI')], 'vsubmit old-login');
  28. echo '</div>';
  29. ?>
  30. <style>
  31. #display-login .singlesignon-box span {
  32. display: inline-block;
  33. margin: 5px;
  34. }
  35. #display-login .singlesignon-box .old-login {
  36. display: none;
  37. }
  38. #boxlogin .singlesignon-box span {
  39. display: block;
  40. }
  41. #boxlogin .singlesignon-box .vsubmit {
  42. display: flex;
  43. justify-content: center;
  44. align-items: center;
  45. font-size: 1.3em !important;
  46. text-align: center;
  47. box-sizing: border-box;
  48. }
  49. #boxlogin .singlesignon-box .vsubmit img {
  50. vertical-align: sub;
  51. }
  52. </style>
  53. <script type="text/javascript">
  54. $(document).ready(function() {
  55. // On click, open a popup
  56. $(document).on("click", ".singlesignon.oauth-login.popup", function(e) {
  57. e.preventDefault();
  58. var url = $(this).attr("href");
  59. var left = ($(window).width() / 2) - (600 / 2);
  60. var top = ($(window).height() / 2) - (800 / 2);
  61. var newWindow = window.open(url, "singlesignon", "width=600,height=800,left=" + left + ",top=" + top);
  62. if (window.focus) {
  63. newWindow.focus();
  64. }
  65. });
  66. var $boxLogin = $('#boxlogin');
  67. var $form = $boxLogin.find('form');
  68. var $boxButtons = $('.singlesignon-box');
  69. // Move the buttons to before form
  70. $boxButtons.prependTo($boxLogin);
  71. $boxButtons.find('span').addClass('login_input');
  72. // Show old form
  73. $(document).on("click", ".singlesignon.old-login", function(e) {
  74. e.preventDefault();
  75. $boxButtons.slideToggle();
  76. $form.slideToggle(function() {
  77. $('#login_name').focus();
  78. });
  79. });
  80. var $line = $('<p />', {
  81. class: 'login_input'
  82. }).prependTo($form);
  83. var $backLogin = $('<label />', {
  84. css: {
  85. cursor: 'pointer'
  86. },
  87. text: "<< " + <?php echo json_encode(__('Back')) ?>,
  88. }).appendTo($line);
  89. $backLogin.on('click', function(e) {
  90. e.preventDefault();
  91. $boxButtons.slideToggle();
  92. $form.slideToggle();
  93. });
  94. $form.hide();
  95. });
  96. </script>
  97. <?php
  98. }
  99. }
  100. function plugin_singlesignon_install() {
  101. /* @var $DB DB */
  102. global $DB;
  103. $currentVersion = '0.0.0';
  104. $default = [];
  105. $current = Config::getConfigurationValues('singlesignon');
  106. if (isset($current['version'])) {
  107. $currentVersion = $current['version'];
  108. }
  109. foreach ($default as $key => $value) {
  110. if (!isset($current[$key])) {
  111. $current[$key] = $value;
  112. }
  113. }
  114. Config::setConfigurationValues('singlesignon', $current);
  115. if (!sso_TableExists("glpi_plugin_singlesignon_providers")) {
  116. $query = "CREATE TABLE `glpi_plugin_singlesignon_providers` (
  117. `id` int(11) NOT NULL auto_increment,
  118. `is_default` tinyint(1) NOT NULL DEFAULT '0',
  119. `popup` tinyint(1) NOT NULL DEFAULT '0',
  120. `split_domain` tinyint(1) NOT NULL DEFAULT '0',
  121. `authorized_domains` varchar(255) COLLATE utf8_unicode_ci NULL,
  122. `type` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  123. `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  124. `client_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  125. `client_secret` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  126. `scope` varchar(255) COLLATE utf8_unicode_ci NULL,
  127. `extra_options` varchar(255) COLLATE utf8_unicode_ci NULL,
  128. `url_authorize` varchar(255) COLLATE utf8_unicode_ci NULL,
  129. `url_access_token` varchar(255) COLLATE utf8_unicode_ci NULL,
  130. `url_resource_owner_details` varchar(255) COLLATE utf8_unicode_ci NULL,
  131. `is_active` tinyint(1) NOT NULL DEFAULT '0',
  132. `is_deleted` tinyint(1) NOT NULL default '0',
  133. `comment` text COLLATE utf8_unicode_ci,
  134. `date_mod` datetime DEFAULT NULL,
  135. `date_creation` datetime DEFAULT NULL,
  136. PRIMARY KEY (`id`),
  137. KEY `date_mod` (`date_mod`),
  138. KEY `date_creation` (`date_creation`)
  139. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
  140. $DB->query($query) or die("error creating glpi_plugin_singlesignon_providers " . $DB->error());
  141. } else {
  142. $query = "SHOW COLUMNS FROM glpi_plugin_singlesignon_providers LIKE 'is_default'";
  143. $result = $DB->query($query) or die($DB->error());
  144. if ($DB->numrows($result) != 1) {
  145. $DB->query("ALTER TABLE glpi_plugin_singlesignon_providers ADD is_default tinyint(1) NOT NULL DEFAULT '0'") or die ($DB->error());
  146. }
  147. $query = "SHOW COLUMNS FROM glpi_plugin_singlesignon_providers LIKE 'popup'";
  148. $result = $DB->query($query) or die($DB->error());
  149. if ($DB->numrows($result) != 1) {
  150. $DB->query("ALTER TABLE glpi_plugin_singlesignon_providers ADD popup tinyint(1) NOT NULL DEFAULT '0'") or die ($DB->error());
  151. }
  152. $query = "SHOW COLUMNS FROM glpi_plugin_singlesignon_providers LIKE 'split_domain'";
  153. $result = $DB->query($query) or die($DB->error());
  154. if ($DB->numrows($result) != 1) {
  155. $DB->query("ALTER TABLE glpi_plugin_singlesignon_providers ADD split_domain tinyint(1) NOT NULL DEFAULT '0'") or die ($DB->error());
  156. }
  157. $query = "SHOW COLUMNS FROM glpi_plugin_singlesignon_providers LIKE 'authorized_domains'";
  158. $result = $DB->query($query) or die($DB->error());
  159. if ($DB->numrows($result) != 1) {
  160. $DB->query("ALTER TABLE glpi_plugin_singlesignon_providers ADD authorized_domains varchar(255) COLLATE utf8_unicode_ci NULL") or die ($DB->error());
  161. }
  162. }
  163. // add display preferences
  164. $query_display_pref = "SELECT id
  165. FROM glpi_displaypreferences
  166. WHERE itemtype = 'PluginSinglesignonProvider'";
  167. $res_display_pref = $DB->query($query_display_pref);
  168. if ($DB->numrows($res_display_pref) == 0) {
  169. $DB->query("INSERT INTO `glpi_displaypreferences` VALUES (NULL,'PluginSinglesignonProvider','2','1','0');");
  170. $DB->query("INSERT INTO `glpi_displaypreferences` VALUES (NULL,'PluginSinglesignonProvider','3','2','0');");
  171. $DB->query("INSERT INTO `glpi_displaypreferences` VALUES (NULL,'PluginSinglesignonProvider','5','4','0');");
  172. $DB->query("INSERT INTO `glpi_displaypreferences` VALUES (NULL,'PluginSinglesignonProvider','6','5','0');");
  173. $DB->query("INSERT INTO `glpi_displaypreferences` VALUES (NULL,'PluginSinglesignonProvider','10','6','0');");
  174. }
  175. if (version_compare($currentVersion, "1.2.0", '<')) {
  176. $query = "ALTER TABLE `glpi_plugin_singlesignon_providers`
  177. ADD `picture` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  178. ADD `bgcolor` varchar(7) DEFAULT NULL,
  179. ADD `color` varchar(7) DEFAULT NULL";
  180. $DB->query($query) or die("error adding picture column " . $DB->error());
  181. }
  182. if (version_compare($currentVersion, "1.3.0", '<')) {
  183. $query = "CREATE TABLE `glpi_plugin_singlesignon_providers_users` (
  184. `id` int(11) NOT NULL AUTO_INCREMENT,
  185. `plugin_singlesignon_providers_id` int(11) NOT NULL DEFAULT '0',
  186. `users_id` int(11) NOT NULL DEFAULT '0',
  187. `remote_id` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  188. PRIMARY KEY (`id`),
  189. UNIQUE KEY `unicity` (`plugin_singlesignon_providers_id`,`users_id`),
  190. UNIQUE KEY `unicity_remote` (`plugin_singlesignon_providers_id`,`remote_id`)
  191. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;";
  192. $DB->query($query) or die("error creating glpi_plugin_singlesignon_providers_users " . $DB->error());
  193. }
  194. Config::setConfigurationValues('singlesignon', [
  195. 'version' => PLUGIN_SINGLESIGNON_VERSION,
  196. ]);
  197. return true;
  198. }
  199. function plugin_singlesignon_uninstall() {
  200. global $DB;
  201. $config = new Config();
  202. $condition = "`context` LIKE 'singlesignon%'";
  203. if (version_compare(GLPI_VERSION, '9.4', '>=')) {
  204. $condition = [$condition];
  205. }
  206. $rows = $config->find($condition);
  207. foreach ($rows as $id => $row) {
  208. $config->delete(['id' => $id]);
  209. }
  210. // Old version tables
  211. if (sso_TableExists("glpi_plugin_singlesignon_providers")) {
  212. $query = "DROP TABLE `glpi_plugin_singlesignon_providers`";
  213. $DB->query($query) or die("error deleting glpi_plugin_singlesignon_providers");
  214. }
  215. return true;
  216. }