summaryrefslogtreecommitdiffstats
path: root/connectivity/source/drivers/mysqlc/mysqlc_user.cxx
blob: 6e8c3d734a677774f10e03ba092438f3285d0226 (plain)
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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <utility>

#include "mysqlc_user.hxx"
#include <comphelper/types.hxx>
#include <connectivity/dbtools.hxx>
#include <com/sun/star/sdbc/XRow.hpp>
#include <com/sun/star/sdbcx/Privilege.hpp>
#include <com/sun/star/sdbcx/PrivilegeObject.hpp>
#include <TConnection.hxx>

using namespace ::connectivity;
using namespace ::connectivity::mysqlc;
using namespace ::connectivity::sdbcx;

using namespace ::com::sun::star;
using namespace ::com::sun::star::sdbc;
using namespace ::com::sun::star::sdbcx;

User::User(css::uno::Reference<css::sdbc::XConnection> xConnection)
    : OUser(true) // Case Sensitive
    , m_xConnection(std::move(xConnection))
{
}

User::User(css::uno::Reference<css::sdbc::XConnection> xConnection, const OUString& rName)
    : OUser(rName,
            true) // Case Sensitive
    , m_xConnection(std::move(xConnection))
{
}

OUserExtend::OUserExtend(const css::uno::Reference<css::sdbc::XConnection>& xConnection,
                         const OUString& rName)
    : User(xConnection, rName)
{
    construct();
}

void OUserExtend::construct()
{
    registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME),
                     PROPERTY_ID_NAME, 0, &m_Name, ::cppu::UnoType<OUString>::get());

    registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PASSWORD),
                     PROPERTY_ID_PASSWORD, 0, &m_Password, ::cppu::UnoType<OUString>::get());
}

void User::changePassword(const OUString& /* oldPassword */, const OUString& newPassword)
{
    css::uno::Reference<XStatement> statement = m_xConnection->createStatement();
    statement->execute("SET PASSWORD FOR " + m_Name + " = PASSWORD('" + newPassword + "')");
    ::comphelper::disposeComponent(statement);
}

cppu::IPropertyArrayHelper* OUserExtend::createArrayHelper() const
{
    css::uno::Sequence<css::beans::Property> aProps;
    describeProperties(aProps);
    return new cppu::OPropertyArrayHelper(aProps);
}

cppu::IPropertyArrayHelper& OUserExtend::getInfoHelper()
{
    return *OUserExtend_PROP::getArrayHelper();
}

typedef connectivity::sdbcx::OUser_BASE OUser_BASE_RBHELPER;

sal_Int32 SAL_CALL User::getPrivileges(const OUString& objName, sal_Int32 objType)
{
    ::osl::MutexGuard aGuard(m_aMutex);
    checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);

    sal_Int32 nRights, nRightsWithGrant;
    findPrivilegesAndGrantPrivileges(objName, objType, nRights, nRightsWithGrant);
    return nRights;
}

void User::findPrivilegesAndGrantPrivileges(const OUString& objName, sal_Int32 objType,
                                            sal_Int32& nRights, sal_Int32& nRightsWithGrant)
{
    nRightsWithGrant = nRights = 0;
    // first we need to create the sql stmt to select the privs
    css::uno::Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
    OUString sCatalog, sSchema, sTable;
    ::dbtools::qualifiedNameComponents(xMeta, objName, sCatalog, sSchema, sTable,
                                       ::dbtools::EComposeRule::InDataManipulation);
    css::uno::Reference<XResultSet> xRes;
    switch (objType)
    {
        case css::sdbcx::PrivilegeObject::TABLE:
        case css::sdbcx::PrivilegeObject::VIEW:
        {
            css::uno::Any aCatalog;
            if (!sCatalog.isEmpty())
                aCatalog <<= sCatalog;
            xRes = xMeta->getTablePrivileges(aCatalog, sSchema, sTable);
        }
        break;

        case css::sdbcx::PrivilegeObject::COLUMN:
        {
            css::uno::Any aCatalog;
            if (!sCatalog.isEmpty())
                aCatalog <<= sCatalog;
            xRes = xMeta->getColumnPrivileges(aCatalog, sSchema, sTable, "%");
        }
        break;
    }

    if (!xRes.is())
        return;

    static const char sYes[] = "YES";

    nRightsWithGrant = nRights = 0;

    css::uno::Reference<XRow> xCurrentRow(xRes, css::uno::UNO_QUERY);
    while (xCurrentRow.is() && xRes->next())
    {
        OUString sGrantee = xCurrentRow->getString(5);
        OUString sPrivilege = xCurrentRow->getString(6);
        OUString sGrantable = xCurrentRow->getString(7);

        if (!m_Name.equalsIgnoreAsciiCase(sGrantee))
            continue;

        if (sPrivilege.equalsIgnoreAsciiCase("SELECT"))
        {
            nRights |= Privilege::SELECT;
            if (sGrantable.equalsIgnoreAsciiCase(sYes))
                nRightsWithGrant |= Privilege::SELECT;
        }
        else if (sPrivilege.equalsIgnoreAsciiCase("INSERT"))
        {
            nRights |= Privilege::INSERT;
            if (sGrantable.equalsIgnoreAsciiCase(sYes))
                nRightsWithGrant |= Privilege::INSERT;
        }
        else if (sPrivilege.equalsIgnoreAsciiCase("UPDATE"))
        {
            nRights |= Privilege::UPDATE;
            if (sGrantable.equalsIgnoreAsciiCase(sYes))
                nRightsWithGrant |= Privilege::UPDATE;
        }
        else if (sPrivilege.equalsIgnoreAsciiCase("DELETE"))
        {
            nRights |= Privilege::DELETE;
            if (sGrantable.equalsIgnoreAsciiCase(sYes))
                nRightsWithGrant |= Privilege::DELETE;
        }
        else if (sPrivilege.equalsIgnoreAsciiCase("READ"))
        {
            nRights |= Privilege::READ;
            if (sGrantable.equalsIgnoreAsciiCase(sYes))
                nRightsWithGrant |= Privilege::READ;
        }
        else if (sPrivilege.equalsIgnoreAsciiCase("CREATE"))
        {
            nRights |= Privilege::CREATE;
            if (sGrantable.equalsIgnoreAsciiCase(sYes))
                nRightsWithGrant |= Privilege::CREATE;
        }
        else if (sPrivilege.equalsIgnoreAsciiCase("ALTER"))
        {
            nRights |= Privilege::ALTER;
            if (sGrantable.equalsIgnoreAsciiCase(sYes))
                nRightsWithGrant |= Privilege::ALTER;
        }
        else if (sPrivilege.equalsIgnoreAsciiCase("REFERENCES"))
        {
            nRights |= Privilege::REFERENCE;
            if (sGrantable.equalsIgnoreAsciiCase(sYes))
                nRightsWithGrant |= Privilege::REFERENCE;
        }
        else if (sPrivilege.equalsIgnoreAsciiCase("DROP"))
        {
            nRights |= Privilege::DROP;
            if (sGrantable.equalsIgnoreAsciiCase(sYes))
                nRightsWithGrant |= Privilege::DROP;
        }
    }
    ::comphelper::disposeComponent(xRes);
}

sal_Int32 SAL_CALL User::getGrantablePrivileges(const OUString& objName, sal_Int32 objType)
{
    ::osl::MutexGuard aGuard(m_aMutex);
    checkDisposed(OUser_BASE_RBHELPER::rBHelper.bDisposed);

    sal_Int32 nRights, nRightsWithGrant;
    findPrivilegesAndGrantPrivileges(objName, objType, nRights, nRightsWithGrant);
    return nRightsWithGrant;
}

//----- IRefreshableGroups ----------------------------------------------------
void User::refreshGroups()
{
    // TODO: implement.
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */