feat: select definer in view creation/edit

This commit is contained in:
Fabio Di Stasio 2020-12-29 10:35:46 +01:00
parent 0df2b836b1
commit ab307f82b1
7 changed files with 98 additions and 15 deletions

View File

@ -4,6 +4,7 @@ import views from './views';
import updates from './updates';
import application from './application';
import database from './database';
import users from './users';
const connections = {};
@ -12,6 +13,7 @@ export default () => {
tables(connections);
views(connections);
database(connections);
users(connections);
updates();
application();
};

View File

@ -0,0 +1,15 @@
import { ipcMain } from 'electron';
export default (connections) => {
ipcMain.handle('get-users', async (event, uid) => {
try {
const result = await connections[uid].getUsers();
return { status: 'success', response: result };
}
catch (err) {
if (err.code === 'ER_TABLEACCESS_DENIED_ERROR')
return { status: 'success', response: [] };
return { status: 'error', response: err.toString() };
}
});
};

View File

@ -145,9 +145,6 @@ export class MySQLClient extends AntaresCore {
};
});
// USERS
// TODO: SELECT `user`, `host`, IF(LENGTH(password)>0, password, authentication_string) AS `password` FROM `mysql`.`user`;
return {
name: db.Database,
tables: remappedTables,
@ -267,6 +264,24 @@ export class MySQLClient extends AntaresCore {
});
}
/**
* SELECT `user`, `host`, IF(LENGTH(password)>0, password, authentication_string) AS `password` FROM `mysql`.`user`
*
* @returns {Array.<Object>} users list
* @memberof MySQLClient
*/
async getUsers () {
const { rows } = await this.raw('SELECT `user`, `host`, IF(LENGTH(password)>0, password, authentication_string) AS `password` FROM `mysql`.`user`');
return rows.map(row => {
return {
name: row.user,
host: row.host,
password: row.password
};
});
}
/**
* SHOW CREATE VIEW
*
@ -308,7 +323,7 @@ export class MySQLClient extends AntaresCore {
*/
async alterView (params) {
const { view } = params;
let sql = `ALTER ALGORITHM = ${view.algorithm} DEFINER=${view.definer} SQL SECURITY ${view.security} VIEW \`${view.oldName}\` AS ${view.sql} ${view.updateOption ? `WITH ${view.updateOption} CHECK OPTION` : ''}`;
let sql = `ALTER ALGORITHM = ${view.algorithm}${view.definer ? ` DEFINER=${view.definer}` : ''} SQL SECURITY ${view.security} VIEW \`${view.oldName}\` AS ${view.sql} ${view.updateOption ? `WITH ${view.updateOption} CHECK OPTION` : ''}`;
if (view.name !== view.oldName)
sql += `; RENAME TABLE \`${view.oldName}\` TO \`${view.name}\``;

View File

@ -27,13 +27,18 @@
<div class="column col-6">
<div class="form-group">
<label class="form-label">{{ $t('word.definer') }}</label>
<input
v-model="localView.definer"
class="form-input"
type="text"
:placeholder="$t('message.currentUser')"
readonly
>
<select v-model="localView.definer" class="form-select">
<option value="">
{{ $t('message.currentUser') }}
</option>
<option
v-for="user in workspace.users"
:key="`${user.name}@${user.host}`"
:value="`\`${user.name}\`@\`${user.host}\``"
>
{{ user.name }}@{{ user.host }}
</option>
</select>
</div>
</div>
</div>

View File

@ -39,12 +39,27 @@
<div class="column col-3">
<div class="form-group">
<label class="form-label">{{ $t('word.definer') }}</label>
<input
<select
v-if="workspace.users.length"
v-model="localView.definer"
class="form-input"
type="text"
readonly
class="form-select"
>
<option value="">
{{ $t('message.currentUser') }}
</option>
<option
v-for="user in workspace.users"
:key="`${user.name}@${user.host}`"
:value="`\`${user.name}\`@\`${user.host}\``"
>
{{ user.name }}@{{ user.host }}
</option>
</select>
<select v-if="!workspace.users.length" class="form-select">
<option value="">
{{ $t('message.currentUser') }}
</option>
</select>
</div>
</div>
</div>

View File

@ -0,0 +1,8 @@
'use strict';
import { ipcRenderer } from 'electron';
export default class {
static getUsers (params) {
return ipcRenderer.invoke('get-users', params);
}
}

View File

@ -1,6 +1,7 @@
'use strict';
import Connection from '@/ipc-api/Connection';
import Database from '@/ipc-api/Database';
import Users from '@/ipc-api/Users';
import { uidGen } from 'common/libs/uidGen';
const tabIndex = [];
let lastBreadcrumbs = {};
@ -100,6 +101,14 @@ export default {
}
: workspace);
},
REFRESH_USERS (state, { uid, users }) {
state.workspaces = state.workspaces.map(workspace => workspace.uid === uid
? {
...workspace,
users
}
: workspace);
},
ADD_WORKSPACE (state, workspace) {
state.workspaces.push(workspace);
},
@ -221,6 +230,7 @@ export default {
dispatch('refreshCollations', connection.uid);
dispatch('refreshVariables', connection.uid);
dispatch('refreshEngines', connection.uid);
dispatch('refreshUsers', connection.uid);
}
}
catch (err) {
@ -275,6 +285,18 @@ export default {
dispatch('notifications/addNotification', { status: 'error', message: err.stack }, { root: true });
}
},
async refreshUsers ({ dispatch, commit }, uid) {
try {
const { status, response } = await Users.getUsers(uid);
if (status === 'error')
dispatch('notifications/addNotification', { status, message: response }, { root: true });
else
commit('REFRESH_USERS', { uid, users: response });
}
catch (err) {
dispatch('notifications/addNotification', { status: 'error', message: err.stack }, { root: true });
}
},
removeConnected ({ commit }, uid) {
Connection.disconnect(uid);
commit('REMOVE_CONNECTED', uid);
@ -300,6 +322,7 @@ export default {
structure: {},
variables: [],
collations: [],
users: [],
breadcrumbs: {}
};