/*****************************************************************************
* Copyright (C) 2008 EnterpriseDB Corporation.
* Copyright (C) 2011 Stado Global Development Group.
*
* This file is part of Stado.
*
* Stado is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Stado is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Stado. If not, see <http://www.gnu.org/licenses/>.
*
* You can find Stado at http://www.stado.us
*
****************************************************************************/
package org.postgresql.stado.parser;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import org.postgresql.stado.common.util.XLogger;
import org.postgresql.stado.engine.Engine;
import org.postgresql.stado.engine.ExecutionResult;
import org.postgresql.stado.engine.IPreparable;
import org.postgresql.stado.engine.XDBSessionContext;
import org.postgresql.stado.exception.XDBSecurityException;
import org.postgresql.stado.metadata.DBNode;
import org.postgresql.stado.metadata.MetaData;
import org.postgresql.stado.metadata.SyncDropUser;
import org.postgresql.stado.metadata.SysDatabase;
import org.postgresql.stado.metadata.SysLogin;
import org.postgresql.stado.metadata.SysTable;
import org.postgresql.stado.metadata.SysUser;
import org.postgresql.stado.metadata.scheduler.ILockCost;
import org.postgresql.stado.metadata.scheduler.LockSpecification;
import org.postgresql.stado.parser.core.syntaxtree.DropUser;
import org.postgresql.stado.parser.core.visitor.DepthFirstVoidArguVisitor;
import org.postgresql.stado.parser.handler.IdentifierHandler;
public class SqlDropUser extends DepthFirstVoidArguVisitor implements IXDBSql,
IPreparable {
private static final XLogger logger = XLogger.getLogger(SqlDropUser.class);
private XDBSessionContext client;
private String iUserName = null;
private SysLogin login;
public SqlDropUser(XDBSessionContext client) {
this.client = client;
}
// ******************************
// BEGIN GRAMMAR
// ******************************
/**
* Grammar production:
* f0 -> <DROP_>
* f1 -> <USER_>
* f2 -> Identifier(prn)
*/
@Override
public void visit(DropUser n, Object argu) {
iUserName = (String) n.f2.accept(new IdentifierHandler(), argu);
}
/**
* This will return the cost of executing this statement in time , milli
* seconds
*/
public long getCost() {
return ILockCost.LOW_COST;
}
/**
* This return the Lock Specification for the system
*
* @param theMData
* @return
*/
public LockSpecification<SysTable> getLockSpecs() {
Collection<SysTable> empty = Collections.emptyList();
LockSpecification<SysTable> aLspec = new LockSpecification<SysTable>(
empty, empty);
return aLspec;
}
/*
* (non-Javadoc)
*
* @see org.postgresql.stado.Parser.IXDBSql#getNodeList()
*/
public Collection<DBNode> getNodeList() {
Collection<DBNode> empty = Collections.emptyList();
return new ArrayList<DBNode>(empty);
}
/**
* @return Returns the iUserName.
*/
public String getUserName() {
return iUserName;
}
/**
* @return Returns the user.
*/
public SysLogin getLogin() {
return login;
}
public boolean isPrepared() {
return login != null;
}
public void prepare() throws Exception {
if (!isPrepared()) {
if (client.getCurrentUser().getUserClass() != SysLogin.USER_CLASS_DBA) {
XDBSecurityException ex = new XDBSecurityException("Only "
+ SysLogin.USER_CLASS_DBA_STR + " can drop users");
logger.throwing(ex);
throw ex;
}
login = MetaData.getMetaData().getSysLogin(iUserName);
// This method will throw an exception if user owns any objects
login.canSetUserClass(SysLogin.USER_CLASS_STANDARD_STR);
// Check if user has any permissions
for (SysDatabase database : MetaData.getMetaData().getSysDatabases()) {
SysUser dbUser = database.getSysUser(iUserName);
String granted = dbUser.getGrantedStr();
if (granted != null) {
XDBSecurityException ex = new XDBSecurityException("User "
+ iUserName + " has permissions on some objects: "
+ granted);
logger.throwing(ex);
throw ex;
}
}
}
}
public ExecutionResult execute(Engine engine) throws Exception {
if (!isPrepared()) {
prepare();
}
SyncDropUser sync = new SyncDropUser(this);
MetaData meta = MetaData.getMetaData();
meta.beginTransaction();
try {
sync.execute(client);
meta.commitTransaction(sync);
} catch (Exception e) {
logger.catching(e);
meta.rollbackTransaction();
throw e;
}
return ExecutionResult
.createSuccessResult(ExecutionResult.COMMAND_DROP_USER);
}
/*
* (non-Javadoc)
*
* @see org.postgresql.stado.MetaData.Scheduler.ILockCost#needCoordinatorConnection()
*/
public boolean needCoordinatorConnection() {
return false;
}
@Override
public boolean isReadOnly() {
// TODO Auto-generated method stub
return false;
}
}