~ubuntu-branches/ubuntu/precise/koffice/precise

« back to all changes in this revision

Viewing changes to kexi/webforms/auth/Authenticator.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2010-09-21 15:36:35 UTC
  • mfrom: (1.4.1 upstream) (60.2.11 maverick)
  • Revision ID: james.westby@ubuntu.com-20100921153635-6tejqkiro2u21ydi
Tags: 1:2.2.2-0ubuntu3
Add kubuntu_03_fix-crash-on-closing-sqlite-connection-2.2.2.diff and
kubuntu_04_support-large-memo-values-for-msaccess-2.2.2.diff as
recommended by upstream http://kexi-
project.org/wiki/wikiview/index.php@Kexi2.2_Patches.html#sqlite_stab
ility

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of the KDE project
 
2
 
 
3
   (C) Copyright 2008 by Lorenzo Villani <lvillani@binaryhelix.net>
 
4
 
 
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.
 
9
 
 
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.
 
14
 
 
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.
 
19
*/
 
20
 
 
21
#include <KDebug>
 
22
 
 
23
 
 
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>
 
29
 
 
30
#include <pion/net/PionUser.hpp>
 
31
#include <pion/net/HTTPAuth.hpp>
 
32
 
 
33
#include "Permission.h"
 
34
#include "../model/DataProvider.h"
 
35
 
 
36
#include "Authenticator.h"
 
37
 
 
38
namespace KexiWebForms {
 
39
namespace Auth {
 
40
Authenticator* Authenticator::m_instance = 0;
 
41
 
 
42
void Authenticator::init(pion::net::HTTPAuthPtr p) {
 
43
    if (!m_instance)
 
44
        m_instance = new Authenticator(p);
 
45
    m_instance->loadStore();
 
46
}
 
47
 
 
48
Authenticator* Authenticator::getInstance() {
 
49
    if (m_instance)
 
50
        return m_instance;
 
51
    else // ouch!
 
52
        return NULL;
 
53
}
 
54
 
 
55
 
 
56
bool Authenticator::loadStore() {
 
57
    KexiDB::TableSchema* table = KexiWebForms::Model::gConnection->tableSchema("kexi__users");
 
58
 
 
59
    if (!table) {
 
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);
 
82
 
 
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();
 
87
            delete kexi__users;
 
88
            return false;
 
89
        } else {
 
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);
 
95
            // root
 
96
            QVariant vtrue(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");
 
112
                m_users.append(*u);
 
113
                m_auth->addUser(u->name().toUtf8().constData(), u->password().toUtf8().constData());
 
114
            } else {
 
115
                kError() << "An error occurred";
 
116
                return false;
 
117
            }
 
118
 
 
119
            // anonymous
 
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");
 
133
                m_users.append(*u);
 
134
                m_auth->addUser(u->name().toUtf8().constData(), u->password().toUtf8().constData());
 
135
            } else {
 
136
                kError() << "An error occurred";
 
137
                return false;
 
138
            }
 
139
            KexiWebForms::Model::gConnection->deleteCursor(cursor);
 
140
        }
 
141
    } else {
 
142
        // load stuff from the store, create appropriated User objects, store them within
 
143
        // Authenticator
 
144
        KexiDB::QuerySchema query(*table);
 
145
        KexiDB::Cursor* cursor = KexiWebForms::Model::gConnection->executeQuery(query);
 
146
        while (cursor->moveNext()) {
 
147
            // Skip id
 
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>;
 
151
 
 
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);
 
157
 
 
158
            User* u = new User(*username, *password, *perms);
 
159
            m_users.append(*u);
 
160
            m_auth->addUser(u->name().toUtf8().constData(), u->password().toUtf8().constData());
 
161
            kDebug() << "Loaded user " << *username << " from store";
 
162
        }
 
163
    }
 
164
 
 
165
    return true;
 
166
}
 
167
 
 
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)) {
 
172
            return u;
 
173
        }
 
174
    }
 
175
    return User("anonymous", "guest");
 
176
}
 
177
 
 
178
User Authenticator::authenticate(const std::string& name, const std::string& password) {
 
179
    return authenticate(name.c_str(), password.c_str());
 
180
}
 
181
 
 
182
User Authenticator::authenticate(pion::net::PionUserPtr p) {
 
183
    return authenticate(p->getUsername(), p->getPassword());
 
184
}
 
185
 
 
186
}
 
187
}