mirror of
https://gitee.com/koogua/course-tencent-cloud.git
synced 2025-07-20 15:06: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;
|
||||
|
||||
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();
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -68,7 +68,9 @@
|
||||
<script>
|
||||
|
||||
layui.use(['jquery', 'form'], function () {
|
||||
|
||||
var $ = layui.jquery;
|
||||
|
||||
var captcha = new TencentCaptcha(
|
||||
$('#captcha-btn')[0],
|
||||
$('#captcha-btn').data('app-id'),
|
||||
@ -81,6 +83,7 @@
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
});
|
||||
|
||||
</script>
|
||||
|
@ -30,8 +30,10 @@
|
||||
<label class="layui-form-label">后台角色</label>
|
||||
<div class="layui-input-block">
|
||||
<input type="radio" name="admin_role" value="0" title="无" checked="checked">
|
||||
{% for item in roles %}
|
||||
<input type="radio" name="admin_role" value="{{ item.id }}" title="{{ item.name }}">
|
||||
{% for role in roles %}
|
||||
{% if role.id > 1 %}
|
||||
<input type="radio" name="admin_role" value="{{ role.id }}" title="{{ role.name }}">
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
</div>
|
||||
</div>
|
||||
|
@ -27,8 +27,8 @@
|
||||
<div class="layui-form-item">
|
||||
<label class="layui-form-label">教学角色</label>
|
||||
<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="2" title="讲师" {% if user.edu_role == '2' %}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 %}>
|
||||
</div>
|
||||
</div>
|
||||
{% if auth_user.root == 1 %}
|
||||
@ -36,8 +36,10 @@
|
||||
<label class="layui-form-label">后台角色</label>
|
||||
<div class="layui-input-block">
|
||||
<input type="radio" name="admin_role" value="0" title="无" {% if user.admin_role == 0 %}checked{% endif %}>
|
||||
{% for item in roles %}
|
||||
<input type="radio" name="admin_role" value="{{ item.id }}" title="{{ item.name }}" {% if user.admin_role == item.id %}checked{% endif %}>
|
||||
{% for role in roles %}
|
||||
{% 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 %}
|
||||
</div>
|
||||
</div>
|
||||
@ -67,7 +69,7 @@
|
||||
<input type="hidden" name="type" value="user">
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</form>
|
||||
|
||||
{% if auth_user.root == 1 %}
|
||||
<form class="layui-form kg-form" method="POST" action="{{ url({'for':'admin.user.update','id':user.id}) }}">
|
||||
|
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