1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-03-14 01:34:47 +08:00

feat: 完成数据库改密及权限编辑操作

This commit is contained in:
ssongliu 2022-10-24 18:46:19 +08:00 committed by ssongliu
parent 7159e941aa
commit 53d8aede66
9 changed files with 323 additions and 68 deletions

View File

@ -25,6 +25,23 @@ func (b *BaseApi) CreateMysql(c *gin.Context) {
helper.SuccessWithData(c, nil)
}
func (b *BaseApi) UpdateMysql(c *gin.Context) {
var req dto.ChangeDBInfo
if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
if err := global.VALID.Struct(req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
if err := mysqlService.ChangeInfo(req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithData(c, nil)
}
func (b *BaseApi) SearchMysql(c *gin.Context) {
var req dto.SearchWithPage
if err := c.ShouldBindJSON(&req); err != nil {
@ -73,7 +90,7 @@ func (b *BaseApi) LoadStatus(c *gin.Context) {
}
func (b *BaseApi) LoadConf(c *gin.Context) {
data, err := mysqlService.LoadConf("")
data, err := mysqlService.LoadVariables("")
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return

View File

@ -15,11 +15,11 @@ type MysqlDBInfo struct {
type MysqlDBCreate struct {
Name string `json:"name" validate:"required"`
Version string `json:"version" validate:"required,oneof=5.7.39 8.0.30"`
Format string `json:"format" validate:"required,oneof=utf8mb4 utf-8 gbk big5"`
Username string `json:"username" validate:"required"`
Password string `json:"password" validate:"required"`
Permission string `json:"permission" validate:"required,oneof=local all ip"`
PermissionIPs string `json:"permissionIPs"`
Permission string `json:"permission" validate:"required"`
Description string `json:"description"`
}
@ -61,7 +61,7 @@ type MysqlStatus struct {
Position string `json:"Position"`
}
type MysqlConf struct {
type MysqlVariables struct {
BinlogCachSize string `json:"binlog_cache_size"`
InnodbBufferPoolSize string `json:"innodb_buffer_pool_size"`
InnodbLogBufferSize string `json:"innodb_log_buffer_size"`
@ -79,3 +79,13 @@ type MysqlConf struct {
ThreadStack string `json:"thread_stack"`
Tmp_tableSize string `json:"tmp_table_size"`
}
type ChangeDBInfo struct {
ID uint `json:"id" validate:"required"`
Operation string `json:"operation" validate:"required,oneof=password privilege"`
Value string `json:"value" validate:"required"`
}
type BatchDeleteByName struct {
Names []string `json:"names" validate:"required"`
}

View File

@ -3,10 +3,10 @@ package model
type DatabaseMysql struct {
BaseModel
Name string `json:"name" gorm:"type:varchar(256);not null"`
Version string `json:"version" gorm:"type:varchar(64);not null"`
Format string `json:"format" gorm:"type:varchar(64);not null"`
Username string `json:"username" gorm:"type:varchar(256);not null"`
Password string `json:"password" gorm:"type:varchar(256);not null"`
Permission string `json:"permission" gorm:"type:varchar(256);not null"`
PermissionIPs string `json:"permissionIPs" gorm:"type:varchar(256)"`
Description string `json:"description" gorm:"type:varchar(256);"`
}

View File

@ -8,9 +8,12 @@ import (
type MysqlRepo struct{}
type IMysqlRepo interface {
Get(opts ...DBOption) (model.DatabaseMysql, error)
List(opts ...DBOption) ([]model.DatabaseMysql, error)
Page(limit, offset int, opts ...DBOption) (int64, []model.DatabaseMysql, error)
Create(mysql *model.DatabaseMysql) error
Delete(opts ...DBOption) error
Update(id uint, vars map[string]interface{}) error
}
func NewIMysqlRepo() IMysqlRepo {
@ -27,6 +30,16 @@ func (u *MysqlRepo) Get(opts ...DBOption) (model.DatabaseMysql, error) {
return mysql, err
}
func (u *MysqlRepo) List(opts ...DBOption) ([]model.DatabaseMysql, error) {
var users []model.DatabaseMysql
db := global.DB.Model(&model.DatabaseMysql{})
for _, opt := range opts {
db = opt(db)
}
err := db.Find(&users).Error
return users, err
}
func (u *MysqlRepo) Page(page, size int, opts ...DBOption) (int64, []model.DatabaseMysql, error) {
var users []model.DatabaseMysql
db := global.DB.Model(&model.DatabaseMysql{})
@ -50,3 +63,7 @@ func (u *MysqlRepo) Delete(opts ...DBOption) error {
}
return db.Delete(&model.DatabaseMysql{}).Error
}
func (u *MysqlRepo) Update(id uint, vars map[string]interface{}) error {
return global.DB.Model(&model.DatabaseMysql{}).Where("id = ?", id).Updates(vars).Error
}

View File

@ -4,6 +4,8 @@ import (
"database/sql"
"encoding/json"
"fmt"
"strconv"
"time"
"github.com/1Panel-dev/1Panel/backend/app/dto"
"github.com/1Panel-dev/1Panel/backend/constant"
@ -17,15 +19,41 @@ type MysqlService struct{}
type IMysqlService interface {
SearchWithPage(search dto.SearchWithPage) (int64, interface{}, error)
Create(mysqlDto dto.MysqlDBCreate) error
ChangeInfo(info dto.ChangeDBInfo) error
Delete(ids []uint) error
LoadStatus(version string) (*dto.MysqlStatus, error)
LoadConf(version string) (*dto.MysqlConf, error)
LoadVariables(version string) (*dto.MysqlVariables, error)
}
func NewIMysqlService() IMysqlService {
return &MysqlService{}
}
func newDatabaseClient() (*sql.DB, error) {
connArgs := fmt.Sprintf("%s:%s@tcp(%s:%d)/?charset=utf8", "root", "Calong@2015", "localhost", 2306)
db, err := sql.Open("mysql", connArgs)
if err != nil {
return nil, err
}
return db, nil
}
func handleSql(db *sql.DB, query string) (map[string]string, error) {
variableMap := make(map[string]string)
rows, err := db.Query(query)
if err != nil {
return variableMap, err
}
for rows.Next() {
var variableName, variableValue string
if err := rows.Scan(&variableName, &variableValue); err != nil {
return variableMap, err
}
variableMap[variableName] = variableValue
}
return variableMap, err
}
func (u *MysqlService) SearchWithPage(search dto.SearchWithPage) (int64, interface{}, error) {
total, mysqls, err := mysqlRepo.Page(search.Page, search.PageSize, commonRepo.WithLikeName(search.Info))
var dtoMysqls []dto.MysqlDBInfo
@ -40,6 +68,9 @@ func (u *MysqlService) SearchWithPage(search dto.SearchWithPage) (int64, interfa
}
func (u *MysqlService) Create(mysqlDto dto.MysqlDBCreate) error {
if mysqlDto.Username == "root" {
return errors.New("Cannot set root as user name")
}
mysql, _ := mysqlRepo.Get(commonRepo.WithByName(mysqlDto.Name))
if mysql.ID != 0 {
return constant.ErrRecordExist
@ -47,6 +78,25 @@ func (u *MysqlService) Create(mysqlDto dto.MysqlDBCreate) error {
if err := copier.Copy(&mysql, &mysqlDto); err != nil {
return errors.WithMessage(constant.ErrStructTransform, err.Error())
}
sql, err := newDatabaseClient()
if err != nil {
return err
}
defer sql.Close()
if _, err := sql.Exec(fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s CHARACTER SET=%s", mysqlDto.Name, mysqlDto.Format)); err != nil {
return err
}
tmpPermission := mysqlDto.Permission
if _, err := sql.Exec(fmt.Sprintf("CREATE USER '%s'@'%s' IDENTIFIED BY '%s';", mysqlDto.Name, tmpPermission, mysqlDto.Password)); err != nil {
return err
}
grantStr := fmt.Sprintf("GRANT ALL PRIVILEGES ON %s.* TO '%s'@'%s'", mysqlDto.Name, mysqlDto.Username, tmpPermission)
if mysqlDto.Version == "5.7.39" {
grantStr = fmt.Sprintf("%s IDENTIFIED BY '%s' WITH GRANT OPTION;", grantStr, mysqlDto.Password)
}
if _, err := sql.Exec(grantStr); err != nil {
return err
}
if err := mysqlRepo.Create(&mysql); err != nil {
return err
}
@ -54,38 +104,78 @@ func (u *MysqlService) Create(mysqlDto dto.MysqlDBCreate) error {
}
func (u *MysqlService) Delete(ids []uint) error {
if len(ids) == 1 {
mysql, _ := mysqlRepo.Get(commonRepo.WithByID(ids[0]))
if mysql.ID == 0 {
return constant.ErrRecordNotFound
dbClient, err := newDatabaseClient()
if err != nil {
return err
}
return mysqlRepo.Delete(commonRepo.WithByID(ids[0]))
}
return mysqlRepo.Delete(commonRepo.WithIdsIn(ids))
defer dbClient.Close()
dbs, err := mysqlRepo.List(commonRepo.WithIdsIn(ids))
if err != nil {
return err
}
func (u *MysqlService) LoadConf(version string) (*dto.MysqlConf, error) {
connArgs := fmt.Sprintf("%s:%s@tcp(%s:%d)/?charset=utf8", "root", "Calong@2015", "localhost", 2306)
db, err := sql.Open("mysql", connArgs)
for _, db := range dbs {
if len(db.Name) != 0 {
if _, err := dbClient.Exec(fmt.Sprintf("DROP USER IF EXISTS '%s'@'%s'", db.Name, db.Permission)); err != nil {
return err
}
if _, err := dbClient.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %s", db.Name)); err != nil {
return err
}
}
_ = mysqlRepo.Delete(commonRepo.WithByID(db.ID))
}
return nil
}
func (u *MysqlService) ChangeInfo(info dto.ChangeDBInfo) error {
mysql, err := mysqlRepo.Get(commonRepo.WithByID(info.ID))
if err != nil {
return err
}
db, err := newDatabaseClient()
if err != nil {
return err
}
defer db.Close()
if info.Operation == "password" {
if _, err := db.Exec(fmt.Sprintf("SET PASSWORD FOR %s@%s = password('%s')", mysql.Username, mysql.Permission, info.Value)); err != nil {
return err
}
_ = mysqlRepo.Update(mysql.ID, map[string]interface{}{"password": info.Value})
return nil
}
if _, err := db.Exec(fmt.Sprintf("DROP USER IF EXISTS '%s'@'%s'", mysql.Name, mysql.Permission)); err != nil {
return err
}
grantStr := fmt.Sprintf("GRANT ALL PRIVILEGES ON %s.* TO '%s'@'%s'", mysql.Name, mysql.Username, info.Value)
if mysql.Version == "5.7.39" {
grantStr = fmt.Sprintf("%s IDENTIFIED BY '%s' WITH GRANT OPTION;", grantStr, mysql.Password)
}
if _, err := db.Exec(grantStr); err != nil {
return err
}
if _, err := db.Exec("FLUSH PRIVILEGES"); err != nil {
return err
}
_ = mysqlRepo.Update(mysql.ID, map[string]interface{}{"permission": info.Value})
return nil
}
func (u *MysqlService) LoadVariables(version string) (*dto.MysqlVariables, error) {
db, err := newDatabaseClient()
if err != nil {
return nil, err
}
defer db.Close()
rows, err := db.Query("show variables")
variableMap, err := handleSql(db, "SHOW VARIABLES")
if err != nil {
return nil, err
}
variableMap := make(map[string]string)
for rows.Next() {
var variableName, variableValue string
if err := rows.Scan(&variableName, &variableValue); err != nil {
continue
}
variableMap[variableName] = variableValue
}
var info dto.MysqlConf
var info dto.MysqlVariables
arr, err := json.Marshal(variableMap)
if err != nil {
return nil, err
@ -95,31 +185,51 @@ func (u *MysqlService) LoadConf(version string) (*dto.MysqlConf, error) {
}
func (u *MysqlService) LoadStatus(version string) (*dto.MysqlStatus, error) {
connArgs := fmt.Sprintf("%s:%s@tcp(%s:%d)/?charset=utf8", "root", "Calong@2015", "localhost", 2306)
db, err := sql.Open("mysql", connArgs)
db, err := newDatabaseClient()
if err != nil {
return nil, err
}
defer db.Close()
rows, err := db.Query("show status")
globalMap, err := handleSql(db, "SHOW GLOBAL STATUS")
if err != nil {
return nil, err
}
variableMap := make(map[string]string)
for rows.Next() {
var variableName, variableValue string
if err := rows.Scan(&variableName, &variableValue); err != nil {
continue
}
variableMap[variableName] = variableValue
}
var info dto.MysqlStatus
arr, err := json.Marshal(variableMap)
arr, err := json.Marshal(globalMap)
if err != nil {
return nil, err
}
_ = json.Unmarshal(arr, &info)
if value, ok := globalMap["Run"]; ok {
uptime, _ := strconv.Atoi(value)
info.Run = time.Unix(time.Now().Unix()-int64(uptime), 0).Format("2006-01-02 15:04:05")
} else {
if value, ok := globalMap["Uptime"]; ok {
uptime, _ := strconv.Atoi(value)
info.Run = time.Unix(time.Now().Unix()-int64(uptime), 0).Format("2006-01-02 15:04:05")
}
}
rows, err := db.Query("SHOW MASTER STATUS")
if err != nil {
return &info, err
}
masterRows := make([]string, 5)
for rows.Next() {
if err := rows.Scan(&masterRows[0], &masterRows[1], &masterRows[2], &masterRows[3], &masterRows[4]); err != nil {
return &info, err
}
}
info.File = masterRows[0]
if len(masterRows[0]) == 0 {
info.File = "OFF"
}
info.Position = masterRows[1]
if len(masterRows[1]) == 0 {
info.Position = "OFF"
}
return &info, nil
}

View File

@ -22,6 +22,7 @@ func (s *DatabaseRouter) InitDatabaseRouter(Router *gin.RouterGroup) {
baseApi := v1.ApiGroupApp.BaseApi
{
withRecordRouter.POST("", baseApi.CreateMysql)
withRecordRouter.PUT("/:id", baseApi.UpdateMysql)
withRecordRouter.POST("/del", baseApi.DeleteMysql)
cmdRouter.POST("/search", baseApi.SearchMysql)
cmdRouter.GET("/conf", baseApi.LoadConf)

View File

@ -7,16 +7,15 @@ export namespace Database {
username: string;
password: string;
permission: string;
permissionIPs: string;
description: string;
}
export interface MysqlDBCreate {
name: string;
version: string;
format: string;
username: string;
password: string;
permission: string;
permissionIPs: string;
description: string;
}
export interface MysqlVariables {
@ -26,9 +25,7 @@ export namespace Database {
join_buffer_size: number;
key_buffer_size: number;
max_connections: number;
max_heap_table_size: number;
query_cache_size: number;
query_cache_type: string;
read_buffer_size: number;
read_rnd_buffer_size: number;
sort_buffer_size: number;
@ -74,4 +71,9 @@ export namespace Database {
File: string;
Position: number;
}
export interface ChangeInfo {
id: number;
operation: string;
value: string;
}
}

View File

@ -9,7 +9,9 @@ export const searchMysqlDBs = (params: ReqPage) => {
export const addMysqlDB = (params: Database.MysqlDBCreate) => {
return http.post(`/databases`, params);
};
export const updateMysqlDBInfo = (params: Database.ChangeInfo) => {
return http.put(`/databases/${params.id}`, params);
};
export const deleteMysqlDB = (params: { ids: number[] }) => {
return http.post(`/databases/del`, params);
};

View File

@ -24,12 +24,12 @@
style="margin-top: 20px; margin-left: 10px"
size="default"
icon="Back"
@click="isOnSetting = false"
@click="onBacklist"
>
{{ $t('commons.button.back') }}列表
</el-button>
<Setting ref="settingRef" v-if="isOnSetting"></Setting>
<Setting ref="settingRef"></Setting>
<el-card v-if="!isOnSetting">
<ComplexTable :pagination-config="paginationConfig" v-model:selects="selects" @search="search" :data="data">
@ -61,6 +61,50 @@
</ComplexTable>
</el-card>
<el-dialog v-model="changeVisiable" :destroy-on-close="true" width="30%">
<template #header>
<div class="card-header">
<span>{{ $t('database.changePassword') }}</span>
</div>
</template>
<el-form>
<el-form ref="changeFormRef" :model="changeForm" label-width="80px">
<div v-if="changeForm.operation === 'password'">
<el-form-item :label="$t('auth.username')" prop="userName">
<el-input disabled v-model="changeForm.userName"></el-input>
</el-form-item>
<el-form-item :label="$t('auth.password')" prop="password" :rules="Rules.requiredInput">
<el-input type="password" clearable show-password v-model="changeForm.password"></el-input>
</el-form-item>
</div>
<div v-if="changeForm.operation === 'privilege'">
<el-form-item :label="$t('database.permission')" prop="privilege">
<el-select style="width: 100%" v-model="changeForm.privilege">
<el-option value="localhost" :label="$t('database.permissionLocal')" />
<el-option value="%" :label="$t('database.permissionAll')" />
<el-option value="ip" :label="$t('database.permissionForIP')" />
</el-select>
</el-form-item>
<el-form-item
v-if="changeForm.privilege === 'ip'"
prop="privilegeIPs"
:rules="Rules.requiredInput"
>
<el-input clearable v-model="changeForm.privilegeIPs" />
</el-form-item>
</div>
</el-form>
</el-form>
<template #footer>
<span class="dialog-footer">
<el-button @click="changeVisiable = false">{{ $t('commons.button.cancel') }}</el-button>
<el-button @click="submitChangeInfo(changeFormRef)">
{{ $t('commons.button.confirm') }}
</el-button>
</span>
</template>
</el-dialog>
<OperatrDialog @search="search" ref="dialogRef" />
</div>
</template>
@ -72,10 +116,13 @@ import Setting from '@/views/database/mysql/setting/index.vue';
import Submenu from '@/views/database/index.vue';
import { dateFromat } from '@/utils/util';
import { onMounted, reactive, ref } from 'vue';
import { deleteMysqlDB, searchMysqlDBs } from '@/api/modules/database';
import { deleteMysqlDB, searchMysqlDBs, updateMysqlDBInfo } from '@/api/modules/database';
import i18n from '@/lang';
import { Cronjob } from '@/api/interface/cronjob';
import { useDeleteData } from '@/hooks/use-delete-data';
import { ElForm, ElMessage } from 'element-plus';
import { Database } from '@/api/interface/database';
import { Rules } from '@/global/form-rules';
const selects = ref<any>([]);
const version = ref<string>('5.7.39');
@ -90,13 +137,49 @@ const paginationConfig = reactive({
const dialogRef = ref();
const onOpenDialog = async () => {
dialogRef.value!.acceptParams();
let params = {
version: version.value,
};
dialogRef.value!.acceptParams(params);
};
const settingRef = ref();
const onSetting = async () => {
isOnSetting.value = true;
console.log(settingRef.value);
let params = {
version: version.value,
};
settingRef.value!.acceptParams(params);
};
const onBacklist = async () => {
isOnSetting.value = false;
search();
settingRef.value!.onClose();
};
const changeVisiable = ref(false);
type FormInstance = InstanceType<typeof ElForm>;
const changeFormRef = ref<FormInstance>();
const changeForm = reactive({
id: 0,
userName: '',
password: '',
operation: '',
privilege: '',
privilegeIPs: '',
value: '',
});
const submitChangeInfo = async (formEl: FormInstance | undefined) => {
console.log(formEl);
if (!formEl) return;
formEl.validate(async (valid) => {
if (!valid) return;
changeForm.value = changeForm.operation === 'password' ? changeForm.password : changeForm.privilege;
await updateMysqlDBInfo(changeForm);
ElMessage.success(i18n.global.t('commons.msg.operationSuccess'));
search();
changeVisiable.value = false;
});
};
const search = async () => {
@ -123,29 +206,42 @@ const onBatchDelete = async (row: Cronjob.CronjobInfo | null) => {
};
const buttons = [
{
label: i18n.global.t('commons.button.edit'),
icon: 'Delete',
click: (row: Cronjob.CronjobInfo) => {
onBatchDelete(row);
label: i18n.global.t('database.changePassword'),
click: (row: Database.MysqlDBInfo) => {
changeForm.id = row.id;
changeForm.operation = 'password';
changeForm.userName = row.username;
changeForm.password = row.password;
changeVisiable.value = true;
},
},
{
label: i18n.global.t('database.permission'),
click: (row: Database.MysqlDBInfo) => {
changeForm.id = row.id;
changeForm.operation = 'privilege';
if (row.permission === '%' || row.permission === 'localhost') {
changeForm.privilege = row.permission;
} else {
changeForm.privilege = 'ip';
}
changeVisiable.value = true;
},
},
{
label: i18n.global.t('database.backupList') + '(1)',
icon: 'Delete',
click: (row: Cronjob.CronjobInfo) => {
onBatchDelete(row);
},
},
{
label: i18n.global.t('database.loadBackup'),
icon: 'Delete',
click: (row: Cronjob.CronjobInfo) => {
onBatchDelete(row);
},
},
{
label: i18n.global.t('commons.button.delete'),
icon: 'Delete',
click: (row: Cronjob.CronjobInfo) => {
onBatchDelete(row);
},