]>
jfr.im git - z_archive/KronOS.git/blob - application/models/user.php
1 <?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
3 class User
extends CI_Model
{
4 protected $cached_sid, $cached_uid, $cached_level, $cached_display_name;
5 public function __construct() {
10 public function try_login($user, $pass) {
11 $this->db
->select('uid, password, salt');
12 $this->db
->where('username', $user);
13 $q = $this->db
->get('users');
14 if ($q->num_rows() > 0) {
16 $pwdigest = sha1($row->salt
.$pass);
17 if ($pwdigest == $row->password
) {
18 return $this->do_login($row->uid
);
23 public function do_login($uid) {
24 $this->db
->select('display_name, level');
25 $this->db
->where('uid', $uid);
26 $q = $this->db
->get('users');
27 if ($q->num_rows() == 0)
31 $this->display_name($row->display_name
);
32 $this->level($row->level
);
38 'lockip' => $this->input
->ip_address(),
40 $this->db
->insert('sessions', $sdata);
41 $this->sid($this->db
->insert_id());
46 public function sid($new=NULL) {
48 $old = $this->cached_sid
;
49 $this->cached_sid
= $new;
53 if (isset($this->cached_sid
)) {
54 return $this->cached_sid
;
56 return FALSE; // FIXME should we fetch SID somehow?
59 public function uid($new=NULL) {
61 $old = $this->cached_uid
;
62 $this->cached_uid
= $new;
66 if (isset($this->cached_uid
)) {
67 return $this->cached_uid
;
71 $this->db
->select('uid');
72 $this->db
->where('sid', $sid);
73 $q = $this->db
->get('sessions');
74 if ($q->num_rows() > 0) {
76 return $this->cached_uid
= $row->uid
;
82 public function display_name($new=NULL) {
84 $old = $this->cached_display_name
;
85 $this->cached_display_name
= $new;
89 if (isset($this->cached_display_name
)) {
90 return $this->cached_display_name
;
94 $this->db
->select('display_name');
95 $this->db
->where('uid', $uid);
96 $q = $this->db
->get('users');
97 if ($q->num_rows() > 0) {
99 return $this->cached_display_name
= $row->display_name
;
105 public function level($new=NULL) {
107 $this->cached_level
= $new;
110 public function is_logged_in() {
111 return $this->sid() > 0;
114 public function check_login() {
115 if (!$this->logged_in
)
116 redirect('account/login/');