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 ***************************************************************************/
23 #include <QStringList>
24 #include <QXmlStreamWriter>
27 #include "dashserver/users/permission.h"
29 namespace DashServer
{
34 Private() : initialized(false) {}
37 QHash
<QString
, Permission
*> permissions
;
40 User::User() : d(new Private
)
51 void User::setName(const QString
&name
)
56 void User::setLogin(const QString
&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
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
;
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();
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();
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();