From d73f6973ee066cb95ee974f6568dcbaa026dcbc2 Mon Sep 17 00:00:00 2001 From: Yuan Chiu Date: Sun, 23 Nov 2014 17:39:27 +0800 Subject: [PATCH] fix userAdmin lib: create (enable_noAppoint default) & add create user api --- htdocs/api/v2/index.php | 118 ++++++++++++++++++++++++++++++++-- htdocs/lib/User/UserAdmin.php | 2 +- 2 files changed, 114 insertions(+), 6 deletions(-) diff --git a/htdocs/api/v2/index.php b/htdocs/api/v2/index.php index 3ee4dbb..0ae5b2f 100644 --- a/htdocs/api/v2/index.php +++ b/htdocs/api/v2/index.php @@ -4,11 +4,13 @@ require UELEARNING_ROOT.'/vendor/autoload.php'; require_once __DIR__.'/src/ApiTemplates.php'; require_once UELEARNING_LIB_ROOT.'/User/User.php'; require_once UELEARNING_LIB_ROOT.'/User/UserSession.php'; +require_once UELEARNING_LIB_ROOT.'/User/UserAdmin.php'; use UElearning\User; use UElearning\Exception; $app = new \Slim\Slim(array( - 'templates.path' => './' // 設定Path + 'templates.path' => './', // 設定Path + 'debug' => true )); $app_template = new ApiTemplates($app); @@ -40,7 +42,113 @@ $app->group('/users', 'APIrequest', function () use ($app, $app_template) { * POST http://localhost/api/v2/users */ $app->post('/', function () use ($app) { - $app_template->noEnableFunc(); + // 取得帶來的參數 + $cType = $app->request->getContentType(); + if($cType == 'application/x-www-form-urlencoded') { + $user_id = $_POST['user_id']; + $password = $_POST['password']; + $group_id = $_POST['group_id']; + $class_id = isset($_POST['class_id']) + ? $_POST['class_id'] : null; + $enable = isset($_POST['enable']) + ? $_POST['enable'] : null; + $learnStyle_mode = isset($_POST['learnStyle_mode']) + ? $_POST['learnStyle_mode'] : null; + $material_mode = isset($_POST['material_mode']) + ? $_POST['material_mode'] : null; + $enable_noAppoint = isset($_POST['enable_noAppoint']) + ? $_POST['enable_noAppoint'] : null; + $nickname = isset($_POST['nickname']) + ? $_POST['nickname'] : null; + $realname = isset($_POST['realname']) + ? $_POST['realname'] : null; + $email = isset($_POST['email']) + ? $_POST['email'] : null; + $memo = isset($_POST['memo']) + ? $_POST['memo'] : null;; + } + else /*if($cType == 'application/json')*/ { + $postData = $app->request->getBody(); + $postDataArray = json_decode($postData); + $user_id = $postDataArray->user_id; + $password = $postDataArray->password; + $group_id = $postDataArray->group_id; + $class_id = isset($postDataArray->class_id) + ? $postDataArray->class_id : null; + $enable = isset($postDataArray->enable) + ? $postDataArray->enable : null; + $learnStyle_mode = isset($postDataArray->learnStyle_mode) + ? $postDataArray->learnStyle_mode : null; + $material_mode = isset($postDataArray->material_mode) + ? $postDataArray->material_mode : null; + $enable_noAppoint = isset($postDataArray->enable_noAppoint) + ? $postDataArray->enable_noAppoint : null; + $nickname = isset($postDataArray->nickname) + ? $postDataArray->nickname : null; + $realname = isset($postDataArray->realname) + ? $postDataArray->realname : null; + $email = isset($postDataArray->email) + ? $postDataArray->email : null; + $memo = isset($postDataArray->memo) + ? $postDataArray->memo : null; + } + /*else { + $app->render(400, array( + 'Content-Type'=> $cType, + 'error' => true, + 'msg' => '', + 'msg_cht' => '輸入參數的Content-Type不在支援範圍內 或是沒有輸入', + 'substatus' => 102 + ) + ); + }*/ + + // 建立使用者帳號 + try { + $userAdmin = new User\UserAdmin(); + $userAdmin->create( + array( 'user_id' => $user_id, + 'password' => $password, + 'group_id' => $group_id, + 'class_id' => $class_id, + 'enable' => $enable, + 'learnStyle_mode' => $learnStyle_mode, + 'material_mode' => $material_mode, + 'enable_noAppoint' => $enable_noAppoint, + 'nickname' => $nickname, + 'realname' => $realname, + 'email' => $email, + 'memo' => $memo) + ); + + // 顯示建立成功 + $app->render(200,array( + 'user_id' => $user_id, + 'group_id' => $group_id, + 'class_id' => $class_id, + 'enable' => $enable, + 'learnStyle_mode' => $learnStyle_mode, + 'material_mode' => $material_mode, + 'enable_noAppoint' => $enable_noAppoint, + 'nickname' => $nickname, + 'realname' => $realname, + 'email' => $email, + 'memo' => $memo, + 'error' => false, + 'msg' => '\''.$user_id.'\' is created.', + 'msg_cht' => '\''.$user_id.'\'使用者已成功建立' + )); + + } + // 若已有重複帳號名稱 + catch (Exception\UserIdExistException $e) { + $app->render(409,array( + 'user_id' => $user_id, + 'error' => true, + 'msg' => '\''.$user_id.'\' is exist.', + 'msg_cht' => '\''.$user_id.'\'使用者名稱已被使用' + )); + } }); /* @@ -81,14 +189,14 @@ $app->group('/users', 'APIrequest', function () use ($app, $app_template) { $password = $_POST['password']; $browser = isset($_POST['browser']) ? $_POST['browser'] : 'api'; } - else if($cType == 'application/json') { + else /*if($cType == 'application/json')*/ { $postData = $app->request->getBody(); $postDataArray = json_decode($postData); $password = $postDataArray->password; $browser = isset($postDataArray->browser) ? $postDataArray->browser : 'api'; } - else { + /*else { $app->render(400, array( 'Content-Type'=> $cType, 'error' => true, @@ -97,7 +205,7 @@ $app->group('/users', 'APIrequest', function () use ($app, $app_template) { 'substatus' => 102 ) ); - } + }*/ // 進行登入 try { diff --git a/htdocs/lib/User/UserAdmin.php b/htdocs/lib/User/UserAdmin.php index c575706..af7a733 100644 --- a/htdocs/lib/User/UserAdmin.php +++ b/htdocs/lib/User/UserAdmin.php @@ -101,7 +101,7 @@ class UserAdmin { $userInfoArray['material_mode'] = null; } if( !isset($userInfoArray['enable_noAppoint']) ){ - $userInfoArray['enable_noAppoint'] = null; + $userInfoArray['enable_noAppoint'] = true; } if( !isset($userInfoArray['nickname']) ){ $userInfoArray['nickname'] = null;