void pgUser::ShowTreeDetail(ctlTree *browser, frmMain *form, ctlListView *properties, ctlSQLBox *sqlPane) { if (!expandedKids) { expandedKids = true; pgSet *set = GetServer()->ExecuteSet(wxT("SELECT groname, grolist FROM pg_group ORDER BY groname")); if (set) { while (!set->Eof()) { wxString groupName = set->GetVal(wxT("groname")); wxString str = set->GetVal(wxT("grolist")); if (!str.IsNull()) { wxStringTokenizer ids(str.Mid(1, str.Length() - 2), wxT(",")); while (ids.HasMoreTokens()) { if (StrToLong(ids.GetNextToken()) == GetUserId()) { groupsIn.Add(groupName); break; } } } set->MoveNext(); } delete set; } } if (properties) { CreateListColumns(properties); properties->AppendItem(_("Name"), GetName()); properties->AppendItem(_("User ID"), GetUserId()); properties->AppendItem(_("Account expires"), DateToAnsiStr(GetAccountExpires())); properties->AppendItem(_("Superuser?"), BoolToYesNo(GetSuperuser())); properties->AppendItem(_("Create databases?"), BoolToYesNo(GetCreateDatabase())); properties->AppendItem(_("Update catalogs?"), BoolToYesNo(GetUpdateCatalog())); wxString groupList; size_t index; for (index = 0 ; index < groupsIn.GetCount() ; index++) { if (!groupList.IsEmpty()) groupList += wxT(", "); groupList += groupsIn.Item(index); } properties->AppendItem(_("Member of"), groupList); for (index = 0; index < configList.GetCount() ; index++) { wxString item = configList.Item(index); properties->AppendItem(item.BeforeFirst('='), item.AfterFirst('=')); } } }
bool pgUser::DropObject(wxFrame *frame, ctlTree *browser, bool cascaded) { if (GetUpdateCatalog()) { wxMessageDialog dlg(frame, _("Deleting a superuser might result in unwanted behaviour (e.g. when restoring the database).\nAre you sure?"), _("Confirm superuser deletion"), wxICON_EXCLAMATION | wxYES_NO | wxNO_DEFAULT); if (dlg.ShowModal() != wxID_YES) return false; } return server->ExecuteVoid(wxT("DROP USER ") + GetQuotedFullIdentifier() + wxT(";")); }
wxString pgUser::GetSql(ctlTree *browser) { if (sql.IsNull()) { sql = wxT("-- User: "******"\n\n") + wxT("-- DROP USER ") + GetQuotedFullIdentifier() + wxT(";") + wxT("\n\nCREATE USER ") + GetQuotedIdentifier() + wxT("\n WITH SYSID ") + NumToStr(userId); if (GetPassword() != wxT("********")) AppendIfFilled(sql, wxT("\n ENCRYPTED PASSWORD "), qtDbString(GetPassword())); sql += wxT("\n "); if (GetCreateDatabase()) sql += wxT(" CREATEDB"); else sql += wxT(" NOCREATEDB"); if (GetUpdateCatalog()) sql += wxT(" CREATEUSER"); else sql += wxT(" NOCREATEUSER"); if (GetAccountExpires().IsValid()) AppendIfFilled(sql, wxT(" VALID UNTIL "), qtDbString(DateToAnsiStr(GetAccountExpires()))); sql += wxT(";\n"); size_t index; for (index = 0 ; index < configList.GetCount() ; index++) { if (configList.Item(index).BeforeFirst('=') != wxT("search_path") && configList.Item(index).BeforeFirst('=') != wxT("temp_tablespaces")) sql += wxT("ALTER USER ") + GetQuotedIdentifier() + wxT(" SET ") + configList.Item(index).BeforeFirst('=') + wxT("='") + configList.Item(index).AfterFirst('=') + wxT("';\n"); else sql += wxT("ALTER USER ") + GetQuotedIdentifier() + wxT(" SET ") + configList.Item(index).BeforeFirst('=') + wxT("=") + configList.Item(index).AfterFirst('=') + wxT(";\n"); } for (index = 0 ; index < groupsIn.GetCount() ; index++) sql += wxT("ALTER GROUP ") + qtIdent(groupsIn.Item(index)) + wxT(" ADD USER ") + GetQuotedIdentifier() + wxT(";\n"); } return sql; }
void pgRole::ShowTreeDetail(ctlTree *browser, frmMain *form, ctlListView *properties, ctlSQLBox *sqlPane) { if (!expandedKids) { expandedKids=true; wxString rolesquery; if (GetConnection()->BackendMinimumVersion(8, 2)) rolesquery = wxT("SELECT rolname, admin_option,\n") wxT(" pg_catalog.shobj_description(r.oid, 'pg_authid') AS description\n"); else rolesquery = wxT("SELECT rolname, admin_option\n"); rolesquery += wxT(" FROM pg_roles r\n") wxT(" JOIN pg_auth_members ON r.oid=roleid\n") wxT(" WHERE member=") + GetOidStr() + wxT("\n") wxT(" ORDER BY rolname"); pgSetIterator roles(GetConnection(), rolesquery); while (roles.RowsLeft()) { wxString role=roles.GetVal(wxT("rolname")); if (roles.GetBool(wxT("admin_option"))) role += PGROLE_ADMINOPTION; rolesIn.Add(role); } } if (properties) { CreateListColumns(properties); properties->AppendItem(_("Name"), GetName()); properties->AppendItem(_("OID"), GetOid()); properties->AppendItem(_("Account expires"), DateToAnsiStr(GetAccountExpires())); properties->AppendItem(_("Can login?"), BoolToYesNo(GetCanLogin())); properties->AppendItem(_("Superuser?"), BoolToYesNo(GetSuperuser())); properties->AppendItem(_("Create databases?"), BoolToYesNo(GetCreateDatabase())); properties->AppendItem(_("Create roles?"), BoolToYesNo(GetCreateRole())); properties->AppendItem(_("Update catalogs?"), BoolToYesNo(GetUpdateCatalog())); properties->AppendItem(_("Inherits?"), BoolToYesNo(GetInherits())); if (server->GetConnection()->BackendMinimumVersion(9, 1)) { properties->AppendItem(_("Replication?"), BoolToYesNo(GetReplication())); } wxString strConnLimit; strConnLimit.Printf(wxT("%ld"), GetConnectionLimit()); properties->AppendItem(_("Connection Limit"), strConnLimit); properties->AppendItem(_("Comment"), firstLineOnly(GetComment())); wxString roleList; size_t index; for (index=0 ; index < rolesIn.GetCount() ; index++) { if (!roleList.IsEmpty()) roleList += wxT(", "); roleList += rolesIn.Item(index); } properties->AppendItem(_("Member of"), roleList); for (index=0; index < configList.GetCount() ; index++) { wxString item=configList.Item(index); properties->AppendItem(item.BeforeFirst('='), item.AfterFirst('=')); } } }
wxString pgRole::GetSql(ctlTree *browser) { if (sql.IsNull()) { sql = wxT("-- Role: ") + GetName() + wxT("\n\n") + wxT("-- DROP ROLE ") + GetQuotedFullIdentifier() + wxT(";") + wxT("\n\nCREATE ROLE ") + GetQuotedIdentifier(); if (GetCanLogin()) { sql += wxT(" LOGIN"); if (GetPassword() != wxT("********") && !GetPassword().IsEmpty()) AppendIfFilled(sql, wxT("\n ENCRYPTED PASSWORD "), qtDbString(GetPassword())); } sql += wxT("\n "); if (this->GetSuperuser()) sql += wxT(" SUPERUSER"); else sql += wxT(" NOSUPERUSER"); if (GetInherits()) sql += wxT(" INHERIT"); else sql += wxT(" NOINHERIT"); if (GetCreateDatabase()) sql += wxT(" CREATEDB"); else sql += wxT(" NOCREATEDB"); if (GetCreateRole()) sql += wxT(" CREATEROLE"); else sql += wxT(" NOCREATEROLE"); if (server->GetConnection()->BackendMinimumVersion(9, 1)) { if (GetReplication()) sql += wxT(" REPLICATION"); else sql += wxT(" NOREPLICATION"); } if (GetConnectionLimit() > 0) sql += wxT(" CONNECTION LIMIT ") + NumToStr(GetConnectionLimit()); if (GetAccountExpires().IsValid()) AppendIfFilled(sql, wxT(" VALID UNTIL "), qtDbString(DateToAnsiStr(GetAccountExpires()))); if (GetRolQueueName().Length() > 0) AppendIfFilled(sql, wxT(" RESOURCE QUEUE "), GetRolQueueName()); sql +=wxT(";\n"); if (this->GetSuperuser() && !GetUpdateCatalog()) sql += wxT("UPDATE pg_authid SET rolcatupdate=false WHERE rolname=") + qtDbString(GetIdentifier()) + wxT(";\n"); size_t index; for (index=0 ; index < configList.GetCount() ; index++) { if (configList.Item(index).BeforeFirst('=') != wxT("search_path") && configList.Item(index).BeforeFirst('=') != wxT("temp_tablespaces")) sql += wxT("ALTER ROLE ") + GetQuotedIdentifier() + wxT(" SET ") + configList.Item(index).BeforeFirst('=') + wxT("='") + configList.Item(index).AfterFirst('=') + wxT("';\n"); else sql += wxT("ALTER ROLE ") + GetQuotedIdentifier() + wxT(" SET ") + configList.Item(index).BeforeFirst('=') + wxT("=") + configList.Item(index).AfterFirst('=') + wxT(";\n"); } for (index=0 ; index < rolesIn.GetCount() ; index++) { wxString role=rolesIn.Item(index); bool admin=false; if (role.Right(PGROLE_ADMINOPTION_LEN) == PGROLE_ADMINOPTION) { admin=true; role=role.Left(role.Length()-PGROLE_ADMINOPTION_LEN); } sql += wxT("GRANT ") + qtIdent(role) + wxT(" TO ") + GetQuotedIdentifier(); if (admin) sql += wxT(" WITH ADMIN OPTION"); sql += wxT(";\n"); } if (!GetComment().IsNull()) { sql += wxT("COMMENT ON ROLE ") + GetQuotedFullIdentifier() + wxT(" IS ") + qtDbString(GetComment()) + wxT(";\n"); } } return sql; }
void pgRole::ShowTreeDetail(ctlTree *browser, frmMain *form, ctlListView *properties, ctlSQLBox *sqlPane) { if (!expandedKids) { expandedKids = true; wxString rolesquery; if (GetConnection()->BackendMinimumVersion(8, 2)) rolesquery = wxT("SELECT rolname, admin_option,\n") wxT(" pg_catalog.shobj_description(r.oid, 'pg_authid') AS description\n"); else rolesquery = wxT("SELECT rolname, admin_option\n"); rolesquery += wxT(" FROM pg_roles r\n") wxT(" JOIN pg_auth_members ON r.oid=roleid\n") wxT(" WHERE member=") + GetOidStr() + wxT("\n") wxT(" ORDER BY rolname"); pgSetIterator roles(GetConnection(), rolesquery); while (roles.RowsLeft()) { wxString role = roles.GetVal(wxT("rolname")); if (roles.GetBool(wxT("admin_option"))) role += PGROLE_ADMINOPTION; rolesIn.Add(role); } // Get configuration wxString query; if (GetConnection()->BackendMinimumVersion(9, 0)) { query = wxT("WITH configs AS ") wxT("(SELECT datname, unnest(setconfig) AS config") wxT(" FROM pg_db_role_setting s") wxT(" LEFT JOIN pg_database d ON d.oid=s.setdatabase") wxT(" WHERE s.setrole=") + NumToStr(GetOid()) + wxT(")\n") wxT("SELECT datname, split_part(config, '=', 1) AS variable, replace(config, split_part(config, '=', 1) || '=', '') AS value\n") wxT("FROM configs"); } else { wxString query_myrole = wxT("SELECT rolconfig FROM pg_roles WHERE oid=") + NumToStr(GetOid()); query = wxT("SELECT '' AS datname, split_part(config, '=', 1) AS variable,\n") wxT(" replace(config,split_part(config, '=', 1) || '=', '') AS value\n") wxT("FROM (\n") wxT(" SELECT\n") wxT(" (\n") wxT(" SELECT rolconfig[i]\n") wxT(" FROM pg_roles\n") wxT(" WHERE oid=") + NumToStr(GetOid()) + wxT("\n") wxT(" ) AS config\n") wxT(" FROM generate_series(array_lower((") + query_myrole + wxT("),1), array_upper((") + query_myrole + wxT("),1)) AS i\n") wxT(" ) configs"); } pgSet *configs = GetConnection()->ExecuteSet(query); if (configs) { while (!configs->Eof()) { variables.Add(configs->GetVal(wxT("datname"))); variables.Add(configs->GetVal(wxT("variable"))); variables.Add(configs->GetVal(wxT("value"))); configs->MoveNext(); } delete configs; } } if (properties) { CreateListColumns(properties); properties->AppendItem(_("Name"), GetName()); properties->AppendItem(_("OID"), GetOid()); properties->AppendItem(_("Account expires"), DateToAnsiStr(GetAccountExpires())); properties->AppendItem(_("Can login?"), BoolToYesNo(GetCanLogin())); properties->AppendItem(_("Superuser?"), BoolToYesNo(GetSuperuser())); properties->AppendItem(_("Create databases?"), BoolToYesNo(GetCreateDatabase())); properties->AppendItem(_("Create roles?"), BoolToYesNo(GetCreateRole())); if (!server->GetConnection()->BackendMinimumVersion(9, 5)) properties->AppendItem(_("Update catalogs?"), BoolToYesNo(GetUpdateCatalog())); properties->AppendItem(_("Inherits?"), BoolToYesNo(GetInherits())); if (server->GetConnection()->BackendMinimumVersion(9, 1)) { properties->AppendItem(_("Replication?"), BoolToYesNo(GetReplication())); } wxString strConnLimit; strConnLimit.Printf(wxT("%ld"), GetConnectionLimit()); properties->AppendItem(_("Connection Limit"), strConnLimit); properties->AppendItem(_("Comment"), firstLineOnly(GetComment())); wxString roleList; size_t index; for (index = 0 ; index < rolesIn.GetCount() ; index++) { if (!roleList.IsEmpty()) roleList += wxT(", "); roleList += rolesIn.Item(index); } properties->AppendItem(_("Member of"), roleList); wxString dbname; wxString parameter; wxString value; for (index = 0; index < variables.GetCount() ; index += 3) { dbname = variables.Item(index); parameter = variables.Item(index + 1); value = variables.Item(index + 2); if (dbname.Length() == 0) { properties->AppendItem(parameter, value); } else { properties->AppendItem(parameter + wxT(" (database ") + dbname + wxT(")"), value); } } if (!GetLabels().IsEmpty()) { wxArrayString seclabels = GetProviderLabelArray(); if (seclabels.GetCount() > 0) { for (unsigned int index = 0 ; index < seclabels.GetCount() - 1 ; index += 2) { properties->AppendItem(seclabels.Item(index), seclabels.Item(index + 1)); } } } } }