Created
July 5, 2011 09:26
-
-
Save nook-scheel/1064547 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Project: Admin | |
* Copyright: ©2006-2011 Pumpkin Inc. and contributors. | |
* Portions ©2007-2011 Pumpkin Inc. All rights reserved. | |
* @package Users | |
*/ | |
class User_Model extends CI_Model | |
{ | |
/** Utility Methods **/ | |
function _required($required, $data) | |
{ | |
foreach($required as $field) | |
if(!isset($data[$field])) return false; | |
return true; | |
} | |
function _default($defaults, $options) | |
{ | |
return array_merge($defaults, $options); | |
} | |
/** User Methods **/ | |
/** | |
* AddUser method creates a record in the users table | |
* | |
* Option: Values | |
* -------------- | |
* userName required | |
* userEmail required | |
* userFirstName required | |
* userLastName required | |
* userPhone required | |
* userPassword required | |
* userKey required | |
* userType | |
* userStatus | |
* | |
* @param array $options | |
* @result int insert_id() | |
*/ | |
function AddUser($options = array()) | |
{ | |
// required values | |
if(!$this->_required( | |
array('userName', 'userEmail', 'userFirstName', 'userLastName', 'userPhone', 'userKey'), | |
$options) | |
) return false; | |
$options = $this->_default(array('userStatus' => 'inactive'), $options); | |
$this->db->insert('users', $options); | |
return $this->db->insert_id(); | |
} | |
/** | |
* UpdateUser method updates a record in the users table | |
* | |
* Option: Values | |
* -------------- | |
* userId required | |
* userName | |
* userEmail | |
* userFirstName | |
* userLastName | |
* userPhone | |
* userPassword | |
* userKey | |
* userType | |
* userStatus | |
* | |
* @param array $options | |
* @return int affected_rows() | |
*/ | |
function UpdateUser($options = array()) | |
{ | |
// required values | |
if(!$this->_required( | |
array('userId'), | |
$options) | |
) return false; | |
if(isset($options['userName'])) | |
$this->db->set('userName', $options['userName']); | |
if(isset($options['userEmail'])) | |
$this->db->set('userEmail', $options['userEmail']); | |
if(isset($options['userFirstName'])) | |
$this->db->set('userFirstName', $options['userFirstName']); | |
if(isset($options['userLastName'])) | |
$this->db->set('userLastName', $options['userLastName']); | |
if(isset($options['userPhone'])) | |
$this->db->set('userPhone', $options['userPhone']); | |
if(isset($options['userPassword'])) | |
$this->db->set('userPassword', md5($options['userPassword'])); | |
if(isset($options['userKey'])) | |
$this->db->set('userKey', $options['userKey']); | |
if(isset($options['userBruteforce'])) | |
$this->db->set('userBruteforce', $options['userBruteforce']); | |
if(isset($options['userType'])) | |
$this->db->set('userType', $options['userType']); | |
if(isset($options['userStatus'])) | |
$this->db->set('userStatus', $options['userStatus']); | |
$this->db->where('userId', $options['userId']); | |
$this->db->update('users'); | |
return $this->db->affected_rows(); | |
} | |
/** | |
* GetUsers method returns a qualified list of users from the users table | |
* | |
* Options: Values | |
* --------------- | |
* userId | |
* userName | |
* userEmail | |
* userFirstName | |
* userLastName | |
* userPhone | |
* userPassword | |
* userKey | |
* userType | |
* userStatus | |
* | |
* limit limit the returned records | |
* offset bypass this many records | |
* sortBy sort by this column | |
* sortDirection (asc, desc) | |
* | |
* Returned Object (array of) | |
* -------------------------- | |
* userId | |
* userName | |
* userEmail | |
* userFirstName | |
* userLastName | |
* userPhone | |
* userPassword | |
* userKey | |
* userType | |
* userStatus | |
* | |
* @param array $options | |
* @return array of objects | |
* | |
*/ | |
function GetUsers($options = array()) | |
{ | |
// select | |
if(isset($options['select'])) | |
$this->db->select($options['select']); | |
// join | |
if(isset($options['join'])) | |
$this->db->join($options['join']['table'], $options['join']['on']); | |
// Qualification | |
if(isset($options['userId'])) | |
$this->db->where('userId', $options['userId']); | |
if(isset($options['userName'])) | |
$this->db->where('userName', $options['userName']); | |
if(isset($options['userEmail'])) | |
$this->db->where('userEmail', $options['userEmail']); | |
if(isset($options['userFirstName'])) | |
$this->db->where('userFirstName', $options['userFirstName']); | |
if(isset($options['userLastName'])) | |
$this->db->where('userLastName', $options['userLastName']); | |
if(isset($options['userPhone'])) | |
$this->db->where('userPhone', $options['userPhone']); | |
if(isset($options['userPassword'])) | |
$this->db->where('userPassword', $options['userPassword']); | |
if(isset($options['userKey'])) | |
$this->db->where('userKey', $options['userKey']); | |
if(isset($options['userStatus']) AND $options['userStatus'] !== FALSE) | |
$this->db->where('userStatus', $options['userStatus']); | |
if(isset($options['userType'])) | |
$this->db->where('userType', $options['userType']); | |
// limit / offset | |
if(isset($options['limit']) && isset($options['offset'])) | |
$this->db->limit($options['limit'], $options['offset']); | |
else if(isset($options['limit'])) | |
$this->db->limit($options['limit']); | |
// sort | |
if(isset($options['sortBy']) && isset($options['sortDirection'])) | |
$this->db->order_by($options['sortBy'], $options['sortDirection']); | |
if(!isset($options['userStatus'])) $this->db->where('userStatus !=', 'deleted'); | |
$query = $this->db->get("users"); | |
if(isset($options['count'])) return $query->num_rows(); | |
if(isset($options['userId']) || isset($options['userEmail']) || isset($options['userName']) || isset($options['userKey'])) | |
return $query->row(0); | |
return $query->result(); | |
} | |
/** authentication methods **/ | |
/** | |
* The login method adds user information from the database to session data. | |
* | |
* Option: Values | |
* -------------- | |
* userName | |
* userPassword | |
* | |
* @param array $options | |
* @return object result() | |
*/ | |
function Login($options = array()) | |
{ | |
// required values | |
if(!$this->_required( | |
array('userName', 'userPassword'), | |
$options) | |
) return false; | |
$user = $this->GetUsers(array('userName' => $options['userName'], 'userPassword' => md5($options['userPassword']))); | |
if(!$user) return false; | |
$this->session->set_userdata('userName', $user->userName); | |
$this->session->set_userdata('userId', $user->userId); | |
$this->session->set_userdata('userType', $user->userType); | |
return true; | |
} | |
/** | |
* The register method adds user information to database. | |
* | |
* Option: Values | |
* -------------- | |
* userName required | |
* userEmail required | |
* userFirstName required | |
* userLastName required | |
* userPhone required | |
* userPassword required | |
* userKey required | |
* userType | |
* userStatus | |
* | |
* @param array $options | |
* @return object result() | |
*/ | |
function Register($options = array()) | |
{ | |
// required values | |
if(!$this->_required( | |
array('userName', 'userEmail', 'userFirstName', 'userLastName', 'userPhone', 'userKey'), | |
$options) | |
) return false; | |
$registerUser = $this->AddUser(array( | |
'userName' => $options['userName'], | |
'userEmail' => $options['userEmail'], | |
'userFirstName' => $options['userFirstName'], | |
'userLastName' => $options['userLastName'], | |
'userPhone' => $options['userPhone'], | |
'userKey' => $options['userKey'] | |
)); | |
if(!$registerUser) return false; | |
return $registerUser; | |
} | |
/** | |
* The secure method checks a user's session against the passed parameters to determine if the user has | |
* access to a specific area. | |
* | |
* Option: Values | |
* -------------- | |
* userType | |
* | |
* @param array @options | |
* @return bool | |
*/ | |
function Secure($options = array()) | |
{ | |
// required values | |
if(!$this->_required( | |
array('userType'), | |
$options) | |
) return false; | |
$userType = $this->session->userdata('userType'); | |
if(is_array($options['userType'])) | |
{ | |
foreach($options['userType'] as $optionUserType) | |
{ | |
if($optionUserType == $userType) return true; | |
} | |
} | |
else | |
{ | |
if($userType == $options['userType']) return true; | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment