BackUser.class.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace Cas\Dao;
  3. use KIF\Dao\DBAgileDev;
  4. use KIF\Data\ResultWrapper;
  5. use KIF\Math\Math;
  6. use KIF\Verify;
  7. /**
  8. *
  9. * 后台用户
  10. * @author lishumingoo@gmail.com
  11. */
  12. class BackUser extends DBAgileDev {
  13. protected $tableName = 'back_user';
  14. protected $primaryKey = 'uid';
  15. /**
  16. * 数据库里的真实字段
  17. * @var array
  18. */
  19. protected $other_field = array(
  20. 'name',
  21. 'password',
  22. 'email',
  23. 'project',
  24. 'permission',
  25. );
  26. public function __construct($master_flag = 'backend') {
  27. parent::__construct($master_flag);
  28. }
  29. public function add(array $info) {
  30. $name = $info['name'];
  31. if (empty($name)) {
  32. return ResultWrapper::fail('name字段不能为空');
  33. }
  34. $name = mb_strtolower($name, 'UTF-8');
  35. # 这是数据表存储上的逻辑
  36. if (mb_strlen($name) > 40) {
  37. return ResultWrapper::fail('name字段超出了40个字');
  38. }
  39. $password = $info['password'];
  40. if (empty($password)) {
  41. return ResultWrapper::fail('password字段不能为空');
  42. }
  43. $password = Math::md5_16($password);
  44. $email = $info['email'];
  45. // if (empty($email)) {
  46. // return ResultWrapper::fail('email字段不能为空');
  47. // }
  48. # 这是数据表存储上的逻辑
  49. if (mb_strlen($email) > 50) {
  50. return ResultWrapper::fail('email字段超出了50个字');
  51. }
  52. if (isset($info['register_time']) && Verify::int($info['register_time'])) {
  53. $register_time = $info['register_time'];
  54. } else {
  55. $register_time = time();
  56. }
  57. $tableInfo = array_merge($info, array(
  58. 'name' => $name,
  59. 'password' => $password,
  60. 'email' => $email,
  61. 'register_time' => $register_time,
  62. ));
  63. $uid = parent::add($tableInfo, self::PARAM_CREATE_ACTION_INSERT_IGNORE);
  64. if (!$uid) {
  65. return ResultWrapper::fail('插入用户库失败');
  66. }
  67. if (!Verify::int($uid)) {
  68. return ResultWrapper::fail("用户名 {$name} 已存在");
  69. }
  70. $user = $tableInfo;
  71. $user[$this->primaryKey] = $uid;
  72. return ResultWrapper::success($user);
  73. }
  74. /**
  75. *
  76. * 根据用户名、密码获取用户信息
  77. * @param string $name
  78. * @param string $password
  79. * @return ResultWrapper
  80. */
  81. public function getByNameAndPassword($name, $password) {
  82. $user = $this->getByName(mb_strtolower($name, 'UTF-8'));
  83. if (!$user) {
  84. return ResultWrapper::fail("用户 {$name} 不存在");
  85. }
  86. if ($user['password'] != Math::md5_16($password)) {
  87. return ResultWrapper::fail("密码 {$password} 不正确");
  88. }
  89. return ResultWrapper::success($user);
  90. }
  91. public function getByName($name) {
  92. $uid = $this->getIdByName($name);
  93. return $this->get($uid);
  94. }
  95. /**
  96. *
  97. * 获取指定用户名 $name 的用户id
  98. * @param string $name
  99. * @return false | int
  100. */
  101. public function getIdByName($name) {
  102. if (empty($name)) {
  103. return false;
  104. }
  105. $condition = array(
  106. 'name' => $name,
  107. );
  108. $result = $this->fetchOne($condition, $this->primaryKey);
  109. if (!$result) {
  110. return false;
  111. }
  112. return array_pop($result);
  113. }
  114. /**
  115. * 修改用户密码
  116. * @param int $uid
  117. * @param string $oldPassword
  118. * @param string $newPassword
  119. * @return ResultWrapper
  120. */
  121. public function modifyPassword($uid, $oldPassword, $newPassword) {
  122. if (!Verify::unsignedInt($uid)) {
  123. return ResultWrapper::fail('无效uid');
  124. }
  125. $user = $this->get($uid);
  126. if ($user['password'] != Math::md5_16($oldPassword)) {
  127. return ResultWrapper::fail('原始密码错误');
  128. }
  129. $newPassword = trim($newPassword);
  130. if (empty($newPassword)) {
  131. return ResultWrapper::fail('新密码不能为空');
  132. }
  133. $newPassword = Math::md5_16($newPassword);
  134. $info = array(
  135. 'uid' => $uid,
  136. 'password' => $newPassword,
  137. );
  138. $cas_token = $user['cas_token'];
  139. $tmpResult = $this->modify($info, null, $cas_token);
  140. if (!$tmpResult->isSuccess()) {
  141. return ResultWrapper::fail('修改密码失败。原因:'.$tmpResult->getData());
  142. }
  143. return ResultWrapper::success();
  144. }
  145. }