Create User Class

This commit is contained in:
Yuan Chiu 2014-10-02 18:19:32 +08:00
parent 2888922baf
commit c77746bbf6
7 changed files with 567 additions and 24 deletions

View File

@ -1,4 +1,11 @@
<?php
/**
* 除錯模式-管理
*
* 警告: 若網站要正式上線請務必設定為false否則會有嚴重的安全問題
*/
define('DEBUG_ADMIN', false);
// 資料庫 =====================================================================
/**
* 資料庫類型

View File

@ -27,6 +27,30 @@ class DBUser extends Database {
/**
* 新增一個使用者
*
* 範例:
*
* require_once __DIR__.'/../config.php';
* require_once UELEARNING_LIB_ROOT.'/Database/DBUser.php';
* use UElearning\Database;
*
* try {
* $db = new Database\DBUser();
*
* $db->insertUser('eric', 'passwd', 'user', null, 1, 'harf-line-learn', '1', '偉人', 'Eric Chiou', 'eric@example.com', null);
*
* echo 'Finish';
* }
*
*
* // 若設定的DBMS不被支援 則丟出例外
* catch (Database\Exception\DatabaseNoSupportException $e) {
* echo 'No Support in ', $e->getType();
* } catch (Exception $e) {
* echo 'Caught other exception: ', $e->getMessage();
* echo '<h2>'. $e->getCode() .'</h2>';
* }
*
* @param string $uId 使用者名稱
* @param string $password 密碼
* @param string $gId 群組
@ -75,8 +99,47 @@ class DBUser extends Database {
/**
* 查詢一位使用者帳號資料
*
*
* 範例:
*
* require_once __DIR__.'/../config.php';
* require_once UELEARNING_LIB_ROOT.'/Database/DBUser.php';
* use UElearning\Database;
*
* try {
* $db = new Database\DBUser();
*
* $userInfo = $db->queryUser('yuan');
* echo '<pre>'; print_r($userInfo); echo '</pre>';
* }
*
*
* // 若設定的DBMS不被支援 則丟出例外
* catch (Database\Exception\DatabaseNoSupportException $e) {
* echo 'No Support in ', $e->getType();
* } catch (Exception $e) {
* echo 'Caught other exception: ', $e->getMessage();
* echo '<h2>'. $e->getCode() .'</h2>';
* }
*
* @param string $uId 使用者名稱
* @return array 使用者資料 (TODO 格式待補)
* @return array 使用者資料陣列,格式為:
* array(
* 'user_id' => <帳號名稱>,
* 'password' => <密碼>,
* 'group_id' => <群組>,
* 'class_id' => <班級>,
* 'enable' => <啟用>,
* 'build_time' => <建立日期>,
* 'learnStyle_mode' => <偏好學習導引模式>,
* 'material_mode' => <偏好教材模式>,
* 'nickname' => <暱稱>,
* 'realname' => <真實姓名>,
* 'email' => <電子郵件地址>,
* 'memo' => <備註>
* );
*
*/
public function queryUser($uId) {
@ -113,6 +176,49 @@ class DBUser extends Database {
}
}
/**
* 修改一位使用者的資料內容
*
* 範例:
*
* $db = new Database\DBUser();
* $db->changeUserData('yuan', 'memo', 'hahaha');
*
* @param string $uId 使用者名稱
* @param string $field 欄位名稱
* @param string $value 內容
*/
public function changeUserData($uId, $field, $value) {
// UPDATE `UElearning`.`chu__User` SET `UMemo` = '測試者' WHERE `chu__User`.`UID` = 'yuan';
$sqlField = null;
switch($field) {
case 'user_id': $sqlField = 'UID'; break;
case 'password': $sqlField = 'UPassword'; break;
case 'group_id': $sqlField = 'GID'; break;
case 'class_id': $sqlField = 'CID'; break;
case 'enable': $sqlField = 'UEnabled'; break;
case 'build_time': $sqlField = 'UBuild_Time'; break;
case 'learnStyle_mode': $sqlField = 'LMode'; break;
case 'material_mode': $sqlField = 'MMode'; break;
case 'nickname': $sqlField = 'UNickname'; break;
case 'realname': $sqlField = 'UReal_Name'; break;
case 'email': $sqlField = 'UEmail'; break;
case 'memo': $sqlField = 'UMemo'; break;
default: $sqlField = $field; break;
}
$sqlString = "UPDATE ".$this->table('User').
" SET `".$sqlField."` = :value".
" WHERE `UID` = :uid";
$query = $this->connDB->prepare($sqlString);
$query->bindParam(':uid', $uId);
$query->bindParam(':value', $value);
$query->execute();
}
/**
* 移除一位使用者
* @param string $uId 使用者名稱

View File

@ -5,4 +5,430 @@
namespace UElearning\User;
// TODO: write this code
require_once UELEARNING_LIB_ROOT.'/Database/DBUser.php';
require_once UELEARNING_LIB_ROOT.'/User/Exception.php';
require_once UELEARNING_LIB_ROOT.'/Exception.php';
require_once UELEARNING_LIB_ROOT.'/Util/Password.php';
use UElearning\Database;
use UElearning\Util;
/**
* 使用者處理專用類別
*
* 一個物件即代表這一位使用者
*
* 建立此物件範例:
*
* require_once __DIR__.'/../config.php';
* require_once UELEARNING_LIB_ROOT.'/User/User.php';
* use UElearning\User;
*
* try {
* $user = new User\User('yuan');
* // TODO: 在這邊寫下要針對這位使用者作什麼事?
* }
* catch (User\Exception\UserNoFoundException $e) {
* echo 'No Found user: '. $e->getUserId();
* }
*
* @version 2.0.0
* @package UElearning
* @subpackage User
*/
class User {
/**
* 使用者ID
* @type string
*/
protected $uId;
// ------------------------------------------------------------------------
/**
* 查詢到此帳號的所有資訊的結果
*
* $this->getQuery() 抓取資料表中所有資訊,並放在此陣列裡
* @type array
*/
protected $queryResultArray;
/**
* 從資料庫取得此帳號查詢
*
* @throw UElearning\User\Exception\UserNoFoundException
* @since 2.0.0
*/
protected function getQuery(){
// 從資料庫查詢使用者
$db = new Database\DBUser();
$userInfo = $db->queryUser($this->uId);
// 判斷有沒有這位使用者
if( $userInfo != null ) {
$this->queryResultArray = $userInfo;
}
else throw new Exception\UserNoFoundException($this->uId);
}
/**
* 從資料庫更新此帳號設定
*
* @since 2.0.0
*/
protected function setUpdate($field, $value){
/// 將新設定寫進資料庫裡
$db = new Database\DBUser();
$db->changeUserData($this->uId, $field, $value);
$this->getQuery();
}
// ========================================================================
/**
* 建構子
*
* @param string $inputUID 使用者ID
* @since 2.0.0
*/
public function __construct($inputUID){
$this->uId = $inputUID;
$this->getQuery();
}
// ========================================================================
/**
* 取得帳號名稱
*
* @return string 帳號名稱
* @since 2.0.0
*/
public function getUsername(){
return $this->uId;
}
// ------------------------------------------------------------------------
/**
* 驗證密碼是否錯誤
*
* @param string $inputPasswd 密碼
* @return bool true:密碼正確false:密碼錯誤
* @since 2.0.0
*/
public function isPasswordCorrect($inputPasswd){
$passUtil = new Util\Password();
$this_passwd = $this->queryResultArray['password'];
return $passUtil->checkSame($this_passwd, $inputPasswd);
}
/**
* 更改密碼
*
* @param string $newPasswd 新密碼
* @param string $newPasswdMode 新密碼加密方式(可省略)
* @return string 狀態回傳
* @since 2.0.0
*/
public function changePassword($newPasswd){
// 進行密碼加密
$passUtil = new Util\Password();
$passwdEncrypted = $passUtil->encrypt($newPasswd);
// 將新密碼寫進資料庫裡
$this->setUpdate('password', $passwdEncrypted);
}
// ========================================================================
/**
* 取得帳號建立時間
*
* @return string 建立時間
* @since 2.0.0
*/
public function getCreateTime(){
return $this->queryResultArray['build_time'];
}
// ========================================================================
/**
* 取得所在群組
*
* @return string 群組ID
* @since 2.0.0
*/
public function getGroup(){
return $this->queryResultArray['group_id'];
}
/**
* 取得所在群組顯式名稱
*
* @return string 群組名稱
* @since 2.0.0
*/
public function getGroupName(){
// TODO: getGroupName
}
/**
* 設定所在群組
*
* @param string $toGroup 群組ID
* @since 2.0.0
*/
public function setGroup($toGroup){
// TODO: setGroup
}
// ------------------------------------------------------------------------
/**
* 取得所在班級
*
* @return string 班級ID
* @since 2.0.0
*/
public function getClass(){
return $this->queryResultArray['class_id'];
}
/**
* 取得所在群組顯式名稱
*
* @return string 班級名稱
* @since 2.0.0
*/
public function getClassName(){
// TODO: getGroupName
}
/**
* 設定所在群組
*
* @param string $toGroup 班級ID
* @since 2.0.0
*/
public function setClass($toClass){
// TODO: setGroup
}
// ========================================================================
/**
* 取得帳號啟用狀態
*
* @return bool 是否已啟用
* @since 2.0.0
*/
public function isEnable(){
return $this->queryResultArray['enable'];
}
/**
* 設定帳號啟用狀態
*
* @param bool $isActive 是否為啟用
* @since 2.0.0
*/
public function setEnable($isActive){
// TODO: 防呆,至少一個帳號是啟用的
// 將新設定寫進資料庫裡
$this->setUpdate('enable', $isActive);
}
// ========================================================================
/**
* 取得這個人的學習導引風格
*
* @return string 學習導引風格
* @since 2.0.0
*/
public function getLearnStyle(){
// TODO:
}
/**
* 設定這個人的學習導引風格
*
* @param string $style 學習導引風格
* @since 2.0.0
*/
public function setLearnStyle($style){
// TODO
}
/**
* 取得這個人的教材風格
*
* @return string 教材風格
* @since 2.0.0
*/
public function getMaterialStyle(){
// TODO:
}
/**
* 設定這個人的教材風格
*
* @param string $style 教材風格
* @since 2.0.0
*/
public function setMaterialStyle($style){
// TODO
}
// ========================================================================
/**
* 取得名稱
*
* @return string 依照有填入多少名字 <br />優先順序: 暱稱→真實名字→帳號名稱
* @since 2.0.0
*/
public function getName(){
// TODO: 代修正
if($this->getNickName() != "") {
return $this->getNickName();
}
else if($this->getRealName() != "") {
return $this->getRealName();
}
else {
return $this->getUsername();
}
}
// ------------------------------------------------------------------------
/**
* 取得暱稱
*
* @return string 暱稱
* @since 2.0.0
*/
public function getNickName(){
return $this->queryResultArray['nickname'];
}
/**
* 修改暱稱
*
* @param string $input 新暱稱
* @since 2.0.0
*/
public function setNickName($input){
// 將新設定寫進資料庫裡
$this->setUpdate('nickname', $input);
}
// ------------------------------------------------------------------------
/**
* 取得真實姓名
*
* @return string 真實姓名
* @since 2.0.0
*/
public function getRealName(){
return $this->queryResultArray['realname'];
}
/**
* 修改真實姓名
*
* @param string $input 新真實姓名
* @since 2.0.0
*/
public function setRealName($input){
// 將新設定寫進資料庫裡
$this->setUpdate('realname', $input);
}
// ------------------------------------------------------------------------
/**
* 取得帳號Email
*
* @return string 使用者資訊的Email
* @since 2.0.0
*/
public function getEmail(){
return $this->queryResultArray['email'];
}
/**
* 修改帳號Email
*
* @param string $input 新Email
* @since 2.0.0
*/
public function setEmail($input){
// 將新設定寫進資料庫裡
$db->changeUserData($this->uId, 'email', $input);
}
// ------------------------------------------------------------------------
/**
* 取得帳號備註資訊
*
* @return string 使用者帳號備註資訊
* @since 2.0.0
*/
public function getMemo(){
return $this->queryResultArray['memo'];
}
/**
* 修改帳號備註資訊
*
* @param string $input 新的帳號備註資訊
* @since 2.0.0
*/
public function setMemo($input){
$this->setUpdate('memo', $input);
}
// ========================================================================
/**
* 取得權限清單
*
* @access public
* @return array 權限清單
*/
public function getPermissionList() {
$thisGroup = new UserGroup($this->getQueryInfo("GID"));
return $thisGroup->getPermissionList();
}
/**
* 是否擁有此權限
*
* @access public
* @global string $FORM_USER /config/db_table_config.php的使用者資料表名稱
* @global string $FORM_USER_GROUP /config/db_table_config.php的使用者群組資料表名稱
* @param string $permissionName 權限名稱
* @return bool 是否擁有
*/
public function havePermission($permissionName) {
// TODO
}
}

View File

@ -8,7 +8,9 @@ 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';
require_once UELEARNING_LIB_ROOT.'/Util/Password.php';
use UElearning\Database;
use UElearning\Util;
/**
* 管理使用者的操作
@ -105,11 +107,8 @@ class UserAdmin {
}
// 進行密碼加密
/*if( isset($userInfoArray['userId']) ) {
// TODO: 密碼加密
}*/
// 加密後的密碼
$passwdEncrypted = $userInfoArray['password'];
$passUtil = new Util\Password();
$passwdEncrypted = $passUtil->encrypt( $userInfoArray['password'] );
// 新增一筆使用者資料進資料庫
$db = new Database\DBUser();

View File

@ -8,6 +8,18 @@ namespace UElearning\Util;
/**
* 密碼以及加密相關的函式庫
*
* 使用範例:
*
* require_once __DIR__.'/../config.php';
* require_once UELEARNING_LIB_ROOT.'/Util/Password.php';
* use UElearning\Util;
*
* $passUtil = new Util\Password();
* echo $passUtil->generator(10); // 產生10個字的密碼
* echo $passUtil->encrypt('abc'); // 加密此字串
*
* // 核對與加密後是否吻合
* echo $passUtil->checkSame('a9993e364706816aba3e25717850c26c9cd0d89d', 'abc');
*
* @author Yuan Chiu <chyuaner@gmail.com>
* @version 2.0.0
@ -47,8 +59,7 @@ class Password {
* @return string 亂數產生產生後的字串
*
*/
public function generator($password_len)
{
public function generator($password_len){
$password = '';
// remove o,0,1,l
@ -70,8 +81,6 @@ class Password {
* @since 2.0.0
*/
public function encrypt($text){
// TODO: 尚未測試
// 從config.php設定檔取得預設加密方式
switch(ENCRYPT_MODE){
case "MD5":
@ -101,8 +110,6 @@ class Password {
* @since 2.0.0
*/
public function checkSame($encrypted, $text) {
// TODO: 尚未測試
// 加密此字串
$textToEncypt = $this->encrypt($text);

View File

@ -31,8 +31,6 @@ class UserAdminTest extends \PHPUnit_Framework_TestCase
$userAdmin->create(
array( 'user_id' => $uId,
'password' => $uPassword,
//'password_encrypt' => null,
//'password_encrypted' => null,
'group_id' => $gId,
'class_id' => $cId,
'enable' => $enable,

View File

@ -25,7 +25,7 @@ class PasswordTest extends \PHPUnit_Framework_TestCase
*
* @dataProvider pass_dataProvider
*/
public function testCheckSame($data){
public function testCheckSame($value){
// 加密字串
$encode = $this->passUtil->encrypt($value);