andrewuk Posted September 10, 2014 Share Posted September 10, 2014 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? Quote Link to comment Share on other sites More sharing options...
trq Posted September 11, 2014 Share Posted September 11, 2014 Firstly, your model sounds like it does too much. Why would you ask a User to find a User? You wouldn't. Models should should model your data is it would be represented in the real world. Users don't know the details about how to login, that is the responsibility of some other model, maybe an Auth model? Then your logic would look more like: ``` $auth->login($user); ``` Auth might also be responsible for checking permissions: ``` $auth->hasPermission($resource, $user); ``` None of these are the responsibility of the User. As for your User needing to be instantiated on each page, your would generally store your User in a Session. This way it can maintain state for the duration of their visit. Quote Link to comment Share on other sites More sharing options...
andrewuk Posted September 11, 2014 Author Share Posted September 11, 2014 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. Quote Link to comment Share on other sites More sharing options...
trq Posted September 11, 2014 Share Posted September 11, 2014 All comments in my previous reply still apply. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.