~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/plugin/authorization.cc

  • Committer: Monty Taylor
  • Date: 2010-04-22 02:46:23 UTC
  • mto: (1497.3.4 enable-dtrace)
  • mto: This revision was merged to the branch mainline in revision 1527.
  • Revision ID: mordred@inaugust.com-20100422024623-4urw8fi8eraci08p
Don't overwrite the pandora_vc_revinfo file if we don't have new
authoratative information.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
#include <vector>
24
24
 
25
25
#include "drizzled/plugin/authorization.h"
26
 
#include "drizzled/identifier.h"
 
26
#include "drizzled/security_context.h"
 
27
#include "drizzled/table_identifier.h"
27
28
#include "drizzled/error.h"
28
29
#include "drizzled/session.h"
 
30
#include "drizzled/plugin/registry.h"
29
31
#include "drizzled/gettext.h"
30
32
 
 
33
using namespace std;
 
34
 
31
35
namespace drizzled
32
36
{
33
37
 
34
 
std::vector<plugin::Authorization *> authorization_plugins;
 
38
vector<plugin::Authorization *> authorization_plugins;
35
39
 
36
40
 
37
41
bool plugin::Authorization::addPlugin(plugin::Authorization *auth)
38
42
{
39
43
  if (auth != NULL)
40
44
    authorization_plugins.push_back(auth);
41
 
 
42
45
  return false;
43
46
}
44
47
 
46
49
{
47
50
  if (auth != NULL)
48
51
  {
49
 
    authorization_plugins.erase(std::find(authorization_plugins.begin(),
50
 
                                          authorization_plugins.end(),
51
 
                                          auth));
 
52
    authorization_plugins.erase(find(authorization_plugins.begin(),
 
53
                                     authorization_plugins.end(),
 
54
                                     auth));
52
55
  }
53
56
}
54
57
 
56
59
{
57
60
 
58
61
class RestrictDbFunctor :
59
 
  public std::unary_function<plugin::Authorization *, bool>
 
62
  public unary_function<plugin::Authorization *, bool>
60
63
{
61
 
  const identifier::User &user_ctx;
62
 
  identifier::Schema::const_reference schema;
63
 
 
 
64
  const SecurityContext &user_ctx;
 
65
  SchemaIdentifier &schema;
64
66
public:
65
 
  RestrictDbFunctor(const identifier::User &user_ctx_arg,
66
 
                    identifier::Schema::const_reference schema_arg) :
67
 
    std::unary_function<plugin::Authorization *, bool>(),
 
67
  RestrictDbFunctor(const SecurityContext &user_ctx_arg,
 
68
                    SchemaIdentifier &schema_arg) :
 
69
    unary_function<plugin::Authorization *, bool>(),
68
70
    user_ctx(user_ctx_arg),
69
71
    schema(schema_arg)
70
72
  { }
76
78
};
77
79
 
78
80
class RestrictTableFunctor :
79
 
  public std::unary_function<plugin::Authorization *, bool>
 
81
  public unary_function<plugin::Authorization *, bool>
80
82
{
81
 
  const identifier::User &user_ctx;
82
 
  identifier::Table &table;
 
83
  const SecurityContext &user_ctx;
 
84
  TableIdentifier &table;
83
85
public:
84
 
  RestrictTableFunctor(const identifier::User &user_ctx_arg,
85
 
                       identifier::Table &table_arg) :
86
 
    std::unary_function<plugin::Authorization *, bool>(),
 
86
  RestrictTableFunctor(const SecurityContext &user_ctx_arg,
 
87
                       TableIdentifier &table_arg) :
 
88
    unary_function<plugin::Authorization *, bool>(),
87
89
    user_ctx(user_ctx_arg),
88
90
    table(table_arg)
89
91
  { }
95
97
};
96
98
 
97
99
class RestrictProcessFunctor :
98
 
  public std::unary_function<plugin::Authorization *, bool>
 
100
  public unary_function<plugin::Authorization *, bool>
99
101
{
100
 
  const identifier::User &user_ctx;
101
 
  const identifier::User &session_ctx;
 
102
  const SecurityContext &user_ctx;
 
103
  const SecurityContext &session_ctx;
102
104
public:
103
 
  RestrictProcessFunctor(const identifier::User &user_ctx_arg,
104
 
                         const identifier::User &session_ctx_arg) :
105
 
    std::unary_function<plugin::Authorization *, bool>(),
 
105
  RestrictProcessFunctor(const SecurityContext &user_ctx_arg,
 
106
                         const SecurityContext &session_ctx_arg) :
 
107
    unary_function<plugin::Authorization *, bool>(),
106
108
    user_ctx(user_ctx_arg),
107
109
    session_ctx(session_ctx_arg)
108
110
  { }
114
116
};
115
117
 
116
118
class PruneSchemaFunctor :
117
 
  public std::unary_function<identifier::Schema&, bool>
 
119
  public unary_function<SchemaIdentifier&, bool>
118
120
{
119
 
  drizzled::identifier::User::const_shared_ptr user_ctx;
 
121
  const SecurityContext &user_ctx;
120
122
public:
121
 
  PruneSchemaFunctor(drizzled::identifier::User::const_shared_ptr user_ctx_arg) :
122
 
    std::unary_function<identifier::Schema&, bool>(),
 
123
  PruneSchemaFunctor(const SecurityContext &user_ctx_arg) :
 
124
    unary_function<SchemaIdentifier&, bool>(),
123
125
    user_ctx(user_ctx_arg)
124
126
  { }
125
127
 
131
133
 
132
134
} /* namespace */
133
135
 
134
 
bool plugin::Authorization::isAuthorized(identifier::User::const_shared_ptr user_ctx,
135
 
                                         identifier::Schema::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
 
                                         identifier::Table &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
 
  return isAuthorized(*user_ctx, session, send_error);
212
 
}
213
 
 
214
 
bool plugin::Authorization::isAuthorized(drizzled::identifier::User::const_reference user_ctx,
215
 
                                         const Session *session,
216
 
                                         bool send_error)
217
 
{
218
 
  drizzled::identifier::User::const_shared_ptr session_ctx= session->user();
219
 
 
220
 
  /* If we never loaded any authorization plugins, just return true */
221
 
  if (authorization_plugins.empty())
222
 
    return true;
223
 
 
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));
 
136
bool plugin::Authorization::isAuthorized(const SecurityContext &user_ctx,
 
137
                                         SchemaIdentifier &schema_identifier,
 
138
                                         bool send_error)
 
139
{
 
140
  /* If we never loaded any authorization plugins, just return true */
 
141
  if (authorization_plugins.empty())
 
142
    return true;
 
143
 
 
144
  /* Use find_if instead of foreach so that we can collect return codes */
 
145
  vector<plugin::Authorization *>::const_iterator iter=
 
146
    find_if(authorization_plugins.begin(),
 
147
            authorization_plugins.end(),
 
148
            RestrictDbFunctor(user_ctx, schema_identifier));
 
149
 
 
150
 
 
151
  /*
 
152
   * If iter is == end() here, that means that all of the plugins returned
 
153
   * false, which means that that each of them believe the user is authorized
 
154
   * to view the resource in question.
 
155
   */
 
156
  if (iter != authorization_plugins.end())
 
157
  {
 
158
    if (send_error)
 
159
    {
 
160
      my_error(ER_DBACCESS_DENIED_ERROR, MYF(0),
 
161
               user_ctx.getUser().c_str(),
 
162
               user_ctx.getIp().c_str(),
 
163
               schema_identifier.getSQLPath().c_str());
 
164
    }
 
165
    return false;
 
166
  }
 
167
  return true;
 
168
}
 
169
 
 
170
bool plugin::Authorization::isAuthorized(const SecurityContext &user_ctx,
 
171
                                         TableIdentifier &table,
 
172
                                         bool send_error)
 
173
{
 
174
  /* If we never loaded any authorization plugins, just return true */
 
175
  if (authorization_plugins.empty())
 
176
    return true;
 
177
 
 
178
  /* Use find_if instead of foreach so that we can collect return codes */
 
179
  vector<plugin::Authorization *>::const_iterator iter=
 
180
    find_if(authorization_plugins.begin(),
 
181
            authorization_plugins.end(),
 
182
            RestrictTableFunctor(user_ctx, table));
 
183
 
 
184
  /*
 
185
   * If iter is == end() here, that means that all of the plugins returned
 
186
   * false, which means that that each of them believe the user is authorized
 
187
   * to view the resource in question.
 
188
   */
 
189
  if (iter != authorization_plugins.end())
 
190
  {
 
191
    if (send_error)
 
192
    {
 
193
      my_error(ER_DBACCESS_DENIED_ERROR, MYF(0),
 
194
               user_ctx.getUser().c_str(),
 
195
               user_ctx.getIp().c_str(),
 
196
               table.getSQLPath().c_str());
 
197
    }
 
198
    return false;
 
199
  }
 
200
  return true;
 
201
}
 
202
 
 
203
bool plugin::Authorization::isAuthorized(const SecurityContext &user_ctx,
 
204
                                         const Session *session,
 
205
                                         bool send_error)
 
206
{
 
207
  const SecurityContext &session_ctx= session->getSecurityContext();
 
208
 
 
209
  /* If we never loaded any authorization plugins, just return true */
 
210
  if (authorization_plugins.empty())
 
211
    return true;
 
212
 
 
213
  /* Use find_if instead of foreach so that we can collect return codes */
 
214
  vector<plugin::Authorization *>::const_iterator iter=
 
215
    find_if(authorization_plugins.begin(),
 
216
            authorization_plugins.end(),
 
217
            RestrictProcessFunctor(user_ctx, session_ctx));
229
218
 
230
219
  /*
231
220
   * If iter is == end() here, that means that all of the plugins returned
241
230
    }
242
231
    return false;
243
232
  }
244
 
 
245
233
  return true;
246
234
}
247
235
 
248
 
void plugin::Authorization::pruneSchemaNames(drizzled::identifier::User::const_shared_ptr user_ctx,
249
 
                                             identifier::Schema::vector &set_of_schemas)
 
236
void plugin::Authorization::pruneSchemaNames(const SecurityContext &user_ctx,
 
237
                                             SchemaIdentifierList &set_of_schemas)
250
238
{
251
239
  /* If we never loaded any authorization plugins, just return true */
252
240
  if (authorization_plugins.empty())
253
241
    return;
254
242
 
255
 
  set_of_schemas.erase(std::remove_if(set_of_schemas.begin(),
256
 
                                      set_of_schemas.end(),
257
 
                                      PruneSchemaFunctor(user_ctx)),
 
243
  set_of_schemas.erase(remove_if(set_of_schemas.begin(),
 
244
                                 set_of_schemas.end(),
 
245
                                 PruneSchemaFunctor(user_ctx)),
258
246
                       set_of_schemas.end());
259
247
}
260
248