BxCASModule.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. * Redirect to remote site login form
  19. *
  20. * @return n/a - redirect or HTML page in case of error
  21. */
  22. public function actionStart()
  23. {
  24. if (isLogged())
  25. $this->_redirect ($this -> _oConfig -> sDefaultRedirectUrl);
  26. if (!getParam('bx_cas_path_simplesamlphp')) {
  27. require_once(BX_DIRECTORY_PATH_INC . 'design.inc.php');
  28. bx_import('BxDolLanguages');
  29. $sCode = MsgBox( _t('_bx_cas_profile_error_config') );
  30. $this->_oTemplate->getPage(_t('_bx_cas'), $sCode);
  31. }
  32. else {
  33. require_once(getParam('bx_cas_path_simplesamlphp') . '/lib/_autoload.php');
  34. $as = new \SimpleSAML\Auth\Simple('default-sp');
  35. $as->requireAuth();
  36. // $as->login(['saml:idp' => 'https://url.to/saml/metadata.php']);
  37. $aAttributes = $as->getAttributes();
  38. // check if user logged in before
  39. $iLocalProfileId = $this->_oDb->getProfileId($aAttributes['salesforce_id'][0]);
  40. require_once(BX_DIRECTORY_PATH_INC . 'design.inc.php');
  41. if ($iLocalProfileId && $oProfile = BxDolProfile::getInstance($iLocalProfileId)) {
  42. // user already exists
  43. $this->setLogged($oProfile->id(), '', true, true); // remember user
  44. }
  45. else {
  46. // register new user
  47. $aAttributes['id'] = $this->_getAttrValue($aAttributes['salesforce_id']);
  48. $this->_createProfile($aAttributes);
  49. }
  50. }
  51. }
  52. /**
  53. * @param $aProfileInfo - remote profile info
  54. * @param $sAlternativeName - suffix to add to NickName to make it unique
  55. * @return profile array info, ready for the local database
  56. */
  57. protected function _convertRemoteFields($aProfileInfo, $sAlternativeName = '')
  58. {
  59. $aProfileFields = [];
  60. $aProfileFields['id'] = $this->_getAttrValue($aProfileInfo['some_id']); // TODO: change to actual value
  61. $aProfileFields['name'] = $this->_getAttrValue($aProfileInfo['some_name']); // TODO: change to actual value
  62. $aProfileFields['fullname'] = $this->_getAttrValue($aProfileInfo['first_name']) . ' ' . (isset($aProfileInfo['last_name']) ? $this->_getAttrValue($aProfileInfo['last_name']) : '');
  63. if (isset($aProfileInfo['first_name']))
  64. $aProfileFields['first_name'] = $this->_getAttrValue($aProfileInfo['first_name']);
  65. if (isset($aProfileInfo['last_name']))
  66. $aProfileFields['last_name'] = $this->_getAttrValue($aProfileInfo['last_name']);
  67. $aProfileFields['email'] = isset($aProfileInfo['email']) ? $this->_getAttrValue($aProfileInfo['email']) : '';
  68. $aProfileFields['allow_view_to'] = getParam('bx_cas_privacy');
  69. return $aProfileFields;
  70. }
  71. protected function _getAttrValue($a)
  72. {
  73. if (isset($a[0]))
  74. return $a[0];
  75. return false;
  76. }
  77. public function serviceAccountAddFormCheck()
  78. {
  79. if (!getParam('bx_cas_disable_join_form'))
  80. return '';
  81. return '<div></div>';
  82. }
  83. }
  84. /** @} */