Add members to project
[dashstudio.git] / src / dashserver / users / user.cpp
blob3de1ded2fce65a9fb4cf095f8852935b26c6f1e0
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 "user.h"
23 #include <QStringList>
24 #include <QXmlStreamWriter>
25 #include <QSet>
27 #include "dashserver/users/permission.h"
29 namespace DashServer {
30 namespace Users {
32 struct User::Private
34 Private() : initialized(false) {}
35 bool initialized;
37 QHash<QString, Permission *> permissions;
40 User::User() : d(new Private)
42 setClass("user");
46 User::~User()
48 delete d;
51 void User::setName(const QString &name)
53 add("name", name);
56 void User::setLogin(const QString &login)
58 setId(login);
59 add("login", login);
62 void User::setPassword(const QString &password)
64 add("password", password);
67 void User::setType(int type)
69 add("type", QString::number(type) );
72 QString User::name() const
74 return value("name");
77 QString User::login() const
79 return value("login");
82 QString User::password() const
84 return value("password");
87 int User::type() const
89 return value("type").toInt();
92 QList<Permission *> User::permissions() const
94 return d->permissions.values();
97 void User::addPermission(int module, Permission::Opts opts)
99 QString m = DashServer::Module::name(module);
101 addPermission(m, opts);
104 void User::addPermission(const QString &module, Permission::Opts opts)
106 addPermission(new Permission(module, opts));
109 void User::addPermission(Permission *perm)
111 QString module = perm->module();
113 if( !d->permissions.contains(module) )
115 d->permissions[module] = perm;
116 add(perm);
118 else
120 delete perm;
121 perm = 0;
125 bool User::canReadOn(int module) const
127 QString m = DashServer::Module::name(module);
129 if( d->permissions.contains(m) )
131 return d->permissions[m]->read();
134 return false;
137 bool User::canWriteOn(int module) const
139 QString m = DashServer::Module::name(module);
141 if( d->permissions.contains(m) )
143 return d->permissions[m]->write();
146 return false;
149 void User::toXml(QXmlStreamWriter *writer)
151 writer->writeStartElement("login");
152 writer->writeCharacters(login());
153 writer->writeEndElement();
155 writer->writeStartElement("name");
156 writer->writeCharacters(name());
157 writer->writeEndElement();
159 writer->writeStartElement("permissions");
161 foreach(Permission *perm, d->permissions)
163 writer->writeStartElement("module");
164 writer->writeAttribute("name", perm->module());
165 writer->writeAttribute("read", perm->read() ? "1" : "0" );
166 writer->writeAttribute("write", perm->write() ? "1" : "0" );
167 writer->writeEndElement();
170 writer->writeEndElement();