Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- User.php
- --------
- <?php
- class User extends Admin_Controller
- {
- public function __construct()
- {
- parent::__construct();
- }
- public function index()
- {
- // Fetch all users
- $this->data['users'] = $this->user_m->get();
- // Load view
- $this->data['subview'] = 'admin/user/index';
- $this->load->view( 'admin/_layout_main', $this->data );
- }
- public function edit( $id = null )
- {
- // Fetch a user or set a new one
- if( $id )
- {
- $this->data['user'] = $this->user_m->get( $id );
- count( $this->data['user'] ) || $this->data['errors'][] = 'User could not be found';
- }
- else
- {
- $this->data['user'] = $this->user_m->get_new();
- }
- // Set up the form
- $rules = $this->user_m->rules_admin;
- $id || $rules['password']['rules'] .= '|required';
- $this->form_validation->set_rules( $rules );
- // Process the form
- if( $this->form_validation->run() == TRUE )
- {
- $data = $this->user_m->array_from_post( array( 'name', 'email', 'password' ) );
- $data['password'] = $this->user_m->hash( $data['password'] );
- $this->user_m->save( $data, $id );
- redirect( 'admin/user' );
- }
- // Load the view
- $this->data['subview'] = 'admin/user/edit';
- $this->load->view( 'admin/_layout_main', $this->data );
- }
- public function delete( $id )
- {
- $this->user_m->delete( $id );
- redirect( 'admin/user' );
- }
- public function login()
- {
- // Redirect the user if he's already logged in
- $this->user_m->loggedin() == FALSE || redirect( 'admin/dashboard' );
- // Set up form
- $rules = $this->user_m->rules;
- $this->form_validation->set_rules( $rules );
- // Process form
- if( $this->form_validation->run() )
- {
- // Login and redirect
- if( $this->user_m->login() == TRUE )
- {
- redirect( 'admin/dashboard' );
- }
- else
- {
- $this->session->set_flashdata( 'error', 'That email/password combination does not exist' );
- redirect( 'admin/user/login', 'refresh' );
- }
- }
- // Load view
- $this->data['subview'] = 'admin/user/login';
- $this->load->view( 'admin/_layout_modal', $this->data );
- }
- public function logout()
- {
- $this->user_m->logout();
- redirect( 'admin/user/login' );
- }
- public function _unique_email( $str )
- {
- // Do NOT validate if email aready exists unless it belongs to current user
- $id = $this->uri->segment( 4 );
- $this->db->where( 'email', $this->input->post( 'email' ) );
- // set where condition if there is an id
- !$id || $this->db->where( 'id !=', $id );
- $user = $this->user_m->get();
- if( count( $user ) )
- {
- $this->form_validation->set_message( '_unique_email', '%s should be unique' );
- return FALSE;
- }
- return TRUE;
- }
- }
- user_m.php
- ----------
- <?php
- class User_M extends MY_Model
- {
- protected $_table_name = 'users';
- protected $_order_by = 'name';
- public $rules = array(
- 'email' => array(
- 'field' => 'email',
- 'label' => 'Email',
- 'rules' => 'trim|required|valid_email|xss_clean'
- ),
- 'password' => array(
- 'field' => 'password',
- 'lable' => 'Password',
- 'rules' => 'trim|required'
- )
- );
- public $rules_admin = array(
- 'name' => array(
- 'field' => 'name',
- 'label' => 'Name',
- 'rules' => 'trim|required|xss_clean'
- ),
- 'email' => array(
- 'field' => 'email',
- 'label' => 'Email',
- 'rules' => 'trim|required|valid_email|callback__unique_email|xss_clean'
- ),
- 'password' => array(
- 'field' => 'password',
- 'lable' => 'Password',
- 'rules' => 'trim|matches[password_confirm]'
- ),
- 'password_confirm' => array(
- 'field' => 'password_confirm',
- 'lable' => 'Confirm Password',
- 'rules' => 'trim|matches[password]'
- )
- );
- function __construct()
- {
- parent::__construct();
- }
- public function login()
- {
- $user = $this->get_by( array(
- 'email' => $this->input->post( 'email' ),
- 'password' => $this->hash( $this->input->post( 'password' ) )
- ), TRUE );
- if( count( $user ) )
- {
- $data = array(
- 'name' => $user->name,
- 'email' => $user->email,
- 'id' => $user->id,
- 'loggedin' => TRUE
- );
- $this->session->set_userdata( $data );
- }
- }
- public function logout()
- {
- $this->session->sess_destroy();
- }
- public function loggedin()
- {
- return (bool) $this->session->userdata( 'loggedin' );
- }
- public function get_new()
- {
- $user = new stdClass();
- $user->name = '';
- $user->email = '';
- $user->password = '';
- return $user;
- }
- public function hash( $string )
- {
- return hash( 'sha512', $string . config_item( 'encryption_key' ) );
- }
- }
- MY_Model.php
- ------------
- <?php
- class MY_Model extends CI_Model
- {
- protected $_table_name = '';
- protected $_primary_key = 'id';
- protected $_primary_filter = 'intval';
- protected $_order_by = '';
- public $rules = array();
- protected $_timestamps = FALSE;
- function __construct()
- {
- parent::__construct();
- }
- public function array_from_post( $fields )
- {
- $data = array();
- foreach( $fields as $field )
- {
- $data[$field] = $this->input->post( $field );
- }
- return $data;
- }
- public function get( $id = null, $single = FALSE )
- {
- if( $id != null )
- {
- $filter = $this->_primary_filter;
- $id = $filter( $id );
- $this->db->where( $this->_primary_key, $id );
- $method = 'row';
- }
- elseif( $single == TRUE )
- {
- $method = 'row';
- }
- else
- {
- $method = 'result';
- }
- if( ! count( $this->db->ar_orderby ) )
- {
- $this->db->order_by( $this->_order_by );
- }
- return $this->db->get( $this->_table_name )->$method();
- }
- public function get_by( $where, $single = FALSE )
- {
- $this->db->where( $where );
- return $this->get( null, $single );
- }
- public function save( $data, $id = null )
- {
- // Set timestamps
- if( $this->_timestamps == TRUE )
- {
- $now = date( 'Y-m-d H:i:s' );
- $id || $data['created'] = $now;
- $data['modified'] = $now;
- }
- // Insert
- if( $id === null )
- {
- // set it to null if it is set
- ! isset( $data[$this->_primary_key] ) || $data[$this->_primary_key] = null;
- $this->db->set( $data );
- $this->db->insert( $this->_table_name );
- $id = $this->db->insert_id();
- }
- // Update
- else
- {
- $filter = $this->_primary_filter;
- $id = $filter( $id );
- $this->db->set( $data );
- $this->db->where( $this->_primary_key, $id );
- $this->db->update( $this->_table_name );
- }
- return $id;
- }
- public function delete( $id )
- {
- $filter = $this->_primary_filter;
- $id = $filter( $id );
- if( ! $id )
- {
- return FALSE;
- }
- else
- {
- $this->db->where( $this->_primary_key, $id );
- $this->db->limit( 1 );
- $this->db->delete( $this->_table_name );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement