mirror of
https://gitee.com/koogua/course-tencent-cloud.git
synced 2025-07-21 07:26:39 +08:00
增加role初始化数据
This commit is contained in:
parent
e3ae3b24ae
commit
a6793b9e90
@ -1,57 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Console\Tasks;
|
|
||||||
|
|
||||||
use App\Models\AccessToken as AccessTokenModel;
|
|
||||||
use App\Models\RefreshToken as RefreshTokenModel;
|
|
||||||
use Phalcon\Cli\Task;
|
|
||||||
use Phalcon\Mvc\Model\Resultset;
|
|
||||||
use Phalcon\Mvc\Model\ResultsetInterface;
|
|
||||||
|
|
||||||
class CleanTokenTask extends Task
|
|
||||||
{
|
|
||||||
|
|
||||||
public function mainAction()
|
|
||||||
{
|
|
||||||
$accessTokens = $this->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();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -3,6 +3,7 @@
|
|||||||
namespace App\Http\Admin\Controllers;
|
namespace App\Http\Admin\Controllers;
|
||||||
|
|
||||||
use App\Http\Admin\Services\User as UserService;
|
use App\Http\Admin\Services\User as UserService;
|
||||||
|
use App\Models\Role as RoleModel;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @RoutePrefix("/admin/user")
|
* @RoutePrefix("/admin/user")
|
||||||
@ -34,14 +35,6 @@ class UserController extends Controller
|
|||||||
$this->view->setVar('pager', $pager);
|
$this->view->setVar('pager', $pager);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @Get("/{id:[0-9]+}/show", name="admin.user.show")
|
|
||||||
*/
|
|
||||||
public function showAction($id)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Get("/add", name="admin.user.add")
|
* @Get("/add", name="admin.user.add")
|
||||||
*/
|
*/
|
||||||
@ -59,6 +52,12 @@ class UserController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function createAction()
|
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 = new UserService();
|
||||||
|
|
||||||
$userService->createUser();
|
$userService->createUser();
|
||||||
@ -84,6 +83,10 @@ class UserController extends Controller
|
|||||||
$account = $userService->getAccount($id);
|
$account = $userService->getAccount($id);
|
||||||
$roles = $userService->getRoles();
|
$roles = $userService->getRoles();
|
||||||
|
|
||||||
|
if ($user->admin_role == RoleModel::ROLE_ROOT) {
|
||||||
|
return $this->response->redirect(['action' => 'list']);
|
||||||
|
}
|
||||||
|
|
||||||
$this->view->setVar('user', $user);
|
$this->view->setVar('user', $user);
|
||||||
$this->view->setVar('account', $account);
|
$this->view->setVar('account', $account);
|
||||||
$this->view->setVar('roles', $roles);
|
$this->view->setVar('roles', $roles);
|
||||||
@ -94,7 +97,13 @@ class UserController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function updateAction($id)
|
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();
|
$userService = new UserService();
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@ use App\Caches\User as UserCache;
|
|||||||
use App\Library\Paginator\Query as PaginateQuery;
|
use App\Library\Paginator\Query as PaginateQuery;
|
||||||
use App\Library\Utils\Password as PasswordUtil;
|
use App\Library\Utils\Password as PasswordUtil;
|
||||||
use App\Models\Account as AccountModel;
|
use App\Models\Account as AccountModel;
|
||||||
|
use App\Models\ImUser as ImUserModel;
|
||||||
use App\Models\User as UserModel;
|
use App\Models\User as UserModel;
|
||||||
use App\Repos\Account as AccountRepo;
|
use App\Repos\Account as AccountRepo;
|
||||||
use App\Repos\Role as RoleRepo;
|
use App\Repos\Role as RoleRepo;
|
||||||
@ -68,6 +69,10 @@ class User extends Service
|
|||||||
$eduRole = $userValidator->checkEduRole($post['edu_role']);
|
$eduRole = $userValidator->checkEduRole($post['edu_role']);
|
||||||
$adminRole = $userValidator->checkAdminRole($post['admin_role']);
|
$adminRole = $userValidator->checkAdminRole($post['admin_role']);
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
$this->db->begin();
|
||||||
|
|
||||||
$account = new AccountModel();
|
$account = new AccountModel();
|
||||||
|
|
||||||
$salt = PasswordUtil::salt();
|
$salt = PasswordUtil::salt();
|
||||||
@ -77,24 +82,42 @@ class User extends Service
|
|||||||
$account->salt = $salt;
|
$account->salt = $salt;
|
||||||
$account->password = $password;
|
$account->password = $password;
|
||||||
|
|
||||||
$account->create();
|
if ($account->create() === false) {
|
||||||
|
throw new \RuntimeException('Create Account Failed');
|
||||||
|
}
|
||||||
|
|
||||||
$userRepo = new UserRepo();
|
$user = new UserModel();
|
||||||
|
|
||||||
$user = $userRepo->findById($account->id);
|
|
||||||
|
|
||||||
|
$user->id = $account->id;
|
||||||
|
$user->name = "user_{$account->id}";
|
||||||
$user->edu_role = $eduRole;
|
$user->edu_role = $eduRole;
|
||||||
$user->admin_role = $adminRole;
|
$user->admin_role = $adminRole;
|
||||||
|
|
||||||
$user->update();
|
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) {
|
if ($adminRole > 0) {
|
||||||
$this->updateAdminUserCount($adminRole);
|
$this->updateAdminUserCount($adminRole);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->rebuildUserCache($user);
|
} catch (\Exception $e) {
|
||||||
|
|
||||||
return $user;
|
$this->db->rollback();
|
||||||
|
|
||||||
|
throw new \RuntimeException('sys.trans_rollback');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function updateUser($id)
|
public function updateUser($id)
|
||||||
@ -164,8 +187,6 @@ class User extends Service
|
|||||||
$this->updateAdminUserCount($user->admin_role);
|
$this->updateAdminUserCount($user->admin_role);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->rebuildUserCache($user);
|
|
||||||
|
|
||||||
return $user;
|
return $user;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,7 +68,9 @@
|
|||||||
<script>
|
<script>
|
||||||
|
|
||||||
layui.use(['jquery', 'form'], function () {
|
layui.use(['jquery', 'form'], function () {
|
||||||
|
|
||||||
var $ = layui.jquery;
|
var $ = layui.jquery;
|
||||||
|
|
||||||
var captcha = new TencentCaptcha(
|
var captcha = new TencentCaptcha(
|
||||||
$('#captcha-btn')[0],
|
$('#captcha-btn')[0],
|
||||||
$('#captcha-btn').data('app-id'),
|
$('#captcha-btn').data('app-id'),
|
||||||
@ -81,6 +83,7 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
@ -30,8 +30,10 @@
|
|||||||
<label class="layui-form-label">后台角色</label>
|
<label class="layui-form-label">后台角色</label>
|
||||||
<div class="layui-input-block">
|
<div class="layui-input-block">
|
||||||
<input type="radio" name="admin_role" value="0" title="无" checked="checked">
|
<input type="radio" name="admin_role" value="0" title="无" checked="checked">
|
||||||
{% for item in roles %}
|
{% for role in roles %}
|
||||||
<input type="radio" name="admin_role" value="{{ item.id }}" title="{{ item.name }}">
|
{% if role.id > 1 %}
|
||||||
|
<input type="radio" name="admin_role" value="{{ role.id }}" title="{{ role.name }}">
|
||||||
|
{% endif %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -27,8 +27,8 @@
|
|||||||
<div class="layui-form-item">
|
<div class="layui-form-item">
|
||||||
<label class="layui-form-label">教学角色</label>
|
<label class="layui-form-label">教学角色</label>
|
||||||
<div class="layui-input-block">
|
<div class="layui-input-block">
|
||||||
<input type="radio" name="edu_role" value="1" title="学员" {% if user.edu_role == '1' %}checked{% endif %}>
|
<input type="radio" name="edu_role" value="1" title="学员" {% if user.edu_role == 1 %}checked{% endif %}>
|
||||||
<input type="radio" name="edu_role" value="2" title="讲师" {% if user.edu_role == '2' %}checked{% endif %}>
|
<input type="radio" name="edu_role" value="2" title="讲师" {% if user.edu_role == 2 %}checked{% endif %}>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{% if auth_user.root == 1 %}
|
{% if auth_user.root == 1 %}
|
||||||
@ -36,8 +36,10 @@
|
|||||||
<label class="layui-form-label">后台角色</label>
|
<label class="layui-form-label">后台角色</label>
|
||||||
<div class="layui-input-block">
|
<div class="layui-input-block">
|
||||||
<input type="radio" name="admin_role" value="0" title="无" {% if user.admin_role == 0 %}checked{% endif %}>
|
<input type="radio" name="admin_role" value="0" title="无" {% if user.admin_role == 0 %}checked{% endif %}>
|
||||||
{% for item in roles %}
|
{% for role in roles %}
|
||||||
<input type="radio" name="admin_role" value="{{ item.id }}" title="{{ item.name }}" {% if user.admin_role == item.id %}checked{% endif %}>
|
{% if role.id > 1 %}
|
||||||
|
<input type="radio" name="admin_role" value="{{ role.id }}" title="{{ role.name }}" {% if user.admin_role == role.id %}checked{% endif %}>
|
||||||
|
{% endif %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
60
db/migrations/20200901121917_insert_role_data.php
Normal file
60
db/migrations/20200901121917_insert_role_data.php
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
use Phinx\Migration\AbstractMigration;
|
||||||
|
|
||||||
|
final class InsertRoleData extends AbstractMigration
|
||||||
|
{
|
||||||
|
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
$now = time();
|
||||||
|
|
||||||
|
$rows = [
|
||||||
|
[
|
||||||
|
'id' => 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');
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user