From 07d17b0a4d94455ff15ec9a3b1833af2d33757ec Mon Sep 17 00:00:00 2001 From: Gnieark Date: Thu, 18 Apr 2019 19:03:35 +0200 Subject: [PATCH] plip --- User.php | 2 -- User_Manager.php | 35 ++++++++++++++++++++++++++++++----- User_Sql.php | 25 +++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 7 deletions(-) diff --git a/User.php b/User.php index 2fb956a..88de0ba 100644 --- a/User.php +++ b/User.php @@ -11,8 +11,6 @@ class User protected $db; - - public function get_id() { if($this->is_connected){ diff --git a/User_Manager.php b/User_Manager.php index cd26c99..3ce508c 100644 --- a/User_Manager.php +++ b/User_Manager.php @@ -6,6 +6,8 @@ class User_Manager private static $table_users = 'users'; private static $table_groups = 'groups'; + //could append 'ldap', 'cas': + private static $available_auth_methods = array('local'); const QUERY_CREATE_TABLE_USERS = " CREATE TABLE %table_users% ( @@ -55,7 +57,15 @@ class User_Manager "; - + /* + * Erase the list of avaliable methods + * @ input methods: Array (non associative) + * return true + */ + public static function set_available_auth_methods($methods) + { + self::$available_auth_methods = $methods; + } public static function create_local_tables(PDO $db) { $searched = array('%table_users%','%table_groups%'); @@ -76,12 +86,27 @@ class User_Manager } } + public function authentificate($db,$login, $password){ - $user = new User_Sql($db); - if($user->authentificate($login,$password)){ - return $user; + + foreach(self::$available_auth_methods as $method){ + + switch($method) + { + case "local": + case "sql'": + $user = new User_Sql($db); + if($user->authentificate($login,$password)){ + return $user; + } + break; + default: + return false; + } } - return false; + $user = new User(); + + return $user; } } \ No newline at end of file diff --git a/User_Sql.php b/User_Sql.php index 8300d41..c9cdf8c 100644 --- a/User_Sql.php +++ b/User_Sql.php @@ -1,6 +1,31 @@ prepare( + "INSERT INTO " . $table_users . " + (login, display_name, auth_method,password,admin,active) + VALUES + (:login, :display_name, 'local', :password, :admin, :active)" + ); + + $stmt->bindParam(':login', $login); + $stmt->bindParam(':display_name', $display_name); + $stmt->bindParam(':password',$hashed_password); + $stmt->bindParam(':admin', $adminInt); + $stmt->bindParam(':active', $activeInt); + + $hashed_password = password_hash($password, PASSWORD_BCRYPT); + $adminInt = $admin? 1 : 0; + $activeInt = $activeInt? 1 : 0; + $stmt->execute(); + + return $db->lastInsertId(); + } + public function authentificate($login,$password) {