1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2010 Monty Taylor
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
#include "drizzled/plugin/authorization.h"
26
#include "drizzled/identifier.h"
27
#include "drizzled/error.h"
28
#include "drizzled/session.h"
29
#include "drizzled/gettext.h"
34
std::vector<plugin::Authorization *> authorization_plugins;
37
bool plugin::Authorization::addPlugin(plugin::Authorization *auth)
40
authorization_plugins.push_back(auth);
45
void plugin::Authorization::removePlugin(plugin::Authorization *auth)
49
authorization_plugins.erase(std::find(authorization_plugins.begin(),
50
authorization_plugins.end(),
58
class RestrictDbFunctor :
59
public std::unary_function<plugin::Authorization *, bool>
61
const identifier::User &user_ctx;
62
identifier::Schema::const_reference schema;
65
RestrictDbFunctor(const identifier::User &user_ctx_arg,
66
identifier::Schema::const_reference schema_arg) :
67
std::unary_function<plugin::Authorization *, bool>(),
68
user_ctx(user_ctx_arg),
72
inline result_type operator()(argument_type auth)
74
return auth->restrictSchema(user_ctx, schema);
78
class RestrictTableFunctor :
79
public std::unary_function<plugin::Authorization *, bool>
81
const identifier::User &user_ctx;
82
identifier::Table &table;
84
RestrictTableFunctor(const identifier::User &user_ctx_arg,
85
identifier::Table &table_arg) :
86
std::unary_function<plugin::Authorization *, bool>(),
87
user_ctx(user_ctx_arg),
91
inline result_type operator()(argument_type auth)
93
return auth->restrictTable(user_ctx, table);
97
class RestrictProcessFunctor :
98
public std::unary_function<plugin::Authorization *, bool>
100
const identifier::User &user_ctx;
101
const identifier::User &session_ctx;
103
RestrictProcessFunctor(const identifier::User &user_ctx_arg,
104
const identifier::User &session_ctx_arg) :
105
std::unary_function<plugin::Authorization *, bool>(),
106
user_ctx(user_ctx_arg),
107
session_ctx(session_ctx_arg)
110
inline result_type operator()(argument_type auth)
112
return auth->restrictProcess(user_ctx, session_ctx);
116
class PruneSchemaFunctor :
117
public std::unary_function<identifier::Schema&, bool>
119
drizzled::identifier::User::const_shared_ptr user_ctx;
121
PruneSchemaFunctor(drizzled::identifier::User::const_shared_ptr user_ctx_arg) :
122
std::unary_function<identifier::Schema&, bool>(),
123
user_ctx(user_ctx_arg)
126
inline result_type operator()(argument_type auth)
128
return not plugin::Authorization::isAuthorized(user_ctx, auth, false);
134
bool plugin::Authorization::isAuthorized(identifier::User::const_shared_ptr user_ctx,
135
identifier::Schema::const_reference schema_identifier,
138
/* If we never loaded any authorization plugins, just return true */
139
if (authorization_plugins.empty())
142
/* Use find_if instead of foreach so that we can collect return codes */
143
std::vector<plugin::Authorization *>::const_iterator iter=
144
std::find_if(authorization_plugins.begin(),
145
authorization_plugins.end(),
146
RestrictDbFunctor(*user_ctx, schema_identifier));
150
* If iter is == end() here, that means that all of the plugins returned
151
* false, which means that that each of them believe the user is authorized
152
* to view the resource in question.
154
if (iter != authorization_plugins.end())
159
schema_identifier.getSQLPath(path);
161
my_error(ER_DBACCESS_DENIED_ERROR, MYF(0),
162
user_ctx->username().c_str(),
163
user_ctx->address().c_str(),
171
bool plugin::Authorization::isAuthorized(drizzled::identifier::User::const_shared_ptr user_ctx,
172
identifier::Table &table,
175
/* If we never loaded any authorization plugins, just return true */
176
if (authorization_plugins.empty())
179
/* Use find_if instead of foreach so that we can collect return codes */
180
std::vector<plugin::Authorization *>::const_iterator iter=
181
std::find_if(authorization_plugins.begin(),
182
authorization_plugins.end(),
183
RestrictTableFunctor(*user_ctx, table));
186
* If iter is == end() here, that means that all of the plugins returned
187
* false, which means that that each of them believe the user is authorized
188
* to view the resource in question.
190
if (iter != authorization_plugins.end())
195
table.getSQLPath(path);
197
my_error(ER_DBACCESS_DENIED_ERROR, MYF(0),
198
user_ctx->username().c_str(),
199
user_ctx->address().c_str(),
207
bool plugin::Authorization::isAuthorized(drizzled::identifier::User::const_shared_ptr user_ctx,
208
const Session *session,
211
return isAuthorized(*user_ctx, session, send_error);
214
bool plugin::Authorization::isAuthorized(drizzled::identifier::User::const_reference user_ctx,
215
const Session *session,
218
drizzled::identifier::User::const_shared_ptr session_ctx= session->user();
220
/* If we never loaded any authorization plugins, just return true */
221
if (authorization_plugins.empty())
224
/* Use find_if instead of foreach so that we can collect return codes */
225
std::vector<plugin::Authorization *>::const_iterator iter=
226
std::find_if(authorization_plugins.begin(),
227
authorization_plugins.end(),
228
RestrictProcessFunctor(user_ctx, *session_ctx));
231
* If iter is == end() here, that means that all of the plugins returned
232
* false, which means that that each of them believe the user is authorized
233
* to view the resource in question.
236
if (iter != authorization_plugins.end())
240
my_error(ER_KILL_DENIED_ERROR, MYF(0), session->thread_id);
248
void plugin::Authorization::pruneSchemaNames(drizzled::identifier::User::const_shared_ptr user_ctx,
249
identifier::Schema::vector &set_of_schemas)
251
/* If we never loaded any authorization plugins, just return true */
252
if (authorization_plugins.empty())
255
set_of_schemas.erase(std::remove_if(set_of_schemas.begin(),
256
set_of_schemas.end(),
257
PruneSchemaFunctor(user_ctx)),
258
set_of_schemas.end());
261
} /* namespace drizzled */