Add members to project
[dashstudio.git] / src / dashserver / users / usermanager.cpp
blob8d1df5c1b89e2b1eba720f89cc773e0739e05a56
1 /***************************************************************************
2 * Copyright (C) 2006 by David Cuadrado *
3 * krawek@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (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 *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "usermanager.h"
23 #include <QHash>
24 #include <QDomDocument>
25 #include <QXmlStreamReader>
27 #include "user.h"
29 #include <dcore/debug.h>
30 #include <dcore/fortunegenerator.h>
32 #include "package.h"
33 #include "logger.h"
35 #include "server.h"
36 #include "connection.h"
38 #include "useractionparser.h"
39 #include "dash/network/package/userlist.h"
40 #include "dash/network/package/ack.h"
41 #include "bans/banmanager.h"
42 #include "dashserver/xdbms/manager.h"
43 #include "dashserver/xdbms/table.h"
45 #include <dash/network/socket.h>
47 namespace DashServer {
48 namespace Users {
50 class Manager::Private
52 public:
53 Private()
57 ~Private()
62 Manager::Manager() : d(new Private())
67 Manager::~Manager()
69 delete d;
72 QList<User *> Manager::users() const
74 QList<User *> users;
76 XDBMS::Table *table = XDBMS::Manager::self()->table("users");
77 foreach(QString login, table->objects())
79 User *user = new User;
80 user->setLogin(login);
81 table->load(user);
83 users << user;
87 return users;
90 void Manager::handlePackage(DashServer::Package *const pkg)
92 QString root = pkg->root();
93 QString package = pkg->xml();
95 DashServer::Connection *cnn = pkg->source();
96 DashServer::TcpServer *server = cnn->server();
99 if ( root == "connect" )
101 pkg->accept();
102 cnn->setValid(false);
104 User *user = parseConnect(package);
106 if( user )
108 cnn->setUser(user);
110 if( user->type() == 1 )
112 server->addAdmin(cnn);
115 QHash<QString, int> userPerms;
116 QList<Permission *> permissions = user->permissions();
117 foreach(Permission *perm, permissions)
119 userPerms[perm->module()] = perm->opts();
122 Dash::Network::Package::Ack ack(QObject::tr("<center><b>Message of the day:</b></center><br/> ")+QObject::tr("Hello, world!"), cnn->sign(), userPerms);
124 cnn->sendToClient(ack.toString(), false);
126 else
128 Dash::Network::Package::Error error(QObject::tr("Invalid login or password"), Dash::Network::Package::Error::Err);
130 dfDebug << error.toString();
132 QString ip = cnn->client()->peerAddress().toString();
133 server->banManager()->failed(ip);
135 cnn->sendToClient(error.toString(), true);
136 cnn->close();
139 else if(root == "adduser")
141 if ( cnn->user()->canWriteOn(DashServer::Module::Admin) )
143 Parsers::UserActionParser parser;
144 if(parser.parse(package))
146 Users::User user = parser.user();
148 if( XDBMS::Manager::self()->addObject("users", &user) )
150 QDomDocument doc;
151 doc.setContent(package);
153 doc.firstChild().removeChild( doc.firstChild().firstChildElement ("password"));
155 server->sendToAdmins(doc.toString());
156 pkg->accept();
158 else
160 cnn->sendError(QObject::tr("Error adding user"), Dash::Network::Package::Error::Err);
164 else
166 cnn->sendError(QObject::tr("Permission denied."), Dash::Network::Package::Error::Err);
169 else if ( root == "listusers" )
171 if ( cnn->user()->canWriteOn(DashServer::Module::Admin) )
173 Dash::Network::Package::UserList info;
175 QList<User *> users = this->users();
177 foreach(Users::User *u, users )
179 info.addUser(u->login(), u->name());
180 //delete u;
183 qDeleteAll(users);
184 users.clear();
186 cnn->sendToClient(info, true);
187 pkg->accept();
189 else
191 cnn->sendError(QObject::tr("Permission denied."), Dash::Network::Package::Error::Err);
194 else if(root == "removeuser")
196 if ( cnn->user()->canWriteOn( DashServer::Module::Admin ) )
198 Parsers::UserActionParser parser;
200 if(parser.parse(package))
202 User user = parser.user();
204 if( XDBMS::Manager::self()->remove("users", &user) )
206 server->sendToAdmins(package);
207 pkg->accept();
211 else
213 cnn->sendError(QObject::tr("Permission denied."), Dash::Network::Package::Error::Err);
216 else if(root == "queryuser")
218 if ( cnn->user()->canReadOn( DashServer::Module::Admin ) )
220 Parsers::UserActionParser parser;
221 if(parser.parse(package))
223 Users::User user = parser.user();
225 if(XDBMS::Manager::self()->load("users", &user))
227 QString xml;
230 QXmlStreamWriter writer(&xml);
232 writer.writeStartDocument();
233 writer.writeStartElement("userquery");
235 user.toXml(&writer);
237 writer.writeEndElement();
238 writer.writeEndDocument();
241 cnn->sendToClient(xml, true);
242 pkg->accept();
246 else
248 cnn->sendError(QObject::tr("Permission denied."), Dash::Network::Package::Error::Err);
251 else if(root == "updateuser")
253 if ( cnn->user()->canWriteOn( DashServer::Module::Admin ) )
255 Parsers::UserActionParser parser;
256 User user = parser.user();
258 if(parser.parse(package))
260 if( XDBMS::Manager::self()->update("users", &user) )
262 server->sendToAdmins(package);
263 pkg->accept();
267 else
269 cnn->sendError(QObject::tr("Permission denied."), Dash::Network::Package::Error::Err);
275 User *Manager::parseConnect(const QString &package)
277 QString login, password;
278 int type = 0;
280 QXmlStreamReader reader(package);
282 while( !reader.atEnd() )
284 switch(reader.readNext())
286 case QXmlStreamReader::StartElement:
288 QString tagName = reader.name().toString();
290 if( tagName == "client" )
292 type = reader.attributes().value("type").toString().toInt();
294 else if( tagName == "login" )
296 reader.readNext();
297 if( reader.isCharacters() )
299 login = reader.text().toString();
302 else if(tagName == "password" )
304 reader.readNext();
306 if( reader.isCharacters() )
308 password = reader.text().toString();
312 break;
313 default: break;
318 if( ! login.isEmpty() )
320 Users::User *user = new Users::User;
321 user->setLogin(login);
323 XDBMS::Manager::self()->load("users", user);
325 if( user->password() == password ) // FIXME: encrypt
327 user->setType(type);
328 return user;
331 delete user;
334 return 0;