hook.php 8.1 KB

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