diff --git a/CHANGELOG.md b/CHANGELOG.md index ae1935b3..3ebb7d15 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,14 @@ +### [v1.5.5](https://gitee.com/koogua/course-tencent-cloud/releases/v1.5.5)(2022-07-27) + +- 修正获分类查询条件 +- 修正锁定账户还能登录的问题 +- 发货增加noMatchedHandler +- 增加demo数据清理脚本 +- 用户课程列表增加角色限定条件 +- 精简模块加载和路由扫描 +- 优化CsrfToken +- 去除无实质作用的数据表优化 + ### [v1.5.4](https://gitee.com/koogua/course-tencent-cloud/releases/v1.5.4)(2022-06-15) - 增加migration助手SettingTrait diff --git a/app/Builders/QuestionList.php b/app/Builders/QuestionList.php index 10a40a3c..aeabd0e8 100644 --- a/app/Builders/QuestionList.php +++ b/app/Builders/QuestionList.php @@ -23,15 +23,15 @@ class QuestionList extends Builder return $questions; } - public function handleCategories(array $articles) + public function handleCategories(array $questions) { $categories = $this->getCategories(); - foreach ($articles as $key => $article) { - $articles[$key]['category'] = $categories[$article['category_id']] ?? new \stdClass(); + foreach ($questions as $key => $article) { + $questions[$key]['category'] = $categories[$article['category_id']] ?? new \stdClass(); } - return $articles; + return $questions; } public function handleUsers(array $questions) diff --git a/app/Console/Tasks/CleanDemoDataTask.php b/app/Console/Tasks/CleanDemoDataTask.php new file mode 100644 index 00000000..b53f97a1 --- /dev/null +++ b/app/Console/Tasks/CleanDemoDataTask.php @@ -0,0 +1,126 @@ +isDemoEnv()) { + + $this->truncateTables(); + $this->createRootUser(); + $this->cleanSearchIndex(); + $this->cleanCache(); + + } else { + + echo '------ access denied ------' . PHP_EOL; + } + } + + protected function truncateTables() + { + echo '------ start truncate tables ------' . PHP_EOL; + + $excludeTables = [ + 'kg_area', 'kg_migration', 'kg_nav', 'kg_page', + 'kg_reward', 'kg_role', 'kg_setting', 'kg_vip', + ]; + + $tables = $this->db->listTables(); + + foreach ($tables as $table) { + if (!in_array($table, $excludeTables)) { + $this->db->execute("TRUNCATE TABLE {$table}"); + } + } + + echo '------ end truncate tables ------' . PHP_EOL; + } + + protected function createRootUser() + { + echo '------ start create root user ------' . PHP_EOL; + + $account = new AccountModel(); + + $account->create([ + 'id' => 10000, + 'email' => '10000@163.com', + 'password' => '1a1e4568f1a3740b8853a8a16e29bc87', + 'salt' => 'MbZWxN3L', + 'create_time' => time(), + ]); + + $userRepo = new UserRepo(); + + $user = $userRepo->findById($account->id); + + $user->update([ + 'admin_role' => 1, + 'edu_role' => 2, + ]); + + echo '------ end create root user ------' . PHP_EOL; + } + + protected function cleanCache() + { + $util = new IndexCourseCacheUtil(); + $util->rebuild(); + + $slideListCache = new IndexSlideListCache(); + $slideListCache->rebuild(); + + $categoryListCache = new CategoryListCache(); + $categoryTreeListCache = new CategoryTreeListCache(); + + foreach (CategoryModel::types() as $key => $value) { + $categoryListCache->rebuild($key); + $categoryTreeListCache->rebuild($key); + } + } + + protected function cleanSearchIndex() + { + $articleIndexTask = new ArticleIndexTask(); + $articleIndexTask->cleanAction(); + + $courseIndexTask = new CourseIndexTask(); + $courseIndexTask->cleanAction(); + + $groupIndexTask = new GroupIndexTask(); + $groupIndexTask->cleanAction(); + + $questionIndexTask = new QuestionIndexTask(); + $questionIndexTask->cleanAction(); + + $userIndexTask = new UserIndexTask(); + $userIndexTask->cleanAction(); + } + + protected function isDemoEnv() + { + $userRepo = new UserRepo(); + + $user = $userRepo->findById(100015); + + return $user ? true : false; + } + +} diff --git a/app/Console/Tasks/DeliverTask.php b/app/Console/Tasks/DeliverTask.php index aec87f1d..de42c7ea 100644 --- a/app/Console/Tasks/DeliverTask.php +++ b/app/Console/Tasks/DeliverTask.php @@ -58,6 +58,9 @@ class DeliverTask extends Task case OrderModel::ITEM_VIP: $this->handleVipOrder($order); break; + default: + $this->noMatchedHandler($order); + break; } $order->status = OrderModel::STATUS_FINISHED; @@ -153,6 +156,11 @@ class DeliverTask extends Task $this->closePendingOrders($user->id); } + protected function noMatchedHandler(OrderModel $order) + { + throw new \RuntimeException("No Matched Handler For Order: {$order->id}"); + } + protected function closePendingOrders($userId) { $orders = $this->findUserPendingOrders($userId); diff --git a/app/Console/Tasks/OptimizeTableTask.php b/app/Console/Tasks/OptimizeTableTask.php deleted file mode 100644 index 97f3e2ac..00000000 --- a/app/Console/Tasks/OptimizeTableTask.php +++ /dev/null @@ -1,139 +0,0 @@ -optimizeUserSessionTable(); - $this->optimizeUserTokenTable(); - $this->optimizeImMessageTable(); - $this->optimizeLearningTable(); - $this->optimizeTaskTable(); - } - - protected function optimizeUserSessionTable() - { - $sessionModel = new UserSessionModel(); - - $tableName = $sessionModel->getSource(); - - if (UserSessionModel::count() < 1000000) { - echo sprintf('no need to optimize table: %s', $tableName) . PHP_EOL; - return; - } - - echo sprintf('------ start optimize table: %s ------', $tableName) . PHP_EOL; - - $this->db->delete($tableName, 'expire_time < :expire_time', [ - 'expire_time' => strtotime('-3 days'), - ]); - - $this->db->execute("OPTIMIZE TABLE {$tableName}"); - - echo sprintf('------ end optimize table: %s ------', $tableName) . PHP_EOL; - } - - protected function optimizeUserTokenTable() - { - $tokenModel = new UserTokenModel(); - - $tableName = $tokenModel->getSource(); - - if (UserTokenModel::count() < 1000000) { - echo sprintf('no need to optimize table: %s', $tableName) . PHP_EOL; - return; - } - - echo sprintf('------ start optimize table: %s ------', $tableName) . PHP_EOL; - - $this->db->delete($tableName, 'expire_time < :expire_time', [ - 'expire_time' => strtotime('-3 days'), - ]); - - $this->db->execute("OPTIMIZE TABLE {$tableName}"); - - echo sprintf('------ end optimize table: %s ------', $tableName) . PHP_EOL; - } - - protected function optimizeImMessageTable() - { - $messageModel = new ImMessageModel(); - - $tableName = $messageModel->getSource(); - - if (ImMessageModel::count() < 1000000) { - echo sprintf('no need to optimize table: %s', $tableName) . PHP_EOL; - return; - } - - echo sprintf('------ start optimize table: %s ------', $tableName) . PHP_EOL; - - $this->db->delete($tableName, 'create_time < :create_time', [ - 'create_time' => strtotime('-6 months'), - ]); - - $this->db->execute("OPTIMIZE TABLE {$tableName}"); - - echo sprintf('------ end optimize table: %s ------', $tableName) . PHP_EOL; - } - - protected function optimizeLearningTable() - { - $learningModel = new LearningModel(); - - $tableName = $learningModel->getSource(); - - if (LearningModel::count() < 1000000) { - echo sprintf('no need to optimize table: %s', $tableName) . PHP_EOL; - return; - } - - echo sprintf('------ start optimize table: %s ------', $tableName) . PHP_EOL; - - $this->db->delete($tableName, 'create_time < :create_time', [ - 'create_time' => strtotime('-6 months'), - ]); - - $this->db->execute("OPTIMIZE TABLE {$tableName}"); - - echo sprintf('------ end optimize table: %s ------', $tableName) . PHP_EOL; - } - - protected function optimizeTaskTable() - { - $taskModel = new TaskModel(); - - $tableName = $taskModel->getSource(); - - if (TaskModel::count() < 1000000) { - echo sprintf('no need to optimize table: %s', $tableName) . PHP_EOL; - return; - } - - echo sprintf('------ start optimize table: %s ------', $tableName) . PHP_EOL; - - $this->db->delete($tableName, 'create_time < :create_time AND status > :status', [ - 'create_time' => strtotime('-6 months'), - 'status' => TaskModel::STATUS_PENDING, - ]); - - $this->db->execute("OPTIMIZE TABLE {$tableName}"); - - echo sprintf('------ end optimize table: %s ------', $tableName) . PHP_EOL; - } - -} \ No newline at end of file diff --git a/app/Http/Admin/Services/Question.php b/app/Http/Admin/Services/Question.php index ac514eda..71caa2b8 100644 --- a/app/Http/Admin/Services/Question.php +++ b/app/Http/Admin/Services/Question.php @@ -46,7 +46,7 @@ class Question extends Service $categoryRepo = new CategoryRepo(); return $categoryRepo->findAll([ - 'type' => CategoryModel::TYPE_ARTICLE, + 'type' => CategoryModel::TYPE_QUESTION, 'level' => 1, 'published' => 1, 'deleted' => 0, diff --git a/app/Library/AppInfo.php b/app/Library/AppInfo.php index 1ead1181..d15bff8a 100644 --- a/app/Library/AppInfo.php +++ b/app/Library/AppInfo.php @@ -16,7 +16,7 @@ class AppInfo protected $link = 'https://koogua.com'; - protected $version = '1.5.4'; + protected $version = '1.5.5'; public function __get($name) { diff --git a/app/Library/CsrfToken.php b/app/Library/CsrfToken.php index 323a4175..e5ba0e95 100644 --- a/app/Library/CsrfToken.php +++ b/app/Library/CsrfToken.php @@ -7,6 +7,7 @@ namespace App\Library; +use Phalcon\Config; use Phalcon\Crypt; use Phalcon\Di; use Phalcon\Text; @@ -19,7 +20,7 @@ class CsrfToken */ protected $crypt; - protected $lifetime = 600; + protected $lifetime = 86400; protected $delimiter = '@@'; @@ -33,7 +34,7 @@ class CsrfToken public function getToken() { $content = [ - time() + $this->lifetime, + $this->getExpiredTime(), $this->fixed, Text::random(8), ]; @@ -62,4 +63,16 @@ class CsrfToken return true; } + protected function getExpiredTime() + { + /** + * @var $config Config + */ + $config = Di::getDefault()->getShared('config'); + + $lifetime = $config->path('csrf_token.lifetime') ?: $this->lifetime; + + return $lifetime + time(); + } + } \ No newline at end of file diff --git a/app/Services/Logic/User/CourseList.php b/app/Services/Logic/User/CourseList.php index d6fca26f..88b78fbd 100644 --- a/app/Services/Logic/User/CourseList.php +++ b/app/Services/Logic/User/CourseList.php @@ -9,6 +9,7 @@ namespace App\Services\Logic\User; use App\Builders\CourseUserList as CourseUserListBuilder; use App\Library\Paginator\Query as PagerQuery; +use App\Models\CourseUser as CourseUserModel; use App\Repos\CourseUser as CourseUserRepo; use App\Services\Logic\Service as LogicService; use App\Services\Logic\UserTrait; @@ -27,6 +28,7 @@ class CourseList extends LogicService $params = $pagerQuery->getParams(); $params['user_id'] = $user->id; + $params['role_type'] = CourseUserModel::ROLE_STUDENT; $params['deleted'] = 0; $sort = $pagerQuery->getSort(); diff --git a/app/Validators/Account.php b/app/Validators/Account.php index b8ed0f0e..004c8045 100644 --- a/app/Validators/Account.php +++ b/app/Validators/Account.php @@ -177,10 +177,10 @@ class Account extends Validator public function checkIfAllowLogin(UserModel $user) { - $locked = $user->locked == 1; - $expired = $user->lock_expiry_time > time(); + $case1 = $user->locked == 1; + $case2 = $user->lock_expiry_time > time(); - if ($locked && !$expired) { + if ($case1 && $case2) { throw new ForbiddenException('account.locked'); } diff --git a/bootstrap/HttpKernel.php b/bootstrap/HttpKernel.php index 00ea375c..06aa8ba4 100644 --- a/bootstrap/HttpKernel.php +++ b/bootstrap/HttpKernel.php @@ -105,20 +105,17 @@ class HttpKernel extends Kernel protected function registerModules() { - $modules = [ - 'api' => [ - 'className' => 'App\Http\Api\Module', - 'path' => app_path('Http/Api/Module.php'), - ], - 'admin' => [ - 'className' => 'App\Http\Admin\Module', - 'path' => app_path('Http/Admin/Module.php'), - ], - 'home' => [ - 'className' => 'App\Http\Home\Module', - 'path' => app_path('Http/Home/Module.php'), - ], - ]; + $aliases = ['api', 'home', 'admin']; + + $modules = []; + + foreach ($aliases as $alias) { + $moduleName = ucfirst($alias); + $modules[$alias] = [ + 'className' => 'App\Http\\' . $moduleName . '\Module', + 'path' => app_path('Http/' . $moduleName . '/Module.php'), + ]; + } $this->app->registerModules($modules); } diff --git a/config/config.default.php b/config/config.default.php index 924f027d..41836295 100644 --- a/config/config.default.php +++ b/config/config.default.php @@ -117,6 +117,11 @@ $config['metadata']['lifetime'] = 7 * 86400; */ $config['annotation']['lifetime'] = 7 * 86400; +/** + * CsrfToken有效期(秒) + */ +$config['csrf_token']['lifetime'] = 86400; + /** * 允许跨域 */ diff --git a/config/routes.php b/config/routes.php index 5a6ab2c7..a8c49855 100644 --- a/config/routes.php +++ b/config/routes.php @@ -19,30 +19,16 @@ $router->notFound([ 'action' => 'show404', ]); -$webFiles = scandir(app_path('Http/Home/Controllers')); +$modules = ['api', 'home', 'admin']; -foreach ($webFiles as $file) { - if (strpos($file, 'Controller.php')) { - $className = str_replace('Controller.php', '', $file); - $router->addModuleResource('home', 'App\Http\Home\Controllers\\' . $className); - } -} - -$apiFiles = scandir(app_path('Http/Api/Controllers')); - -foreach ($apiFiles as $file) { - if (strpos($file, 'Controller.php')) { - $className = str_replace('Controller.php', '', $file); - $router->addModuleResource('api', 'App\Http\Api\Controllers\\' . $className); - } -} - -$adminFiles = scandir(app_path('Http/Admin/Controllers')); - -foreach ($adminFiles as $file) { - if (strpos($file, 'Controller.php')) { - $className = str_replace('Controller.php', '', $file); - $router->addModuleResource('admin', 'App\Http\Admin\Controllers\\' . $className); +foreach ($modules as $module) { + $moduleName = ucfirst($module); + $files = scandir(app_path('Http/' . $moduleName . '/Controllers')); + foreach ($files as $file) { + if (strpos($file, 'Controller.php')) { + $className = str_replace('Controller.php', '', $file); + $router->addModuleResource($module, 'App\Http\\' . $moduleName . '\Controllers\\' . $className); + } } } diff --git a/scheduler.php b/scheduler.php index a4549786..93fb1d59 100644 --- a/scheduler.php +++ b/scheduler.php @@ -102,7 +102,4 @@ $scheduler->php($script, $bin, ['--task' => 'renew_demo_live_course', '--action' $scheduler->php($script, $bin, ['--task' => 'teacher_live_notice', '--action' => 'provide']) ->daily(4, 7); -$scheduler->php($script, $bin, ['--task' => 'optimize_table', '--action' => 'main']) - ->weekly(6, 5, 3); - $scheduler->run();