The model isn't as big or does as much as it may have sounded. And it doesn't ask a user to find a user, when the user submits the sign in form part of the model functionality is to query the database to find the member based on their sign in credentials. Essentially checking that they exist. See my code below.
class user_model {
private $_data,
$_database,
$_cookieName,
$_sessionName,
$_signedIn;
public function __construct($member = NULL){
$this->_database = Database::getInstance();
$this->_sessionName = Config::get('session/sessionName');
$this->_cookieName = Config::get('remember/cookieName');
if(!$member):
if(Session::exists($this->_sessionName)):
$member = Session::get($this->_sessionName);
if($this->find($member)):
$this->_signedIn = true;
endif;
endif;
else:
$this->find($member);
endif;
}
public function find($member = NULL){
if($member):
$field = (is_numeric($member)) ? 'id' : 'emailAddress';
$data = $this->_database->get('member', array($field, '=', $member));
if($data->count()):
$this->_data = $data->first();
return true;
endif;
endif;
}
public function data(){
return $this->_data;
}
public function create($fields = array()){
if(!$this->_database->insert('member', $fields)):
throw new Exception('Insert error.');
endif;
}
public function update($fields = array(), $id = NULL){
if(!$id and $this->signedIn()):
$id = $this->data()->id;
endif;
if(!$this->_database->update('member', 'id', $id, $fields)):
throw new Exception('Update error.');
endif;
}
public function signedIn(){
return $this->_signedIn;
}
public function signIn($emailAddress = NULL, $password = NULL, $remember = false){
if(!$emailAddress and !$password and $this->exists()):
Session::put($this->_sessionName, $this->data()->id);
else:
$member = $this->find($emailAddress);
if($member):
if($this->data()->password === Hash::make($password, $this->data()->salt)):
Session::put($this->_sessionName, $this->data()->id);
if($remember):
$hash = Hash::unique();
$check = $this->_database->get('sessions', array('memberId', '=', $this->data()->id));
if(!$check->count()):
$this->_database->insert('sessions', array(
'memberId' => $this->data()->id,
'hash' => $hash
));
else:
$hash = $check->first()->hash;
endif;
Cookie::put($this->_cookieName, $hash, Config::get('remember/cookieExpiry'));
endif;
return true;
endif;
endif;
endif;
return false;
}
public function exists(){
return(!empty($this->_data)) ? true : false;
}
public function signOut(){
$this->_database->delete('sessions', array('memberId', '=>', $this->data()->id));
Session::delete($this->_sessionName);
Cookie::delete($this->_cookieName);
}
}
This model is critial for my application, and needs to be accessed on every page.