Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class User {
- private $_db,
- $_data,
- $_sessionName,
- $_cookieName,
- $isLoggedIn;
- public function __construct($user = null) {
- $this->_db = DB::getInstance();
- $this->_sessionName = Config::get('sessions/session_name');
- $this->_cookieName = Config::get('remember/cookie_name');
- if(!$user) {
- if(Session::exists($this->_sessionName)) {
- $user = Session::get($this->_sessionName);
- if($this->find($user)) {
- $this->isLoggedIn = true;
- } else {
- //Logout
- }
- }
- } else {
- $this->find($user);
- }
- }
- public function create($fields = array()) {
- if(!$this->_db->insert('users', $fields)) {
- throw new Exception('Sorry, there was a problem creating your account;');
- }
- }
- public function delete($fields = array()) {
- if(!$this->_db->delete('users', $fields)) {
- throw new Exception('Sorry, there was a problem deleting your account;');
- }
- }
- public function update($fields = array(), $id = null) {
- if(!$id && $this->isLoggedIn()) {
- $id = $this->data()->id;
- }
- if(!$this->_db->update('users', $id, $fields)) {
- throw new Exception('There was a problem updating');
- }
- }
- public function addChild($id,$childID,$leftOrRight){
- $errors=0;
- if(!strlen($leftOrRight)){
- $errors++;
- //throw new Exception('There was a problem');
- }
- $field = ($leftOrRight === '2')? "rightID" : "leftID";
- $fields=array(
- $field => $childID
- );
- //var_dump($fields);
- $data = $this->_db->get('users', array("username", '=', $id))->first();
- if((strlen($this->_db->get('users', array('id', '=', $data->id))->first()->rightID)>3 && $field === "rightID") || (strlen($this->_db->get('users', array('id', '=', $data->id))->first()->leftID)>3 && $field === "leftID")){
- $errors++;
- //throw new Exception('There was a problems, child exists');
- }
- //var_dump(strlen($this->_db->get('users', array('id', '=', $data->id))->first()->rightID));
- if(!$this->_db->update('users', $data->id, $fields)) {
- $errors++;
- //throw new Exception('There was a problem assigning child');
- }
- if($errors>0)return false;
- return true;
- }
- public function find($user = null) {
- if($user) {
- $field = (is_numeric($user)) ? 'id' : 'username';
- $data = $this->_db->get('users', array($field, '=', $user));
- if($data->count()) {
- $this->_data = $data->first();
- return true;
- }
- }
- return false;
- }
- public function login($username = null, $password = null, $remember = false) {
- if(!$username && !$password && $this->exists()) {
- Session::put($this->_sessionName, $this->data()->id);
- } else {
- $user = $this->find($username);
- if ($user) {
- if ($this->data()->password === Hash::make($password, $this->data()->salt)) {
- Session::put($this->_sessionName, $this->data()->id);
- if ($remember) {
- $hash = Hash::unique();
- $hashCheck = $this->_db->get('users_session', array('user_id', '=', $this->data()->id));
- if (!$hashCheck->count()) {
- $this->_db->insert('users_session', array(
- 'user_id' => $this->data()->id,
- 'hash' => $hash
- ));
- } else {
- $hash = $hashCheck->first()->hash;
- }
- Cookie::put($this->_cookieName, $hash, Config::get('remember/cookie_expiry'));
- }
- return true;
- }
- }
- }
- return false;
- }
- public function hasPermission($key) {
- $group = $this->_db->get('groups', array('id', '=', $this->data()->group));
- if($group->count()) {
- $permissions = json_decode($group->first()->permissions, true);
- return !empty($permissions[$key]);
- }
- return false;
- }
- public function exists() {
- return (!empty($this->_data)) ? true : false;
- }
- public function logout() {
- $this->_db->delete('users_session', array('user_id', '=', $this->data()->id));
- Session::delete($this->_sessionName);
- Cookie::delete($this->_cookieName);
- }
- public function data(){
- return $this->_data;
- }
- public function isLoggedIn() {
- return $this->isLoggedIn;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement