zen.php 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * The zen file of gogs module of ZenTaoPMS.
  4. *
  5. * @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.zentao.net)
  6. * @license ZPL(https://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
  7. * @author Yanyi Cao <caoyanyi@easycorp.ltd>
  8. * @package gogs
  9. * @link https://www.zentao.net
  10. */
  11. class gogsZen extends gogs
  12. {
  13. /**
  14. * 检查gogs服务器的token是否有效。
  15. * Check post token has admin permissions.
  16. *
  17. * @param object $gogs
  18. * @access protected
  19. * @return array|bool
  20. */
  21. protected function checkToken($gogs)
  22. {
  23. $this->dao->update('gogs')->data($gogs)->batchCheck($this->config->gogs->create->requiredFields, 'notempty');
  24. if(dao::isError()) return array('result' => 'fail', 'message' => dao::getError());
  25. $result = $this->gogs->checkTokenAccess($gogs->url, $gogs->token);
  26. if($result === false) return array('result' => 'fail', 'message' => array('url' => array($this->lang->gogs->hostError)));
  27. if(!$result) return array('result' => 'fail', 'message' => array('token' => array($this->lang->gogs->tokenLimit)));
  28. return true;
  29. }
  30. /**
  31. * 根据账号,名字,邮箱获取gogs用户匹配的禅道用户。
  32. * Get matched zentao users by account, name, email.
  33. *
  34. * @param int $gogsID
  35. * @param array $gogsUsers
  36. * @access protected
  37. * @return array
  38. */
  39. protected function getMatchedUsers($gogsID, $gogsUsers)
  40. {
  41. $userList = $this->loadModel('user')->getList('all', 'account,realname,email');
  42. $zentaoUsers = array();
  43. foreach($userList as $user) $zentaoUsers[$user->account] = $user;
  44. $matches = new stdclass();
  45. foreach($gogsUsers as $gogsUser)
  46. {
  47. foreach($zentaoUsers as $zentaoUser)
  48. {
  49. if($gogsUser->account == $zentaoUser->account) $matches->accounts[$gogsUser->account][] = $zentaoUser->account;
  50. if($gogsUser->realname == $zentaoUser->realname) $matches->names[$gogsUser->realname][] = $zentaoUser->account;
  51. if($gogsUser->email == $zentaoUser->email) $matches->emails[$gogsUser->email][] = $zentaoUser->account;
  52. }
  53. }
  54. $bindedUsers = $this->loadModel('pipeline')->getUserBindedPairs($gogsID, 'gogs', 'openID,account');
  55. $matchedUsers = array();
  56. foreach($gogsUsers as $gogsUser)
  57. {
  58. if(isset($bindedUsers[$gogsUser->account]))
  59. {
  60. $gogsUser->zentaoAccount = $bindedUsers[$gogsUser->account];
  61. $matchedUsers[$gogsUser->id] = $gogsUser;
  62. continue;
  63. }
  64. $matchedZentaoUsers = array();
  65. if(isset($matches->accounts[$gogsUser->account])) $matchedZentaoUsers = array_merge($matchedZentaoUsers, $matches->accounts[$gogsUser->account]);
  66. if(isset($matches->emails[$gogsUser->email])) $matchedZentaoUsers = array_merge($matchedZentaoUsers, $matches->emails[$gogsUser->email]);
  67. if(isset($matches->names[$gogsUser->realname])) $matchedZentaoUsers = array_merge($matchedZentaoUsers, $matches->names[$gogsUser->realname]);
  68. $matchedZentaoUsers = array_unique($matchedZentaoUsers);
  69. if(count($matchedZentaoUsers) == 1)
  70. {
  71. $gogsUser->zentaoAccount = current($matchedZentaoUsers);
  72. $matchedUsers[$gogsUser->id] = $gogsUser;
  73. }
  74. }
  75. return $matchedUsers;
  76. }
  77. }