Add UserGroupAdmin Class

This commit is contained in:
Yuan Chiu 2014-10-06 16:42:59 +08:00
parent f724d77820
commit f5b044e3b5
6 changed files with 467 additions and 72 deletions

View File

@ -68,7 +68,7 @@ class DBUser extends Database {
$nickName, $realName, $email, $memo){
// 檢查是否有支援所設定的DBMS
if($this->db_type == 'mysql') {
//if($this->db_type == 'mysql') {
//紀錄使用者帳號進資料庫
$sqlString = "INSERT INTO ".$this->table('User').
@ -90,13 +90,32 @@ class DBUser extends Database {
$query->bindParam(":email", $email);
$query->bindParam(":memo", $memo);
$query->execute();
}
else {
throw new Exception\DatabaseNoSupportException($this->db_type);
}
//}
//else {
// throw new Exception\DatabaseNoSupportException($this->db_type);
//}
}
/**
* 移除一位使用者
* @param string $uId 使用者名稱
*/
public function deleteUser($uId) {
//if($this->db_type == 'mysql') {
$sqlString = "DELETE FROM ".$this->table(self::FORM_USER).
" WHERE `UID` = :id ";
$query = $this->connDB->prepare($sqlString);
$query->bindParam(":id", $uId);
$query->execute();
//}
//else {
// throw new Exception\DatabaseNoSupportException($this->db_type);
//}
}
/**
* 查詢一位使用者帳號資料
*
@ -151,6 +170,7 @@ class DBUser extends Database {
$query->execute();
$queryResultAll = $query->fetchAll();
// 如果有查到一筆以上
if( count($queryResultAll) >= 1 ) {
$queryResult = $queryResultAll[0];
@ -171,6 +191,66 @@ class DBUser extends Database {
return $result;
}
// 若都沒查到的話
else {
return null;
}
}
/**
* 查詢所有的使用者帳號資料
*
* @return array 使用者資料陣列,格式為:
*
* array(
* array(
* 'user_id' => <帳號名稱>,
* 'password' => <密碼>,
* 'group_id' => <群組>,
* 'class_id' => <班級>,
* 'enable' => <啟用>,
* 'build_time' => <建立日期>,
* 'learnStyle_mode' => <偏好學習導引模式>,
* 'material_mode' => <偏好教材模式>,
* 'nickname' => <暱稱>,
* 'realname' => <真實姓名>,
* 'email' => <電子郵件地址>,
* 'memo' => <備註>
* )
* );
*
*/
public function queryAllUser() {
$sqlString = "SELECT * FROM ".$this->table('User');
$query = $this->connDB->prepare($sqlString);
$query->execute();
$queryResultAll = $query->fetchAll();
// 如果有查到一筆以上
if( count($queryResultAll) >= 1 ) {
// 製作回傳結果陣列
$result = array();
foreach($queryResultAll as $key => $thisResult) {
array_push($result,
array( 'user_id' => $thisResult['UID'],
'password' => $thisResult['UPassword'],
'group_id' => $thisResult['GID'],
'class_id' => $thisResult['CID'],
'enable' => $thisResult['UEnabled'],
'build_time' => $thisResult['UBuild_Time'],
'learnStyle_mode' => $thisResult['LMode'],
'material_mode' => $thisResult['MMode'],
'nickname' => $thisResult['UNickname'],
'realname' => $thisResult['UReal_Name'],
'email' => $thisResult['UEmail'],
'memo' => $thisResult['UMemo'])
);
}
return $result;
}
// 若都沒查到的話
else {
return null;
}
@ -219,23 +299,132 @@ class DBUser extends Database {
$query->execute();
}
// ========================================================================
/**
* 移除一位使用者
* @param string $uId 使用者名稱
* 插入群組資料
*
* @param string $gId 群組ID
* @param string $name 群組顯示名稱
* @param string $memo 備註
* @param string $auth_admin Server端管理權
* @param string $auth_clientAdmin Client端管理權
*/
public function deleteUser($uId) {
public function insertGroup($gId, $name, $memo, $auth_admin, $auth_clientAdmin) {
if($this->db_type == 'mysql') {
$sqlString = "DELETE FROM ".$this->table(self::FORM_USER).
" WHERE `UID` = :id ";
// 紀錄使用者帳號進資料庫
$sqlString = "INSERT INTO ".$this->table('AGroup').
" (`GID`, `GName`, `GMemo`, `GAuth_Admin`, `GAuth_ClientAdmin`)
VALUES ( :id , :name, :memo , :auth_admin , :auth_clientAdmin )";
$query = $this->connDB->prepare($sqlString);
$query->bindParam(":id", $uId);
$query->execute();
$query = $this->connDB->prepare($sqlString);
$query->bindParam(":id", $gId);
$query->bindParam(":name", $name);
$query->bindParam(":memo", $memo);
$query->bindParam(":auth_admin", $auth_admin);
$query->bindParam(":auth_clientAdmin", $auth_clientAdmin);
$query->execute();
}
/**
* 移除一個使用者群組
* @param string $gId
*/
public function deleteGroup($gId) {
$sqlString = "DELETE FROM ".$this->table('AGroup').
" WHERE `GID` = :id ";
$query = $this->connDB->prepare($sqlString);
$query->bindParam(":id", $gId);
$query->execute();
}
/**
* 查詢一個使用者群組資料
*
* @return array 使用者群組資料陣列,格式為:
*
* array( 'group_id' => <群組ID>,
* 'name' => <群組顯示名稱>,
* 'memo' => <備註>,
* 'auth_admin' => <Server端管理權>,
* 'auth_clientAdmin' => <Client端管理權>
* );
*
*/
public function queryGroup($gId) {
$sqlString = "SELECT * FROM ".$this->table('AGroup').
" WHERE `GID` = :gid";
$query = $this->connDB->prepare($sqlString);
$query->bindParam(':gid', $gId);
$query->execute();
$queryResultAll = $query->fetchAll();
// 如果有查到一筆以上
if( count($queryResultAll) >= 1 ) {
$thisResult = $queryResultAll[0];
// 製作回傳結果陣列
$result = array('group_id' => $thisResult['GID'],
'name' => $thisResult['GName'],
'memo' => $thisResult['GMemo'],
'auth_admin' => $thisResult['GAuth_Admin'],
'auth_clientAdmin' => $thisResult['GAuth_ClientAdmin']
);
return $result;
}
// 若都沒查到的話
else {
throw new Exception\DatabaseNoSupportException($this->db_type);
return null;
}
}
/**
* 查詢所有的使用者群組資料
*
* @return array 使用者群組資料陣列,格式為:
*
* array(
* array(
* 'group_id' => <群組ID>,
* 'name' => <群組顯示名稱>,
* 'memo' => <備註>,
* 'auth_admin' => <Server端管理權>,
* 'auth_clientAdmin' => <Client端管理權>
* )
* );
*
*/
public function queryAllGroup() {
$sqlString = "SELECT * FROM ".$this->table('AGroup');
$query = $this->connDB->prepare($sqlString);
$query->execute();
$queryResultAll = $query->fetchAll();
// 如果有查到一筆以上
if( count($queryResultAll) >= 1 ) {
// 製作回傳結果陣列
$result = array();
foreach($queryResultAll as $key => $thisResult) {
array_push($result,
array( 'group_id' => $thisResult['GID'],
'name' => $thisResult['GName'],
'memo' => $thisResult['GMemo'],
'auth_admin' => $thisResult['GAuth_Admin'],
'auth_clientAdmin' => $thisResult['GAuth_ClientAdmin'])
);
}
return $result;
}
// 若都沒查到的話
else {
return null;
}
}
}

View File

@ -95,3 +95,66 @@ class UserIdExistException extends UserException {
parent::__construct($userId, 'UserId: "'.$userId.'" is exist.');
}
}
// ============================================================================
/**
* 使用者群組例外
* @since 2.0.0
* @package UElearning
* @subpackage User
*/
abstract class GroupException extends \UnexpectedValueException {
/**
* 指定的使用者群組ID
* @type string
*/
private $groupId;
/**
* 使用者帳號例外
* @param string $groupId 輸入的使用者群組ID
* @param string $description 描述
*/
public function __construct($groupId, $description) {
$this->groupId = $groupId;
parent::__construct($description);
}
/**
* 取得輸入的資料庫系統名稱
* @return string 錯誤訊息內容
*/
public function getGroupId() {
return $this->groupId;
}
}
/**
* 已有重複的使用者群組ID
* @since 2.0.0
*/
class GroupIdExistException extends GroupException {
/**
* 已有重複的使用者名稱
* @param string $groupId 輸入的使用者群組ID
*/
public function __construct($groupId) {
parent::__construct($groupId, 'GroupId: "'.$groupId.'" is exist.');
}
}
/**
* 沒有找到此使用者群組ID
* @since 2.0.0
*/
class GroupNoFoundException extends GroupException {
/**
* 沒有找到此帳號
* @param string $groupId 輸入的使用者群組ID
*/
public function __construct($groupId) {
parent::__construct($groupId, 'Group: "'.$groupId.'" is no found.');
}
}

View File

@ -149,6 +149,16 @@ class UserAdmin {
/**
* 移除此使用者
*
* 範例:
*
* try {
* $userAdmin = new User\UserAdmin();
* $userAdmin->remove('eric');
* }
* catch (User\Exception\UserNoFoundException $e) {
* echo 'No Found user: ', $e->getUserId();
* }
*
* @param string $userName 帳號名稱
* @throw UElearning\User\Exception\UserNoFoundException
* @since 2.0.0
@ -168,7 +178,6 @@ class UserAdmin {
else {
throw new Exception\UserNoFoundException($userName);
}
}
}

View File

@ -5,6 +5,10 @@
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;
/**
* 管理使用者權限群組的操作
@ -22,8 +26,8 @@ class UserGroupAdmin {
* 建立使用者範例:
*
* try {
* $userAdmin = new User\UserAdmin();
* $userAdmin->create(
* $groupAdmin = new User\UserGroupAdmin();
* $groupAdmin->create(
* array( 'group_id' => 'student',
* 'name' => '學生',
* 'memo' => null,
@ -48,71 +52,48 @@ class UserGroupAdmin {
*/
public function create($groupArray) {
/*// 檢查必填項目有無填寫
if(isset($userInfoArray)) {
// 檢查有無填寫
if(isset($groupArray)) {
// 若無填寫
if( !isset($userInfoArray['user_id']) ||
!isset($userInfoArray['password']) ||
!isset($userInfoArray['group_id']) ) {
// 若必填項目無填寫
if( !isset($groupArray['group_id']) ) {
throw new UElearning\Exception\NoDataException();
}
// 若此id已存在
else if($this->isExist($userInfoArray['user_id'])) {
throw new Exception\UserIdExistException(
$userInfoArray['user_id'] );
else if( $this->isExist($groupArray['group_id']) ) {
throw new Exception\GroupIdExistException(
$groupArray['group_id'] );
}
// 沒有問題
else {
// 處理未帶入的資料
if( !isset($userInfoArray['class_id']) ){
$userInfoArray['class_id'] = null;
if( !isset($groupArray['name']) ){
$groupArray['name'] = null;
}
if( !isset($userInfoArray['enable']) ){
$userInfoArray['enable'] = true;
// 處理未帶入的資料
if( !isset($groupArray['memo']) ){
$groupArray['memo'] = null;
}
if( !isset($userInfoArray['learnStyle_mode']) ){
$userInfoArray['learnStyle_mode'] = null;
if( !isset($groupArray['auth_server_admin']) ){
$groupArray['auth_server_admin'] = false;
}
if( !isset($userInfoArray['material_mode']) ){
$userInfoArray['material_mode'] = null;
if( !isset($groupArray['auth_client_admin']) ){
$groupArray['auth_client_admin'] = false;
}
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;
}
// 進行密碼加密
$passUtil = new Util\Password();
$passwdEncrypted = $passUtil->encrypt( $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']
$db->insertGroup(
$groupArray['group_id'],
$groupArray['name'],
$groupArray['memo'],
$groupArray['auth_server_admin'],
$groupArray['auth_client_admin']
);
}
}
else throw Exception\NoDataException();*/
else throw Exception\NoDataException();
}
/**
@ -124,36 +105,47 @@ class UserGroupAdmin {
*/
public function isExist($group_id) {
/*$db = new Database\DBUser();
$info = $db->queryUser($userName);
$db = new Database\DBUser();
$info = $db->queryGroup($group_id);
if( $info != null ) return true;
else return false;*/
else return false;
}
/**
* 移除此群組
*
* 範例:
*
* try {
* $groupAdmin = new User\UserGroupAdmin();
* $groupAdmin->remove('test_student');
*
* }
* catch (User\Exception\GroupNoFoundException $e) {
* echo 'No Found group: ', $e->getGroupId();
* }
*
* @param string $group_id 群組ID
* @throw UElearning\User\Exception\GroupNoFoundException
* @since 2.0.0
*/
public function remove($group_id) {
/*
// 若有此使用者
if($this->isExist($userName)) {
if($this->isExist($group_id)) {
// TODO: 檢查所有關聯的資料,確認是否可以移除
// 移除資料庫中的使用者
$db = new Database\DBUser();
$db->deleteUser($userName);
$db->deleteGroup($group_id);
}
// 若沒有這位使用者
else {
throw new Exception\UserNoFoundException($userName);
throw new Exception\GroupNoFoundException($group_id);
}
*/
}
}

View File

@ -69,6 +69,17 @@ class DBUserTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($info['memo'], $memo);
}
/**
* 測試查詢所有使用者
*
* 僅測試是否能成功執行,不驗證結果
*/
public function testQueryAllUser(){
// 查詢使用者
$infoAll = $this->db->queryAllUser();
}
/**
* 測試更改使用者資料
*
@ -123,4 +134,52 @@ class DBUserTest extends \PHPUnit_Framework_TestCase
'sss', 'Yuan Chiu', 'chyuanesr@gmail.com', null)
);
}
// ========================================================================
/**
* 測試建立群組
*
* @dataProvider groupDataProvider
*/
public function testCreateGroup($gId, $name, $memo, $auth_admin, $auth_clientAdmin){
$this->db->insertGroup($gId, $name, $memo, $auth_admin, $auth_clientAdmin);
}
/**
* 測試查詢群組
*
* @dataProvider groupDataProvider
*/
public function testQueryGroup($gId, $name, $memo, $auth_admin, $auth_clientAdmin){
// 查詢使用者
$info = $this->db->queryGroup($gId);
// 比對資料是否吻合
$this->assertEquals($info['group_id'], $gId);
$this->assertEquals($info['name'], $name);
$this->assertEquals($info['memo'], $memo);
$this->assertEquals($info['auth_admin'], $auth_admin);
$this->assertEquals($info['auth_clientAdmin'], $auth_clientAdmin);
}
/**
* 測試移除使用者
*
* @dataProvider groupDataProvider
*/
public function testDeleteGroup($gId) {
$this->db->deleteGroup($gId);
}
/**
* 測試時要填的資料
*/
public function groupDataProvider(){
return array(
array('testG_a', '測試用群組a', null, '1', '0'),
array('testG_b', '測試用群組b', 'testhahaha Groups', '0', '1')
);
}
}

View File

@ -0,0 +1,83 @@
<?php
/**
* UserGroupAdminTest.php
*
* @package UElearning
* @author Yuan Chiu <chyuaner@gmail.com>
*/
namespace UElearning;
require_once UELEARNING_LIB_ROOT.'/User/UserGroupAdmin.php';
use UElearning\User\UserGroupAdmin;
class UserGroupAdminTest extends \PHPUnit_Framework_TestCase
{
/**
* 測試建立群組
*
* @dataProvider groupDataProvider
*/
public function testCreateGroup($gId, $name, $memo, $auth_admin, $auth_clientAdmin){
try {
$groupAdmin = new User\UserGroupAdmin();
$groupAdmin->create(
array( 'group_id' => $gId,
'name' => $name,
'memo' => $memo,
'auth_server_admin' => $auth_admin,
'auth_client_admin' => $auth_clientAdmin
));
}
// 若已有重複帳號名稱
catch (User\Exception\GroupIdExistException $e) {
throw $e;
}
}
/**
* 測試查詢群組
*
* @dataProvider groupDataProvider
*/
public function testCheckExist($gId){
$groupAdmin = new User\UserGroupAdmin();
// 比對資料是否吻合
$this->assertEquals($groupAdmin->isExist($gId), true);
}
/**
* 測試移除使用者
*
* @dataProvider groupDataProvider
*/
public function testDeleteGroup($gId) {
try {
$groupAdmin = new User\UserGroupAdmin();
$groupAdmin->remove($gId);
$this->assertEquals($groupAdmin->isExist($gId), false);
}
catch (User\Exception\GroupNoFoundException $e) {
throw $e;
}
}
/**
* 測試時要填的資料
*/
public function groupDataProvider(){
return array(
array('testG_a', '測試用群組a', null, '1', '0'),
array('testG_b', '測試用群組b', 'testhahaha Groups', '0', '1')
);
}
}