From 7785baa7931592ccf7989431be30f763cac7ec6d Mon Sep 17 00:00:00 2001 From: Yuan Chiu Date: Tue, 2 Sep 2014 23:51:13 +0800 Subject: [PATCH] =?UTF-8?q?UserAdmin=E6=89=80=E6=9C=89=E7=9B=B8=E9=97=9C?= =?UTF-8?q?=E7=9A=84=E5=85=A7=E9=83=A8=E5=87=BD=E5=BC=8F=E5=BA=AB=20(?= =?UTF-8?q?=E5=90=AB=E8=B3=87=E6=96=99=E5=BA=AB=E4=BF=AE=E5=BE=A9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Config/{Exceptions.php => Exception.php} | 0 htdocs/lib/Database/DBAdmin.php | 2 +- htdocs/lib/Database/DBUser.php | 49 +++++-- htdocs/lib/Database/Database.php | 2 +- .../{Exceptions.php => Exception.php} | 0 htdocs/lib/Exception.php | 52 ++++++++ .../User/{Exceptions.php => Exception.php} | 8 +- htdocs/lib/User/UserAdmin.php | 126 ++++++++++++++++-- tests/Database/DBUserTest.php | 37 ++++- tests/User/UserAdminTest.php | 71 +++++++++- 10 files changed, 311 insertions(+), 36 deletions(-) rename htdocs/lib/Config/{Exceptions.php => Exception.php} (100%) rename htdocs/lib/Database/{Exceptions.php => Exception.php} (100%) create mode 100644 htdocs/lib/Exception.php rename htdocs/lib/User/{Exceptions.php => Exception.php} (86%) diff --git a/htdocs/lib/Config/Exceptions.php b/htdocs/lib/Config/Exception.php similarity index 100% rename from htdocs/lib/Config/Exceptions.php rename to htdocs/lib/Config/Exception.php diff --git a/htdocs/lib/Database/DBAdmin.php b/htdocs/lib/Database/DBAdmin.php index ca5bf50..a010c3a 100644 --- a/htdocs/lib/Database/DBAdmin.php +++ b/htdocs/lib/Database/DBAdmin.php @@ -8,7 +8,7 @@ namespace UElearning\Database; require_once UELEARNING_LIB_ROOT.'/Database/Database.php'; -require_once UELEARNING_LIB_ROOT.'/Database/Exceptions.php'; +require_once UELEARNING_LIB_ROOT.'/Database/Exception.php'; /** * 資料庫整體管理 diff --git a/htdocs/lib/Database/DBUser.php b/htdocs/lib/Database/DBUser.php index fb6e693..0db933a 100644 --- a/htdocs/lib/Database/DBUser.php +++ b/htdocs/lib/Database/DBUser.php @@ -8,7 +8,7 @@ namespace UElearning\Database; require_once UELEARNING_LIB_ROOT.'/Database/Database.php'; -require_once UELEARNING_LIB_ROOT.'/Database/Exceptions.php'; +require_once UELEARNING_LIB_ROOT.'/Database/Exception.php'; /** * 使用者帳號資料表 @@ -47,7 +47,11 @@ class DBUser extends Database { if($this->db_type == 'mysql') { //紀錄使用者帳號進資料庫 - $sqlString = "INSERT INTO ".$this->table(self::FORM_USER). "(`UID`, `UPassword`, `GID`, `CID`, `UEnabled`, `UBuild_Time`, `LMode`, `MMode`, `UNickname`, `UReal_Name`, `UEmail`, `UMemo`) VALUES ( :id , :passwd, :gid , :cid , :enable , NOW() , :lmode , :mmode , :nickname , :realname , :email , :memo )"; + $sqlString = "INSERT INTO ".$this->table('User'). + " (`UID`, `UPassword`, `GID`, `CID`, `UEnabled`, `UBuild_Time`, + `LMode`, `MMode`, `UNickname`, `UReal_Name`, `UEmail`, `UMemo`) + VALUES ( :id , :passwd, :gid , :cid , :enable , NOW() , + :lmode , :mmode , :nickname , :realname , :email , :memo )"; $query = $this->connDB->prepare($sqlString); $query->bindParam(":id", $uId); @@ -76,17 +80,37 @@ class DBUser extends Database { */ public function queryUser($uId) { - $sqlString = "SELECT * FROM ".$db->table('User')." WHERE `UID` = :uid"; + $sqlString = "SELECT * FROM ".$this->table('User'). + " WHERE `UID` = :uid"; - $query = $this->prepare($sqlString); - $query->bindParam(':uid',$this->thisUID); + $query = $this->connDB->prepare($sqlString); + $query->bindParam(':uid', $uId); $query->execute(); - $result = $query->fetchAll(); - $this->infoArray = $result; - return $this->infoArray; - - // TODO unTested + $queryResultAll = $query->fetchAll(); + if( count($queryResultAll) >= 1 ) { + $queryResult = $queryResultAll[0]; + + $result = array( + 'user_id' => $queryResult['UID'], + 'password' => $queryResult['UPassword'], + 'group_id' => $queryResult['GID'], + 'class_id' => $queryResult['CID'], + 'enable' => $queryResult['UEnabled'], + 'build_time' => $queryResult['UBuild_Time'], + 'learnStyle_mode' => $queryResult['LMode'], + 'material_mode' => $queryResult['MMode'], + 'nickname' => $queryResult['UNickname'], + 'realname' => $queryResult['UReal_Name'], + 'email' => $queryResult['UEmail'], + 'memo' => $queryResult['UMemo'] + ); + + return $result; + } + else { + return null; + } } /** @@ -94,8 +118,11 @@ class DBUser extends Database { * @param string $uId 使用者名稱 */ public function deleteUser($uId) { + if($this->db_type == 'mysql') { - $sqlString = "DELETE FROM ".$this->table(self::FORM_USER). " WHERE `UID` = :id "; + $sqlString = "DELETE FROM ".$this->table(self::FORM_USER). + " WHERE `UID` = :id "; + $query = $this->connDB->prepare($sqlString); $query->bindParam(":id", $uId); $query->execute(); diff --git a/htdocs/lib/Database/Database.php b/htdocs/lib/Database/Database.php index f1bb2c7..ac5de92 100644 --- a/htdocs/lib/Database/Database.php +++ b/htdocs/lib/Database/Database.php @@ -9,7 +9,7 @@ namespace UElearning\Database; require_once UELEARNING_LIB_ROOT.'/Database/MySQLDB.php'; -require_once UELEARNING_LIB_ROOT.'/Database/Exceptions.php'; +require_once UELEARNING_LIB_ROOT.'/Database/Exception.php'; use UElearning\Database\Exception; /** diff --git a/htdocs/lib/Database/Exceptions.php b/htdocs/lib/Database/Exception.php similarity index 100% rename from htdocs/lib/Database/Exceptions.php rename to htdocs/lib/Database/Exception.php diff --git a/htdocs/lib/Exception.php b/htdocs/lib/Exception.php new file mode 100644 index 0000000..aace491 --- /dev/null +++ b/htdocs/lib/Exception.php @@ -0,0 +1,52 @@ +fieldName = $fieldName; + parent::__construct(); + } + else { + $this->fieldName = array(); + } + } + + /** + * 新增一項未輸入的欄位名稱 + */ + public function addFieldName($fieldName) { + $this->fieldName += array($fieldName); + } + + /** + * 取得未輸入的欄位名稱 + * @return string|array 欄位名稱 + */ + public function getFieldName() { + return $this->fieldName; + } +} diff --git a/htdocs/lib/User/Exceptions.php b/htdocs/lib/User/Exception.php similarity index 86% rename from htdocs/lib/User/Exceptions.php rename to htdocs/lib/User/Exception.php index 7cd1119..2d9b44e 100644 --- a/htdocs/lib/User/Exceptions.php +++ b/htdocs/lib/User/Exception.php @@ -49,7 +49,7 @@ class UserNoFoundException extends UserException { * @param string $userId 輸入的使用者名稱 */ public function __construct($userId) { - parent::__construct($userId, 'User: "'.$this->type.'" is no found.'); + parent::__construct($userId, 'User: "'.$userId.'" is no found.'); } } @@ -63,7 +63,7 @@ class UserPasswordErrException extends UserException { * @param string $userId 輸入的使用者名稱 */ public function __construct($userId) { - parent::__construct($userId, 'User: "'.$this->type.'" password is wrong.'); + parent::__construct($userId, 'User: "'.$userId.'" password is wrong.'); } } @@ -77,7 +77,7 @@ class UserNoActivatedException extends UserException { * @param string $userId 輸入的使用者名稱 */ public function __construct($userId) { - parent::__construct($userId, 'User: "'.$this->type.'" is no activated.'); + parent::__construct($userId, 'User: "'.$userId.'" is no activated.'); } } @@ -92,6 +92,6 @@ class UserIdExistException extends UserException { * @param string $userId 輸入的使用者名稱 */ public function __construct($userId) { - parent::__construct($userId, 'UserId: "'.$this->type.'" is exist.'); + parent::__construct($userId, 'UserId: "'.$userId.'" is exist.'); } } diff --git a/htdocs/lib/User/UserAdmin.php b/htdocs/lib/User/UserAdmin.php index ae63d97..db887a1 100644 --- a/htdocs/lib/User/UserAdmin.php +++ b/htdocs/lib/User/UserAdmin.php @@ -5,6 +5,11 @@ namespace UElearning\User; +require_once UELEARNING_LIB_ROOT.'/Database/DBUser.php'; +require_once UELEARNING_LIB_ROOT.'/User/Exception.php'; +require_once UELEARNING_LIB_ROOT.'/Exception.php'; +use UElearning\Database; + /** * 管理使用者的操作 * @@ -18,23 +23,111 @@ class UserAdmin { /** * 建立使用者 * + * 建立使用者範例: + * + * try { + * $userAdmin = new User\UserAdmin(); + * $userAdmin->create( + * array( 'user_id' => 'eric', + * 'password' => 'pass123', + * 'group_id' => 'admin', + * 'enable' => true, + * 'nickname' => '艾瑞克', + * 'email' => 'eric@example.com' + * )); + * + * } + * // 若已有重複帳號名稱 + * catch (User\Exception\UserIdExistException $e) { + * echo 'Is exist user: ', $e->getUserId(); + * } + * * @param array $userInfoArray 使用者資訊陣列,格式為: - * array( 'userId' => 'root', - * 'password' => 'pass123', - * 'password_encrypt' => null, // (optional) 預設為null - * 'groupId' => 'user', - * 'classId' => '5-2', // (optional) - * 'enable' => true, // (optional) 預設為true - * 'learnStyle_mode' => 'harf-line-learn', // (optional) - * 'material_mode' => 1, // (optional) - * 'nickname' => 'eric', // (optional) - * 'realname' => 'Eric Chiu', // (optional) - * 'email' => 'eric@example.tw', // (optional) - * 'memo' => '' ) // (optional) + * array( 'user_id' => 'root', + * 'password' => 'pass123', + * 'password_encrypt' => null, // (optional) 預設為null + * 'password_encrypted' => null, // (optional) 預設為false + * 'group_id' => 'user', + * 'class_id' => '5-2', // (optional) + * 'enable' => true, // (optional) 預設為true + * 'learnStyle_mode' => 'harf-line-learn', // (optional) + * 'material_mode' => 1, // (optional) + * 'nickname' => 'eric', // (optional) + * 'realname' => 'Eric Chiu', // (optional) + * 'email' => 'eric@example.tw', // (optional) + * 'memo' => '' ) // (optional) * @since 2.0.0 */ public function create($userInfoArray) { - // TODO: Fill code in + + // 檢查必填項目有無填寫 + if(isset($userInfoArray)) { + + // 若無填寫 + if( !isset($userInfoArray['user_id']) || + !isset($userInfoArray['password']) || + !isset($userInfoArray['group_id']) ) { + throw new UElearning\Exception\NoDataException(); + } + // 若此id已存在 + else if($this->isExist($userInfoArray['user_id'])) { + throw new Exception\UserIdExistException( + $userInfoArray['user_id'] ); + } + // 沒有問題 + else { + + // 處理未帶入的資料 + if( !isset($userInfoArray['class_id']) ){ + $userInfoArray['class_id'] = null; + } + if( !isset($userInfoArray['enable']) ){ + $userInfoArray['enable'] = true; + } + if( !isset($userInfoArray['learnStyle_mode']) ){ + $userInfoArray['learnStyle_mode'] = null; + } + if( !isset($userInfoArray['material_mode']) ){ + $userInfoArray['material_mode'] = null; + } + if( !isset($userInfoArray['nickname']) ){ + $userInfoArray['nickname'] = null; + } + if( !isset($userInfoArray['realname']) ){ + $userInfoArray['realname'] = null; + } + if( !isset($userInfoArray['email']) ){ + $userInfoArray['email'] = null; + } + if( !isset($userInfoArray['memo']) ){ + $userInfoArray['memo'] = null; + } + + // 進行密碼加密 + /*if( isset($userInfoArray['userId']) ) { + // TODO: 密碼加密 + }*/ + // 加密後的密碼 + $passwdEncrypted = $userInfoArray['password']; + + // 新增一筆使用者資料進資料庫 + $db = new Database\DBUser(); + $db->insertUser( + $userInfoArray['user_id'], + $passwdEncrypted, + $userInfoArray['group_id'], + $userInfoArray['class_id'], + $userInfoArray['enable'], + $userInfoArray['learnStyle_mode'], + $userInfoArray['material_mode'], + $userInfoArray['nickname'], + $userInfoArray['realname'], + $userInfoArray['email'], + $userInfoArray['memo'] + ); + } + } + else throw Exception\NoDataException(); } /** @@ -45,7 +138,12 @@ class UserAdmin { * @since 2.0.0 */ public function isExist($userName) { - // TODO: Fill code in + + $db = new Database\DBUser(); + $info = $db->queryUser($userName); + + if( $info != null ) return true; + else return false; } } \ No newline at end of file diff --git a/tests/Database/DBUserTest.php b/tests/Database/DBUserTest.php index 9b6ef1b..86a89ac 100644 --- a/tests/Database/DBUserTest.php +++ b/tests/Database/DBUserTest.php @@ -8,7 +8,7 @@ namespace UElearning; require_once UELEARNING_LIB_ROOT.'/Database/DBUser.php'; -require_once UELEARNING_LIB_ROOT.'/Database/Exceptions.php'; +require_once UELEARNING_LIB_ROOT.'/Database/Exception.php'; use UElearning\Database\DBUser; use UElearning\Database\Exception; @@ -43,6 +43,32 @@ class DBUserTest extends \PHPUnit_Framework_TestCase $nickName, $realName, $email, $memo); } + /** + * 測試查詢使用者 + * + * @dataProvider userDataProvider + */ + public function testQueryUser($uId, $uPassword, $gId, $cId, $enable, + $l_mode, $m_mode, + $nickName, $realName, $email, $memo){ + + // 查詢使用者 + $info = $this->db->queryUser($uId); + + // 比對資料是否吻合 + $this->assertEquals($info['user_id'], $uId); + $this->assertEquals($info['password'], $uPassword); + $this->assertEquals($info['group_id'], $gId); + $this->assertEquals($info['class_id'], $cId); + $this->assertEquals($info['enable'], $enable); + $this->assertEquals($info['learnStyle_mode'], $l_mode); + $this->assertEquals($info['material_mode'], $m_mode); + $this->assertEquals($info['nickname'], $nickName); + $this->assertEquals($info['realname'], $realName); + $this->assertEquals($info['email'], $email); + $this->assertEquals($info['memo'], $memo); + } + /** * 測試移除使用者 * @@ -57,8 +83,13 @@ class DBUserTest extends \PHPUnit_Framework_TestCase */ public function userDataProvider(){ return array( - array('yuan_unittest', 'pass123', 'admin', null, true, 'harf-line-learn', 1, '元兒~', 'Yuan Chiu', 'chyuaner@gmail.com', null), - array('eee_unittest', 'qqqssss', 'admin', null, 1, 'harf-line-learn', '1', 'sss', 'Yuan Chiu', 'chyuanesr@gmail.com', null) + array('yuan_unittest', 'pass123', 'admin', null, true, + 'harf-line-learn', 1, + '元兒~', 'Yuan Chiu', 'chyuaner@gmail.com', null), + + array('eee_unittest', 'qqqssss', 'admin', null, 1, + 'harf-line-learn', '1', + 'sss', 'Yuan Chiu', 'chyuanesr@gmail.com', null) ); } } \ No newline at end of file diff --git a/tests/User/UserAdminTest.php b/tests/User/UserAdminTest.php index 1beed5a..873c4ee 100644 --- a/tests/User/UserAdminTest.php +++ b/tests/User/UserAdminTest.php @@ -15,19 +15,86 @@ class UserAdminTest extends \PHPUnit_Framework_TestCase /** * 測試安裝初始化資料庫 + * + * @dataProvider userDataProvider */ - public function testCreateUser() + public function testCreateUser($uId, $uPassword, $gId, $cId, $enable, + $l_mode, $m_mode, + $nickName, $realName, $email, $memo) { try { // 建立資料庫管理物件 - $dbAdmin = new UserAdmin(); + $userAdmin = new UserAdmin(); // TODO: 建立使用者 + $userAdmin->create( + array( 'user_id' => $uId, + 'password' => $uPassword, + //'password_encrypt' => null, + //'password_encrypted' => null, + 'group_id' => $gId, + 'class_id' => $cId, + 'enable' => $enable, + 'learnStyle_mode' => $l_mode, + 'material_mode' => $m_mode, + 'nickname' => $nickName, + 'realname' => $realName, + 'email' => $email, + 'memo' => $memo + )); } // 若設定的DBMS不被支援 則丟出例外 catch (Database\Exception\DatabaseNoSupportException $e) { throw $e; } } + + /** + * 檢查是否已確實建立 + * + * @dataProvider userDataProvider + */ + public function testCheckExist($uId) + { + // 建立資料庫管理物件 + $userAdmin = new UserAdmin(); + + // 檢查是否已確實建立 + $this->assertEquals($userAdmin->isExist($uId), true); + } + + /** + * 刪除建立的帳號(恢復原狀用) + * + * @dataProvider userDataProvider + */ + public function testDeleteUser($uId) + { + // TODO: 待UserAdmin的移除功能寫好後,請改用UserAdmin + require_once UELEARNING_LIB_ROOT.'/Database/DBUser.php'; + + // 建立資料庫管理物件 + $db = new Database\DBUser(); + $db->deleteUser($uId); + + } + + + + /** + * 測試時要填的資料 + */ + public function userDataProvider(){ + return array( + array('yuan_unittest', 'pass123', 'admin', null, true, + 'harf-line-learn', 1, + '元兒~', 'Yuan Chiu', 'chyuaner@gmail.com', null), + + array('eee_unittest', 'qqqssss', 'admin', null, 1, + 'harf-line-learn', '1', + 'sss', 'Yuan Chiu', 'chyuanesr@gmail.com', null) + ); + } + } \ No newline at end of file