Jump to content

andrewuk

New Members
  • Posts

    2
  • Joined

  • Last visited

andrewuk's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. 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.
  2. Hey all, I am making a application that requires a user to login in. I have a user model, which does such things as finds, registers, logins in, updates etc a user. It also grabs all of a users data from a database, such as their username, email address etc and checks whether or not a user is logged in. The problem I face is this. I want to control what content a user can see depending on if they are logged in or not. In addition to this, I want to output the users data, such as their username, in other parts of the website – such as the header and footer. There is no real easy way of doing this, and requires the user model to be instantiated on every page. My question is should this class be a model or just a core library class?
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.