/* * DBeaver - Universal Database Manager * Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org) * Copyright (C) 2011-2012 Eugene Fradkin (eugene.fradkin@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.jkiss.dbeaver.ext.mysql.edit; import org.jkiss.code.Nullable; import org.jkiss.dbeaver.ext.mysql.MySQLMessages; import org.jkiss.dbeaver.ext.mysql.model.MySQLDataSource; import org.jkiss.dbeaver.ext.mysql.model.MySQLUser; import org.jkiss.dbeaver.model.edit.*; import org.jkiss.dbeaver.model.edit.prop.DBECommandComposite; import org.jkiss.dbeaver.model.exec.DBCSession; import org.jkiss.dbeaver.model.impl.DBSObjectCache; import org.jkiss.dbeaver.model.impl.edit.AbstractObjectManager; import org.jkiss.dbeaver.model.impl.edit.DBECommandAbstract; import org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction; import org.jkiss.dbeaver.model.impl.edit.SQLScriptCommand; import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor; import org.jkiss.dbeaver.model.struct.DBSObject; import java.util.Map; /** * MySQLUserManager */ public class MySQLUserManager extends AbstractObjectManager<MySQLUser> implements DBEObjectMaker<MySQLUser, MySQLDataSource>, DBECommandFilter<MySQLUser> { @Override public long getMakerOptions() { return FEATURE_EDITOR_ON_CREATE; } @Nullable @Override public DBSObjectCache<? extends DBSObject, MySQLUser> getObjectsCache(MySQLUser object) { return null; } @Override public boolean canCreateObject(MySQLDataSource parent) { return true; } @Override public boolean canDeleteObject(MySQLUser object) { return true; } @Override public MySQLUser createNewObject(DBRProgressMonitor monitor, DBECommandContext commandContext, MySQLDataSource parent, Object copyFrom) { MySQLUser newUser = new MySQLUser(parent, null); if (copyFrom instanceof MySQLUser) { MySQLUser tplUser = (MySQLUser)copyFrom; newUser.setUserName(tplUser.getUserName()); newUser.setHost(tplUser.getHost()); newUser.setMaxQuestions(tplUser.getMaxQuestions()); newUser.setMaxUpdates(tplUser.getMaxUpdates()); newUser.setMaxConnections(tplUser.getMaxConnections()); newUser.setMaxUserConnections(tplUser.getMaxUserConnections()); } commandContext.addCommand(new CommandCreateUser(newUser), new CreateObjectReflector<>(this), true); return newUser; } @Override public void deleteObject(DBECommandContext commandContext, MySQLUser user, Map<String, Object> options) { commandContext.addCommand(new CommandDropUser(user), new DeleteObjectReflector<>(this), true); } @Override public void filterCommands(DBECommandQueue<MySQLUser> queue) { if (!queue.isEmpty()) { // Add privileges flush to the tail queue.add( new SQLScriptCommand<>( queue.getObject(), MySQLMessages.edit_user_manager_command_flush_privileges, "FLUSH PRIVILEGES")); //$NON-NLS-1$ } } private static class CommandCreateUser extends DBECommandAbstract<MySQLUser> { protected CommandCreateUser(MySQLUser user) { super(user, MySQLMessages.edit_user_manager_command_create_user); } } private static class CommandDropUser extends DBECommandComposite<MySQLUser, UserPropertyHandler> { protected CommandDropUser(MySQLUser user) { super(user, MySQLMessages.edit_user_manager_command_drop_user); } @Override public DBEPersistAction[] getPersistActions() { return new DBEPersistAction[] { new SQLDatabasePersistAction(MySQLMessages.edit_user_manager_command_drop_user, "DROP USER " + getObject().getFullName()) { //$NON-NLS-2$ @Override public void handleExecute(DBCSession session, Throwable error) { if (error == null) { getObject().setPersisted(false); } } }}; } } }