Página principal | Lista alfabética | Lista de componentes | Lista de archivos | Miembros de las clases | Archivos de los miembros | Páginas relacionadas

Guardian.cpp

Ir a la documentación de este archivo.
00001 
00006 /*
00007    MUnDoCAAD MUD Engine
00008    Copyright (C) 2002-2005 José Manuel Ferrer Ortiz
00009 
00010    *****************************************************************************
00011    *                                                                           *
00012    *  This program is free software; you can redistribute it and/or modify it  *
00013    *  under the terms of the GNU General Public License version 2, as          *
00014    *  published by the Free Software Foundation.                               *
00015    *                                                                           *
00016    *  This program is distributed in the hope that it will be useful, but      *
00017    *  WITHOUT ANY WARRANTY; without even the implied warranty of               *
00018    *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU         *
00019    *  General Public License version 2 for more details.                       *
00020    *                                                                           *
00021    *  You should have received a copy of the GNU General Public License        *
00022    *  version 2 along with this program; if not, write to the Free Software    *
00023    *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA  *
00024    *                                                                           *
00025    *****************************************************************************
00026 */
00027 
00028 
00029 #include <QDir>
00030 #include <QDomDocument>
00031 
00032 #include "Guardian.hpp"
00033 #include "Log.hpp"
00034 
00035 
00036 extern QDir dir_datos;
00037 
00038 
00040 Guardian::Guardian ()
00041 {
00042   if (!CargaEntradas())
00043     qFatal ("No se ha podido cargar el fichero de usuarios.");
00044 
00045   Log (5, "Fichero de usuarios cargado satisfactoriamente.\n");
00046   QString num_usuarios;
00047   num_usuarios.setNum (usuarios.count());
00048   Log (3, "Número actual de usuarios: " + num_usuarios + "\n");
00049 }
00050 
00052 Guardian::~Guardian ()
00053 {
00054   qDebug ("Destructor de Guardian");
00055 
00056   // Eliminamos todas las entradas de usuario
00057   foreach (Entrada *ent, usuarios)
00058     delete ent;
00059 }
00060 
00062 
00066 bool Guardian::Autentifica (const QString &nombre, const QString &clave)
00067 {
00068   Entrada *entrada = usuarios.value (nombre.toLower(), 0);
00069 
00070   if (entrada == 0)  // No existe el usuario
00071     return (false);
00072 
00073   if (entrada->clave != clave)
00074     return (false);
00075 
00076   return (true);
00077 }
00078 
00080 
00083 bool Guardian::CargaEntradas ()
00084 {
00085   QFile fichero (dir_datos.filePath ("usuarios.xml"));
00086 
00087   if (!fichero.open (QIODevice::ReadOnly | QIODevice::Text))
00088   {
00089     // No se ha podido abrir el archivo
00090     qWarning ("\"%s\": %s", fichero.fileName().toLatin1().constData(),
00091               fichero.errorString().toLatin1().constData());
00092     return (false);
00093   }
00094 
00095   QDomDocument documento ("usuarios");
00096   QString dsc_error;  // Descripción del error
00097   int     lin_error, col_error;  // Línea y columna del error, respectivamente
00098 
00099   if (!documento.setContent (&fichero, &dsc_error, &lin_error, &col_error))
00100   {
00101     fichero.close();
00102 
00103     qWarning ("\"%s\" (%d, %d): %s", fichero.fileName().toLatin1().constData(),
00104               lin_error, col_error, dsc_error.toLatin1().constData());
00105     return (false);
00106   }
00107 
00108   Entrada     *entrada;
00109   QDomAttr     atributo;
00110   QDomElement  elemento = documento.documentElement();
00111   QDomNode     nodo     = elemento.firstChild();
00112 
00113   while (!nodo.isNull())
00114   {
00115     elemento = nodo.toElement();
00116     if (!elemento.isNull())
00117     {
00118       atributo = elemento.attributeNode ("nombre");
00119       entrada  = new Entrada;
00120       usuarios.insert (atributo.value().toLower(), entrada);
00121 
00122       atributo         = elemento.attributeNode ("clave");
00123       entrada->clave   = atributo.value();
00124       atributo         = elemento.attributeNode ("admin");
00125       entrada->esadmin = atributo.value() == "verdadero";
00126 
00127       // Obtenemos la lista de personajes del usuario
00128       QDomNode subnodo = elemento.firstChild();
00129       while (!subnodo.isNull())
00130       {
00131         elemento = subnodo.toElement();
00132         if (!elemento.isNull())
00133         {
00134           atributo = elemento.attributeNode ("nombre");
00135           entrada->personajes.append (atributo.value());
00136         }
00137         subnodo = subnodo.nextSibling();
00138       }
00139     }
00140     nodo = nodo.nextSibling();
00141   }
00142 
00143   fichero.close();
00144   return (true);
00145 }
00146 
00148 
00152 bool Guardian::ValidaPersonaje (const QString &usuario,
00153                                 const QString &personaje)
00154 {
00155   Entrada *entrada = usuarios.value (usuario.toLower(), 0);
00156 
00157   if (entrada == 0)  // No existe el usuario
00158     return (false);
00159 
00160   return (entrada->personajes.contains (personaje, Qt::CaseInsensitive));
00161 }

Generado el Tue Nov 29 01:04:33 2005 para MUnDoCAAD MUD Engine por  doxygen 1.4.4