1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
|
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin III - PostgreSQL Tools
//
// Copyright (C) 2002 - 2014, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
// dlgUserMapping.cpp - PostgreSQL User Mapping Property
//
//////////////////////////////////////////////////////////////////////////
// wxWindows headers
#include <wx/wx.h>
// App headers
#include "pgAdmin3.h"
#include "utils/misc.h"
#include "utils/pgDefs.h"
#include "dlg/dlgUserMapping.h"
#include "schema/pgUserMapping.h"
// pointer to controls
#define cbUser CTRL_COMBOBOX("cbUser")
#define lstOptions CTRL_LISTVIEW("lstOptions")
#define txtOption CTRL_TEXT("txtOption")
#define txtValue CTRL_TEXT("txtValue")
#define btnAdd CTRL_BUTTON("wxID_ADD")
#define btnRemove CTRL_BUTTON("wxID_REMOVE")
dlgProperty *pgUserMappingFactory::CreateDialog(frmMain *frame, pgObject *node, pgObject *parent)
{
return new dlgUserMapping(this, frame, (pgUserMapping *)node, (pgForeignServer *)parent);
}
BEGIN_EVENT_TABLE(dlgUserMapping, dlgProperty)
EVT_TEXT(XRCID("cbUser"), dlgUserMapping::OnChange)
EVT_COMBOBOX(XRCID("cbUser"), dlgUserMapping::OnChange)
EVT_LIST_ITEM_SELECTED(XRCID("lstOptions"), dlgUserMapping::OnSelChangeOption)
EVT_TEXT(XRCID("txtOption"), dlgUserMapping::OnChangeOptionName)
EVT_BUTTON(wxID_ADD, dlgUserMapping::OnAddOption)
EVT_BUTTON(wxID_REMOVE, dlgUserMapping::OnRemoveOption)
END_EVENT_TABLE();
dlgUserMapping::dlgUserMapping(pgaFactory *f, frmMain *frame, pgUserMapping *node, pgForeignServer *parent)
: dlgProperty(f, frame, wxT("dlgUserMapping"))
{
foreignserver = parent;
usermapping = node;
}
pgObject *dlgUserMapping::GetObject()
{
return usermapping;
}
int dlgUserMapping::Go(bool modal)
{
// Fill user combobox
cbUser->Append(wxT("CURRENT_USER"));
cbUser->Append(wxT("PUBLIC"));
pgSet *set = connection->ExecuteSet(
wxT("SELECT rolname\n")
wxT(" FROM pg_roles\n")
wxT(" ORDER BY rolname"));
if (set)
{
while (!set->Eof())
{
wxString rolname = set->GetVal(wxT("rolname"));
cbUser->Append(rolname);
set->MoveNext();
}
delete set;
}
cbUser->SetSelection(0);
// Initialize options listview and buttons
lstOptions->AddColumn(_("Option"), 80);
lstOptions->AddColumn(_("Value"), 40);
txtOption->SetValue(wxT(""));
txtValue->SetValue(wxT(""));
btnAdd->Disable();
btnRemove->Disable();
if (usermapping)
{
// edit mode
cbUser->SetValue(usermapping->GetUsr());
cbUser->Disable();
wxString options = usermapping->GetOptions();
wxString option, optionname, optionvalue;
while (options.Length() > 0)
{
option = options.BeforeFirst(',');
optionname = option.BeforeFirst(wxT('=')).Trim(false).Trim();
optionvalue = option.AfterFirst(wxT('=')).Trim(false).Trim();
lstOptions->AppendItem(optionname, optionvalue);
options = options.AfterFirst(',');
}
}
else
{
// create mode
}
txtComment->Disable();
return dlgProperty::Go(modal);
}
pgObject *dlgUserMapping::CreateObject(pgCollection *collection)
{
pgObject *obj = userMappingFactory.CreateObjects(collection, 0, wxT("\n AND true")); //srvname ILIKE ") + qtDbString(name));
return obj;
}
void dlgUserMapping::CheckChange()
{
bool didChange = true;
if (usermapping)
{
didChange = GetOptionsSql().Length() > 0;
EnableOK(didChange);
}
else
{
bool enable = true;
CheckValid(enable, !cbUser->GetValue().IsEmpty(), _("Please specify user."));
EnableOK(enable);
}
}
void dlgUserMapping::OnChange(wxCommandEvent &ev)
{
CheckChange();
}
void dlgUserMapping::OnChangeOptionName(wxCommandEvent &ev)
{
btnAdd->Enable(txtOption->GetValue().Length() > 0);
}
void dlgUserMapping::OnSelChangeOption(wxListEvent &ev)
{
int row = lstOptions->GetSelection();
if (row >= 0)
{
txtOption->SetValue(lstOptions->GetText(row, 0));
txtValue->SetValue(lstOptions->GetText(row, 1));
}
btnRemove->Enable(row >= 0);
}
void dlgUserMapping::OnAddOption(wxCommandEvent &ev)
{
bool found = false;
for (int pos = 0 ; pos < lstOptions->GetItemCount() ; pos++)
{
if (lstOptions->GetText(pos).IsSameAs(txtOption->GetValue(), false))
{
lstOptions->SetItem(pos, 1, txtValue->GetValue());
found = true;
break;
}
}
if (!found)
{
lstOptions->AppendItem(txtOption->GetValue(), txtValue->GetValue());
}
txtOption->SetValue(wxT(""));
txtValue->SetValue(wxT(""));
btnAdd->Disable();
CheckChange();
}
void dlgUserMapping::OnRemoveOption(wxCommandEvent &ev)
{
int sel = lstOptions->GetSelection();
lstOptions->DeleteItem(sel);
txtOption->SetValue(wxT(""));
txtValue->SetValue(wxT(""));
btnRemove->Disable();
CheckChange();
}
wxString dlgUserMapping::GetOptionsSql()
{
wxString options = usermapping->GetOptions();
wxString option, optionname, optionvalue, sqloptions;
bool found;
int pos;
while (options.Length() > 0)
{
option = options.BeforeFirst(',');
optionname = option.BeforeFirst(wxT('=')).Trim(false).Trim();
optionvalue = option.AfterFirst(wxT('=')).Trim(false).Trim();
// check for options
found = false;
for (pos = 0 ; pos < lstOptions->GetItemCount() && !found; pos++)
{
found = lstOptions->GetText(pos, 0).Cmp(optionname) == 0;
if (found) break;
}
if (found)
{
if (lstOptions->GetText(pos, 1).Cmp(optionvalue) != 0)
{
if (sqloptions.Length() > 0)
sqloptions += wxT(", ");
sqloptions += wxT("SET ") + optionname + wxT(" '") + lstOptions->GetText(pos, 1) + wxT("'");
}
}
else
{
if (sqloptions.Length() > 0)
sqloptions += wxT(", ");
sqloptions += wxT("DROP ") + optionname;
}
options = options.AfterFirst(',');
}
for (pos = 0 ; pos < lstOptions->GetItemCount() ; pos++)
{
options = usermapping->GetOptions();
found = false;
while (options.Length() > 0 && !found)
{
option = options.BeforeFirst(',');
optionname = option.BeforeFirst(wxT('=')).Trim(false).Trim();
found = lstOptions->GetText(pos, 0).Cmp(optionname) == 0;
options = options.AfterFirst(',');
}
if (!found)
{
optionvalue = option.AfterFirst(wxT('=')).Trim(false).Trim();
if (sqloptions.Length() > 0)
sqloptions += wxT(", ");
sqloptions += wxT("ADD ") + lstOptions->GetText(pos, 0) + wxT(" '") + lstOptions->GetText(pos, 1) + wxT("'");
}
}
return sqloptions;
}
wxString dlgUserMapping::GetSql()
{
wxString sql;
if (usermapping)
{
// edit mode
wxString sqloptions = GetOptionsSql();
if (sqloptions.Length() > 0)
{
sql += wxT("ALTER USER MAPPING FOR ") + usermapping->GetUsr() + wxT(" SERVER ") + qtIdent(foreignserver->GetName())
+ wxT("\n OPTIONS (") + sqloptions + wxT(");\n");
}
}
else
{
// create mode
sql = wxT("CREATE USER MAPPING FOR ") + cbUser->GetValue() + wxT(" SERVER ") + qtIdent(foreignserver->GetName());
// check for options
if (lstOptions->GetItemCount() > 0)
{
wxString options = wxEmptyString;
for (int pos = 0 ; pos < lstOptions->GetItemCount() ; pos++)
{
if (options.Length() > 0)
options += wxT(", ");
options += lstOptions->GetText(pos, 0)
+ wxT(" '") + lstOptions->GetText(pos, 1) + wxT("' ");
}
sql += wxT("\n OPTIONS (") + options + wxT(")");
}
sql += wxT(";\n");
}
return sql;
}
|