Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * Effective License of whole file:
5 : : *
6 : : * This library is free software; you can redistribute it and/or
7 : : * modify it under the terms of the GNU Lesser General Public
8 : : * License version 2.1, as published by the Free Software Foundation.
9 : : *
10 : : * This library is distributed in the hope that it will be useful,
11 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 : : * Lesser General Public License for more details.
14 : : *
15 : : * You should have received a copy of the GNU Lesser General Public
16 : : * License along with this library; if not, write to the Free Software
17 : : * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
18 : : * MA 02111-1307 USA
19 : : *
20 : : * Parts "Copyright by Sun Microsystems, Inc" prior to August 2011:
21 : : *
22 : : * The Contents of this file are made available subject to the terms of
23 : : * the GNU Lesser General Public License Version 2.1
24 : : *
25 : : * Copyright: 2000 by Sun Microsystems, Inc.
26 : : *
27 : : * Contributor(s): Joerg Budischewski
28 : : *
29 : : * All parts contributed on or after August 2011:
30 : : *
31 : : * Version: MPL 1.1 / GPLv3+ / LGPLv2.1+
32 : : *
33 : : * The contents of this file are subject to the Mozilla Public License Version
34 : : * 1.1 (the "License"); you may not use this file except in compliance with
35 : : * the License or as specified alternatively below. You may obtain a copy of
36 : : * the License at http://www.mozilla.org/MPL/
37 : : *
38 : : * Software distributed under the License is distributed on an "AS IS" basis,
39 : : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
40 : : * for the specific language governing rights and limitations under the
41 : : * License.
42 : : *
43 : : * Major Contributor(s):
44 : : * [ Copyright (C) 2011 Lionel Elie Mamane <lionel@mamane.lu> ]
45 : : *
46 : : * All Rights Reserved.
47 : : *
48 : : * For minor contributions see the git repository.
49 : : *
50 : : * Alternatively, the contents of this file may be used under the terms of
51 : : * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
52 : : * the GNU Lesser General Public License Version 2.1 or later (the "LGPLv2.1+"),
53 : : * in which case the provisions of the GPLv3+ or the LGPLv2.1+ are applicable
54 : : * instead of those above.
55 : : *
56 : : ************************************************************************/
57 : :
58 : : #include <rtl/ustrbuf.hxx>
59 : :
60 : : #include <com/sun/star/sdbc/XRow.hpp>
61 : : #include <com/sun/star/sdbc/XParameters.hpp>
62 : : #include <com/sun/star/sdbcx/Privilege.hpp>
63 : :
64 : : #include "pq_xusers.hxx"
65 : : #include "pq_xuser.hxx"
66 : : #include "pq_statics.hxx"
67 : : #include "pq_tools.hxx"
68 : :
69 : : using osl::MutexGuard;
70 : :
71 : : using rtl::OUString;
72 : : using rtl::OUStringBuffer;
73 : : using rtl::OUStringToOString;
74 : :
75 : : using com::sun::star::beans::XPropertySet;
76 : :
77 : : using com::sun::star::uno::Any;
78 : : using com::sun::star::uno::makeAny;
79 : : using com::sun::star::uno::UNO_QUERY;
80 : : using com::sun::star::uno::Type;
81 : : using com::sun::star::uno::XInterface;
82 : : using com::sun::star::uno::Reference;
83 : : using com::sun::star::uno::Sequence;
84 : : using com::sun::star::uno::RuntimeException;
85 : :
86 : : using com::sun::star::container::NoSuchElementException;
87 : : using com::sun::star::lang::WrappedTargetException;
88 : :
89 : : using com::sun::star::sdbc::XRow;
90 : : using com::sun::star::sdbc::XCloseable;
91 : : using com::sun::star::sdbc::XStatement;
92 : : using com::sun::star::sdbc::XResultSet;
93 : : using com::sun::star::sdbc::XParameters;
94 : : using com::sun::star::sdbc::XPreparedStatement;
95 : : using com::sun::star::sdbc::XDatabaseMetaData;
96 : :
97 : : namespace pq_sdbc_driver
98 : : {
99 : : #define ASCII_STR(x) OUString( RTL_CONSTASCII_USTRINGPARAM( x ) )
100 : 0 : Users::Users(
101 : : const ::rtl::Reference< RefCountedMutex > & refMutex,
102 : : const ::com::sun::star::uno::Reference< com::sun::star::sdbc::XConnection > & origin,
103 : : ConnectionSettings *pSettings )
104 : 0 : : Container( refMutex, origin, pSettings, getStatics().USER )
105 : 0 : {}
106 : :
107 : 0 : Users::~Users()
108 : 0 : {}
109 : :
110 : 0 : void Users::refresh()
111 : : throw (::com::sun::star::uno::RuntimeException)
112 : : {
113 : : try
114 : : {
115 : 0 : osl::MutexGuard guard( m_refMutex->mutex );
116 : 0 : Statics & st = getStatics();
117 : :
118 : 0 : Reference< XStatement > stmt = m_origin->createStatement();
119 : :
120 : : Reference< XResultSet > rs =
121 : 0 : stmt->executeQuery( ASCII_STR( "SELECT usename FROM pg_shadow" ) );
122 : :
123 : 0 : Reference< XRow > xRow( rs , UNO_QUERY );
124 : :
125 : 0 : String2IntMap map;
126 : :
127 : 0 : m_values = Sequence< com::sun::star::uno::Any > ( );
128 : 0 : sal_Int32 tableIndex = 0;
129 : 0 : while( rs->next() )
130 : : {
131 : : User * pUser =
132 : 0 : new User( m_refMutex, m_origin, m_pSettings );
133 : 0 : Reference< com::sun::star::beans::XPropertySet > prop = pUser;
134 : :
135 : 0 : OUString name = xRow->getString( 1);
136 : : pUser->setPropertyValue_NoBroadcast_public(
137 : 0 : st.NAME , makeAny(xRow->getString( TABLE_INDEX_CATALOG+1) ) );
138 : :
139 : : {
140 : 0 : const int currentTableIndex = tableIndex++;
141 : : assert(currentTableIndex == m_values.getLength());
142 : 0 : m_values.realloc( tableIndex );
143 : 0 : m_values[currentTableIndex] = makeAny( prop );
144 : 0 : map[ name ] = currentTableIndex;
145 : : }
146 : 0 : }
147 : 0 : m_name2index.swap( map );
148 : : }
149 : 0 : catch ( com::sun::star::sdbc::SQLException & e )
150 : : {
151 : 0 : throw RuntimeException( e.Message , e.Context );
152 : : }
153 : :
154 : 0 : fire( RefreshedBroadcaster( *this ) );
155 : 0 : }
156 : :
157 : :
158 : 0 : void Users::appendByDescriptor(
159 : : const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& descriptor )
160 : : throw (::com::sun::star::sdbc::SQLException,
161 : : ::com::sun::star::container::ElementExistException,
162 : : ::com::sun::star::uno::RuntimeException)
163 : : {
164 : 0 : osl::MutexGuard guard( m_refMutex->mutex );
165 : :
166 : 0 : OUStringBuffer update( 128 );
167 : 0 : update.appendAscii( RTL_CONSTASCII_STRINGPARAM( "CREATE USER " ) );
168 : 0 : bufferQuoteIdentifier( update, extractStringProperty( descriptor, getStatics().NAME ), m_pSettings );
169 : 0 : update.appendAscii( RTL_CONSTASCII_STRINGPARAM( " PASSWORD " ) );
170 : 0 : bufferQuoteConstant( update, extractStringProperty( descriptor, getStatics().PASSWORD ), m_pSettings );
171 : :
172 : 0 : Reference< XStatement > stmt = m_origin->createStatement( );
173 : 0 : DisposeGuard disposeGuard( stmt );
174 : 0 : stmt->executeUpdate( update.makeStringAndClear() );
175 : 0 : }
176 : :
177 : 0 : void Users::dropByName( const ::rtl::OUString& elementName )
178 : : throw (::com::sun::star::sdbc::SQLException,
179 : : ::com::sun::star::container::NoSuchElementException,
180 : : ::com::sun::star::uno::RuntimeException)
181 : : {
182 : 0 : String2IntMap::const_iterator ii = m_name2index.find( elementName );
183 : 0 : if( ii == m_name2index.end() )
184 : : {
185 : 0 : OUStringBuffer buf( 128 );
186 : 0 : buf.appendAscii( "User " );
187 : 0 : buf.append( elementName );
188 : 0 : buf.appendAscii( " is unknown, so it can't be dropped" );
189 : : throw com::sun::star::container::NoSuchElementException(
190 : 0 : buf.makeStringAndClear(), *this );
191 : : }
192 : 0 : dropByIndex( ii->second );
193 : 0 : }
194 : :
195 : 0 : void Users::dropByIndex( sal_Int32 index )
196 : : throw (::com::sun::star::sdbc::SQLException,
197 : : ::com::sun::star::lang::IndexOutOfBoundsException,
198 : : ::com::sun::star::uno::RuntimeException)
199 : : {
200 : :
201 : 0 : osl::MutexGuard guard( m_refMutex->mutex );
202 : 0 : if( index < 0 || index >= m_values.getLength() )
203 : : {
204 : 0 : OUStringBuffer buf( 128 );
205 : 0 : buf.appendAscii( "USERS: Index out of range (allowed 0 to " );
206 : 0 : buf.append( (sal_Int32) (m_values.getLength() -1) );
207 : 0 : buf.appendAscii( ", got " );
208 : 0 : buf.append( index );
209 : 0 : buf.appendAscii( ")" );
210 : : throw com::sun::star::lang::IndexOutOfBoundsException(
211 : 0 : buf.makeStringAndClear(), *this );
212 : : }
213 : :
214 : 0 : Reference< XPropertySet > set;
215 : 0 : m_values[index] >>= set;
216 : 0 : OUString name;
217 : 0 : set->getPropertyValue( getStatics().NAME ) >>= name;
218 : :
219 : 0 : OUStringBuffer update( 128 );
220 : 0 : update.appendAscii( "DROP USER " );
221 : 0 : bufferQuoteIdentifier( update, name, m_pSettings );
222 : :
223 : 0 : Reference< XStatement > stmt = m_origin->createStatement( );
224 : 0 : DisposeGuard disposeGuard( stmt );
225 : 0 : stmt->executeUpdate( update.makeStringAndClear() );
226 : 0 : }
227 : :
228 : :
229 : 0 : ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet > Users::createDataDescriptor()
230 : : throw (::com::sun::star::uno::RuntimeException)
231 : : {
232 : 0 : return new UserDescriptor( m_refMutex, m_origin, m_pSettings );
233 : : }
234 : :
235 : 0 : Reference< com::sun::star::container::XNameAccess > Users::create(
236 : : const ::rtl::Reference< RefCountedMutex > & refMutex,
237 : : const ::com::sun::star::uno::Reference< com::sun::star::sdbc::XConnection > & origin,
238 : : ConnectionSettings *pSettings )
239 : : {
240 : 0 : Users *pUsers = new Users( refMutex, origin, pSettings );
241 : 0 : Reference< com::sun::star::container::XNameAccess > ret = pUsers;
242 : 0 : pUsers->refresh();
243 : :
244 : 0 : return ret;
245 : : }
246 : :
247 : 0 : void Users::disposing()
248 : : {
249 : 0 : }
250 : :
251 : : };
|