diff --git a/app/Console/Tasks/CleanTokenTask.php b/app/Console/Tasks/CleanTokenTask.php deleted file mode 100644 index b5bf806d..00000000 --- a/app/Console/Tasks/CleanTokenTask.php +++ /dev/null @@ -1,57 +0,0 @@ -findAccessTokens(); - - if ($accessTokens->count() > 0) { - $accessTokens->delete(); - } - - $refreshTokens = $this->findRefreshTokens(); - - if ($refreshTokens->count() > 0) { - $refreshTokens->delete(); - } - } - - /** - * 查找待清理访问令牌 - * - * @return ResultsetInterface|Resultset|AccessTokenModel[] - */ - protected function findAccessTokens() - { - $expiryTime = strtotime('-30 days'); - - return AccessTokenModel::query() - ->where('expiry_time < :expiry_time:', ['expiry_time' => $expiryTime]) - ->execute(); - } - - /** - * 查找待清理刷新令牌 - * - * @return ResultsetInterface|Resultset|RefreshTokenModel[] - */ - protected function findRefreshTokens() - { - $expiryTime = strtotime('-30 days'); - - return RefreshTokenModel::query() - ->where('expiry_time < :expiry_time:', ['expiry_time' => $expiryTime]) - ->execute(); - } - -} diff --git a/app/Http/Admin/Controllers/UserController.php b/app/Http/Admin/Controllers/UserController.php index 520f539b..a768e4f6 100644 --- a/app/Http/Admin/Controllers/UserController.php +++ b/app/Http/Admin/Controllers/UserController.php @@ -3,6 +3,7 @@ namespace App\Http\Admin\Controllers; use App\Http\Admin\Services\User as UserService; +use App\Models\Role as RoleModel; /** * @RoutePrefix("/admin/user") @@ -34,14 +35,6 @@ class UserController extends Controller $this->view->setVar('pager', $pager); } - /** - * @Get("/{id:[0-9]+}/show", name="admin.user.show") - */ - public function showAction($id) - { - - } - /** * @Get("/add", name="admin.user.add") */ @@ -59,6 +52,12 @@ class UserController extends Controller */ public function createAction() { + $adminRole = $this->request->getPost('admin_role', 'int', 0); + + if ($adminRole == RoleModel::ROLE_ROOT) { + return $this->response->redirect(['action' => 'list']); + } + $userService = new UserService(); $userService->createUser(); @@ -84,6 +83,10 @@ class UserController extends Controller $account = $userService->getAccount($id); $roles = $userService->getRoles(); + if ($user->admin_role == RoleModel::ROLE_ROOT) { + return $this->response->redirect(['action' => 'list']); + } + $this->view->setVar('user', $user); $this->view->setVar('account', $account); $this->view->setVar('roles', $roles); @@ -94,7 +97,13 @@ class UserController extends Controller */ public function updateAction($id) { - $type = $this->request->getPost('type'); + $adminRole = $this->request->getPost('admin_role', 'int', 0); + + if ($adminRole == RoleModel::ROLE_ROOT) { + return $this->response->redirect(['action' => 'list']); + } + + $type = $this->request->getPost('type', 'string', 'user'); $userService = new UserService(); diff --git a/app/Http/Admin/Services/User.php b/app/Http/Admin/Services/User.php index 22e4f258..91d2a06c 100644 --- a/app/Http/Admin/Services/User.php +++ b/app/Http/Admin/Services/User.php @@ -7,6 +7,7 @@ use App\Caches\User as UserCache; use App\Library\Paginator\Query as PaginateQuery; use App\Library\Utils\Password as PasswordUtil; use App\Models\Account as AccountModel; +use App\Models\ImUser as ImUserModel; use App\Models\User as UserModel; use App\Repos\Account as AccountRepo; use App\Repos\Role as RoleRepo; @@ -68,33 +69,55 @@ class User extends Service $eduRole = $userValidator->checkEduRole($post['edu_role']); $adminRole = $userValidator->checkAdminRole($post['admin_role']); - $account = new AccountModel(); + try { - $salt = PasswordUtil::salt(); - $password = PasswordUtil::hash($password, $salt); + $this->db->begin(); - $account->phone = $phone; - $account->salt = $salt; - $account->password = $password; + $account = new AccountModel(); - $account->create(); + $salt = PasswordUtil::salt(); + $password = PasswordUtil::hash($password, $salt); - $userRepo = new UserRepo(); + $account->phone = $phone; + $account->salt = $salt; + $account->password = $password; - $user = $userRepo->findById($account->id); + if ($account->create() === false) { + throw new \RuntimeException('Create Account Failed'); + } - $user->edu_role = $eduRole; - $user->admin_role = $adminRole; + $user = new UserModel(); - $user->update(); + $user->id = $account->id; + $user->name = "user_{$account->id}"; + $user->edu_role = $eduRole; + $user->admin_role = $adminRole; - if ($adminRole > 0) { - $this->updateAdminUserCount($adminRole); + if ($user->create() === false) { + throw new \RuntimeException('Create User Failed'); + } + + $imUser = new ImUserModel(); + + $imUser->id = $user->id; + $imUser->name = $user->name; + + if ($imUser->create() === false) { + throw new \RuntimeException('Create Im User Failed'); + } + + $this->db->commit(); + + if ($adminRole > 0) { + $this->updateAdminUserCount($adminRole); + } + + } catch (\Exception $e) { + + $this->db->rollback(); + + throw new \RuntimeException('sys.trans_rollback'); } - - $this->rebuildUserCache($user); - - return $user; } public function updateUser($id) @@ -164,8 +187,6 @@ class User extends Service $this->updateAdminUserCount($user->admin_role); } - $this->rebuildUserCache($user); - return $user; } diff --git a/app/Http/Admin/Views/public/login.volt b/app/Http/Admin/Views/public/login.volt index 9cf3247d..5e583d7d 100644 --- a/app/Http/Admin/Views/public/login.volt +++ b/app/Http/Admin/Views/public/login.volt @@ -68,7 +68,9 @@ diff --git a/app/Http/Admin/Views/user/add.volt b/app/Http/Admin/Views/user/add.volt index 30ae9d7e..2173e42f 100644 --- a/app/Http/Admin/Views/user/add.volt +++ b/app/Http/Admin/Views/user/add.volt @@ -30,8 +30,10 @@
- {% for item in roles %} - + {% for role in roles %} + {% if role.id > 1 %} + + {% endif %} {% endfor %}
diff --git a/app/Http/Admin/Views/user/edit.volt b/app/Http/Admin/Views/user/edit.volt index 93af9891..54f28c32 100644 --- a/app/Http/Admin/Views/user/edit.volt +++ b/app/Http/Admin/Views/user/edit.volt @@ -27,8 +27,8 @@
- - + +
{% if auth_user.root == 1 %} @@ -36,8 +36,10 @@
- {% for item in roles %} - + {% for role in roles %} + {% if role.id > 1 %} + + {% endif %} {% endfor %}
@@ -67,7 +69,7 @@ - + {% if auth_user.root == 1 %}
diff --git a/db/migrations/20200901121917_insert_role_data.php b/db/migrations/20200901121917_insert_role_data.php new file mode 100644 index 00000000..879b2cbc --- /dev/null +++ b/db/migrations/20200901121917_insert_role_data.php @@ -0,0 +1,60 @@ + 1, + 'type' => 1, + 'name' => '管理员', + 'summary' => '管理员', + 'routes' => '', + 'user_count' => 1, + 'create_time' => $now, + ], + [ + 'id' => 2, + 'type' => 1, + 'name' => '运营', + 'summary' => '运营人员', + 'routes' => '', + 'user_count' => 0, + 'create_time' => $now, + ], + [ + 'id' => 3, + 'type' => 1, + 'name' => '编辑', + 'summary' => '编辑人员', + 'routes' => '', + 'user_count' => 0, + 'create_time' => $now, + ], + [ + 'id' => 4, + 'type' => 1, + 'name' => '财务', + 'summary' => '财务人员', + 'routes' => '', + 'user_count' => 0, + 'create_time' => $now, + ], + ]; + + $this->table('kg_role')->insert($rows)->save(); + } + + public function down() + { + $this->execute('DELETE FROM kg_role'); + } + +}