1
/* This file is part of the KDE project
3
(C) Copyright 2008 by Lorenzo Villani <lvillani@binaryhelix.net>
5
This program is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Library General Public
7
License as published by the Free Software Foundation; either
8
version 2 of the License, or (at your option) any later version.
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
Library General Public License for more details.
15
You should have received a copy of the GNU Library General Public License
16
along with this library; see the file COPYING.LIB. If not, write to
17
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18
Boston, MA 02110-1301, USA.
24
#include <kexidb/cursor.h>
25
#include <kexidb/connection.h>
26
#include <kexidb/tableschema.h>
27
#include <kexidb/queryschema.h>
28
#include <kexidb/roweditbuffer.h>
30
#include <pion/net/PionUser.hpp>
31
#include <pion/net/HTTPAuth.hpp>
33
#include "Permission.h"
34
#include "../model/DataProvider.h"
36
#include "Authenticator.h"
38
namespace KexiWebForms {
40
Authenticator* Authenticator::m_instance = 0;
42
void Authenticator::init(pion::net::HTTPAuthPtr p) {
44
m_instance = new Authenticator(p);
45
m_instance->loadStore();
48
Authenticator* Authenticator::getInstance() {
56
bool Authenticator::loadStore() {
57
KexiDB::TableSchema* table = KexiWebForms::Model::gConnection->tableSchema("kexi__users");
60
// the table doesn't exist, create it (programmatically)
61
kDebug() << "kexi__users table does not exist, creating it";
62
KexiDB::TableSchema* kexi__users = new KexiDB::TableSchema("kexi__users");
63
kexi__users->setNative(true);
64
KexiDB::Field* id = new KexiDB::Field("u_id", KexiDB::Field::Integer);
65
id->setAutoIncrement(true);
66
id->setPrimaryKey(true);
67
kexi__users->insertField(0, id);
68
KexiDB::Field* name = new KexiDB::Field("u_name", KexiDB::Field::Text);
69
kexi__users->insertField(1, name);
70
KexiDB::Field* password = new KexiDB::Field("u_password", KexiDB::Field::Text);
71
kexi__users->insertField(2, password);
72
KexiDB::Field* create = new KexiDB::Field("u_create", KexiDB::Field::Boolean);
73
kexi__users->insertField(3, create);
74
KexiDB::Field* read = new KexiDB::Field("u_read", KexiDB::Field::Boolean);
75
kexi__users->insertField(4, read);
76
KexiDB::Field* update = new KexiDB::Field("u_update", KexiDB::Field::Boolean);
77
kexi__users->insertField(5, update);
78
KexiDB::Field* fdelete = new KexiDB::Field("u_delete", KexiDB::Field::Boolean);
79
kexi__users->insertField(6, fdelete);
80
KexiDB::Field* fquery = new KexiDB::Field("u_query", KexiDB::Field::Boolean);
81
kexi__users->insertField(7, fquery);
83
if (!KexiWebForms::Model::gConnection->createTable(kexi__users)) {
84
// Table was not created, fatal error
85
kError() << "Failed to create system table kexi__users";
86
kError() << "Error string: " << KexiWebForms::Model::gConnection->errorMsg();
90
// Table was created, create two standard accounts
91
KexiDB::QuerySchema query(*kexi__users);
92
KexiDB::Cursor* cursor = KexiWebForms::Model::gConnection->prepareQuery(query);
93
KexiDB::RecordData recordData(kexi__users->fieldCount());
94
KexiDB::RowEditBuffer editBuffer(true);
97
QVariant vfalse(false);
98
kDebug() << "Creating user root with password root";
99
QVariant user_root("root");
100
QVariant password_root("root");
101
editBuffer.insert(*query.columnInfo(name->name()), user_root);
102
editBuffer.insert(*query.columnInfo(password->name()), password_root);
103
editBuffer.insert(*query.columnInfo(create->name()), vtrue);
104
editBuffer.insert(*query.columnInfo(read->name()), vtrue);
105
editBuffer.insert(*query.columnInfo(update->name()), vtrue);
106
editBuffer.insert(*query.columnInfo(fdelete->name()), vtrue);
107
editBuffer.insert(*query.columnInfo(fquery->name()), vtrue);
108
kDebug() << "Registering user within database";
109
if (cursor->insertRow(recordData, editBuffer)) {
110
kDebug() << "Succeeded";
111
User* u = new User("root", "root");
113
m_auth->addUser(u->name().toUtf8().constData(), u->password().toUtf8().constData());
115
kError() << "An error occurred";
120
kDebug() << "Creating user anonymous with password guest";
121
QVariant user_anonymous("anonymous");
122
QVariant password_anonymous("guest");
123
editBuffer.insert(*query.columnInfo(name->name()), user_anonymous);
124
editBuffer.insert(*query.columnInfo(password->name()), password_anonymous);
125
editBuffer.insert(*query.columnInfo(create->name()), vfalse);
126
editBuffer.insert(*query.columnInfo(read->name()), vfalse);
127
editBuffer.insert(*query.columnInfo(update->name()), vfalse);
128
editBuffer.insert(*query.columnInfo(fdelete->name()), vfalse);
129
editBuffer.insert(*query.columnInfo(fquery->name()), vfalse);
130
if (cursor->insertRow(recordData, editBuffer)) {
131
kDebug() << "Succeeded";
132
User* u = new User("anonymous", "guest");
134
m_auth->addUser(u->name().toUtf8().constData(), u->password().toUtf8().constData());
136
kError() << "An error occurred";
139
KexiWebForms::Model::gConnection->deleteCursor(cursor);
142
// load stuff from the store, create appropriated User objects, store them within
144
KexiDB::QuerySchema query(*table);
145
KexiDB::Cursor* cursor = KexiWebForms::Model::gConnection->executeQuery(query);
146
while (cursor->moveNext()) {
148
QString* username = new QString(cursor->value(1).toString());
149
QString* password = new QString(cursor->value(2).toString());
150
QList<Permission>* perms = new QList<Permission>;
152
if (cursor->value(3).toBool()) perms->append(CREATE);
153
if (cursor->value(4).toBool()) perms->append(READ);
154
if (cursor->value(5).toBool()) perms->append(UPDATE);
155
if (cursor->value(6).toBool()) perms->append(DELETE);
156
if (cursor->value(7).toBool()) perms->append(QUERY);
158
User* u = new User(*username, *password, *perms);
160
m_auth->addUser(u->name().toUtf8().constData(), u->password().toUtf8().constData());
161
kDebug() << "Loaded user " << *username << " from store";
168
User Authenticator::authenticate(const char* name, const char* password) {
169
for (int i = 0; i < m_users.size(); ++i) {
170
User u = m_users.at(i);
171
if ((u.name() == name) && (u.password() == password)) {
175
return User("anonymous", "guest");
178
User Authenticator::authenticate(const std::string& name, const std::string& password) {
179
return authenticate(name.c_str(), password.c_str());
182
User Authenticator::authenticate(pion::net::PionUserPtr p) {
183
return authenticate(p->getUsername(), p->getPassword());