BxOAuthAPI.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 OAuth2 OAuth2 server
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. use OAuth2\Response;
  12. class BxOAuthAPI extends BxDol
  13. {
  14. protected $_oModule;
  15. protected $_oDb;
  16. public $aAction2Scope = array (
  17. 'me' => 'basic,market,service',
  18. 'user' => 'basic,market,service',
  19. 'friends' => 'basic,market,service',
  20. 'service' => 'service',
  21. 'market' => 'market',
  22. );
  23. function __construct($oModule)
  24. {
  25. $this->_oModule = $oModule;
  26. $this->_oDb = $oModule->_oDb;
  27. }
  28. /**
  29. * @page API
  30. * @section api_me /m/oauth2/api/me
  31. *
  32. * Provides information about current profile.
  33. * Can be used in 'basic', 'service' and 'market' grant types.
  34. *
  35. * **HTTP Method:**
  36. * `GET`
  37. *
  38. * **Request header:**
  39. * @code
  40. * Authorization: Bearer 9802c4a34e1535d8c3b721604ee0e7fb04116c49
  41. * @endcode
  42. *
  43. * **Response (success):**
  44. * @code
  45. * {
  46. * "id":"123",
  47. * "type":"bx_organizations",
  48. * "email":"test@example.com",
  49. * "role":"1",
  50. * "name":"Test",
  51. * "profile_display_name":"Test",
  52. * "profile_link":"http:\/\/example.com\/path-to-una\/page\/view-organization-profile?id=12",
  53. * "picture":"http:\/\/example.com\/path-to-una\/image_transcoder.php?o=bx_organizations_picture&h=36&dpx=1&t=1496247306"
  54. * }
  55. * @endcode
  56. *
  57. * **Response (error):**
  58. * @code
  59. * {
  60. * "error":"short error description here",
  61. * "error_description":"long error description here"
  62. * }
  63. * @endcode
  64. *
  65. */
  66. function me($aToken)
  67. {
  68. if (!($oProfile = BxDolProfile::getInstance($aToken['user_id']))) {
  69. $this->errorOutput('404', 'not_found', 'Profile was not found');
  70. return;
  71. }
  72. $this->output($this->_prepareProfileArray($oProfile, false));
  73. }
  74. /**
  75. * @page API
  76. * @section api_user /m/oauth2/api/user
  77. *
  78. * Provides information about particular profile profile.
  79. *
  80. * **Parameters:**
  81. * - `id` - profile ID
  82. *
  83. * Everything else is equivalent to @ref api_me
  84. */
  85. function user($aToken)
  86. {
  87. $iProfileId = (int)bx_get('id');
  88. if ($iProfileId == $aToken['user_id']) {
  89. $this->me($aToken);
  90. return;
  91. }
  92. if (!($oProfile = $this->_getProfileWithAccessChecking($iProfileId)))
  93. return;
  94. $this->output($this->_prepareProfileArray($oProfile, !isAdmin($aToken['user_id'])));
  95. }
  96. function friends($aToken)
  97. {
  98. $iProfileId = (int)bx_get('id');
  99. if (!($oProfile = $this->_getProfileWithAccessChecking($iProfileId)))
  100. return;
  101. $this->output(array(
  102. 'user_id' => $iProfileId,
  103. 'friends' => getMyFriendsEx($iProfileId),
  104. ));
  105. }
  106. function market($aToken)
  107. {
  108. $_GET['key'] = $_POST['key'] = $aToken['client_id'];
  109. $_GET['module'] = $_POST['module'] = 'bx_market_api';
  110. $this->service($aToken);
  111. }
  112. /**
  113. * Service call should look like this
  114. * http://example.com/m/oauth2/api/service?module=bx_market&method=test&params[]=1&params[]=abc&class=custom_class_name_or_remove_it_if_module_class
  115. * or
  116. * http://example.com/m/oauth2/api/service?module=bx_market&method=test&params=serialized_string_of_params
  117. */
  118. function service($aToken)
  119. {
  120. bx_login($aToken['user_id'], false, false);
  121. $sModule = bx_get('module');
  122. $sMethod = bx_get('method');
  123. if (!($aParams = bx_get('params')))
  124. $aParams = array();
  125. elseif (is_string($aParams) && preg_match('/^a:[\d+]:\{/', $aParams))
  126. $aParams = @unserialize($aParams);
  127. if (!is_array($aParams))
  128. $aParams = array($aParams);
  129. if (!($sClass = bx_get('class')))
  130. $sClass = 'Module';
  131. if (!BxDolRequest::serviceExists($sModule, $sMethod, $sClass)) {
  132. $this->errorOutput(404, 'not_found', 'Service was not found');
  133. return false;
  134. }
  135. $mixedRet = BxDolService::call($sModule, $sMethod, $aParams, $sClass);
  136. $this->output(array(
  137. 'module' => $sModule,
  138. 'method' => $sMethod,
  139. 'data' => $mixedRet,
  140. ));
  141. }
  142. function errorOutput($iHttpCode, $sError, $sErrorDesc)
  143. {
  144. $oReponse = new Response();
  145. $oReponse->setError($iHttpCode, $sError, $sErrorDesc);
  146. $oReponse->send();
  147. }
  148. function output($a)
  149. {
  150. $oReponse = new Response();
  151. $oReponse->setParameters($a);
  152. $oReponse->send();
  153. }
  154. protected function _getProfileWithAccessChecking ($iProfileId)
  155. {
  156. if (!$oProfile = BxDolProfile::getInstance($iProfileId)) {
  157. $this->errorOutput('404', 'not_found', 'Profile was not found');
  158. return false;
  159. }
  160. /*
  161. // TODO: check visibility
  162. if (!bx_check_profile_visibility($iProfileId, $aToken['user_id'], true)) {
  163. $this->errorOutput(403, 'access_denied', 'You have no rights to view this user info');
  164. return false;
  165. }
  166. */
  167. return $oProfile;
  168. }
  169. protected function _prepareProfileArray ($oProfile, $bPublicFieldsOnly = true)
  170. {
  171. $oAccount = $oProfile->getAccountObject();
  172. $aProfileInfo = $oProfile->getInfo();
  173. $aAccountInfo = $oAccount->getInfo();
  174. if ($bPublicFieldsOnly) {
  175. $aProfileInfo = array(
  176. 'id' => $aProfileInfo['id'],
  177. );
  178. }
  179. else {
  180. unset($aProfileInfo['account_id']);
  181. unset($aProfileInfo['content_id']);
  182. unset($aProfileInfo['status']);
  183. $aProfileInfo['email'] = $oAccount->getEmail();
  184. // TODO: fetch extended info from profile module
  185. }
  186. $aProfileInfo['role'] = $aAccountInfo['role'];
  187. $aProfileInfo['name'] = $oAccount->getDisplayName();
  188. $aProfileInfo['profile_display_name'] = $oProfile->getDisplayName();
  189. $aProfileInfo['profile_link'] = $oProfile->getUrl();
  190. $aProfileInfo['picture'] = $oProfile->getPicture();
  191. return $aProfileInfo;
  192. }
  193. }
  194. /** @} */