preference.class.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. /**
  3. * ---------------------------------------------------------------------
  4. * SingleSignOn is a plugin which allows to use SSO for auth
  5. * ---------------------------------------------------------------------
  6. * Copyright (C) 2022 Edgard
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. * ---------------------------------------------------------------------
  21. * @copyright Copyright © 2021 - 2022 Edgard
  22. * @license http://www.gnu.org/licenses/gpl.txt GPLv3+
  23. * @link https://github.com/edgardmessias/glpi-singlesignon/
  24. * ---------------------------------------------------------------------
  25. */
  26. class PluginSinglesignonPreference extends CommonDBTM {
  27. static protected $notable = true;
  28. static $rightname = '';
  29. // Provider data
  30. public $user_id = null;
  31. public $providers = [];
  32. public $providers_users = [];
  33. public function __construct($user_id = null) {
  34. parent::__construct();
  35. $this->user_id = $user_id;
  36. }
  37. public function loadProviders() {
  38. $signon_provider = new PluginSinglesignonProvider();
  39. $condition = '`is_active` = 1';
  40. if (version_compare(GLPI_VERSION, '9.4', '>=')) {
  41. $condition = [$condition];
  42. }
  43. $this->providers = $signon_provider->find($condition);
  44. $provider_user = new PluginSinglesignonProvider_User();
  45. $condition = "`users_id` = {$this->user_id}";
  46. if (version_compare(GLPI_VERSION, '9.4', '>=')) {
  47. $condition = [$condition];
  48. }
  49. $this->providers_users = $provider_user->find($condition);
  50. }
  51. public function update(array $input, $history = 1, $options = []) {
  52. if (!isset($input['_remove_sso']) || !is_array($input['_remove_sso'])) {
  53. return false;
  54. }
  55. $ids = $input['_remove_sso'];
  56. if (empty($ids)) {
  57. return false;
  58. }
  59. $provider_user = new PluginSinglesignonProvider_User();
  60. $condition = "`users_id` = {$this->user_id} AND `id` IN (" . implode(',', $ids) . ")";
  61. if (version_compare(GLPI_VERSION, '9.4', '>=')) {
  62. $condition = [$condition];
  63. }
  64. $providers_users = $provider_user->find($condition);
  65. foreach ($providers_users as $pu) {
  66. $provider_user->delete($pu);
  67. }
  68. }
  69. function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) {
  70. switch (get_class($item)) {
  71. case 'Preference':
  72. case 'User':
  73. return [1 => __sso('Single Sign-on')];
  74. default:
  75. return '';
  76. }
  77. }
  78. static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0) {
  79. switch (get_class($item)) {
  80. case 'User':
  81. $prefer = new self($item->fields['id']);
  82. $prefer->loadProviders();
  83. $prefer->showFormUser($item);
  84. break;
  85. case 'Preference':
  86. $prefer = new self(Session::getLoginUserID());
  87. $prefer->loadProviders();
  88. $prefer->showFormPreference($item);
  89. break;
  90. }
  91. return true;
  92. }
  93. function showFormUser(CommonGLPI $item) {
  94. global $CFG_GLPI;
  95. if (!User::canView()) {
  96. return false;
  97. }
  98. $canedit = Session::haveRight(User::$rightname, UPDATE);
  99. if ($canedit) {
  100. echo "<form name='form' action=\"" . $CFG_GLPI['root_doc'] . "/plugins/singlesignon/front/user.form.php\" method='post'>";
  101. }
  102. echo Html::hidden('user_id', ['value' => $this->user_id]);
  103. echo "<div class='center' id='tabsbody'>";
  104. echo "<table class='tab_cadre_fixe'>";
  105. echo "<tr><th colspan='4'>" . __('Settings') . "</th></tr>";
  106. $this->showFormDefault($item);
  107. if ($canedit) {
  108. echo "<tr class='tab_bg_2'>";
  109. echo "<td colspan='4' class='center'>";
  110. echo "<input type='submit' name='update' class='submit' value=\"" . _sx('button', 'Save') . "\">";
  111. echo "</td></tr>";
  112. }
  113. echo "</table></div>";
  114. Html::closeForm();
  115. }
  116. function showFormPreference(CommonGLPI $item) {
  117. $user = new User();
  118. if (!$user->can($this->user_id, READ) && ($this->user_id != Session::getLoginUserID())) {
  119. return false;
  120. }
  121. $canedit = $this->user_id == Session::getLoginUserID();
  122. if ($canedit) {
  123. echo "<form name='form' action=\"" . Toolbox::getItemTypeFormURL(__CLASS__) . "\" method='post'>";
  124. }
  125. echo "<div class='center' id='tabsbody'>";
  126. echo "<table class='tab_cadre_fixe'>";
  127. echo "<tr><th colspan='4'>" . __('Settings') . "</th></tr>";
  128. $this->showFormDefault($item);
  129. if ($canedit) {
  130. echo "<tr class='tab_bg_2'>";
  131. echo "<td colspan='4' class='center'>";
  132. echo "<input type='submit' name='update' class='submit' value=\"" . _sx('button', 'Save') . "\">";
  133. echo "</td></tr>";
  134. }
  135. echo "</table></div>";
  136. Html::closeForm();
  137. }
  138. function showFormDefault(CommonGLPI $item) {
  139. echo "<tr class='tab_bg_2'>";
  140. echo "<td> " . __sso('Single Sign-on Provider') . "</td><td>";
  141. foreach ($this->providers as $p) {
  142. switch (get_class($item)) {
  143. case 'User':
  144. $redirect = $item->getFormURLWithID($this->user_id, true);
  145. break;
  146. case 'Preference':
  147. $redirect = $item->getSearchURL(false);
  148. break;
  149. default:
  150. $redirect = '';
  151. }
  152. $url = PluginSinglesignonToolbox::getCallbackUrl($p['id'], ['redirect' => $redirect]);
  153. echo PluginSinglesignonToolbox::renderButton($url, $p);
  154. echo " ";
  155. }
  156. echo "</td></tr>";
  157. echo "<tr class='tab_bg_2'>";
  158. if (!empty($this->providers_users)) {
  159. echo "<tr><th colspan='2'>" . __sso('Linked accounts') . "</th></tr>";
  160. foreach ($this->providers_users as $pu) {
  161. /** @var PluginSinglesignonProvider */
  162. $provider = PluginSinglesignonProvider::getById($pu['plugin_singlesignon_providers_id']);
  163. echo "<tr><td>";
  164. echo $provider->fields['name'] . ' (ID:' . $pu['remote_id'] . ')';
  165. echo "</td><td>";
  166. echo Html::getCheckbox([
  167. 'title' => __('Clear'),
  168. 'name' => "_remove_sso[]",
  169. 'value' => $pu['id'],
  170. ]);
  171. echo "&nbsp;" . __('Clear');
  172. echo "</td></tr>";
  173. }
  174. }
  175. ?>
  176. <script type="text/javascript">
  177. $(document).ready(function() {
  178. // On click, open a popup
  179. $(document).on("click", ".singlesignon.oauth-login", function(e) {
  180. e.preventDefault();
  181. var url = $(this).attr("href");
  182. var left = ($(window).width() / 2) - (600 / 2);
  183. var top = ($(window).height() / 2) - (800 / 2);
  184. var newWindow = window.open(url, "singlesignon", "width=600,height=800,left=" + left + ",top=" + top);
  185. if (window.focus) {
  186. newWindow.focus();
  187. }
  188. });
  189. });
  190. </script>
  191. <?php
  192. }
  193. }