1 /***************************************************************************
2 * Copyright (C) 2006 by David Cuadrado *
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. *
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. *
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"
24 #include <QDomDocument>
25 #include <QXmlStreamReader>
29 #include <dcore/debug.h>
30 #include <dcore/fortunegenerator.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
{
50 class Manager::Private
62 Manager::Manager() : d(new Private())
72 QList
<User
*> Manager::users() const
76 XDBMS::Table
*table
= XDBMS::Manager::self()->table("users");
77 foreach(QString login
, table
->objects())
79 User
*user
= new User
;
80 user
->setLogin(login
);
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" )
102 cnn
->setValid(false);
104 User
*user
= parseConnect(package
);
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);
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);
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
) )
151 doc
.setContent(package
);
153 doc
.firstChild().removeChild( doc
.firstChild().firstChildElement ("password"));
155 server
->sendToAdmins(doc
.toString());
160 cnn
->sendError(QObject::tr("Error adding user"), Dash::Network::Package::Error::Err
);
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());
186 cnn
->sendToClient(info
, true);
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
);
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
))
230 QXmlStreamWriter
writer(&xml
);
232 writer
.writeStartDocument();
233 writer
.writeStartElement("userquery");
237 writer
.writeEndElement();
238 writer
.writeEndDocument();
241 cnn
->sendToClient(xml
, true);
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
);
269 cnn
->sendError(QObject::tr("Permission denied."), Dash::Network::Package::Error::Err
);
275 User
*Manager::parseConnect(const QString
&package
)
277 QString login
, password
;
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" )
297 if( reader
.isCharacters() )
299 login
= reader
.text().toString();
302 else if(tagName
== "password" )
306 if( reader
.isCharacters() )
308 password
= reader
.text().toString();
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