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
const identifier::Schema& schema;
65
RestrictDbFunctor(const identifier::User &user_ctx_arg,
66
const identifier::Schema& 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
const identifier::Table& table;
84
RestrictTableFunctor(const identifier::User& user_ctx_arg,
85
const 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
const drizzled::identifier::User& user_ctx;
121
PruneSchemaFunctor(const drizzled::identifier::User& 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(const identifier::User& user_ctx,
135
const identifier::Schema& 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())
158
error::access(user_ctx, schema_identifier);
165
bool plugin::Authorization::isAuthorized(const drizzled::identifier::User& user_ctx,
166
const identifier::Table& table_identifier,
169
/* If we never loaded any authorization plugins, just return true */
170
if (authorization_plugins.empty())
173
/* Use find_if instead of foreach so that we can collect return codes */
174
std::vector<plugin::Authorization *>::const_iterator iter=
175
std::find_if(authorization_plugins.begin(),
176
authorization_plugins.end(),
177
RestrictTableFunctor(user_ctx, table_identifier));
180
* If iter is == end() here, that means that all of the plugins returned
181
* false, which means that that each of them believe the user is authorized
182
* to view the resource in question.
184
if (iter != authorization_plugins.end())
188
error::access(user_ctx, table_identifier);
195
bool plugin::Authorization::isAuthorized(const drizzled::identifier::User& user_ctx,
196
const Session& session,
199
/* If we never loaded any authorization plugins, just return true */
200
if (authorization_plugins.empty())
203
// To make sure we hold the user structure we need to have a shred_ptr so
204
// that we increase the count on the object.
205
drizzled::identifier::user::ptr session_ctx= session.user();
208
/* Use find_if instead of foreach so that we can collect return codes */
209
std::vector<plugin::Authorization *>::const_iterator iter=
210
std::find_if(authorization_plugins.begin(),
211
authorization_plugins.end(),
212
RestrictProcessFunctor(user_ctx, *session_ctx));
215
* If iter is == end() here, that means that all of the plugins returned
216
* false, which means that that each of them believe the user is authorized
217
* to view the resource in question.
220
if (iter != authorization_plugins.end())
224
my_error(ER_KILL_DENIED_ERROR, MYF(0), session.thread_id);
232
void plugin::Authorization::pruneSchemaNames(const drizzled::identifier::User& user_ctx,
233
identifier::schema::vector &set_of_schemas)
235
/* If we never loaded any authorization plugins, just return true */
236
if (authorization_plugins.empty())
239
set_of_schemas.erase(std::remove_if(set_of_schemas.begin(),
240
set_of_schemas.end(),
241
PruneSchemaFunctor(user_ctx)),
242
set_of_schemas.end());
245
} /* namespace drizzled */