BxCASModule.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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('dev-sp');
  31. $as->logout(BX_DOL_URL_ROOT);
  32. \SimpleSAML\Session::getSessionFromRequest()->cleanup();
  33. }
  34. /**
  35. * Single Logout Service in SAML logout chain,
  36. * this URL ned to be specified in ResponceLocation for SingleLogoutService in SAML metadata.
  37. * Other words: logout in UNA only
  38. */
  39. public function actionLogout()
  40. {
  41. if (!getParam('bx_cas_path_simplesamlphp')) {
  42. echoJson(['code' => 500, 'message' => "SimpleSAML folder isn't configred."]);
  43. exit;
  44. }
  45. else {
  46. if (!isLogged()) {
  47. echo "You aren't logged in.";
  48. exit;
  49. }
  50. // cleanup UNA sessioon
  51. bx_logout(false /* disable logout hook, so serviceLogout isn't called at all */);
  52. // cleanup SAML related staff locally only
  53. require_once(getParam('bx_cas_path_simplesamlphp') . '/src/_autoload.php');
  54. \SimpleSAML\Session::getSessionFromRequest()->cleanup();
  55. echo 'OK';
  56. exit;
  57. }
  58. }
  59. /**
  60. * Redirect to remote site login form
  61. *
  62. * @return n/a - redirect or HTML page in case of error
  63. */
  64. public function actionStart()
  65. {
  66. if (isLogged())
  67. $this->_redirect ($this -> _oConfig -> sDefaultRedirectUrl);
  68. if (!getParam('bx_cas_path_simplesamlphp')) {
  69. require_once(BX_DIRECTORY_PATH_INC . 'design.inc.php');
  70. bx_import('BxDolLanguages');
  71. $sCode = MsgBox( _t('_bx_cas_profile_error_config') );
  72. $this->_oTemplate->getPage(_t('_bx_cas'), $sCode);
  73. }
  74. else {
  75. // add referrer to the session to redirect after login
  76. if(isset($_SERVER['HTTP_REFERER']) && mb_stripos($_SERVER['HTTP_REFERER'], BX_DOL_URL_ROOT) === 0)
  77. BxDolSession::getInstance()->setValue($this->_oConfig->getName() . '_relocate', $_SERVER['HTTP_REFERER']);
  78. require_once(getParam('bx_cas_path_simplesamlphp') . '/lib/_autoload.php');
  79. $as = new \SimpleSAML\Auth\Simple('default-sp');
  80. $as->requireAuth();
  81. // $as->login(['saml:idp' => 'https://url.to/saml/metadata.php']);
  82. $aAttributes = $as->getAttributes();
  83. // check if user logged in before
  84. $iLocalProfileId = $this->_oDb->getProfileId($aAttributes['salesforce_id'][0]);
  85. require_once(BX_DIRECTORY_PATH_INC . 'design.inc.php');
  86. if ($iLocalProfileId && $oProfile = BxDolProfile::getInstance($iLocalProfileId)) {
  87. // user already exists
  88. $this->setLogged($oProfile->id(), '', true, true); // remember user
  89. }
  90. else {
  91. // register new user
  92. $aAttributes['id'] = $this->_getAttrValue($aAttributes['salesforce_id']);
  93. $this->_createProfile($aAttributes);
  94. }
  95. }
  96. }
  97. /**
  98. * @param $aProfileInfo - remote profile info
  99. * @param $sAlternativeName - suffix to add to NickName to make it unique
  100. * @return profile array info, ready for the local database
  101. */
  102. protected function _convertRemoteFields($aProfileInfo, $sAlternativeName = '')
  103. {
  104. $aProfileFields = [];
  105. $aProfileFields['id'] = $this->_getAttrValue($aProfileInfo['some_id']); // TODO: change to actual value
  106. $aProfileFields['name'] = $this->_getAttrValue($aProfileInfo['some_name']); // TODO: change to actual value
  107. $aProfileFields['fullname'] = $this->_getAttrValue($aProfileInfo['first_name']) . ' ' . (isset($aProfileInfo['last_name']) ? $this->_getAttrValue($aProfileInfo['last_name']) : '');
  108. if (isset($aProfileInfo['first_name']))
  109. $aProfileFields['first_name'] = $this->_getAttrValue($aProfileInfo['first_name']);
  110. if (isset($aProfileInfo['last_name']))
  111. $aProfileFields['last_name'] = $this->_getAttrValue($aProfileInfo['last_name']);
  112. $aProfileFields['email'] = isset($aProfileInfo['email']) ? $this->_getAttrValue($aProfileInfo['email']) : '';
  113. $aProfileFields['allow_view_to'] = getParam('bx_cas_privacy');
  114. return $aProfileFields;
  115. }
  116. protected function _getAttrValue($a)
  117. {
  118. if (isset($a[0]))
  119. return $a[0];
  120. return false;
  121. }
  122. public function serviceAccountAddFormCheck()
  123. {
  124. if (!getParam('bx_cas_disable_join_form'))
  125. return '';
  126. return '<div></div>';
  127. }
  128. }
  129. /** @} */