users.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * The users 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 usersEntry extends entry
  13. {
  14. /**
  15. * GET method.
  16. *
  17. * @access public
  18. * @return string
  19. */
  20. public function get()
  21. {
  22. $full = (int)$this->param('full', 0);
  23. if($full == 1 && !common::hasPriv('company', 'browse')) return $this->sendError(400, 'error: no company-browse priv.');
  24. $appendFields = $this->param('fileds', '');
  25. $type = $this->param('type', 'bydept');
  26. $limit = (int)$this->param('limit', 20);
  27. $pager = null;
  28. if($limit)
  29. {
  30. $this->app->loadClass('pager', $static = true);
  31. $pager = pager::init(0, $limit, $this->param('page', 1));
  32. }
  33. $users = $this->loadModel('company')->getUsers($this->param('browse', 'inside'), $type, 0, 0, $this->param('order', 'id_desc'), $pager);
  34. $result = array();
  35. foreach($users as $user)
  36. {
  37. if($full == 1)
  38. {
  39. $user = $this->filterFields($user, 'id,dept,account,realname,role,pinyin,email,' . $appendFields);
  40. $result[] = $this->format($user, 'locked:time');
  41. }
  42. else
  43. {
  44. $result[] = array('account' => $user->account, 'realname' => $user->realname);
  45. }
  46. }
  47. $pageID = $pager ? $pager->pageID : 1;
  48. $total = $pager ? $pager->recTotal : count($result);
  49. $limit = $pager ? $pager->recPerPage : $total;
  50. return $this->send(200, array('page' => $pageID, 'total' => $total, 'limit' => $limit, 'users' => $result));
  51. }
  52. /**
  53. * POST method.
  54. *
  55. * @access public
  56. * @return string
  57. */
  58. public function post()
  59. {
  60. $control = $this->loadController('user', 'create');
  61. $fields = 'type,dept,account,password,visions,realname,join,role,email,commiter,gender,group,passwordStrength';
  62. $this->batchSetPost($fields);
  63. if(!in_array($this->request('gender', zget($_POST, 'gender', 'f')), array('f', 'm'))) return $this->sendError(400, "The value of gender must be 'f' or 'm'");
  64. $password = $this->request('password', zget($_POST, 'password', '')) ? md5($this->request('password', zget($_POST, 'password', ''))) : '';
  65. $visions = $this->request('visions', array('rnd'));
  66. if(!is_array($visions)) $visions = explode(',', $visions);
  67. if($this->request('group')) $this->setPost('group', explode(',', $this->request('group')));
  68. $this->setPost('password1', $password . $this->app->session->rand);
  69. $this->setPost('password2', $password . $this->app->session->rand);
  70. $this->setPost('passwordStrength', 3);
  71. $this->setPost('visions', $visions);
  72. $this->setPost('verifyPassword', md5($this->app->user->password . $this->app->session->rand));
  73. $this->setPost('passwordLength', strlen($_POST['password1']));
  74. unset($_POST['password']);
  75. $this->requireFields('account,gender,password1,realname');
  76. $control->create();
  77. $data = $this->getData();
  78. if(isset($data->status) and $data->status == 'fail') return $this->sendError(zget($data, 'code', 400), $data->message);
  79. if(isset($data->result) and !isset($data->id)) return $this->sendError(400, $data->message);
  80. $user = $this->loadModel('user')->getByID($data->id, 'id');
  81. unset($user->password);
  82. return $this->send(201, $this->format($user, 'last:time,locked:time'));
  83. }
  84. }