hook.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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['id'], $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. <?php if (version_compare(GLPI_VERSION, '10', '>=')) : ?>
  67. var $boxButtons = $('.singlesignon-box');
  68. $boxButtons.parent().hide();
  69. var $line = $boxButtons.prev('hr');
  70. if ($line.length) {
  71. $line.remove();
  72. }
  73. var $row = $boxButtons.closest('.row');
  74. var $boxLogin = $row.find('div:eq(0)');
  75. $boxButtons.addClass('col-md-5 text-center');
  76. $boxButtons.prependTo($row);
  77. $boxButtons.find('span').addClass("row mb-2");
  78. $boxButtons.find('span a').addClass("col-md-12");
  79. $boxLogin.hide();
  80. $(document).on("click", ".singlesignon.old-login", function(e) {
  81. e.preventDefault();
  82. $boxButtons.slideUp(function() {
  83. $boxLogin.slideDown(function() {
  84. $boxLogin.find(':input:eq(0)').focus();
  85. });
  86. });
  87. });
  88. var $backLogin = $('<label />', {
  89. css: {
  90. cursor: 'pointer'
  91. },
  92. text: "<< " + <?php echo json_encode(__('Back')) ?>,
  93. }).prependTo($boxLogin);
  94. $backLogin.on('click', function(e) {
  95. e.preventDefault();
  96. $boxLogin.slideUp(function() {
  97. $boxButtons.slideDown();
  98. });
  99. });
  100. <?php else : ?>
  101. var $boxLogin = $('#boxlogin');
  102. var $form = $boxLogin.find('form');
  103. var $boxButtons = $('.singlesignon-box');
  104. // Move the buttons to before form
  105. $boxButtons.prependTo($boxLogin);
  106. $boxButtons.find('span').addClass('login_input');
  107. // Show old form
  108. $(document).on("click", ".singlesignon.old-login", function(e) {
  109. e.preventDefault();
  110. $boxButtons.slideUp();
  111. $form.slideDown(function() {
  112. $('#login_name').focus();
  113. });
  114. });
  115. var $line = $('<p />', {
  116. class: 'login_input'
  117. }).prependTo($form);
  118. var $backLogin = $('<label />', {
  119. css: {
  120. cursor: 'pointer'
  121. },
  122. text: "<< " + <?php echo json_encode(__('Back')) ?>,
  123. }).appendTo($line);
  124. $backLogin.on('click', function(e) {
  125. e.preventDefault();
  126. $boxButtons.slideDown();
  127. $form.slideUp();
  128. });
  129. $form.hide();
  130. <?php endif; ?>
  131. });
  132. </script>
  133. <?php
  134. }
  135. }
  136. function plugin_singlesignon_install() {
  137. /* @var $DB DB */
  138. global $DB;
  139. $currentVersion = '0.0.0';
  140. $default = [];
  141. $current = Config::getConfigurationValues('singlesignon');
  142. if (isset($current['version'])) {
  143. $currentVersion = $current['version'];
  144. }
  145. foreach ($default as $key => $value) {
  146. if (!isset($current[$key])) {
  147. $current[$key] = $value;
  148. }
  149. }
  150. Config::setConfigurationValues('singlesignon', $current);
  151. if (!sso_TableExists("glpi_plugin_singlesignon_providers")) {
  152. $query = "CREATE TABLE `glpi_plugin_singlesignon_providers` (
  153. `id` int(11) NOT NULL auto_increment,
  154. `is_default` tinyint(1) NOT NULL DEFAULT '0',
  155. `popup` tinyint(1) NOT NULL DEFAULT '0',
  156. `split_domain` tinyint(1) NOT NULL DEFAULT '0',
  157. `authorized_domains` varchar(255) COLLATE utf8_unicode_ci NULL,
  158. `type` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  159. `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  160. `client_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  161. `client_secret` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  162. `scope` varchar(255) COLLATE utf8_unicode_ci NULL,
  163. `extra_options` varchar(255) COLLATE utf8_unicode_ci NULL,
  164. `url_authorize` varchar(255) COLLATE utf8_unicode_ci NULL,
  165. `url_access_token` varchar(255) COLLATE utf8_unicode_ci NULL,
  166. `url_resource_owner_details` varchar(255) COLLATE utf8_unicode_ci NULL,
  167. `is_active` tinyint(1) NOT NULL DEFAULT '0',
  168. `is_deleted` tinyint(1) NOT NULL default '0',
  169. `comment` text COLLATE utf8_unicode_ci,
  170. `date_mod` datetime DEFAULT NULL,
  171. `date_creation` datetime DEFAULT NULL,
  172. PRIMARY KEY (`id`),
  173. KEY `date_mod` (`date_mod`),
  174. KEY `date_creation` (`date_creation`)
  175. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
  176. $DB->query($query) or die("error creating glpi_plugin_singlesignon_providers " . $DB->error());
  177. } else {
  178. $query = "SHOW COLUMNS FROM glpi_plugin_singlesignon_providers LIKE 'is_default'";
  179. $result = $DB->query($query) or die($DB->error());
  180. if ($DB->numrows($result) != 1) {
  181. $DB->query("ALTER TABLE glpi_plugin_singlesignon_providers ADD is_default tinyint(1) NOT NULL DEFAULT '0'") or die($DB->error());
  182. }
  183. $query = "SHOW COLUMNS FROM glpi_plugin_singlesignon_providers LIKE 'popup'";
  184. $result = $DB->query($query) or die($DB->error());
  185. if ($DB->numrows($result) != 1) {
  186. $DB->query("ALTER TABLE glpi_plugin_singlesignon_providers ADD popup tinyint(1) NOT NULL DEFAULT '0'") or die($DB->error());
  187. }
  188. $query = "SHOW COLUMNS FROM glpi_plugin_singlesignon_providers LIKE 'split_domain'";
  189. $result = $DB->query($query) or die($DB->error());
  190. if ($DB->numrows($result) != 1) {
  191. $DB->query("ALTER TABLE glpi_plugin_singlesignon_providers ADD split_domain tinyint(1) NOT NULL DEFAULT '0'") or die($DB->error());
  192. }
  193. $query = "SHOW COLUMNS FROM glpi_plugin_singlesignon_providers LIKE 'authorized_domains'";
  194. $result = $DB->query($query) or die($DB->error());
  195. if ($DB->numrows($result) != 1) {
  196. $DB->query("ALTER TABLE glpi_plugin_singlesignon_providers ADD authorized_domains varchar(255) COLLATE utf8_unicode_ci NULL") or die($DB->error());
  197. }
  198. }
  199. // add display preferences
  200. $query_display_pref = "SELECT id
  201. FROM glpi_displaypreferences
  202. WHERE itemtype = 'PluginSinglesignonProvider'";
  203. $res_display_pref = $DB->query($query_display_pref);
  204. if ($DB->numrows($res_display_pref) == 0) {
  205. $DB->query("INSERT INTO `glpi_displaypreferences` VALUES (NULL,'PluginSinglesignonProvider','2','1','0');");
  206. $DB->query("INSERT INTO `glpi_displaypreferences` VALUES (NULL,'PluginSinglesignonProvider','3','2','0');");
  207. $DB->query("INSERT INTO `glpi_displaypreferences` VALUES (NULL,'PluginSinglesignonProvider','5','4','0');");
  208. $DB->query("INSERT INTO `glpi_displaypreferences` VALUES (NULL,'PluginSinglesignonProvider','6','5','0');");
  209. $DB->query("INSERT INTO `glpi_displaypreferences` VALUES (NULL,'PluginSinglesignonProvider','10','6','0');");
  210. }
  211. if (version_compare($currentVersion, "1.2.0", '<')) {
  212. $query = "ALTER TABLE `glpi_plugin_singlesignon_providers`
  213. ADD `picture` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  214. ADD `bgcolor` varchar(7) DEFAULT NULL,
  215. ADD `color` varchar(7) DEFAULT NULL";
  216. $DB->query($query) or die("error adding picture column " . $DB->error());
  217. }
  218. if (version_compare($currentVersion, "1.3.0", '<')) {
  219. $query = "CREATE TABLE `glpi_plugin_singlesignon_providers_users` (
  220. `id` int(11) NOT NULL AUTO_INCREMENT,
  221. `plugin_singlesignon_providers_id` int(11) NOT NULL DEFAULT '0',
  222. `users_id` int(11) NOT NULL DEFAULT '0',
  223. `remote_id` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  224. PRIMARY KEY (`id`),
  225. UNIQUE KEY `unicity` (`plugin_singlesignon_providers_id`,`users_id`),
  226. UNIQUE KEY `unicity_remote` (`plugin_singlesignon_providers_id`,`remote_id`)
  227. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;";
  228. $DB->query($query) or die("error creating glpi_plugin_singlesignon_providers_users " . $DB->error());
  229. }
  230. Config::setConfigurationValues('singlesignon', [
  231. 'version' => PLUGIN_SINGLESIGNON_VERSION,
  232. ]);
  233. return true;
  234. }
  235. function plugin_singlesignon_uninstall() {
  236. global $DB;
  237. $config = new Config();
  238. $condition = "`context` LIKE 'singlesignon%'";
  239. if (version_compare(GLPI_VERSION, '9.4', '>=')) {
  240. $condition = [$condition];
  241. }
  242. $rows = $config->find($condition);
  243. foreach ($rows as $id => $row) {
  244. $config->delete(['id' => $id]);
  245. }
  246. // Old version tables
  247. if (sso_TableExists("glpi_plugin_singlesignon_providers")) {
  248. $query = "DROP TABLE `glpi_plugin_singlesignon_providers`";
  249. $DB->query($query) or die("error deleting glpi_plugin_singlesignon_providers");
  250. }
  251. return true;
  252. }