1
0

BxCASModule.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup CASConnect CAS Connect
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxCASModule extends BxBaseModConnectModule
  12. {
  13. function __construct(&$aModule)
  14. {
  15. parent::__construct($aModule);
  16. }
  17. /**
  18. * This service need to be called when we call logout from UNA,
  19. * so it will propagate logout to IDP
  20. * Other words: logout in UNA, IDP and all other SP
  21. */
  22. public function serviceLogout()
  23. {
  24. if (!getParam('bx_cas_path_simplesamlphp'))
  25. return;
  26. if (!isLogged())
  27. return;
  28. bx_logout(false /* disable logout hook, so serviceLogout isn't called in a loop*/);
  29. require_once(getParam('bx_cas_path_simplesamlphp') . '/src/_autoload.php');
  30. $as = new \SimpleSAML\Auth\Simple('default-sp');
  31. $as->logout(BX_DOL_URL_ROOT);
  32. \SimpleSAML\Session::getSessionFromRequest()->cleanup();
  33. }
  34. /**
  35. * Redirect to remote site login form
  36. *
  37. * @return n/a - redirect or HTML page in case of error
  38. */
  39. public function actionStart()
  40. {
  41. if (isLogged())
  42. $this->_redirect ($this -> _oConfig -> sDefaultRedirectUrl);
  43. if (!getParam('bx_cas_path_simplesamlphp')) {
  44. require_once(BX_DIRECTORY_PATH_INC . 'design.inc.php');
  45. bx_import('BxDolLanguages');
  46. $sCode = MsgBox( _t('_bx_cas_profile_error_config') );
  47. $this->_oTemplate->getPage(_t('_bx_cas'), $sCode);
  48. }
  49. else {
  50. // add referrer to the session to redirect after login
  51. if(isset($_SERVER['HTTP_REFERER']) && mb_stripos($_SERVER['HTTP_REFERER'], BX_DOL_URL_ROOT) === 0)
  52. BxDolSession::getInstance()->setValue($this->_oConfig->getName() . '_relocate', $_SERVER['HTTP_REFERER']);
  53. require_once(getParam('bx_cas_path_simplesamlphp') . '/src/_autoload.php');
  54. require_once(getParam('bx_cas_path_simplesamlphp') . '/una_helpers/session_cleanup_class.php');
  55. // session_cleanup_class.php must be included twice:
  56. // - UNA: in BxCASModule.php (here)
  57. // - SimpleSAMLPHP: in config/config.php
  58. //
  59. // SAMPLE session_cleanup_class.php:
  60. // ```
  61. // if (!defined('BX_DOL_VERSION'))
  62. // require_once('/var/www/staging.novaliscircle.org/current/html/inc/header.inc.php');
  63. //
  64. // class BxCasLogoutCallback
  65. // {
  66. // public static function run()
  67. // {
  68. // bx_logout(false);
  69. // }
  70. // }
  71. // ```
  72. $as = new \SimpleSAML\Auth\Simple('default-sp');
  73. $as->requireAuth();
  74. \SimpleSAML\Session::getSessionFromRequest()->registerLogoutHandler('default-sp', 'BxCasLogoutCallback', 'run');
  75. $aAttributes = $as->getAttributes();
  76. // check if user logged in before
  77. $iLocalProfileId = $this->_oDb->getProfileId($aAttributes['salesforce_id'][0]);
  78. require_once(BX_DIRECTORY_PATH_INC . 'design.inc.php');
  79. if ($iLocalProfileId && $oProfile = BxDolProfile::getInstance($iLocalProfileId)) {
  80. // user already exists
  81. $this->setLogged($oProfile->id(), '', true, true);
  82. }
  83. else {
  84. // register new user
  85. $aAttributes['id'] = $this->_getAttrValue($aAttributes['salesforce_id']);
  86. $this->_createProfile($aAttributes);
  87. }
  88. }
  89. }
  90. /**
  91. * @param $aProfileInfo - remote profile info
  92. * @param $sAlternativeName - suffix to add to NickName to make it unique
  93. * @return profile array info, ready for the local database
  94. */
  95. protected function _convertRemoteFields($aProfileInfo, $sAlternativeName = '')
  96. {
  97. $aProfileFields = [];
  98. $aProfileFields['id'] = $this->_getAttrValue($aProfileInfo['some_id']); // TODO: change to actual value
  99. $aProfileFields['name'] = $this->_getAttrValue($aProfileInfo['some_name']); // TODO: change to actual value
  100. $aProfileFields['fullname'] = $this->_getAttrValue($aProfileInfo['first_name']) . ' ' . (isset($aProfileInfo['last_name']) ? $this->_getAttrValue($aProfileInfo['last_name']) : '');
  101. if (isset($aProfileInfo['first_name']))
  102. $aProfileFields['first_name'] = $this->_getAttrValue($aProfileInfo['first_name']);
  103. if (isset($aProfileInfo['last_name']))
  104. $aProfileFields['last_name'] = $this->_getAttrValue($aProfileInfo['last_name']);
  105. $aProfileFields['email'] = isset($aProfileInfo['email']) ? $this->_getAttrValue($aProfileInfo['email']) : '';
  106. $aProfileFields['allow_view_to'] = getParam('bx_cas_privacy');
  107. return $aProfileFields;
  108. }
  109. protected function _getAttrValue($a)
  110. {
  111. if (isset($a[0]))
  112. return $a[0];
  113. return false;
  114. }
  115. public function serviceAccountAddFormCheck()
  116. {
  117. if (!getParam('bx_cas_disable_join_form'))
  118. return '';
  119. return '<div></div>';
  120. }
  121. }
  122. /** @} */