~ubuntu-branches/ubuntu/saucy/drizzle/saucy-proposed

« back to all changes in this revision

Viewing changes to .pc/debian-changes-2010.12.06-0ubuntu4/drizzled/plugin/authorization.cc

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2011-01-04 09:31:58 UTC
  • mfrom: (1.2.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20110104093158-smhgvkfdi2y9au3i
Tags: 2011.01.07-0ubuntu1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 
 *
4
 
 *  Copyright (C) 2010 Monty Taylor
5
 
 *
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.
10
 
 *
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.
15
 
 *
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
19
 
 */
20
 
 
21
 
#include "config.h"
22
 
 
23
 
#include <vector>
24
 
 
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"
30
 
 
31
 
namespace drizzled
32
 
{
33
 
 
34
 
std::vector<plugin::Authorization *> authorization_plugins;
35
 
 
36
 
 
37
 
bool plugin::Authorization::addPlugin(plugin::Authorization *auth)
38
 
{
39
 
  if (auth != NULL)
40
 
    authorization_plugins.push_back(auth);
41
 
 
42
 
  return false;
43
 
}
44
 
 
45
 
void plugin::Authorization::removePlugin(plugin::Authorization *auth)
46
 
{
47
 
  if (auth != NULL)
48
 
  {
49
 
    authorization_plugins.erase(std::find(authorization_plugins.begin(),
50
 
                                          authorization_plugins.end(),
51
 
                                          auth));
52
 
  }
53
 
}
54
 
 
55
 
namespace
56
 
{
57
 
 
58
 
class RestrictDbFunctor :
59
 
  public std::unary_function<plugin::Authorization *, bool>
60
 
{
61
 
  const identifier::User &user_ctx;
62
 
  SchemaIdentifier::const_reference schema;
63
 
 
64
 
public:
65
 
  RestrictDbFunctor(const identifier::User &user_ctx_arg,
66
 
                    SchemaIdentifier::const_reference schema_arg) :
67
 
    std::unary_function<plugin::Authorization *, bool>(),
68
 
    user_ctx(user_ctx_arg),
69
 
    schema(schema_arg)
70
 
  { }
71
 
 
72
 
  inline result_type operator()(argument_type auth)
73
 
  {
74
 
    return auth->restrictSchema(user_ctx, schema);
75
 
  }
76
 
};
77
 
 
78
 
class RestrictTableFunctor :
79
 
  public std::unary_function<plugin::Authorization *, bool>
80
 
{
81
 
  const identifier::User &user_ctx;
82
 
  TableIdentifier &table;
83
 
public:
84
 
  RestrictTableFunctor(const identifier::User &user_ctx_arg,
85
 
                       TableIdentifier &table_arg) :
86
 
    std::unary_function<plugin::Authorization *, bool>(),
87
 
    user_ctx(user_ctx_arg),
88
 
    table(table_arg)
89
 
  { }
90
 
 
91
 
  inline result_type operator()(argument_type auth)
92
 
  {
93
 
    return auth->restrictTable(user_ctx, table);
94
 
  }
95
 
};
96
 
 
97
 
class RestrictProcessFunctor :
98
 
  public std::unary_function<plugin::Authorization *, bool>
99
 
{
100
 
  const identifier::User &user_ctx;
101
 
  const identifier::User &session_ctx;
102
 
public:
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)
108
 
  { }
109
 
 
110
 
  inline result_type operator()(argument_type auth)
111
 
  {
112
 
    return auth->restrictProcess(user_ctx, session_ctx);
113
 
  }
114
 
};
115
 
 
116
 
class PruneSchemaFunctor :
117
 
  public std::unary_function<SchemaIdentifier&, bool>
118
 
{
119
 
  drizzled::identifier::User::const_shared_ptr user_ctx;
120
 
public:
121
 
  PruneSchemaFunctor(drizzled::identifier::User::const_shared_ptr user_ctx_arg) :
122
 
    std::unary_function<SchemaIdentifier&, bool>(),
123
 
    user_ctx(user_ctx_arg)
124
 
  { }
125
 
 
126
 
  inline result_type operator()(argument_type auth)
127
 
  {
128
 
    return not plugin::Authorization::isAuthorized(user_ctx, auth, false);
129
 
  }
130
 
};
131
 
 
132
 
} /* namespace */
133
 
 
134
 
bool plugin::Authorization::isAuthorized(identifier::User::const_shared_ptr user_ctx,
135
 
                                         SchemaIdentifier::const_reference schema_identifier,
136
 
                                         bool send_error)
137
 
{
138
 
  /* If we never loaded any authorization plugins, just return true */
139
 
  if (authorization_plugins.empty())
140
 
    return true;
141
 
 
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));
147
 
 
148
 
 
149
 
  /*
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.
153
 
   */
154
 
  if (iter != authorization_plugins.end())
155
 
  {
156
 
    if (send_error)
157
 
    {
158
 
      std::string path;
159
 
      schema_identifier.getSQLPath(path);
160
 
 
161
 
      my_error(ER_DBACCESS_DENIED_ERROR, MYF(0),
162
 
               user_ctx->username().c_str(),
163
 
               user_ctx->address().c_str(),
164
 
               path.c_str());
165
 
    }
166
 
    return false;
167
 
  }
168
 
  return true;
169
 
}
170
 
 
171
 
bool plugin::Authorization::isAuthorized(drizzled::identifier::User::const_shared_ptr user_ctx,
172
 
                                         TableIdentifier &table,
173
 
                                         bool send_error)
174
 
{
175
 
  /* If we never loaded any authorization plugins, just return true */
176
 
  if (authorization_plugins.empty())
177
 
    return true;
178
 
 
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));
184
 
 
185
 
  /*
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.
189
 
   */
190
 
  if (iter != authorization_plugins.end())
191
 
  {
192
 
    if (send_error)
193
 
    {
194
 
      std::string path;
195
 
      table.getSQLPath(path);
196
 
 
197
 
      my_error(ER_DBACCESS_DENIED_ERROR, MYF(0),
198
 
               user_ctx->username().c_str(),
199
 
               user_ctx->address().c_str(),
200
 
               path.c_str());
201
 
    }
202
 
    return false;
203
 
  }
204
 
  return true;
205
 
}
206
 
 
207
 
bool plugin::Authorization::isAuthorized(drizzled::identifier::User::const_shared_ptr user_ctx,
208
 
                                         const Session *session,
209
 
                                         bool send_error)
210
 
{
211
 
  drizzled::identifier::User::const_shared_ptr session_ctx= session->user();
212
 
 
213
 
  /* If we never loaded any authorization plugins, just return true */
214
 
  if (authorization_plugins.empty())
215
 
    return true;
216
 
 
217
 
  /* Use find_if instead of foreach so that we can collect return codes */
218
 
  std::vector<plugin::Authorization *>::const_iterator iter=
219
 
    std::find_if(authorization_plugins.begin(),
220
 
                 authorization_plugins.end(),
221
 
                 RestrictProcessFunctor(*user_ctx, *session_ctx));
222
 
 
223
 
  /*
224
 
   * If iter is == end() here, that means that all of the plugins returned
225
 
   * false, which means that that each of them believe the user is authorized
226
 
   * to view the resource in question.
227
 
   */
228
 
 
229
 
  if (iter != authorization_plugins.end())
230
 
  {
231
 
    if (send_error)
232
 
    {
233
 
      my_error(ER_KILL_DENIED_ERROR, MYF(0), session->thread_id);
234
 
    }
235
 
    return false;
236
 
  }
237
 
  return true;
238
 
}
239
 
 
240
 
void plugin::Authorization::pruneSchemaNames(drizzled::identifier::User::const_shared_ptr user_ctx,
241
 
                                             SchemaIdentifier::vector &set_of_schemas)
242
 
{
243
 
  /* If we never loaded any authorization plugins, just return true */
244
 
  if (authorization_plugins.empty())
245
 
    return;
246
 
 
247
 
  set_of_schemas.erase(std::remove_if(set_of_schemas.begin(),
248
 
                                      set_of_schemas.end(),
249
 
                                      PruneSchemaFunctor(user_ctx)),
250
 
                       set_of_schemas.end());
251
 
}
252
 
 
253
 
} /* namespace drizzled */