From 3f01ddce9dff69a49493541882de85854cbcebe5 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Sun, 8 Apr 2012 23:13:15 +0200 Subject: start working on users Signed-off-by: Florian Pritz --- application/models/muser.php | 70 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 application/models/muser.php (limited to 'application/models/muser.php') diff --git a/application/models/muser.php b/application/models/muser.php new file mode 100644 index 000000000..0b3d26be7 --- /dev/null +++ b/application/models/muser.php @@ -0,0 +1,70 @@ +load->library("session"); + } + + function logged_in() + { + return $this->session->userdata('logged_in') == true; + } + + function login($username, $password) + { + $query = $this->db->query(' + SELECT * + FROM `users` + WHERE `username` = ? + ', array($username))->row_array(); + + if (crypt($password, $query["password"] == $password)) { + $this->session->set_userdata('logged_in', true); + $this->session->set_userdata('username', $username); + return true; + } else { + return false; + } + } + + function logout() + { + $this->session->unset_userdata('logged_in'); + $this->session->unset_userdata('username'); + } + + function get_username() + { + return $this->session->userdata('username'); + } + + function get_userid() + { + $query = $this->db->query(" + SELECT id + FROM users + WHERE username = ? + ", array($this->get_username()))->row_array(); + return $query["id"]; + } + + function require_access() + { + if ($this->logged_in()) { + return true; + } else { + $this->session->set_flashdata("uri", $this->uri->uri_string()); + redirect('user/login'); + } + } + + function hash_password($password) + { + $salt = random_alphanum(22); + return crypt($password, "$2a$10$$salt$"); + } + +} + -- cgit v1.2.3-24-g4f1b