<?php

/**
 * @filename model_user.php 
 * @encoding UTF-8 
 * @author 邱伟平 
 * @copyright 连云港龙泽商务有限公司 
 * @datetime 2013-6-25  10:42:48
 * @version 1.0
 * @Description
 * 
 * todo:三次错误被锁定
 */
class Model_user extends CI_Model {

    public function check_user($user) {
        $sql = "select * from cms_user where email = '" . $user['email'] . "'";
        $query = $this->db->query($sql);
        if ($query->num_rows == 0) {
            return "无此用户";
        }

        $row = $query->row(0);
        if ($row->errors >= 3) {
            return "登录错误次数超过了三次，请联系管理员！";
        }
        if ($row->password == md5($user['password'])) {
            $this->db->update("cms_user", array("errors" => 0), array("id" => $row->id)); //验证密码通过，将errors错误记录次数设为0    
            return "OK";
        } else {
            //密码错误，将errors登录次数累加,每次加1.
            $this->db->update("cms_user", array("errors" => $row->errors + 1), array("id" => $row->id));
            return "密码错误";
        }
    }

    /*  修改密码.
     *  获取session里面的当前用户id
      
      如果密码正确更新user新密码，更新成功返回ok
      如果错误返回密码错误
     */

    function change_password() {
        $id = $this->session->userdata('user_id');
        $password_new = md5($this->input->post('password_new'));  //新密码
        $sql_update = "update cms_user set password = '{$password_new}' where id = '{$id}'";
        $this->db->query($sql_update);
        return "OK";
    }

}

/* End of file : model_user.php */
