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 : * This Source Code Form is subject to the terms of the Mozilla Public
32 : * License, v. 2.0. If a copy of the MPL was not distributed with this
33 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
34 : *
35 : ************************************************************************/
36 :
37 : #include <rtl/ustrbuf.hxx>
38 :
39 : #include <com/sun/star/sdbc/XRow.hpp>
40 : #include <com/sun/star/sdbc/XParameters.hpp>
41 : #include <com/sun/star/sdbcx/Privilege.hpp>
42 :
43 : #include "pq_xusers.hxx"
44 : #include "pq_xuser.hxx"
45 : #include "pq_statics.hxx"
46 : #include "pq_tools.hxx"
47 :
48 : using osl::MutexGuard;
49 :
50 :
51 : using com::sun::star::beans::XPropertySet;
52 :
53 : using com::sun::star::uno::Any;
54 : using com::sun::star::uno::makeAny;
55 : using com::sun::star::uno::UNO_QUERY;
56 : using com::sun::star::uno::Type;
57 : using com::sun::star::uno::XInterface;
58 : using com::sun::star::uno::Reference;
59 : using com::sun::star::uno::Sequence;
60 : using com::sun::star::uno::RuntimeException;
61 :
62 : using com::sun::star::container::NoSuchElementException;
63 : using com::sun::star::lang::WrappedTargetException;
64 :
65 : using com::sun::star::sdbc::XRow;
66 : using com::sun::star::sdbc::XCloseable;
67 : using com::sun::star::sdbc::XStatement;
68 : using com::sun::star::sdbc::XResultSet;
69 : using com::sun::star::sdbc::XParameters;
70 : using com::sun::star::sdbc::XPreparedStatement;
71 : using com::sun::star::sdbc::XDatabaseMetaData;
72 :
73 : namespace pq_sdbc_driver
74 : {
75 0 : Users::Users(
76 : const ::rtl::Reference< RefCountedMutex > & refMutex,
77 : const ::com::sun::star::uno::Reference< com::sun::star::sdbc::XConnection > & origin,
78 : ConnectionSettings *pSettings )
79 0 : : Container( refMutex, origin, pSettings, getStatics().USER )
80 0 : {}
81 :
82 0 : Users::~Users()
83 0 : {}
84 :
85 0 : void Users::refresh()
86 : throw (::com::sun::star::uno::RuntimeException, std::exception)
87 : {
88 : try
89 : {
90 0 : osl::MutexGuard guard( m_refMutex->mutex );
91 0 : Statics & st = getStatics();
92 :
93 0 : Reference< XStatement > stmt = m_origin->createStatement();
94 :
95 0 : Reference< XResultSet > rs = stmt->executeQuery( "SELECT usename FROM pg_shadow" );
96 :
97 0 : Reference< XRow > xRow( rs , UNO_QUERY );
98 :
99 0 : String2IntMap map;
100 :
101 0 : m_values = Sequence< com::sun::star::uno::Any > ( );
102 0 : sal_Int32 tableIndex = 0;
103 0 : while( rs->next() )
104 : {
105 : User * pUser =
106 0 : new User( m_refMutex, m_origin, m_pSettings );
107 0 : Reference< com::sun::star::beans::XPropertySet > prop = pUser;
108 :
109 0 : OUString name = xRow->getString( 1);
110 : pUser->setPropertyValue_NoBroadcast_public(
111 0 : st.NAME , makeAny(xRow->getString( TABLE_INDEX_CATALOG+1) ) );
112 :
113 : {
114 0 : const int currentTableIndex = tableIndex++;
115 : assert(currentTableIndex == m_values.getLength());
116 0 : m_values.realloc( tableIndex );
117 0 : m_values[currentTableIndex] = makeAny( prop );
118 0 : map[ name ] = currentTableIndex;
119 : }
120 0 : }
121 0 : m_name2index.swap( map );
122 : }
123 0 : catch ( com::sun::star::sdbc::SQLException & e )
124 : {
125 0 : throw RuntimeException( e.Message , e.Context );
126 : }
127 :
128 0 : fire( RefreshedBroadcaster( *this ) );
129 0 : }
130 :
131 :
132 0 : void Users::appendByDescriptor(
133 : const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& descriptor )
134 : throw (::com::sun::star::sdbc::SQLException,
135 : ::com::sun::star::container::ElementExistException,
136 : ::com::sun::star::uno::RuntimeException, std::exception)
137 : {
138 0 : osl::MutexGuard guard( m_refMutex->mutex );
139 :
140 0 : OUStringBuffer update( 128 );
141 0 : update.append( "CREATE USER " );
142 0 : bufferQuoteIdentifier( update, extractStringProperty( descriptor, getStatics().NAME ), m_pSettings );
143 0 : update.append( " PASSWORD " );
144 0 : bufferQuoteConstant( update, extractStringProperty( descriptor, getStatics().PASSWORD ), m_pSettings );
145 :
146 0 : Reference< XStatement > stmt = m_origin->createStatement( );
147 0 : DisposeGuard disposeGuard( stmt );
148 0 : stmt->executeUpdate( update.makeStringAndClear() );
149 0 : }
150 :
151 0 : void Users::dropByName( const OUString& elementName )
152 : throw (::com::sun::star::sdbc::SQLException,
153 : ::com::sun::star::container::NoSuchElementException,
154 : ::com::sun::star::uno::RuntimeException, std::exception)
155 : {
156 0 : String2IntMap::const_iterator ii = m_name2index.find( elementName );
157 0 : if( ii == m_name2index.end() )
158 : {
159 0 : OUStringBuffer buf( 128 );
160 0 : buf.appendAscii( "User " );
161 0 : buf.append( elementName );
162 0 : buf.appendAscii( " is unknown, so it can't be dropped" );
163 : throw com::sun::star::container::NoSuchElementException(
164 0 : buf.makeStringAndClear(), *this );
165 : }
166 0 : dropByIndex( ii->second );
167 0 : }
168 :
169 0 : void Users::dropByIndex( sal_Int32 index )
170 : throw (::com::sun::star::sdbc::SQLException,
171 : ::com::sun::star::lang::IndexOutOfBoundsException,
172 : ::com::sun::star::uno::RuntimeException, std::exception)
173 : {
174 :
175 0 : osl::MutexGuard guard( m_refMutex->mutex );
176 0 : if( index < 0 || index >= m_values.getLength() )
177 : {
178 0 : OUStringBuffer buf( 128 );
179 0 : buf.appendAscii( "USERS: Index out of range (allowed 0 to " );
180 0 : buf.append( (sal_Int32) (m_values.getLength() -1) );
181 0 : buf.appendAscii( ", got " );
182 0 : buf.append( index );
183 0 : buf.appendAscii( ")" );
184 : throw com::sun::star::lang::IndexOutOfBoundsException(
185 0 : buf.makeStringAndClear(), *this );
186 : }
187 :
188 0 : Reference< XPropertySet > set;
189 0 : m_values[index] >>= set;
190 0 : OUString name;
191 0 : set->getPropertyValue( getStatics().NAME ) >>= name;
192 :
193 0 : OUStringBuffer update( 128 );
194 0 : update.appendAscii( "DROP USER " );
195 0 : bufferQuoteIdentifier( update, name, m_pSettings );
196 :
197 0 : Reference< XStatement > stmt = m_origin->createStatement( );
198 0 : DisposeGuard disposeGuard( stmt );
199 0 : stmt->executeUpdate( update.makeStringAndClear() );
200 0 : }
201 :
202 :
203 0 : ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet > Users::createDataDescriptor()
204 : throw (::com::sun::star::uno::RuntimeException, std::exception)
205 : {
206 0 : return new UserDescriptor( m_refMutex, m_origin, m_pSettings );
207 : }
208 :
209 0 : Reference< com::sun::star::container::XNameAccess > Users::create(
210 : const ::rtl::Reference< RefCountedMutex > & refMutex,
211 : const ::com::sun::star::uno::Reference< com::sun::star::sdbc::XConnection > & origin,
212 : ConnectionSettings *pSettings )
213 : {
214 0 : Users *pUsers = new Users( refMutex, origin, pSettings );
215 0 : Reference< com::sun::star::container::XNameAccess > ret = pUsers;
216 0 : pUsers->refresh();
217 :
218 0 : return ret;
219 : }
220 :
221 0 : void Users::disposing()
222 : {
223 0 : }
224 :
225 : };
226 :
227 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|