preference.class.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. class PluginSinglesignonPreference extends CommonDBTM {
  3. static protected $notable = true;
  4. static $rightname = '';
  5. // Provider data
  6. public $user_id = null;
  7. public $providers = [];
  8. public $providers_users = [];
  9. public function __construct($user_id = null) {
  10. parent::__construct();
  11. $this->user_id = $user_id;
  12. }
  13. public function loadProviders() {
  14. $signon_provider = new PluginSinglesignonProvider();
  15. $condition = '`is_active` = 1';
  16. if (version_compare(GLPI_VERSION, '9.4', '>=')) {
  17. $condition = [$condition];
  18. }
  19. $this->providers = $signon_provider->find($condition);
  20. $provider_user = new PluginSinglesignonProvider_User();
  21. $condition = "`users_id` = {$this->user_id}";
  22. if (version_compare(GLPI_VERSION, '9.4', '>=')) {
  23. $condition = [$condition];
  24. }
  25. $this->providers_users = $provider_user->find($condition);
  26. }
  27. public function update(array $input, $history = 1, $options = []) {
  28. if (!isset($input['_remove_sso']) || !is_array($input['_remove_sso'])) {
  29. return false;
  30. }
  31. $ids = $input['_remove_sso'];
  32. if (empty($ids)) {
  33. return false;
  34. }
  35. $provider_user = new PluginSinglesignonProvider_User();
  36. $condition = "`users_id` = {$this->user_id} AND `id` IN (" . implode(',', $ids) . ")";
  37. if (version_compare(GLPI_VERSION, '9.4', '>=')) {
  38. $condition = [$condition];
  39. }
  40. $providers_users = $provider_user->find($condition);
  41. foreach ($providers_users as $pu) {
  42. $provider_user->delete($pu);
  43. }
  44. }
  45. function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) {
  46. switch (get_class($item)) {
  47. case 'Preference':
  48. case 'User':
  49. return [1 => __sso('Single Sign-on')];
  50. default:
  51. return '';
  52. }
  53. }
  54. static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0) {
  55. switch (get_class($item)) {
  56. case 'User':
  57. $prefer = new self($item->fields['id']);
  58. $prefer->loadProviders();
  59. $prefer->showFormUser($item);
  60. break;
  61. case 'Preference':
  62. $prefer = new self(Session::getLoginUserID());
  63. $prefer->loadProviders();
  64. $prefer->showFormPreference($item);
  65. break;
  66. }
  67. return true;
  68. }
  69. function showFormUser(CommonGLPI $item) {
  70. global $CFG_GLPI;
  71. if (!User::canView()) {
  72. return false;
  73. }
  74. $canedit = Session::haveRight(User::$rightname, UPDATE);
  75. if ($canedit) {
  76. echo "<form name='form' action=\"" . $CFG_GLPI['root_doc'] . "/plugins/singlesignon/front/user.form.php\" method='post'>";
  77. }
  78. echo Html::hidden('user_id', ['value' => $this->user_id]);
  79. echo "<div class='center' id='tabsbody'>";
  80. echo "<table class='tab_cadre_fixe'>";
  81. echo "<tr><th colspan='4'>" . __('Settings') . "</th></tr>";
  82. $this->showFormDefault($item);
  83. if ($canedit) {
  84. echo "<tr class='tab_bg_2'>";
  85. echo "<td colspan='4' class='center'>";
  86. echo "<input type='submit' name='update' class='submit' value=\"" . _sx('button', 'Save') . "\">";
  87. echo "</td></tr>";
  88. }
  89. echo "</table></div>";
  90. Html::closeForm();
  91. }
  92. function showFormPreference(CommonGLPI $item) {
  93. $user = new User();
  94. if (!$user->can($this->user_id, READ) && ($this->user_id != Session::getLoginUserID())) {
  95. return false;
  96. }
  97. $canedit = $this->user_id == Session::getLoginUserID();
  98. if ($canedit) {
  99. echo "<form name='form' action=\"" . Toolbox::getItemTypeFormURL(__CLASS__) . "\" method='post'>";
  100. }
  101. echo "<div class='center' id='tabsbody'>";
  102. echo "<table class='tab_cadre_fixe'>";
  103. echo "<tr><th colspan='4'>" . __('Settings') . "</th></tr>";
  104. $this->showFormDefault($item);
  105. if ($canedit) {
  106. echo "<tr class='tab_bg_2'>";
  107. echo "<td colspan='4' class='center'>";
  108. echo "<input type='submit' name='update' class='submit' value=\"" . _sx('button', 'Save') . "\">";
  109. echo "</td></tr>";
  110. }
  111. echo "</table></div>";
  112. Html::closeForm();
  113. }
  114. function showFormDefault(CommonGLPI $item) {
  115. echo "<tr class='tab_bg_2'>";
  116. echo "<td> " . __sso('Single Sign-on Provider') . "</td><td>";
  117. foreach ($this->providers as $p) {
  118. switch (get_class($item)) {
  119. case 'User':
  120. $redirect = $item->getFormURLWithID($this->user_id, true);
  121. break;
  122. case 'Preference':
  123. $redirect = $item->getSearchURL(false);
  124. break;
  125. default:
  126. $redirect = '';
  127. }
  128. $url = PluginSinglesignonToolbox::getCallbackUrl($p['id'], ['redirect' => $redirect]);
  129. echo PluginSinglesignonToolbox::renderButton($url, $p);
  130. echo " ";
  131. }
  132. echo "</td></tr>";
  133. echo "<tr class='tab_bg_2'>";
  134. if (!empty($this->providers_users)) {
  135. echo "<tr><th colspan='2'>" . __sso('Linked accounts') . "</th></tr>";
  136. foreach ($this->providers_users as $pu) {
  137. /** @var PluginSinglesignonProvider */
  138. $provider = PluginSinglesignonProvider::getById($pu['plugin_singlesignon_providers_id']);
  139. echo "<tr><td>";
  140. echo $provider->fields['name'] . ' (ID:' . $pu['remote_id'] . ')';
  141. echo "</td><td>";
  142. echo Html::getCheckbox([
  143. 'title' => __('Clear'),
  144. 'name' => "_remove_sso[]",
  145. 'value' => $pu['id'],
  146. ]);
  147. echo "&nbsp;" . __('Clear');
  148. echo "</td></tr>";
  149. }
  150. }
  151. ?>
  152. <script type="text/javascript">
  153. $(document).ready(function() {
  154. // On click, open a popup
  155. $(document).on("click", ".singlesignon.oauth-login", function(e) {
  156. e.preventDefault();
  157. var url = $(this).attr("href");
  158. var left = ($(window).width() / 2) - (600 / 2);
  159. var top = ($(window).height() / 2) - (800 / 2);
  160. var newWindow = window.open(url, "singlesignon", "width=600,height=800,left=" + left + ",top=" + top);
  161. if (window.focus) {
  162. newWindow.focus();
  163. }
  164. });
  165. });
  166. </script>
  167. <?php
  168. }
  169. }