123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- <?php
- namespace Cas\Dao;
- use KIF\Dao\DBAgileDev;
- use KIF\Data\ResultWrapper;
- use KIF\Math\Math;
- use KIF\Verify;
- /**
- *
- * 后台用户
- * @author lishumingoo@gmail.com
- */
- class BackUser extends DBAgileDev {
- protected $tableName = 'back_user';
-
- protected $primaryKey = 'uid';
-
- /**
- * 数据库里的真实字段
- * @var array
- */
- protected $other_field = array(
- 'name',
- 'password',
- 'email',
- 'project',
- 'permission',
- );
-
- public function __construct($master_flag = 'backend') {
- parent::__construct($master_flag);
- }
-
- public function add(array $info) {
- $name = $info['name'];
- if (empty($name)) {
- return ResultWrapper::fail('name字段不能为空');
- }
- $name = mb_strtolower($name, 'UTF-8');
- # 这是数据表存储上的逻辑
- if (mb_strlen($name) > 40) {
- return ResultWrapper::fail('name字段超出了40个字');
- }
- $password = $info['password'];
- if (empty($password)) {
- return ResultWrapper::fail('password字段不能为空');
- }
- $password = Math::md5_16($password);
- $email = $info['email'];
- // if (empty($email)) {
- // return ResultWrapper::fail('email字段不能为空');
- // }
-
- # 这是数据表存储上的逻辑
- if (mb_strlen($email) > 50) {
- return ResultWrapper::fail('email字段超出了50个字');
- }
- if (isset($info['register_time']) && Verify::int($info['register_time'])) {
- $register_time = $info['register_time'];
- } else {
- $register_time = time();
- }
- $tableInfo = array_merge($info, array(
- 'name' => $name,
- 'password' => $password,
- 'email' => $email,
- 'register_time' => $register_time,
- ));
- $uid = parent::add($tableInfo, self::PARAM_CREATE_ACTION_INSERT_IGNORE);
- if (!$uid) {
- return ResultWrapper::fail('插入用户库失败');
- }
- if (!Verify::int($uid)) {
- return ResultWrapper::fail("用户名 {$name} 已存在");
- }
- $user = $tableInfo;
- $user[$this->primaryKey] = $uid;
- return ResultWrapper::success($user);
- }
-
- /**
- *
- * 根据用户名、密码获取用户信息
- * @param string $name
- * @param string $password
- * @return ResultWrapper
- */
- public function getByNameAndPassword($name, $password) {
- $user = $this->getByName(mb_strtolower($name, 'UTF-8'));
- if (!$user) {
- return ResultWrapper::fail("用户 {$name} 不存在");
- }
- if ($user['password'] != Math::md5_16($password)) {
- return ResultWrapper::fail("密码 {$password} 不正确");
- }
-
- return ResultWrapper::success($user);
- }
-
- public function getByName($name) {
- $uid = $this->getIdByName($name);
- return $this->get($uid);
- }
-
- /**
- *
- * 获取指定用户名 $name 的用户id
- * @param string $name
- * @return false | int
- */
- public function getIdByName($name) {
- if (empty($name)) {
- return false;
- }
-
- $condition = array(
- 'name' => $name,
- );
- $result = $this->fetchOne($condition, $this->primaryKey);
- if (!$result) {
- return false;
- }
- return array_pop($result);
- }
-
- /**
- * 修改用户密码
- * @param int $uid
- * @param string $oldPassword
- * @param string $newPassword
- * @return ResultWrapper
- */
- public function modifyPassword($uid, $oldPassword, $newPassword) {
- if (!Verify::unsignedInt($uid)) {
- return ResultWrapper::fail('无效uid');
- }
-
- $user = $this->get($uid);
- if ($user['password'] != Math::md5_16($oldPassword)) {
- return ResultWrapper::fail('原始密码错误');
- }
-
- $newPassword = trim($newPassword);
- if (empty($newPassword)) {
- return ResultWrapper::fail('新密码不能为空');
- }
-
- $newPassword = Math::md5_16($newPassword);
- $info = array(
- 'uid' => $uid,
- 'password' => $newPassword,
- );
-
- $cas_token = $user['cas_token'];
- $tmpResult = $this->modify($info, null, $cas_token);
- if (!$tmpResult->isSuccess()) {
- return ResultWrapper::fail('修改密码失败。原因:'.$tmpResult->getData());
- }
-
- return ResultWrapper::success();
- }
- }
|