GOFIGURE2  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
QGoDBInitCreateUserPage.cxx
Go to the documentation of this file.
1 /*=========================================================================
2  Authors: The GoFigure Dev. Team.
3  at Megason Lab, Systems biology, Harvard Medical school, 2009-11
4 
5  Copyright (c) 2009-11, President and Fellows of Harvard College.
6  All rights reserved.
7 
8  Redistribution and use in source and binary forms, with or without
9  modification, are permitted provided that the following conditions are met:
10 
11  Redistributions of source code must retain the above copyright notice,
12  this list of conditions and the following disclaimer.
13  Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16  Neither the name of the President and Fellows of Harvard College
17  nor the names of its contributors may be used to endorse or promote
18  products derived from this software without specific prior written
19  permission.
20 
21  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
25  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
26  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27  OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
30  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 
33 =========================================================================*/
35 #include "CreateDataBaseHelper.h"
37 #include "vtkSQLQuery.h"
38 #include <QFormLayout>
39 #include <QMessageBox>
40 #include <QVariant>
41 #include <QInputDialog>
42 #include <QDir>
43 #include <QDialogButtonBox>
44 #include <QLabel>
45 #include <QTextLayout>
46 
48  QWizardPage(iParent)
49 {
50  QFont tfont;
51 
52  tfont.setBold(false);
53  this->setFont(tfont);
54 
55  m_ServerName = "localhost";
56  m_DBName = "gofiguredatabase";
57 
58  setSubTitle( tr("Create a user for MySQL Database local Server:") );
59 
60  QFormLayout *formLayout = new QFormLayout;
61  lineUserName = new QLineEdit;
62  linePassword = new QLineEdit;
63  linePassword->setEchoMode(QLineEdit::Password);
65 
66  formLayout->addRow(tr("Choose a User name:"), lineUserName);
67  formLayout->addRow(tr("Choose a Password:"), linePassword);
68  setLayout(formLayout);
69 
70  setLayout(formLayout);
71 
72  registerField("User", lineUserName);
73  registerField("Password", linePassword);
74 }
75 
76 //-------------------------------------------------------------------------
77 
78 //-------------------------------------------------------------------------
80 {
81  QMessageBox msgBox;
82 
83  if ( field("User").toString() == ""
84  || field("Password").toString() == "" )
85  {
86  msgBox.setText(
87  tr("Please fill all the fields.") );
88  msgBox.exec();
89  return false;
90  }
91  if ( !this->CreateUser() )
92  {
93  return false;
94  }
95  if (
96  !CreateGoFigureDataBase(this->m_ServerName, field("User").toString().toStdString(),
97  field("Password").toString().toStdString(), this->m_DBName) )
98  {
99  msgBox.setText(
100  tr("There is a problem with the creation of your database,check the password for your user.") );
101  msgBox.exec();
102  return false;
103  }
104  emit UserAndDatabaseCreated();
105  return true;
106 }
107 
108 //-------------------------------------------------------------------------
109 
110 //-------------------------------------------------------------------------
112 {
113  std::string Login = field("User").toString().toStdString();
114  std::string Password = field("Password").toString().toStdString();
115 
116  vtkMySQLDatabase *DataBaseConnector = vtkMySQLDatabase::New();
117 
118  DataBaseConnector->SetHostName( this->m_ServerName.c_str() );
119  DataBaseConnector->SetUser("root");
120  bool ok;
121  QString text = QInputDialog::getText(0, "Enter your root password for MySQL:",
122  "Root Password for MySQL:", QLineEdit::Password,
123  QDir::home().dirName(), &ok);
124  if ( ok )
125  {
126  if ( text.isEmpty() )
127  {
128  DataBaseConnector->SetPassword("");
129  }
130  else
131  {
132  DataBaseConnector->SetPassword( text.toStdString().c_str() );
133  }
134  if ( !DataBaseConnector->Open() )
135  {
136  QMessageBox msgBox;
137  msgBox.setText(
138  tr("There is a problem with the connection to your root.") );
139  msgBox.exec();
140  return false;
141  }
142  }
143  else //the user clicks on something else than ok when asking for the mysql
144  // root password:
145  {
146  return false;
147  }
148  if ( !this->UserNameAlreadyExits(DataBaseConnector, Login) )
149  {
150  if ( this->QuestionToUser(
151  tr("Do you want to create this new user with a new database?") ) )
152  {
153  return this->CreateGofigureUserWithDatabaseConnector(DataBaseConnector, Login,
154  this->m_ServerName, Password);
155  }
156  else
157  {
158  return false;
159  }
160  }
161  else
162  {
163  return this->QuestionToUser(
164  tr("The user you entered already exists, \ndo you want to create a database for this user?") );
165  }
166  return false;
167 }
168 
169 //-------------------------------------------------------------------------
170 
171 //-------------------------------------------------------------------------
173  vtkMySQLDatabase *DatabaseConnector, std::string iLogin,
174  std::string iServerName, std::string iPassword)
175 {
176  vtkSQLQuery * query = DatabaseConnector->GetQueryInstance();
177  std::stringstream queryScript;
178 
179  queryScript << "CREATE USER '";
180  queryScript << iLogin;
181  queryScript << "'@'";
182  queryScript << iServerName;
183  queryScript << "' IDENTIFIED BY '";
184  queryScript << iPassword;
185  queryScript << "';";
186  query->SetQuery( queryScript.str().c_str() );
187  query->Execute();
188  query->Delete();
189 
190  vtkSQLQuery * queryPrivileges = DatabaseConnector->GetQueryInstance();
191  std::stringstream PrivilegesScript;
192  PrivilegesScript << "GRANT ALL PRIVILEGES ON *.* TO '";
193  PrivilegesScript << iLogin;
194  PrivilegesScript << "'@'";
195  PrivilegesScript << iServerName;
196  PrivilegesScript << "';";
197  queryPrivileges->SetQuery( PrivilegesScript.str().c_str() );
198  if ( !queryPrivileges->Execute() )
199  {
200  DatabaseConnector->Close();
201  DatabaseConnector->Delete();
202  queryPrivileges->Delete();
203  QMessageBox msgBox;
204  msgBox.setText(
205  tr("Sorry, there is a problem with the creation of your user.") );
206  msgBox.exec();
207  return false;
208  }
209  queryPrivileges->Delete();
210  DatabaseConnector->Close();
211  DatabaseConnector->Delete();
212  return true;
213 }
214 
215 //-------------------------------------------------------------------------
216 
217 //-------------------------------------------------------------------------
218 bool
219 QGoDBInitCreateUserPage::UserNameAlreadyExits(vtkMySQLDatabase *DatabaseConnector,
220  std::string iLogin)
221 {
222  vtkSQLQuery * queryUserExist = DatabaseConnector->GetQueryInstance();
223  std::stringstream UserExistScript;
224 
225  UserExistScript << "SELECT USER FROM mysql.user WHERE user = '";
226  UserExistScript << iLogin;
227  UserExistScript << "';";
228  queryUserExist->SetQuery( UserExistScript.str().c_str() );
229  if ( !queryUserExist->Execute() )
230  {
231  QMessageBox msgBox;
232  msgBox.setText(
233  tr("There is a problem to check your existing users.") );
234  msgBox.exec();
235  queryUserExist->Delete();
236  DatabaseConnector->Close();
237  DatabaseConnector->Delete();
238  queryUserExist->Delete();
239  return false;
240  }
241  if ( queryUserExist->NextRow() )
242  {
243  queryUserExist->Delete();
244  DatabaseConnector->Close();
245  DatabaseConnector->Delete();
246  return true;
247  }
248 
249  queryUserExist->Delete();
250  return false;
251 }
252 
253 //-------------------------------------------------------------------------
254 
255 //-------------------------------------------------------------------------
257 {
258  QDialogButtonBox *button = new QDialogButtonBox(QDialogButtonBox::Ok
259  | QDialogButtonBox::Cancel);
260  QDialog * Dialog = new QDialog;
261  QLabel * Label = new QLabel(iQuestion);
262  QFormLayout *Layout = new QFormLayout(this);
263 
264  Layout->addWidget(Label);
265  Layout->addWidget(button);
266  Dialog->setLayout(Layout);
267  QObject::connect( button, SIGNAL( accepted() ), Dialog, SLOT( accept() ) );
268  QObject::connect( button, SIGNAL( rejected() ), Dialog, SLOT( reject() ) );
269 
270  int Result = Dialog->exec();
271  if ( Result == 0 )
272  {
273  return false;
274  }
275  return true;
276 }
std::string toStdString() const
void registerField(const QString &name, QWidget *widget, const char *property, const char *changedSignal)
void setSubTitle(const QString &subTitle)
void setEchoMode(EchoMode)
int exec()
QString tr(const char *sourceText, const char *disambiguation, int n)
bool CreateGofigureUserWithDatabaseConnector(vtkMySQLDatabase *DatabaseConnector, std::string iLogin, std::string iServerName, std::string iPassword)
void setBold(bool enable)
QGoDBInitCreateUserPage(QWidget *iparent=0)
void setLayout(QLayout *layout)
bool isEmpty() const
void setText(const QString &text)
void addWidget(QWidget *w)
void addRow(QWidget *label, QWidget *field)
QVariant field(const QString &name) const
QString getText(QWidget *parent, const QString &title, const QString &label, QLineEdit::EchoMode mode, const QString &text, bool *ok, QFlags< Qt::WindowType > flags, QFlags< Qt::InputMethodHint > inputMethodHints)
void setFont(const QFont &)
bool QuestionToUser(QString iQuestion)
create a dialog with OK and Cancel button asking iquestion to the user, return true if the user press...
bool UserNameAlreadyExits(vtkMySQLDatabase *DatabaseConnector, std::string iLogin)
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
bool CreateGoFigureDataBase(std::string ServerName, std::string login, std::string Password, std::string DBName)
QString toString() const
QDir home()