provider.class.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. <?php
  2. class PluginSinglesignonProvider extends CommonDBTM {
  3. // From CommonDBTM
  4. public $dohistory = true;
  5. static $rightname = 'config';
  6. /**
  7. * Provider instance
  8. * @var null|\League\OAuth2\Client\Provider\GenericProvider
  9. */
  10. protected $_provider = null;
  11. protected $_code = null;
  12. /**
  13. *
  14. * @var null|\League\OAuth2\Client\Token\AccessToken
  15. */
  16. protected $_token = null;
  17. /**
  18. *
  19. * @var null|\League\OAuth2\Client\Provider\ResourceOwnerInterface
  20. */
  21. protected $_resource_owner = null;
  22. public static function canCreate() {
  23. return static::canUpdate();
  24. }
  25. public static function canDelete() {
  26. return static::canUpdate();
  27. }
  28. public static function canPurge() {
  29. return static::canUpdate();
  30. }
  31. public static function canView() {
  32. return static::canUpdate();
  33. }
  34. // Should return the localized name of the type
  35. static function getTypeName($nb = 0) {
  36. return __sso('Single Sign-on Provider');
  37. }
  38. /**
  39. * @see CommonGLPI::getMenuName()
  40. * */
  41. static function getMenuName() {
  42. return __sso('Single Sign-on');
  43. }
  44. /**
  45. * @see CommonGLPI::getAdditionalMenuLinks()
  46. * */
  47. static function getAdditionalMenuLinks() {
  48. global $CFG_GLPI;
  49. $links = array();
  50. // $links['add'] = '/plugins/singlesignon/front/provider.form.php';
  51. // $links['config'] = '/plugins/singlesignon/index.php';
  52. $links["<img src='" . $CFG_GLPI["root_doc"] . "/pics/menu_showall.png' title='" . __s('Show all') . "' alt='" . __s('Show all') . "'>"] = '/plugins/singlesignon/index.php';
  53. $links[__s('Test link', 'singlesignon')] = '/plugins/singlesignon/index.php';
  54. return $links;
  55. }
  56. function defineTabs($options = array()) {
  57. $ong = array();
  58. $this->addDefaultFormTab($ong);
  59. $this->addStandardTab(__CLASS__, $ong, $options);
  60. $this->addStandardTab('Log', $ong, $options);
  61. return $ong;
  62. }
  63. function post_getEmpty() {
  64. $this->fields["type"] = 'generic';
  65. $this->fields["is_active"] = 1;
  66. }
  67. function showForm($ID, $options = array()) {
  68. global $CFG_GLPI;
  69. $this->initForm($ID, $options);
  70. $this->showFormHeader($options);
  71. if (empty($this->fields["type"])) {
  72. $this->fields["type"] = 'generic';
  73. }
  74. echo "<tr class='tab_bg_1'>";
  75. echo "<td>" . __('Name') . "</td>";
  76. echo "<td>";
  77. Html::autocompletionTextField($this, "name");
  78. echo "</td>";
  79. echo "<td>" . __('Comments') . "</td>";
  80. echo "<td>";
  81. echo "<textarea name='comment' >" . $this->fields["comment"] . "</textarea>";
  82. echo "</td></tr>";
  83. $on_change = 'var _value = this.options[this.selectedIndex].value; $(".sso_url").toggle(_value == "generic");';
  84. echo "<tr class='tab_bg_1'>";
  85. echo "<td>" . __sso('SSO Type') . "</td><td>";
  86. self::dropdownType('type', array('value' => $this->fields["type"], 'on_change' => $on_change));
  87. echo "<td>" . __('Active') . "</td>";
  88. echo "<td>";
  89. Dropdown::showYesNo("is_active", $this->fields["is_active"]);
  90. echo "</td></tr>\n";
  91. echo "<tr class='tab_bg_1'>";
  92. echo "<td>" . __sso('Client ID') . "</td>";
  93. echo "<td><input type='text' style='width:96%' name='client_id' value='" . $this->fields["client_id"] . "'></td>";
  94. echo "<td>" . __sso('Client Secret') . "</td>";
  95. echo "<td><input type='text' style='width:96%' name='client_secret' value='" . $this->fields["client_secret"] . "'></td>";
  96. echo "</tr>\n";
  97. echo "<tr class='tab_bg_1'>";
  98. echo "<td>" . __sso('Scope') . "</td>";
  99. echo "<td><input type='text' style='width:96%' name='scope' value='" . $this->fields["scope"] . "'></td>";
  100. echo "<td>" . __sso('Extra Options') . "</td>";
  101. echo "<td><input type='text' style='width:96%' name='extra_options' value='" . $this->fields["extra_options"] . "'></td>";
  102. echo "</tr>\n";
  103. $url_style = "";
  104. if ($this->fields["type"] != 'generic') {
  105. $url_style = 'style="display: none;"';
  106. }
  107. echo "<tr class='tab_bg_1 sso_url' $url_style>";
  108. echo "<td>" . __sso('Authorize URL') . "</td>";
  109. echo "<td colspan='3'><input type='text' style='width:96%' name='url_authorize' value='" . $this->fields["url_authorize"] . "'></td>";
  110. echo "</tr>\n";
  111. echo "<tr class='tab_bg_1 sso_url' $url_style>";
  112. echo "<td>" . __sso('Access Token URL') . "</td>";
  113. echo "<td colspan='3'><input type='text' style='width:96%' name='url_access_token' value='" . $this->fields["url_access_token"] . "'></td>";
  114. echo "</tr>\n";
  115. echo "<tr class='tab_bg_1 sso_url' $url_style>";
  116. echo "<td>" . __sso('Resource Owner Details URL') . "</td>";
  117. echo "<td colspan='3'><input type='text' style='width:96%' name='url_resource_owner_details' value='" . $this->fields["url_resource_owner_details"] . "'></td>";
  118. echo "</tr>\n";
  119. $this->showFormButtons($options);
  120. return true;
  121. }
  122. function prepareInputForAdd($input) {
  123. return $this->prepareInput($input);
  124. }
  125. function prepareInputForUpdate($input) {
  126. return $this->prepareInput($input);
  127. }
  128. /**
  129. * Prepares input (for update and add)
  130. *
  131. * @param array $input Input data
  132. *
  133. * @return array
  134. */
  135. private function prepareInput($input) {
  136. $error_detected = array();
  137. $type = '';
  138. //check for requirements
  139. if (isset($input['type'])) {
  140. $type = $input['type'];
  141. }
  142. if (!isset($input['name']) || empty($input['name'])) {
  143. $error_detected[] = __sso('A Name is required');
  144. }
  145. if (empty($type)) {
  146. $error_detected[] = __('An item type is required');
  147. } else if (!isset(static::getTypes()[$type])) {
  148. $error_detected[] = sprintf(__sso('The "%s" is a Invalid type'), $type);
  149. }
  150. if (!isset($input['client_id']) || empty($input['client_id'])) {
  151. $error_detected[] = __sso('A Client ID is required');
  152. }
  153. if (!isset($input['client_secret']) || empty($input['client_secret'])) {
  154. $error_detected[] = __sso('A Client Secret is required');
  155. }
  156. if ($type === 'generic') {
  157. if (!isset($input['url_authorize']) || empty($input['url_authorize'])) {
  158. $error_detected[] = __sso('An Authorize URL is required');
  159. } else if (!filter_var($input['url_authorize'], FILTER_VALIDATE_URL)) {
  160. $error_detected[] = __sso('The Authorize URL is invalid');
  161. }
  162. if (!isset($input['url_access_token']) || empty($input['url_access_token'])) {
  163. $error_detected[] = __sso('An Access Token URL is required');
  164. } else if (!filter_var($input['url_access_token'], FILTER_VALIDATE_URL)) {
  165. $error_detected[] = __sso('The Access Token URL is invalid');
  166. }
  167. if (!isset($input['url_resource_owner_details']) || empty($input['url_resource_owner_details'])) {
  168. $error_detected[] = __sso('A Resource Owner Details URL is required');
  169. } else if (!filter_var($input['url_resource_owner_details'], FILTER_VALIDATE_URL)) {
  170. $error_detected[] = __sso('The Resource Owner Details URL is invalid');
  171. }
  172. }
  173. if (count($error_detected)) {
  174. foreach ($error_detected as $error) {
  175. Session::addMessageAfterRedirect(
  176. $error,
  177. true,
  178. ERROR
  179. );
  180. }
  181. return false;
  182. }
  183. return $input;
  184. }
  185. function getSearchOptions() {
  186. $tab = array();
  187. $tab['common'] = __('Characteristics');
  188. $tab[1]['table'] = $this->getTable();
  189. $tab[1]['field'] = 'type';
  190. $tab[1]['name'] = __('Type');
  191. $tab[1]['searchtype'] = 'equals';
  192. $tab[1]['datatype'] = 'specific';
  193. $tab[2]['table'] = $this->getTable();
  194. $tab[2]['field'] = 'name';
  195. $tab[2]['name'] = __('Name');
  196. $tab[2]['datatype'] = 'text';
  197. $tab[3]['table'] = $this->getTable();
  198. $tab[3]['field'] = 'client_id';
  199. $tab[3]['name'] = __sso('Client ID');
  200. $tab[3]['datatype'] = 'text';
  201. $tab[4]['table'] = $this->getTable();
  202. $tab[4]['field'] = 'client_secret';
  203. $tab[4]['name'] = __sso('Client Secret');
  204. $tab[4]['datatype'] = 'text';
  205. $tab[5]['table'] = $this->getTable();
  206. $tab[5]['field'] = 'scope';
  207. $tab[5]['name'] = __sso('Scope');
  208. $tab[5]['datatype'] = 'text';
  209. $tab[6]['table'] = $this->getTable();
  210. $tab[6]['field'] = 'extra_options';
  211. $tab[6]['name'] = __sso('Extra Options');
  212. $tab[6]['datatype'] = 'text';
  213. $tab[7]['table'] = $this->getTable();
  214. $tab[7]['field'] = 'url_authorize';
  215. $tab[7]['name'] = __sso('Authorize URL');
  216. $tab[7]['datatype'] = 'weblink';
  217. $tab[8]['table'] = $this->getTable();
  218. $tab[8]['field'] = 'url_access_token';
  219. $tab[8]['name'] = __sso('Access Token URL');
  220. $tab[8]['datatype'] = 'weblink';
  221. $tab[9]['table'] = $this->getTable();
  222. $tab[9]['field'] = 'url_resource_owner_details';
  223. $tab[9]['name'] = __sso('Resource Owner Details URL');
  224. $tab[9]['datatype'] = 'weblink';
  225. $tab[10]['table'] = $this->getTable();
  226. $tab[10]['field'] = 'is_active';
  227. $tab[10]['name'] = __('Active');
  228. $tab[10]['searchtype'] = 'equals';
  229. $tab[10]['datatype'] = 'bool';
  230. $tab[30]['table'] = $this->getTable();
  231. $tab[30]['field'] = 'id';
  232. $tab[30]['name'] = __('ID');
  233. return $tab;
  234. }
  235. static function getSpecificValueToDisplay($field, $values, array $options = array()) {
  236. if (!is_array($values)) {
  237. $values = array($field => $values);
  238. }
  239. switch ($field) {
  240. case 'type':
  241. return self::getTicketTypeName($values[$field]);
  242. case 'extra_options':
  243. return '<pre>' . $values[$field] . '</pre>';
  244. }
  245. return '';
  246. }
  247. static function getSpecificValueToSelect($field, $name = '', $values = '', array $options = array()) {
  248. if (!is_array($values)) {
  249. $values = array($field => $values);
  250. }
  251. $options['display'] = false;
  252. switch ($field) {
  253. case 'type':
  254. $options['value'] = $values[$field];
  255. return self::dropdownType($name, $options);
  256. }
  257. return parent::getSpecificValueToSelect($field, $name, $values, $options);
  258. }
  259. /**
  260. * Get ticket types
  261. *
  262. * @return array of types
  263. * */
  264. static function getTypes() {
  265. $options['generic'] = __sso('Generic');
  266. $options['facebook'] = __sso('Facebook');
  267. $options['github'] = __sso('GitHub');
  268. $options['google'] = __sso('Google');
  269. $options['instagram'] = __sso('Instagram');
  270. $options['linkedin'] = __sso('LinkdeIn');
  271. return $options;
  272. }
  273. /**
  274. * Get ticket type Name
  275. *
  276. * @param $value type ID
  277. * */
  278. static function getTicketTypeName($value) {
  279. $tab = static::getTypes();
  280. // Return $value if not defined
  281. return (isset($tab[$value]) ? $tab[$value] : $value);
  282. }
  283. /**
  284. * Dropdown of ticket type
  285. *
  286. * @param $name select name
  287. * @param $options array of options:
  288. * - value : integer / preselected value (default 0)
  289. * - toadd : array / array of specific values to add at the begining
  290. * - on_change : string / value to transmit to "onChange"
  291. * - display : boolean / display or get string (default true)
  292. *
  293. * @return string id of the select
  294. * */
  295. static function dropdownType($name, $options = array()) {
  296. $params['value'] = 0;
  297. $params['toadd'] = array();
  298. $params['on_change'] = '';
  299. $params['display'] = true;
  300. if (is_array($options) && count($options)) {
  301. foreach ($options as $key => $val) {
  302. $params[$key] = $val;
  303. }
  304. }
  305. $items = array();
  306. if (count($params['toadd']) > 0) {
  307. $items = $params['toadd'];
  308. }
  309. $items += self::getTypes();
  310. return Dropdown::showFromArray($name, $items, $params);
  311. }
  312. /**
  313. * Get an history entry message
  314. *
  315. * @param $data Array from glpi_logs table
  316. *
  317. * @since GLPI version 0.84
  318. *
  319. * @return string
  320. * */
  321. static function getHistoryEntry($data) {
  322. switch ($data['linked_action'] - Log::HISTORY_PLUGIN) {
  323. case 0:
  324. return __('History from plugin example', 'example');
  325. }
  326. return '';
  327. }
  328. //////////////////////////////
  329. ////// SPECIFIC MODIF MASSIVE FUNCTIONS ///////
  330. /**
  331. * @since version 0.85
  332. *
  333. * @see CommonDBTM::getSpecificMassiveActions()
  334. * */
  335. function getSpecificMassiveActions($checkitem = null) {
  336. $actions = parent::getSpecificMassiveActions($checkitem);
  337. $actions['Document_Item' . MassiveAction::CLASS_ACTION_SEPARATOR . 'add'] = _x('button', 'Add a document'); // GLPI core one
  338. $actions[__CLASS__ . MassiveAction::CLASS_ACTION_SEPARATOR . 'do_nothing'] = __('Do Nothing - just for fun', 'example'); // Specific one
  339. return $actions;
  340. }
  341. /**
  342. * @since version 0.85
  343. *
  344. * @see CommonDBTM::showMassiveActionsSubForm()
  345. * */
  346. static function showMassiveActionsSubForm(MassiveAction $ma) {
  347. switch ($ma->getAction()) {
  348. case 'DoIt':
  349. echo "&nbsp;<input type='hidden' name='toto' value='1'>" .
  350. Html::submit(_x('button', 'Post'), array('name' => 'massiveaction')) .
  351. " " . __('Write in item history', 'example');
  352. return true;
  353. case 'do_nothing' :
  354. echo "&nbsp;" . Html::submit(_x('button', 'Post'), array('name' => 'massiveaction')) .
  355. " " . __('but do nothing :)', 'example');
  356. return true;
  357. }
  358. return parent::showMassiveActionsSubForm($ma);
  359. }
  360. /**
  361. * @since version 0.85
  362. *
  363. * @see CommonDBTM::processMassiveActionsForOneItemtype()
  364. * */
  365. static function processMassiveActionsForOneItemtype(MassiveAction $ma, CommonDBTM $item, array $ids) {
  366. global $DB;
  367. switch ($ma->getAction()) {
  368. case 'DoIt' :
  369. if ($item->getType() == 'Computer') {
  370. Session::addMessageAfterRedirect(__("Right it is the type I want...", 'example'));
  371. Session::addMessageAfterRedirect(__('Write in item history', 'example'));
  372. $changes = array(0, 'old value', 'new value');
  373. foreach ($ids as $id) {
  374. if ($item->getFromDB($id)) {
  375. Session::addMessageAfterRedirect("- " . $item->getField("name"));
  376. Log::history($id, 'Computer', $changes, 'PluginExampleExample', Log::HISTORY_PLUGIN);
  377. $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_OK);
  378. } else {
  379. // Example of ko count
  380. $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_KO);
  381. }
  382. }
  383. } else {
  384. // When nothing is possible ...
  385. $ma->itemDone($item->getType(), $ids, MassiveAction::ACTION_KO);
  386. }
  387. return;
  388. case 'do_nothing' :
  389. If ($item->getType() == 'PluginExampleExample') {
  390. Session::addMessageAfterRedirect(__("Right it is the type I want...", 'example'));
  391. Session::addMessageAfterRedirect(__("But... I say I will do nothing for:", 'example'));
  392. foreach ($ids as $id) {
  393. if ($item->getFromDB($id)) {
  394. Session::addMessageAfterRedirect("- " . $item->getField("name"));
  395. $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_OK);
  396. } else {
  397. // Example for noright / Maybe do it with can function is better
  398. $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_KO);
  399. }
  400. }
  401. } else {
  402. $ma->itemDone($item->getType(), $ids, MassiveAction::ACTION_KO);
  403. }
  404. Return;
  405. }
  406. parent::processMassiveActionsForOneItemtype($ma, $item, $ids);
  407. }
  408. /**
  409. *
  410. * @param string $type
  411. * @param array $options
  412. * @param array $collaborators
  413. * @return \League\OAuth2\Client\Provider\AbstractProvider
  414. */
  415. public static function createInstance($type = 'generic', array $options = array(), array $collaborators = array()) {
  416. if (!isset($options['scope'])) {
  417. $options['scope'] = array();
  418. }
  419. switch ($type) {
  420. case 'facebook':
  421. if (!isset($options['graphApiVersion'])) {
  422. $options['graphApiVersion'] = 'v2.12';
  423. }
  424. return new League\OAuth2\Client\Provider\Facebook($options, $collaborators);
  425. case 'github':
  426. $options['scope'][] = 'user:email';
  427. return new League\OAuth2\Client\Provider\Github($options, $collaborators);
  428. case 'google':
  429. return new League\OAuth2\Client\Provider\Google($options, $collaborators);
  430. case 'instagram':
  431. return new League\OAuth2\Client\Provider\Instagram($options, $collaborators);
  432. case 'linkedin':
  433. $options['scope'][] = 'r_emailaddress';
  434. return new League\OAuth2\Client\Provider\LinkedIn($options, $collaborators);
  435. case 'generic':
  436. default:
  437. return new League\OAuth2\Client\Provider\GenericProvider($options, $collaborators);
  438. }
  439. }
  440. private function getCurrentURL() {
  441. $currentURL = (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") ? "https://" : "http://";
  442. $currentURL .= $_SERVER["SERVER_NAME"];
  443. if ($_SERVER["SERVER_PORT"] != "80" && $_SERVER["SERVER_PORT"] != "443") {
  444. $currentURL .= ":" . $_SERVER["SERVER_PORT"];
  445. }
  446. $currentURL .= $_SERVER["REQUEST_URI"];
  447. return $currentURL;
  448. }
  449. public function prepareProviderInstance(array $options = array(), array $collaborators = array()) {
  450. global $CFG_GLPI;
  451. if ($this->_provider === null) {
  452. $redirect_uri = $this->getCurrentURL();
  453. $type = $this->fields['type'];
  454. $default = array(
  455. 'clientId' => $this->fields['client_id'],
  456. 'clientSecret' => $this->fields['client_secret'],
  457. 'redirectUri' => $redirect_uri,
  458. );
  459. if ($type === 'generic') {
  460. $default['urlAuthorize'] = $this->fields['url_authorize'];
  461. $default['urlAccessToken'] = $this->fields['url_accessToken'];
  462. $default['urlResourceOwnerDetails'] = $this->fields['url_resource_owner_details'];
  463. }
  464. if (!empty($this->fields['extra_options'])) {
  465. try {
  466. $extra = json_decode($this->fields['extra_options'], true);
  467. } catch (Exception $ex) {
  468. $extra = array();
  469. }
  470. if (!empty($extra)) {
  471. $default = array_merge($default, $extra);
  472. }
  473. }
  474. $options = array_merge($default, $options);
  475. $this->_provider = self::createInstance($type, $options, $collaborators);
  476. }
  477. return $this->_provider;
  478. }
  479. /**
  480. *
  481. * @return boolean|string
  482. */
  483. public function checkAuthorization() {
  484. if ($this->_provider === null) {
  485. return false;
  486. }
  487. if (!isset($_GET['code'])) {
  488. $scope = array();
  489. if (!empty($this->fields['scope'])) {
  490. $scope = explode(',', $this->fields['scope']);
  491. }
  492. $options = array(
  493. 'scope' => $scope,
  494. 'state' => Session::getNewCSRFToken(),
  495. );
  496. $this->_provider->authorize($options);
  497. }
  498. // Check given state against previously stored one to mitigate CSRF attack
  499. $state = isset($_GET['state']) ? $_GET['state'] : '';
  500. Session::checkCSRF(array(
  501. '_glpi_csrf_token' => $state,
  502. ));
  503. $this->_code = $_GET['code'];
  504. return $_GET['code'];
  505. }
  506. /**
  507. *
  508. * @return boolean|\League\OAuth2\Client\Token\AccessToken
  509. */
  510. public function getAccessToken() {
  511. if ($this->_token !== null) {
  512. return $this->_token;
  513. }
  514. if ($this->_provider === null || $this->_code === null) {
  515. return false;
  516. }
  517. $this->_token = $this->_provider->getAccessToken('authorization_code', array(
  518. 'code' => $this->_code
  519. ));
  520. return $this->_token;
  521. }
  522. /**
  523. *
  524. * @return boolean|\League\OAuth2\Client\Provider\ResourceOwnerInterface
  525. */
  526. public function getResourceOwner() {
  527. if ($this->_resource_owner !== null) {
  528. return $this->_resource_owner;
  529. }
  530. $token = $this->getAccessToken();
  531. if (!$token) {
  532. return false;
  533. }
  534. $this->_resource_owner = $this->_provider->getResourceOwner($token);
  535. return $this->_resource_owner;
  536. }
  537. public function findUser() {
  538. $resource = $this->getResourceOwner();
  539. $resource_array = $resource->toArray();
  540. $user = new User();
  541. //First: check linked user
  542. $email = false;
  543. $email_fields = array('email', 'e-mail', 'email-address');
  544. foreach ($email_fields as $field) {
  545. if (isset($resource_array[$field]) && is_string($resource_array[$field])) {
  546. $email = $resource_array[$field];
  547. break;
  548. }
  549. }
  550. $default_condition = '';
  551. if (version_compare(GLPI_VERSION, '9.3', '>=')) {
  552. $default_condition = array();
  553. }
  554. if ($email && $user->getFromDBbyEmail($email, $default_condition)) {
  555. return $user;
  556. }
  557. $login = false;
  558. $login_fields = array('login', 'username');
  559. foreach ($login_fields as $field) {
  560. if (isset($resource_array[$field]) && is_string($resource_array[$field])) {
  561. $login = $resource_array[$field];
  562. break;
  563. }
  564. }
  565. if ($login && $user->getFromDBbyName($login)) {
  566. return $user;
  567. }
  568. return false;
  569. }
  570. public function login() {
  571. $user = $this->findUser();
  572. if (!$user) {
  573. return false;
  574. }
  575. //Create fake auth
  576. $auth = new Auth();
  577. $auth->user = $user;
  578. $auth->auth_succeded = true;
  579. $auth->extauth = 1;
  580. $auth->user_present = $auth->user->getFromDBbyName(addslashes($user->fields['name']));
  581. $auth->user->fields['authtype'] = Auth::DB_GLPI;
  582. Session::init($auth);
  583. return $auth->auth_succeded;
  584. }
  585. }