blueset

model

May 17th, 2014
437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.56 KB | None | 0 0
  1. <?php
  2. /*
  3. 用户等级设定:
  4. 0:注册用户。
  5. 1:投递者 允许发布,允许修改自己的
  6. 2:编辑 允许发布,允许修改
  7. 3:管理员 允许发布 允许修改 允许删除*/
  8. class user_model extends CI_Model {
  9.     public function __construct()
  10.   {
  11.     parent::__construct();
  12.     $this->load->database();
  13.     $this->load->library('session');
  14.  
  15.   }
  16.     /**
  17.       * 添加用户session数据,设置用户在线状态
  18.       * @param string $username
  19.       */
  20.      function login($userinfo)
  21.      {
  22.          $data = array('username'=>$userinfo->username,
  23.                        'user_id'=>$userinfo->id,
  24.                        'role'=>$userinfo->role,
  25.                        'logged_in'=>TRUE);
  26.          $this->session->set_userdata($data);                    //添加session数据
  27.      }
  28.      function write_session($userinfo)
  29.      {
  30.         $user_json = json_encode($userinfo);
  31.         $cookie = array(
  32.             'name'   => 'userinfo',
  33.             'value'  => $user_json,
  34.             'expire' => '2592000',
  35.             'secure' => TRUE
  36.         );
  37.         $this->input->set_cookie($cookie);
  38.      }
  39.      /**
  40.       * 通过用户名获得用户记录
  41.       * @param string $username
  42.       */
  43.      function get_by_username($username)
  44.      {
  45.          $this->db->where('username', $username);
  46.          $query = $this->db->get('users');
  47.          //return $query->row();                            //不判断获得什么直接返回
  48.          if ($query->num_rows() == 1)
  49.          {
  50.              return $query->row();
  51.          }
  52.          else
  53.          {
  54.              return FALSE;
  55.          }
  56.      }
  57.      function get_by_id($id){  
  58.         $this->db->where('id', $id);
  59.         $query = $this->db->get('users');
  60.         if ($query->num_rows() == 1)
  61.         {
  62.             return $query->row();
  63.         }
  64.         else
  65.         {
  66.             return FALSE;
  67.         }
  68.      }
  69.      
  70.      /**
  71.       * 用户名不存在时,返回false
  72.       * 用户名存在时,验证密码是否正确
  73.       */
  74.      function password_check($username, $password)
  75.      {                
  76.          if($user = $this->get_by_username($username))
  77.          {
  78.              return $user->password == $password ? TRUE : FALSE;
  79.          }
  80.          return FALSE;  //当用户名不存在时
  81.      }
  82.      /**
  83.       * 添加用户
  84.       */
  85.      function add_user($username, $password, $email, $display_name)
  86.      {
  87.          $data = array('username'=>$username, 'password'=>$password, 'email'=>$email,
  88.                        'display_name'=>$display_name ,'role'=>0);
  89.          $this->db->insert('users', $data);
  90.          if ($this->db->affected_rows() > 0)
  91.          {
  92.              $this->login($username);
  93.              return TRUE;
  94.          }
  95.          return FALSE;
  96.      }
  97.      public function update_profile($id=-1)
  98.      {
  99.         $pw = ($this->input->post('newpwconf') == "")?$this->input->post('password'):$this->input->post('newpw');
  100.         if ($id == -1){
  101.             $data = array('username' => $this->input->post('username'),
  102.                         'password' => md5($pw),
  103.                         'email' => $this->input->post('email'),
  104.                         'display_name' => $this->input->post('display_name'));
  105.             $this->db->where('id', $this->get_session('user_id'));
  106.         }else{
  107.             $data = array('username' => $this->input->post('username'),
  108.                         'password' => md5($pw),
  109.                         'email' => $this->input->post('email'),
  110.                         'role' => $this->input->post('role'),
  111.                         'display_name' => $this->input->post('display_name'));
  112.             $this->db->where('id', $id);
  113.         }
  114.        
  115.         $this->db->update('users', $data);
  116.  
  117.        
  118.      }
  119.      function email_exists($email)
  120.      {
  121.          $this->db->where('email', $email);
  122.          $query = $this->db->get('users');
  123.          return $query->num_rows() ? TRUE : FALSE;
  124.      }
  125.      public function get_session($data_name)
  126.      {
  127.          if (isset( $this->session->userdata[$data_name]))
  128.             {return  $this->session->userdata[$data_name];}
  129.             else {return null;}
  130.      }
  131.     public function logged_in()
  132.      {
  133.          if( $this->get_session('logged_in')===TRUE){
  134.             return TRUE;
  135.          }else{
  136.             return 'You have not logged in.';
  137.          }
  138.      }
  139.      function logout()
  140.      {
  141.          if ($this->logged_in() === TRUE)
  142.          {
  143.              $this->session->sess_destroy();                //销毁所有session的数据
  144.  
  145.              return TRUE;
  146.          }
  147.          return FALSE;
  148.      }
  149. /*
  150.      public function allow_to_post()
  151.      {
  152.          if(!$this->logged_in()===TRUE){return $this->logged_in();}else{
  153.             if ($this->get_session('role')>0){return 'Your account is not eligible to post.';}
  154.             else{return TRUE;}
  155.          }
  156.      }
  157.      public function allow_to_edit($post)
  158.      {
  159.          if (!$this->allow_to_post()===TRUE){return $this->allow_to_post();}else{
  160.             if ($this->get_session('role')==1 && $post->user_id == $this->get_session('user_id')){return TRUE;}
  161.             elseif($this->get_session('role')>1){return TRUE;}
  162.             else{return 'Your account is not eligible to edit this post.';}
  163.          }
  164.      }
  165.      public function allow_to_delete($post)
  166.      {
  167.          if(!$this->allow_to_edit($post)===TRUE){return $this->allow_to_edit($post);}else{
  168.             if($this->get_session('role')<=2 && $post->user_id == $this->get_session('user_id')){return TRUE;}
  169.             elseif($this->get_session('role')==3){return TRUE;}
  170.             else{return 'Your account is not eligible to delete this post.';}
  171.          }
  172.      }
  173.      */
  174.      public function get_user_number(){
  175.         return $this->db->count_all('users');
  176.     }
  177.     public function is_own($user_id)
  178.     {
  179.         $string = $this->session->userdata('user_id') == $user_id ? "_own" : "";
  180.         return $string;
  181.     }
  182.     public function get_users($per_page=20,$offset=0){
  183.         $query = $this->db->get('users',$per_page,$offset);
  184.         return $query->result();
  185.     }
  186.     public function access_to($activity,$user_role=-1)
  187.     {
  188.         if ($user_role == -1){$user_role = (int)$this->session->userdata('role');}
  189.         $table=array(
  190.             "edit" => array( 0 => FALSE,
  191.                              1 => FALSE,
  192.                              2 => TRUE,
  193.                              3 => TRUE),
  194.             "edit_own" => array( 0 => FALSE,
  195.                                  1 => TRUE,
  196.                                  2 => TRUE,
  197.                                  3 => TRUE),
  198.             "delete" => array( 0 => FALSE,
  199.                                1 => FALSE,
  200.                                2 => TRUE,
  201.                                3 => TRUE),
  202.             "delete_own" => array( 0 => FALSE,
  203.                                    1 => TRUE,
  204.                                    2 => TRUE,
  205.                                    3 => TRUE),
  206.             "system_admin" => array( 0 => FALSE,
  207.                                    1 => TRUE,
  208.                                    2 => TRUE,
  209.                                    3 => TRUE),
  210.  
  211.             "post" =>     array( 0 => FALSE,
  212.                                  1 => TRUE,
  213.                                  2 => TRUE,
  214.                                  3 => TRUE)
  215.             );
  216.         return $table[$activity][$user_role];
  217.     }
  218.     public function delete_user($id)
  219.   {
  220.     $this->db->where('id', $id);
  221.     $this->db->delete('users');
  222.     if($this->db->affected_rows()==0){
  223.       return false;
  224.     }else{
  225.       return TRUE;
  226.     }
  227.   }
  228. }
Advertisement
Add Comment
Please, Sign In to add comment