UserAdmin所有相關的內部函式庫 (含資料庫修復)

This commit is contained in:
Yuan Chiu 2014-09-02 23:51:13 +08:00
parent 5d92668456
commit 7785baa793
10 changed files with 311 additions and 36 deletions

View File

@ -8,7 +8,7 @@
namespace UElearning\Database; namespace UElearning\Database;
require_once UELEARNING_LIB_ROOT.'/Database/Database.php'; require_once UELEARNING_LIB_ROOT.'/Database/Database.php';
require_once UELEARNING_LIB_ROOT.'/Database/Exceptions.php'; require_once UELEARNING_LIB_ROOT.'/Database/Exception.php';
/** /**
* 資料庫整體管理 * 資料庫整體管理

View File

@ -8,7 +8,7 @@
namespace UElearning\Database; namespace UElearning\Database;
require_once UELEARNING_LIB_ROOT.'/Database/Database.php'; 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') { 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 = $this->connDB->prepare($sqlString);
$query->bindParam(":id", $uId); $query->bindParam(":id", $uId);
@ -76,17 +80,37 @@ class DBUser extends Database {
*/ */
public function queryUser($uId) { 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 = $this->connDB->prepare($sqlString);
$query->bindParam(':uid',$this->thisUID); $query->bindParam(':uid', $uId);
$query->execute(); $query->execute();
$result = $query->fetchAll(); $queryResultAll = $query->fetchAll();
$this->infoArray = $result; if( count($queryResultAll) >= 1 ) {
return $this->infoArray; $queryResult = $queryResultAll[0];
// TODO unTested $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 使用者名稱 * @param string $uId 使用者名稱
*/ */
public function deleteUser($uId) { public function deleteUser($uId) {
if($this->db_type == 'mysql') { 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 = $this->connDB->prepare($sqlString);
$query->bindParam(":id", $uId); $query->bindParam(":id", $uId);
$query->execute(); $query->execute();

View File

@ -9,7 +9,7 @@
namespace UElearning\Database; namespace UElearning\Database;
require_once UELEARNING_LIB_ROOT.'/Database/MySQLDB.php'; 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; use UElearning\Database\Exception;
/** /**

52
htdocs/lib/Exception.php Normal file
View File

@ -0,0 +1,52 @@
<?php
/**
* 通用例外類別檔案
*/
namespace UElearning\Exception;
/**
* 沒填入資料例外
* @since 2.0.0
* @package UElearning
*/
class NoDataException extends \UnexpectedValueException {
/**
* 指定的使用者名稱
* @type string|array 欄位名稱
*/
private $fieldName;
/**
* 未填資料例外
* @param string|array $fieldName 欄位名稱
*/
public function __construct() {
if(func_num_args() == 1){
$args = func_get_args();
$fieldName = $args[0];
$this->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;
}
}

View File

@ -49,7 +49,7 @@ class UserNoFoundException extends UserException {
* @param string $userId 輸入的使用者名稱 * @param string $userId 輸入的使用者名稱
*/ */
public function __construct($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 輸入的使用者名稱 * @param string $userId 輸入的使用者名稱
*/ */
public function __construct($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 輸入的使用者名稱 * @param string $userId 輸入的使用者名稱
*/ */
public function __construct($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 輸入的使用者名稱 * @param string $userId 輸入的使用者名稱
*/ */
public function __construct($userId) { public function __construct($userId) {
parent::__construct($userId, 'UserId: "'.$this->type.'" is exist.'); parent::__construct($userId, 'UserId: "'.$userId.'" is exist.');
} }
} }

View File

@ -5,6 +5,11 @@
namespace UElearning\User; 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,12 +23,32 @@ 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 使用者資訊陣列,格式為: * @param array $userInfoArray 使用者資訊陣列,格式為:
* array( 'userId' => 'root', * array( 'user_id' => 'root',
* 'password' => 'pass123', * 'password' => 'pass123',
* 'password_encrypt' => null, // (optional) 預設為null * 'password_encrypt' => null, // (optional) 預設為null
* 'groupId' => 'user', * 'password_encrypted' => null, // (optional) 預設為false
* 'classId' => '5-2', // (optional) * 'group_id' => 'user',
* 'class_id' => '5-2', // (optional)
* 'enable' => true, // (optional) 預設為true * 'enable' => true, // (optional) 預設為true
* 'learnStyle_mode' => 'harf-line-learn', // (optional) * 'learnStyle_mode' => 'harf-line-learn', // (optional)
* 'material_mode' => 1, // (optional) * 'material_mode' => 1, // (optional)
@ -34,7 +59,75 @@ class UserAdmin {
* @since 2.0.0 * @since 2.0.0
*/ */
public function create($userInfoArray) { 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 * @since 2.0.0
*/ */
public function isExist($userName) { public function isExist($userName) {
// TODO: Fill code in
$db = new Database\DBUser();
$info = $db->queryUser($userName);
if( $info != null ) return true;
else return false;
} }
} }

View File

@ -8,7 +8,7 @@
namespace UElearning; namespace UElearning;
require_once UELEARNING_LIB_ROOT.'/Database/DBUser.php'; 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\DBUser;
use UElearning\Database\Exception; use UElearning\Database\Exception;
@ -43,6 +43,32 @@ class DBUserTest extends \PHPUnit_Framework_TestCase
$nickName, $realName, $email, $memo); $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(){ public function userDataProvider(){
return array( return array(
array('yuan_unittest', 'pass123', 'admin', null, true, 'harf-line-learn', 1, '元兒~', 'Yuan Chiu', 'chyuaner@gmail.com', null), array('yuan_unittest', 'pass123', 'admin', null, true,
array('eee_unittest', 'qqqssss', 'admin', null, 1, 'harf-line-learn', '1', 'sss', 'Yuan Chiu', 'chyuanesr@gmail.com', null) '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)
); );
} }
} }

View File

@ -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 { try {
// 建立資料庫管理物件 // 建立資料庫管理物件
$dbAdmin = new UserAdmin(); $userAdmin = new UserAdmin();
// TODO: 建立使用者 // 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不被支援 則丟出例外 // 若設定的DBMS不被支援 則丟出例外
catch (Database\Exception\DatabaseNoSupportException $e) { catch (Database\Exception\DatabaseNoSupportException $e) {
throw $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)
);
}
} }