diff --git a/core/App/App.php b/core/App/App.php index 85368fd..ed32db6 100644 --- a/core/App/App.php +++ b/core/App/App.php @@ -7,6 +7,8 @@ class App extends Session{ + use UserManagement; + static $user = false;//variable to store the user static $url = null;//variable to store the url static $extras = [];//extras @@ -63,102 +65,12 @@ static function extra(){ } } - static function connect($usr, $remind = false){//connect the app to the user passed in parameter as $usr - self::$user = $usr; - self::session(); - $_SESSION['user'] = self::$user;//store the user in session - if($remind){ - self::saveusercookie($usr); - } - self::save('ip', $_SERVER['REMOTE_ADDR']); - } - - static function saveusercookie($usr){ - setcookie('user', self::cookieinfo($usr), time() + 3600 * 24, '/', 'wallp.local', false, true); - } - - static function cookieinfo($user){ - return $user->id .'*'. sha1($user->name . $user->pass . $_SERVER['REMOTE_ADDR']); - } - - static function fromcookie(){ - if(isset($_COOKIE['user'])){ - $cookie = $_COOKIE['user']; - $cookie = explode('*',$cookie); - $usr = self::getuser($cookie[0]); - if($usr == false){ - return false; - } - if(self::cookieinfo($usr) == implode('*', $cookie)){ - self::connect($usr); - self::saveusercookie($usr); - } - } - return false; - } - - static function getuser($id){ - require __DIR__.'/../Models/UserModel.php'; - $model = new User(); - return $model->user_info([ - 'id' => $id - ]); - } - - static function is_connected(){//return true if the user exist, else false - - if(self::$user != false){ - - return true; - - } - - return false; - - } - - static function getname(){//return the name of the user and false if we are not connected - - if(self::is_connected()){ - - $usr = self::$user; - return $usr->name; - - } - - return false; - - } - - static function getid(){//return the id of the user and false if we are not connected - - if(self::is_connected()){ - - $usr = self::$user; - return $usr->id; - - } - - return false; - - } - static function back(){//a shortcut to go to the prevent page echo ''; } - static function logout(){//logout the current user if we are connected - - self::$user = false; - if(isset($_SESSION) && isset($_SESSION['user'])){ - $_SESSION['user'] = false; - } - self::clear_session(); - - } - static function seturl($nurl = null){//set the url if($nurl != null){ @@ -168,27 +80,6 @@ static function seturl($nurl = null){//set the url } } - - static function is_role($role_name){ - if(self::is_connected()){ - if(isset(self::$user->group_name) && in_array($role_name, self::$user->group_name)){ - return true; - } - return false; - } - return false; - } - - static function is_available($action_name){ - if(self::is_connected()){ - $actions = self::$user->actions; - if(in_array($action_name, $actions)){ - return true; - } - return false; - } - return false; - } static function debug($v){ self::save('debug', debug($v)); @@ -253,9 +144,9 @@ function endWith($name, $test){ $name = str_replace('Pickle\\', '', $name); } if(endWith($name, 'Model')){ - require_once __DIR__."/../Models/$name.php"; + require_once ROOT."/src/Models/$name.php"; }elseif (endWith($name, 'Controller')){ - require_once __DIR__."/../Controllers/$name.php"; + require_once ROOT."/src/Controllers/$name.php"; } }); } diff --git a/core/App/Components/default/UserManagement.php b/core/App/Components/default/UserManagement.php new file mode 100644 index 0000000..0a8cfa3 --- /dev/null +++ b/core/App/Components/default/UserManagement.php @@ -0,0 +1,131 @@ +user_info([ + 'id' => $id + ]); + } + + + + static function is_connected(){//return true if the user exist, else false + + if (self::$user != false) { + + return true; + + } + + return false; + + } + + static function is_role($role_name){ + if (self::is_connected()) { + if (isset(self::$user->group_name) && in_array($role_name, self::$user->group_name)) { + return true; + } + return false; + } + return false; + } + + static function is_available($action_name){ + if (self::is_connected()) { + $actions = self::$user->actions; + if (in_array($action_name, $actions)) { + return true; + } + return false; + } + return false; + } + + + + static function saveusercookie($usr){ + setcookie('user', self::cookieinfo($usr), time() + 3600 * 24, '/', Config::$website, false, true); + } + + static function cookieinfo($user){ + return $user->id . '*' . sha1($user->name . $user->pass . $_SERVER['REMOTE_ADDR']); + } + + static function fromcookie(){ + if (isset($_COOKIE['user'])) { + $cookie = $_COOKIE['user']; + $cookie = explode('*', $cookie); + $usr = self::getuser($cookie[0]); + if ($usr == false) { + return false; + } + if (self::cookieinfo($usr) == implode('*', $cookie)) { + self::connect($usr); + self::saveusercookie($usr); + } + } + return false; + } + + + + static function getid(){//return the id of the user and false if we are not connected + + if (self::is_connected()) { + + $usr = self::$user; + return $usr->id; + + } + + return false; + + } + + static function getname(){//return the name of the user and false if we are not connected + + if (self::is_connected()) { + + $usr = self::$user; + return $usr->name; + + } + + return false; + + } + +} + + +?> \ No newline at end of file diff --git a/core/App/config.php b/core/App/config.php index f50ec01..55387c3 100644 --- a/core/App/config.php +++ b/core/App/config.php @@ -3,7 +3,15 @@ class AppConfig{ static $uses = [ - 'Session' => 'default/Session' + /** + Class(es) + */ + 'Session' => 'default/Session', + + /** + * Trait(s) + */ + 'UserManagement' => 'default/UserManagement' ]; static function load(){ diff --git a/core/Tools/config.php b/core/Tools/config.php index f03a2ed..c13897b 100644 --- a/core/Tools/config.php +++ b/core/Tools/config.php @@ -14,4 +14,6 @@ class Config{ static $devmail = null; + static $website = 'localhost/pickle'; + } \ No newline at end of file