user.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. <?php
  2. /**
  3. * The user entry point of ZenTaoPMS.
  4. *
  5. * @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.cnezsoft.com)
  6. * @license ZPL(http://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
  7. * @author Chunsheng Wang <chunsheng@cnezsoft.com>
  8. * @package entries
  9. * @version 1
  10. * @link https://www.zentao.net
  11. */
  12. class userEntry extends entry
  13. {
  14. /**
  15. * GET method.
  16. *
  17. * @param int|string $userID
  18. * @access public
  19. * @return string
  20. */
  21. public function get($userID = 0)
  22. {
  23. /* Get my info defaultly. */
  24. if(!$userID) return $this->getInfo($this->param('fields', ''));
  25. if(!is_numeric($userID))
  26. {
  27. $user = $this->loadModel('user')->getById($userID, 'account');
  28. if(!$user) return $this->send404();
  29. $userID = $user->id;
  30. }
  31. /* Get user by id. */
  32. $control = $this->loadController('user', 'profile');
  33. $control->profile($userID);
  34. $data = $this->getData();
  35. if(!$data) return $this->send404(); // If no user, send 404.
  36. $user = $data->data->user;
  37. unset($user->password);
  38. return $this->send(200, $user);
  39. }
  40. /**
  41. * Get my info.
  42. *
  43. * @param string $fields
  44. *
  45. * @access private
  46. * @return string
  47. */
  48. private function getInfo($fields = '')
  49. {
  50. $info = new stdclass();
  51. $profile = $this->loadModel('user')->getById($this->app->user->account);
  52. unset($profile->password);
  53. $info->profile = $this->format($profile, 'last:time,locked:time,birthday:date,join:date');
  54. $info->profile->role = array('code' => $info->profile->role, 'name' => $this->lang->user->roleList[$info->profile->role]);
  55. $info->profile->admin = strpos($this->app->company->admins, ",{$profile->account},") !== false;
  56. $info->profile->superReviewer = isset($this->config->story) ? strpos(',' . trim(zget($this->config->story, 'superReviewers', ''), ',') . ',', ',' . $this->app->user->account . ',') : false;
  57. $info->profile->view = $this->app->user->view;
  58. if(!$fields) return $this->send(200, $info);
  59. /* Set other fields. */
  60. $fields = explode(',', strtolower($fields));
  61. $this->loadModel('my');
  62. foreach($fields as $field)
  63. {
  64. switch($field)
  65. {
  66. case 'product':
  67. $info->product = array('total' => 0, 'products' => array());
  68. $products = $this->my->getProducts('ownbyme');
  69. if($products)
  70. {
  71. $info->product['total'] = $products->unclosedCount;
  72. $info->product['products'] = $products->products;
  73. }
  74. break;
  75. case 'undoneproduct':
  76. $info->undoneProduct = array('total' => 0, 'products' => array());
  77. $products = $this->my->getProducts('undone');
  78. if($products)
  79. {
  80. $info->undoneProduct['total'] = $products->allCount;
  81. $info->undoneProduct['products'] = $products->products;
  82. }
  83. break;
  84. case 'project':
  85. $info->project = array('total' => 0, 'projects' => array());
  86. $projects = $this->my->getDoingProjects();
  87. if($projects)
  88. {
  89. $info->project['total'] = $projects->doingCount;
  90. $info->project['projects'] = $projects->projects;
  91. }
  92. break;
  93. case 'lastproject':
  94. $info->lastProject = array('total' => 0, 'projects' => array());
  95. $control = $this->loadController('project', 'ajaxGetDropMenu');
  96. $control->ajaxGetDropMenu(0, 'project', 'index');
  97. $data = $this->getData();
  98. if($data->status == 'success')
  99. {
  100. $myProjects['owner'] = array();
  101. $myProjects['other'] = array();
  102. foreach($data->data->projects as $programID => $programProjects)
  103. {
  104. foreach($programProjects as $project)
  105. {
  106. if($project->status == 'closed') continue;
  107. $project = $this->filterFields($project, 'id,model,type,name,code,parent,status,PM');
  108. if($project->PM == $this->app->user->account)
  109. {
  110. $myProjects['owner'][] = $project;
  111. }
  112. else
  113. {
  114. $myProjects['other'][] = $project;
  115. }
  116. }
  117. }
  118. $lastProjects = array_merge($myProjects['owner'], $myProjects['other']);
  119. $lastProjects = array_slice($lastProjects, 0, 3);
  120. $info->lastProject['total'] = count($lastProjects);
  121. $info->lastProject['projects'] = $lastProjects;
  122. }
  123. break;
  124. case 'execution':
  125. $info->execution = array('total' => 0, 'executions' => array());
  126. if(!common::hasPriv('my', 'work')) break;
  127. $this->config->logonMethods[] = 'my.execution';
  128. $control = $this->loadController('my', 'execution');
  129. $control->execution($this->param('type', 'undone'), $this->param('order', 'id_desc'), $this->param('total', 0), $this->param('limit', 5), $this->param('page', 1));
  130. $data = $this->getData();
  131. if($data->status == 'success')
  132. {
  133. $info->execution['total'] = $data->data->pager->recTotal;
  134. $info->execution['executions'] = array_values((array)$data->data->executions);
  135. }
  136. break;
  137. case 'lastexecution':
  138. $info->lastExecution = array('total' => 0, 'executions' => array());
  139. $control = $this->loadController('execution', 'ajaxGetDropMenu');
  140. $control->ajaxGetDropMenu(0, 'execution', 'browse', '');
  141. $data = $this->getData();
  142. $account = $this->app->user->account;
  143. if($data->status == 'success')
  144. {
  145. $myExecutions['owner'] = array();
  146. $myExecutions['other'] = array();
  147. foreach($data->data->projectExecutions as $executionList)
  148. {
  149. foreach($executionList as $execution)
  150. {
  151. if($execution->status == 'done' or $execution->status == 'closed') continue;
  152. if($execution->PM == $account or isset($execution->teams->$account))
  153. {
  154. $myExecutions['owner'][] = $this->filterFields($execution, 'id,model,type,name,code,parent,status,PM');
  155. }
  156. else
  157. {
  158. $myExecutions['other'][] = $this->filterFields($execution, 'id,model,type,name,code,parent,status,PM');
  159. }
  160. }
  161. }
  162. $lastExecutions = array_merge($myExecutions['owner'], $myExecutions['other']);
  163. $lastExecutions = array_slice($lastExecutions, 0, 3);
  164. $info->lastExecution['total'] = count($lastExecutions);
  165. $info->lastExecution['executions'] = $lastExecutions;
  166. }
  167. break;
  168. case 'actions':
  169. $info->actions = $this->my->getActions();
  170. break;
  171. case 'task':
  172. $info->task = array('total' => 0, 'tasks' => array());
  173. if(!common::hasPriv('my', 'work')) break;
  174. $this->config->logonMethods[] = 'my.task';
  175. global $app;
  176. $app->rawMethod = 'work';
  177. $control = $this->loadController('my', 'task');
  178. $control->task($this->param('type', 'assignedTo'), 0, $this->param('order', 'id_desc'), $this->param('total', 0), $this->param('limit', 5), $this->param('page', 1));
  179. $data = $this->getData();
  180. if($data->status == 'success')
  181. {
  182. $info->task['total'] = $data->data->pager->recTotal;
  183. $info->task['tasks'] = array_values((array)$data->data->tasks);
  184. }
  185. break;
  186. case 'bug':
  187. $info->bug = array('total' => 0, 'bugs' => array());
  188. if(!common::hasPriv('my', 'work')) break;
  189. $this->config->logonMethods[] = 'my.bug';
  190. global $app;
  191. $app->rawMethod = 'work';
  192. $control = $this->loadController('my', 'bug');
  193. $control->bug($this->param('type', 'assignedTo'), 0, $this->param('order', 'id_desc'), $this->param('total', 0), $this->param('limit', 5), $this->param('page', 1));
  194. $data = $this->getData();
  195. if($data->status == 'success')
  196. {
  197. $bugs = array();
  198. foreach($data->data->bugs as $bug)
  199. {
  200. $status = array('code' => $bug->status, 'name' => $this->lang->bug->statusList[$bug->status]);
  201. if($bug->status == 'active' and $bug->confirmed) $status = array('code' => 'confirmed', 'name' => $this->lang->bug->labelConfirmed);
  202. if($bug->resolution == 'postponed') $status = array('code' => 'postponed', 'name' => $this->lang->bug->labelPostponed);
  203. if(!empty($bug->delay)) $status = array('code' => 'delay', 'name' => $this->lang->bug->overdueBugs);
  204. $bug->status = $status['code'];
  205. $bug->statusName = $status['name'];
  206. $bugs[$bug->id] = $bug;
  207. }
  208. $storyChangeds = $this->dao->select('t1.id')->from(TABLE_BUG)->alias('t1')
  209. ->leftJoin(TABLE_STORY)->alias('t2')->on('t1.story=t2.id')
  210. ->where('t1.id')->in(array_keys($bugs))
  211. ->andWhere('t1.story')->ne('0')
  212. ->andWhere('t1.storyVersion != t2.version')
  213. ->fetchPairs('id', 'id');
  214. foreach($storyChangeds as $bugID)
  215. {
  216. $status = array('code' => 'storyChanged', 'name' => $this->lang->bug->changed);
  217. $bugs[$bugID]->status = $status['code'];
  218. $bugs[$bugID]->statusName = $status['name'];
  219. }
  220. $info->bug['total'] = $data->data->pager->recTotal;
  221. $info->bug['bugs'] = array_values($bugs);
  222. }
  223. break;
  224. case 'todo':
  225. $info->todo = array('total' => 0, 'todos' => array());
  226. if(!common::hasPriv('my', 'todo')) break;
  227. $control = $this->loadController('my', 'todo');
  228. $control->todo($this->param('date', 'before'), '', 'all', 'date_desc', 0, $this->param('limit', 5), 1);
  229. $data = $this->getData();
  230. if($data->status == 'success')
  231. {
  232. $info->todo['total'] = $data->data->pager->recTotal;
  233. $info->todo['todos'] = array_values((array)$data->data->todos);
  234. }
  235. break;
  236. case 'story':
  237. $info->story = array('total' => 0, 'stories' => array());
  238. if(!common::hasPriv('my', 'work')) break;
  239. $this->config->logonMethods[] = 'my.story';
  240. global $app;
  241. $app->rawMethod = 'work';
  242. $control = $this->loadController('my', 'story');
  243. $control->story($this->param('type', 'assignedTo'), 0, $this->param('order', 'id_desc'), $this->param('total', 0), $this->param('limit', 5), $this->param('page', 1));
  244. $data = $this->getData();
  245. if($data->status == 'success')
  246. {
  247. $stories = array();
  248. foreach($data->data->stories as $story)
  249. {
  250. $story->statusName = $this->lang->story->statusList[$story->status];
  251. $stories[$story->id] = $story;
  252. }
  253. $info->story['total'] = $data->data->pager->recTotal;
  254. $info->story['stories'] = array_values($stories);
  255. }
  256. break;
  257. case 'issue':
  258. $info->issue = array('total' => 0, 'issues' => array());
  259. if(!common::hasPriv('my', 'work')) break;
  260. if(in_array($this->config->edition, array('max', 'ipd')))
  261. {
  262. global $app;
  263. $app->rawMethod = 'work';
  264. $this->config->logonMethods[] = 'my.issue';
  265. $control = $this->loadController('my', 'issue');
  266. $control->issue('createdBy', 0, 'id_desc', 0, $this->param('limit', 5), 1);
  267. $data = $this->getData();
  268. if($data->status == 'success')
  269. {
  270. $info->issue['total'] = $data->data->pager->recTotal;
  271. $info->issue['issues'] = array_values((array)$data->data->issues);
  272. }
  273. }
  274. break;
  275. case 'risk':
  276. $info->risk = array('total' => 0, 'risks' => array());
  277. if(!common::hasPriv('my', 'work')) break;
  278. if(in_array($this->config->edition, array('max', 'ipd')))
  279. {
  280. global $app;
  281. $app->rawMethod = 'work';
  282. $this->config->logonMethods[] = 'my.risk';
  283. $control = $this->loadController('my', 'risk');
  284. $control->risk('assignedTo', 0, 'id_desc', 0, $this->param('limit', 5), 1);
  285. $data = $this->getData();
  286. if($data->status == 'success')
  287. {
  288. $info->risk['total'] = $data->data->pager->recTotal;
  289. $info->risk['risks'] = array_values((array)$data->data->risks);
  290. }
  291. }
  292. break;
  293. case 'meeting':
  294. $info->meeting = array('total' => 0, 'meetings' => array());
  295. if(!common::hasPriv('my', 'work')) break;
  296. if(in_array($this->config->edition, array('max', 'ipd')))
  297. {
  298. global $app;
  299. $app->rawMethod = 'work';
  300. $this->config->logonMethods[] = 'my.myMeeting';
  301. $control = $this->loadController('my', 'myMeeting');
  302. $control->myMeeting('futureMeeting', 0, 'id_desc', 0, $this->param('limit', 5), 1);
  303. $data = $this->getData();
  304. if($data->status == 'success')
  305. {
  306. $info->meeting['total'] = $data->data->pager->recTotal;
  307. $info->meeting['meetings'] = array_values((array)$data->data->meetings);
  308. }
  309. }
  310. break;
  311. case 'overview':
  312. $info->overview = $this->my->getOverview();
  313. break;
  314. case 'contribute':
  315. $info->contribute = $this->my->getContribute();
  316. break;
  317. case 'rights':
  318. $inAdminGroup = $this->dao->select('t1.*')->from(TABLE_USERGROUP)->alias('t1')
  319. ->leftJoin(TABLE_GROUP)->alias('t2')->on('t1.group=t2.id')
  320. ->where('t1.account')->eq($info->profile->account)
  321. ->andWhere('t2.role')->eq('admin')
  322. ->fetch();
  323. $info->rights = array();
  324. $info->rights['admin'] = (!empty($inAdminGroup) or $this->app->user->admin);
  325. $info->rights['rights'] = $this->app->user->rights['rights'];
  326. if(isset($info->rights['rights']['task']['recordworkhour'])) $info->rights['rights']['task']['recordestimate'] = 1;
  327. }
  328. }
  329. return $this->send(200, $info);
  330. }
  331. /**
  332. * PUT method.
  333. *
  334. * @param int|string $userID
  335. * @access public
  336. * @return string
  337. */
  338. public function put($userID)
  339. {
  340. $control = $this->loadController('user', 'edit');
  341. if(!is_numeric($userID))
  342. {
  343. $user = $this->loadModel('user')->getById($userID, 'account');
  344. if(!$user) return $this->send404();
  345. $userID = $user->id;
  346. }
  347. $oldUser = $this->loadModel('user')->getByID($userID, 'id');
  348. /* Set $_POST variables. */
  349. $fields = 'dept,realname,role,join,type,visions,mobile,phone,qq,dingding,weixin,skype,whatsapp,slack,address,address,email,commiter,gender,`groups`,passwordStrength,locked,fails,birthday';
  350. $this->batchSetPost($fields, $oldUser);
  351. $this->setPost('account', $oldUser->account);
  352. $userGroups = $this->dao->select('`group`')->from(TABLE_USERGROUP)->where('account')->eq($oldUser->account)->fetchPairs('group', 'group');
  353. $this->setPost('group', $this->request('group', zget($_POST, 'group', array_values($userGroups))));
  354. $gender = $this->request('gender', zget($_POST, 'gender', 'f'));
  355. if(!in_array($gender, array('f', 'm'))) return $this->sendError(400, "The value of gender must be 'f' or 'm'");
  356. if($this->request('gender') and !in_array($this->request('gender'), array('f', 'm'))) return $this->sendError(400, "The value of gendar must be 'f' or 'm'");
  357. $this->setPost('gender', $gender);
  358. $password = $this->request('password', zget($_POST, 'password', ''));
  359. $setPassword = $password ? md5($password) . $this->app->session->rand : '';
  360. $this->setPost('password1', $setPassword);
  361. $this->setPost('password2', $setPassword);
  362. $this->setPost('passwordStrength', 3);
  363. $this->setPost('passwordLength', strlen($setPassword));
  364. $this->setPost('verifyPassword', md5($this->app->user->password . $this->app->session->rand));
  365. $control->edit($userID);
  366. $data = $this->getData();
  367. if(isset($data->result) and $data->result == 'fail') return $this->sendError(zget($data, 'code', 400), $data->message);
  368. $user = $this->user->getByID($userID, 'id');
  369. unset($user->password);
  370. return $this->send(200, $this->format($user, 'last:time,locked:time'));
  371. }
  372. /**
  373. * DELETE method.
  374. *
  375. * @param int|string $userID
  376. * @access public
  377. * @return string
  378. */
  379. public function delete($userID)
  380. {
  381. if(!is_numeric($userID))
  382. {
  383. $user = $this->loadModel('user')->getById($userID, 'account');
  384. if(!$user) return $this->send404();
  385. $userID = $user->id;
  386. }
  387. $this->setPost('verifyPassword', md5($this->app->user->password . $this->app->session->rand));
  388. $control = $this->loadController('user', 'delete');
  389. $control->delete($userID);
  390. $this->getData();
  391. return $this->sendSuccess(200, 'success');
  392. }
  393. }