User.class.php

gehe zur Dokumentation dieser Datei
00001 <?php
00002 // ---------------------------------------------------------------------------
00003 // $Id$
00004 // ---------------------------------------------------------------------------
00005 // DaCMS Content Management System
00006 // Copyright (C) 2002 Jan Dankert, jandankert@jandankert.de
00007 //
00008 // This program is free software; you can redistribute it and/or
00009 // modify it under the terms of the GNU General Public License
00010 // as published by the Free Software Foundation; either version 2
00011 // of the License, or (at your option) any later version.
00012 //
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU General Public License
00019 // along with this program; if not, write to the Free Software
00020 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00021 // ---------------------------------------------------------------------------
00022 // $Log$
00023 // Revision 1.33  2009-03-22 15:14:41  dankert
00024 // Keine Aktion mehr in Methode loadRights()
00025 //
00026 // Revision 1.32  2009-03-19 10:05:05  dankert
00027 // Bugfix bei Ermitteln der zur Verfügung stehenden Projekte: Nur die Root-Objekte der Projekte berücksichtigen.
00028 //
00029 // Revision 1.31  2009-03-19 04:29:39  dankert
00030 // Bei LDAP-Login Gruppenzugehörigkeiten synchronisieren.
00031 //
00032 // Revision 1.30  2009-03-19 02:01:07  dankert
00033 // Für die Liste der lesbaren Projekte müssen nur die Root-Ordner herangezogen werden.
00034 //
00035 // Revision 1.29  2008-03-06 21:19:15  dankert
00036 // Nur Logging-Ausgabe verbessert.
00037 //
00038 // Revision 1.28  2007-11-08 23:03:41  dankert
00039 // Kommentare erg?nzt.
00040 //
00041 // Revision 1.27  2007-11-05 20:45:36  dankert
00042 // *** empty log message ***
00043 //
00044 // Revision 1.26  2007-10-24 18:53:21  dankert
00045 // Automatisches Hinzuf?gen von Gruppen zu neuen Benutzern.
00046 //
00047 // Revision 1.25  2007-10-10 18:23:38  dankert
00048 // Anzeigen der Gruppen-Mitgliedschaften im Profil.
00049 //
00050 // Revision 1.24  2007-04-24 20:55:22  dankert
00051 // Autorisierung ?ber LDAP.
00052 //
00053 // Revision 1.23  2007-04-23 21:48:01  dankert
00054 // Authentisierung gegen einen externen Server mit HTTP-Basic-Auth erm?glichen.
00055 //
00056 // Revision 1.22  2007-04-21 11:52:24  dankert
00057 // Default-Style ist konfigurierbar.
00058 //
00059 // Revision 1.21  2007-04-08 21:51:55  dankert
00060 // Korrektur Benutzer l?schen, wenn noch Referenzen in der Datenbank vorhanden sind.
00061 //
00062 // Revision 1.20  2007/02/15 22:12:02  dankert
00063 // Neue Methode "isValid()"
00064 //
00065 // Revision 1.19  2007/01/27 00:16:36  dankert
00066 // Neuer Loginmechanismus: "authdb"
00067 //
00068 // Revision 1.18  2007/01/21 22:20:12  dankert
00069 // Erweiterungen bei LDAP-Zugriff, Auslagerung von LDAP-Befehlen in eigene Klasse.
00070 //
00071 // Revision 1.17  2007/01/20 15:20:31  dankert
00072 // Neue Methode "getName()"
00073 //
00074 // Revision 1.16  2006/11/28 21:05:00  dankert
00075 // Wenn Klartextkennwort (kein MD5), dann Benutzer nicht anmelden.
00076 //
00077 // Revision 1.15  2006/11/16 19:58:57  dankert
00078 // Pflicht zu Kennwort?nderung ermitteln.
00079 //
00080 // Revision 1.14  2006/08/30 19:15:26  dankert
00081 // Erzeugen Kennwort und Laden ?ber Benutzername.
00082 //
00083 // Revision 1.13  2005/04/16 22:25:06  dankert
00084 // Verbindung zum LDAP-Server schlie?en
00085 //
00086 // Revision 1.12  2005/01/24 21:41:25  dankert
00087 // Abfrage auf Readonly-Mode
00088 //
00089 // Revision 1.11  2004/12/20 23:29:51  dankert
00090 // neue Methode setDatabaseRow()
00091 //
00092 // Revision 1.10  2004/12/20 23:19:41  dankert
00093 // Neue Methode getAllUsers()
00094 //
00095 // Revision 1.9  2004/12/19 19:24:27  dankert
00096 // getAvailableStyles()
00097 //
00098 // Revision 1.8  2004/11/28 22:32:33  dankert
00099 // getAllAcls(): Lesen aller Rechte des Benutzers
00100 //
00101 // Revision 1.7  2004/11/28 16:56:38  dankert
00102 // Beruecksichtigen von Berechtigungen fuer "alle"
00103 //
00104 // Revision 1.6  2004/11/15 21:35:39  dankert
00105 // Berechtigungen mit Bitmasken
00106 //
00107 // Revision 1.5  2004/11/10 22:48:25  dankert
00108 // Neue Methoden zum Einlesen der Berechtigungen
00109 //
00110 // Revision 1.4  2004/10/14 21:12:59  dankert
00111 // Methoden fuer Berechtigungen
00112 //
00113 // Revision 1.3  2004/05/07 21:29:16  dankert
00114 // Url ?ber Html::url erzeugen
00115 //
00116 // Revision 1.2  2004/05/02 14:41:31  dankert
00117 // Einf?gen package-name (@package)
00118 //
00119 // ---------------------------------------------------------------------------
00120 
00128 class User
00129 {
00130      var $userid   = 0;
00131      var $error    = '';
00132 
00133      var $name     = '';
00134      var $fullname = '';
00135      var $ldap_dn;
00136      var $tel;
00137      var $mail;
00138      var $desc;
00139      var $style;
00140      var $isAdmin;
00141      var $projects;
00142      var $rights;
00143      var $loginDate = 0;
00144      
00145      var $mustChangePassword = false;
00146      var $groups = null;
00147 
00148      // Konstruktor
00149      function User( $userid='' )
00150      {
00151           if   ( is_numeric($userid) )
00152                $this->userid = $userid;
00153      }
00154 
00155 
00156      // Lesen Benutzer aus der Datenbank
00157      function listAll()
00158      {
00159           global $conf;
00160           $db = db_connection();
00161 
00162           $sql = new Sql( 'SELECT id,name '.
00163                           '  FROM {t_user}'.
00164                           '  ORDER BY name' );
00165 
00166           return $db->getAssoc( $sql->query );
00167      }
00168 
00169 
00170      // Lesen Benutzer aus der Datenbank
00171      function getAllUsers()
00172      {
00173           $list = array();
00174           $db = db_connection();
00175 
00176           $sql = new Sql( 'SELECT * '.
00177                           '  FROM {t_user}'.
00178                           '  ORDER BY name' );
00179 
00180           foreach( $db->getAll( $sql->query ) as $row )
00181           {
00182                $user = new User();
00183                $user->setDatabaseRow( $row );
00184 
00185                $list[] = $user;
00186           }
00187           
00188           return $list;
00189      }
00190 
00191 
00195      function setCurrent()
00196      {
00197           $this->loadProjects();
00198           $this->loginDate = time();
00199 
00200           Session::setUser( $this );
00201      }
00202 
00203 
00210      function getGroupClause()
00211      {
00212           $groupIds = $this->getGroupIds();
00213           
00214           if   ( count($groupIds) > 0 )
00215                $groupclause = ' groupid='.implode(' OR groupid=',$groupIds );
00216           else
00217                $groupclause = ' 1=0 ';
00218 
00219           return $groupclause;
00220      }
00221 
00222 
00223      // Prueft, ob der Benutzer fuer ein Projekt berechtigt ist
00224      function hasProject( $projectid )
00225      {
00226           $db = db_connection();
00227 
00228           $sql = new Sql( 'SELECT COUNT(*)'.
00229                           '  FROM {t_acl}'.
00230                           '  LEFT JOIN {t_object} ON {t_object}.id={t_acl}.objectid '.
00231                           '  WHERE projectid={projectidid} AND '.
00232                           '        ( userid={userid} OR'.
00233                           '          '.$this->getGroupClause().'    )' );
00234           $sql->setInt   ( 'userid',$this->userid );
00235 
00236           return $db->getOne( $sql->query ) > 0;
00237      }
00238 
00239 
00240 
00246      function getReadableProjects()
00247      {
00248           $db = db_connection();
00249 
00250           if   ( $this->isAdmin )
00251           {
00252                // Administratoren haben Rechte auf alle Projekte.
00253                return Project::getAllProjects();
00254           }
00255           else
00256           {
00257                $groupClause = $this->getGroupClause();
00258                $sql = new Sql(<<<SQL
00259 SELECT DISTINCT {t_project}.id,{t_project}.name
00260   FROM {t_object}
00261   LEFT JOIN {t_acl}     ON {t_object}.id  = {t_acl}.objectid 
00262   LEFT JOIN {t_project} ON {t_project}.id = {t_object}.projectid 
00263  WHERE {t_object}.parentid IS NULL     AND
00264        {t_acl}.id          IS NOT NULL AND
00265        (  {t_acl}.userid={userid} OR
00266        $groupClause OR 
00267        ({t_acl}.userid IS NULL AND {t_acl}.groupid IS NULL)) 
00268  ORDER BY {t_project}.name
00269 SQL
00270 );
00271           $sql->setInt   ( 'userid',$this->userid );
00272 
00273                return $db->getAssoc( $sql->query );
00274           }
00275           
00276      }
00277 
00278 
00279 
00284      function getReadableProjectIds()
00285      {
00286           return array_keys( $this->getReadableProjects() );
00287      }
00288 
00289 
00294      function loadProjects()
00295      {
00296           $this->projects = $this->getReadableProjects();
00297      }
00298 
00299 
00300 
00304      function load()
00305      {
00306           global $conf;
00307           $db = db_connection();
00308 
00309           $sql = new Sql( 'SELECT * FROM {t_user}'.
00310                           ' WHERE id={userid}' );
00311           $sql->setInt( 'userid',$this->userid );
00312           $row = $db->getRow( $sql->query );
00313 
00314           $this->setDatabaseRow( $row );          
00315      }
00316 
00317 
00325      function loadWithName( $name )
00326      {
00327           global $conf;
00328           $db = db_connection();
00329 
00330           // Benutzer �ber Namen suchen
00331           $sql = new Sql( 'SELECT id FROM {t_user}'.
00332                           ' WHERE name={name}' );
00333           $sql->setString( 'name',$name );
00334           $userId = $db->getOne( $sql->query );
00335 
00336           // Benutzer �ber Id instanziieren
00337           $neuerUser = new User( $userId );
00338           $neuerUser->load();
00339           
00340           return $neuerUser;
00341      }
00342      
00343      
00344      
00348      function isValid()
00349      {
00350           return intval($this->userid) > 0;
00351      }
00352 
00353 
00354 
00355      // Lesen Benutzer aus der Datenbank
00356      function setDatabaseRow( $row )
00357      {
00358           global $conf;
00359           
00360           if   ( count($row) > 1 )
00361           {
00362                $this->userid   = $row['id'      ];
00363                $this->name     = $row['name'    ];
00364                $this->style    = $row['style'   ];
00365                $this->isAdmin  = ( $row['is_admin'] == '1');
00366                $this->ldap_dn  = $row['ldap_dn' ];
00367                $this->fullname = $row['fullname'];
00368                $this->tel      = $row['tel'     ];
00369                $this->mail     = $row['mail'    ];
00370                $this->desc     = $row['descr'   ];
00371                
00372                if   ( $this->fullname == '' )
00373                     $this->fullname = $this->name;
00374                     
00375                if   ( $this->style == '' )
00376                     $this->style = $conf['interface']['style']['default'];
00377           }
00378           else
00379           {
00380                $this->userid   = -99;
00381                $this->name     = lang('UNKNOWN');
00382                $this->style    = $conf['interface']['style']['default'];
00383                $this->isAdmin  = false;
00384                $this->ldap_dn  = '';
00385                $this->fullname = lang('UNKNOWN');
00386                $this->tel      = '';
00387                $this->mail     = '';
00388                $this->desc     = '';
00389           }
00390 
00391           /* vorerst unbenutzt:
00392           if   ( $row['use_ldap'] == '1' )
00393           {
00394                // Daten aus LDAP-Verzeichnisdienst lesen
00395 
00396                // Verbindung zum LDAP-Server herstellen
00397                $ldap_conn = @ldap_connect( $conf['ldap']['host'],$conf['ldap']['port'] );
00398                
00399                if   ( !$ldap_conn )
00400                {
00401                     logger( 'INFO','cannot connect to LDAP server '.$conf['ldap']['host'].' '.$conf['ldap']['port'] );
00402                     $this->error = 'cannot connect to LDAP server';
00403                     return false;
00404                }
00405                
00406                // Anonymes LDAP-Login versuchen
00407                $ldap_bind = @ldap_bind( $ldap_conn );
00408                
00409                if   ( $ldap_bind )
00410                {
00411                     // Login erfolgreich
00412                     $sr = ldap_read( $ldap_conn,$row['ldap_dn'],'(objectclass=*)' );
00413                     
00414                     $daten   = ldap_get_entries( $ldap_conn,$sr );
00415                     
00416                     $this->fullname = $daten[0]['givenName'][0].' '.$daten[0]['sn'][0];
00417                     $this->tel      = $daten[0]['telephoneNumber'][0];
00418                     $this->mail     = $daten[0]['mail'][0];
00419                     $this->desc     = $daten[0]['description'][0];
00420                }
00421                
00422           }
00423           */
00424      }
00425 
00426 
00427 
00432      function getName()
00433      {
00434           if   ( empty($this->fullname))
00435                return $this->name;
00436           else
00437                return $this->fullname;
00438      }
00439      
00440      
00441      
00448      function getUserName( $userid )
00449      {
00450           $db = db_connection();
00451 
00452           $sql = new Sql( 'SELECT name FROM {t_user}'.
00453                           ' WHERE id={userid}' );
00454           $sql->setInt( 'userid',$userid );
00455 
00456           $name = $db->getOne( $sql->query );
00457           
00458           if   ( $name == '' )
00459                return lang('UNKNOWN');
00460           else return $name;
00461      }
00462 
00463 
00467      function save()
00468      {
00469           $db = db_connection();
00470 
00471           $sql = new Sql( 'UPDATE {t_user}'.
00472                           ' SET name={name},'.
00473                           '     fullname={fullname},'.
00474                           '     ldap_dn ={ldap_dn} ,'.
00475                           '     tel     ={tel}     ,'.
00476                           '     descr   ={desc}    ,'.
00477                           '     mail    ={mail}    ,'.
00478                           '     style   ={style}   ,'.
00479                           '     is_admin={isAdmin} '.
00480                           ' WHERE id={userid}' );
00481           $sql->setInt    ( 'userid'  ,$this->userid  );
00482           $sql->setString ( 'fullname',$this->fullname);
00483           $sql->setString ( 'name'    ,$this->name    );
00484           $sql->setString ( 'ldap_dn' ,$this->ldap_dn );
00485           $sql->setString ( 'tel'     ,$this->tel     );
00486           $sql->setString ( 'desc'    ,$this->desc    );
00487           $sql->setString ( 'mail'    ,$this->mail    );
00488           $sql->setString ( 'style'   ,$this->style   );
00489           $sql->setBoolean( 'isAdmin' ,$this->isAdmin );
00490           
00491           // Datenbankabfrage ausfuehren
00492           $db->query( $sql->query );
00493      }
00494 
00495 
00501      function add( $name = '' )
00502      {
00503           if   ( $name != '' )
00504                $this->name = $name;
00505 
00506           $db = db_connection();
00507 
00508           $sql = new Sql('SELECT MAX(id) FROM {t_user}');
00509           $this->userid = intval($db->getOne($sql->query))+1;
00510 
00511           $sql = new Sql('INSERT INTO {t_user}'.
00512                          ' (id,name,password,ldap_dn,fullname,tel,mail,descr,style,is_admin)'.
00513                          " VALUES( {userid},{name},'','','','','','','default',0 )" );
00514           $sql->setInt   ('userid',$this->userid);
00515           $sql->setString('name'  ,$this->name  );
00516 
00517           // Datenbankbefehl ausfuehren
00518           $db->query( $sql->query );
00519           
00520           $this->addNewUserGroups(); // Neue Gruppen hinzuf�gen.
00521      }
00522 
00523      
00524 
00529      function addNewUserGroups()
00530      {
00531           global $conf;
00532           $groupNames = explode(',',@$conf['security']['newuser']['groups']);
00533           
00534           if   ( count($groupNames) == 0 )
00535                return; // Nichts zu tun.
00536                
00537           $db = db_connection();
00538 
00539           $sql = new Sql('SELECT id FROM {t_group} WHERE name IN({names})');
00540           $sql->setStringList('names',$groupNames);
00541           $groupIds = array_unique( $db->getCol($sql->query) );
00542           
00543           // Wir brauchen hier nicht weiter pr�fen, ob der Benutzer eine Gruppe schon hat, denn
00544           // - passiert dies nur bei der Neuanlage eines Benutzers
00545           // - Enth�lt die Group-Id-Liste eine ID nur 1x.
00546 
00547           // Gruppen diesem Benutzer zuordnen.
00548           foreach( $groupIds as $groupId )
00549                $this->addGroup( $groupId );
00550      }
00551 
00552 
00563      function delete()
00564      {
00565           $db = db_connection();
00566 
00567           // "Erzeugt von" f�r diesen Benutzer entfernen.
00568           $sql = new Sql( 'UPDATE {t_object} '.
00569                           'SET create_userid=null '.
00570                           'WHERE create_userid={userid}' );
00571           $sql->setInt   ('userid',$this->userid );
00572           $db->query( $sql->query );
00573 
00574           // "Letzte �nderung von" f�r diesen Benutzer entfernen
00575           $sql = new Sql( 'UPDATE {t_object} '.
00576                           'SET lastchange_userid=null '.
00577                           'WHERE lastchange_userid={userid}' );
00578           $sql->setInt   ('userid',$this->userid );
00579           $db->query( $sql->query );
00580 
00581           // Alle Archivdaten in Dateien mit diesem Benutzer entfernen
00582           $sql = new Sql( 'UPDATE {t_value} '.
00583                           'SET lastchange_userid=null '.
00584                           'WHERE lastchange_userid={userid}' );
00585           $sql->setInt   ('userid',$this->userid );
00586           $db->query( $sql->query );
00587 
00588           // Alle Berechtigungen dieses Benutzers l?schen
00589           $sql = new Sql( 'DELETE FROM {t_acl} '.
00590                           'WHERE userid={userid}' );
00591           $sql->setInt   ('userid',$this->userid );
00592           $db->query( $sql->query );
00593 
00594           // Alle Gruppenzugehoerigkeiten dieses Benutzers l?schen
00595           $sql = new Sql( 'DELETE FROM {t_usergroup} '.
00596                           'WHERE userid={userid}' );
00597           $sql->setInt   ('userid',$this->userid );
00598           $db->query( $sql->query );
00599 
00600           // Benutzer loeschen
00601           $sql = new Sql( 'DELETE FROM {t_user} '.
00602                           'WHERE id={userid}' );
00603           $sql->setInt   ('userid',$this->userid );
00604           $db->query( $sql->query );
00605      }
00606 
00607 
00613      function getProperties()
00614      {
00615           return Array( 'userid'  => $this->userid,
00616                         'id'      => $this->userid,
00617                         'fullname'=> $this->fullname,
00618                         'name'    => $this->name,
00619                         'ldap_dn' => $this->ldap_dn,
00620                         'tel'     => $this->tel,
00621                         'desc'    => $this->desc,
00622                         'mail'    => $this->mail,
00623                         'style'   => $this->style,
00624                         'is_admin'=> $this->isAdmin,
00625                         'isAdmin' => $this->isAdmin );
00626      }
00627 
00628 
00637      function checkPassword( $password )
00638      {
00639           global $conf;
00640 
00641           $db = db_connection();
00642           $this->mustChangePassword = false;
00643           
00644           // Lesen des Benutzers aus der DB-Tabelle
00645           $sql = new Sql( <<<SQL
00646 SELECT * FROM {t_user}
00647  WHERE name={name}
00648 SQL
00649           );
00650           $sql->setString('name',$this->name);
00651      
00652           $res_user = $db->query( $sql->query );
00653 
00654           $check = false;
00655           $authType = $conf['security']['auth']['type']; // Entweder 'ldap', 'authdb', 'http', oder 'database'
00656           
00657           if   ( $res_user->numRows() == 1 )
00658           {
00659                // Benutzername ist bereits in der Datenbank.
00660                $row_user = $res_user->fetchRow();
00661                $this->userid  = $row_user['id'];
00662                $this->ldap_dn = $row_user['ldap_dn'];
00663                $check   = true;
00664                $autoAdd = false; // Darf nicht hinzugef�gt werden, da schon vorhanden.
00665           }
00666           elseif( $res_user->numRows() == 0 && $authType == 'ldap' && $conf['ldap']['search']['add'] )
00667           {
00668                // Benutzer noch nicht in der Datenbank vorhanden.
00669                // Falls ein LDAP-Account gefunden wird, wird dieser �bernommen.
00670                $check   = true;
00671                $autoAdd = true;
00672           }
00673           elseif( $res_user->numRows() == 0 && $authType == 'authdb' && $conf['security']['authdb']['add'] )
00674           {
00675                $check = true;
00676                $autoAdd = true;
00677           }
00678           elseif( $res_user->numRows() == 0 && $authType == 'http' && $conf['security']['http']['add'] )
00679           {
00680                $check = true;
00681                $autoAdd = true;
00682           }
00683 
00684           if   ( $check )
00685           {
00686                // Falls benutzerspezifischer LDAP-dn vorhanden wird Benutzer per LDAP authentifiziert
00687                if   ( $conf['security']['auth']['userdn'] && !empty($this->ldap_dn ) )
00688                {
00689                     Logger::debug( 'checking login via ldap' );
00690                     $ldap = new Ldap();
00691                     $ldap->connect();
00692                     
00693                     // Benutzer ist bereits in Datenbank
00694                     // LDAP-Login mit dem bereits vorhandenen DN versuchen
00695                     $ok = $ldap->bind( $this->ldap_dn, $password );
00696                     
00697                     // Verbindung zum LDAP-Server brav beenden
00698                     $ldap->close();
00699 
00700                     return $ok;
00701                }
00702                elseif( $authType == 'ldap' )
00703                {
00704                     Logger::debug( 'checking login via ldap' );
00705                     $ldap = new Ldap();
00706                     $ldap->connect();
00707                     
00708                     if   ( empty($conf['ldap']['dn']) )
00709                     {
00710                          // Der Benutzername wird im LDAP-Verzeichnis gesucht.
00711                          // Falls gefunden, wird der DN (=der eindeutige Schl�ssel im Verzeichnis) ermittelt.
00712                          $dn = $ldap->searchUser( $this->name );
00713                          
00714                          if    ( empty($dn) )
00715                          {
00716                               Logger::debug( 'User not found in LDAP directory' );
00717                               return false; // Kein LDAP-Account gefunden.
00718                          }
00719 
00720                          Logger::debug( 'User found: '.$dn );
00721                     }
00722                     else
00723                     {
00724                          $dn = str_replace( '{user}',$this->name,$conf['ldap']['dn'] );
00725                     }
00726                          
00727                     // LDAP-Login versuchen
00728                     $ok = $ldap->bind( $dn, $password );
00729                     
00730                     Logger::debug( 'LDAP bind: '.($ok?'success':'failed') );
00731                     
00732                     if   ( $ok && $conf['security']['authorize']['type'] == 'ldap' )
00733                     {
00734                          $sucheAttribut = $conf['ldap']['authorize']['group_name'];
00735                          $sucheFilter   = str_replace('{dn}',$dn,$conf['ldap']['authorize']['group_filter']);
00736                          
00737                          $ldap_groups = $ldap->searchAttribute( $sucheFilter, $sucheAttribut );
00738                          
00739                          $sql = new Sql( <<<SQL
00740 SELECT id,name FROM {t_group}
00741  WHERE name IN({name_list})
00742  ORDER BY name ASC
00743 SQL
00744                          );
00745                          $sql->setStringList('name_list',$ldap_groups);
00746                          $oldGroups = $this->getGroupIds();
00747                          $this->groups = $db->getAssoc( $sql->query );
00748                          
00749                          foreach( $this->groups as $groupid=>$groupname)
00750                          {
00751                               if   ( ! in_array($groupid,$oldGroups))
00752                                    $this->addGroup($groupid);
00753                          }
00754                          foreach( $oldGroups as $groupid)
00755                          {
00756                               if   ( !isset($this->groups[$groupid]) )
00757                                    $this->delGroup($groupid);
00758                          }
00759                          
00760                          
00761                          // Pr�fen, ob Gruppen fehlen. Diese dann ggf. in der OpenRat-Datenbank hinzuf�gen.
00762                          if   ( $conf['ldap']['authorize']['auto_add'] )
00763                          {
00764                               foreach( $ldap_groups as $group )
00765                               {
00766                                    if   ( !in_array($group,$this->groups) ) // Gruppe schon da?
00767                                    {
00768                                         $g = new Group();
00769                                         $g->name = $group;
00770                                         $g->add(); // Gruppe hinzuf�gen
00771                                         
00772                                         $this->groups[$g->groupid] = $group;
00773                                    }
00774                               }
00775                          }
00776 //                       Html::debug($this->groups,'Gruppen/Ids des Benutzers');
00777                     }
00778                     
00779                     // Verbindung zum LDAP-Server brav beenden
00780                     $ldap->close();
00781 
00782                     if   ( $ok && $autoAdd )
00783                     {
00784                          // Falls die Authentifizierung geklappt hat, wird der
00785                          // LDAP-Account in die Datenbank �bernommen.
00786                          $this->ldap_dn  = $dn;
00787                          $this->fullname = $this->name;
00788                          $this->add();
00789                          $this->save();
00790                     }
00791                     
00792                     return $ok;
00793                }
00794                elseif( $authType == 'database' )
00795                {
00796                     // Pruefen ob Kennwort mit Datenbank uebereinstimmt
00797                     if   ( $row_user['password'] == $password )
00798                     {
00799                          // Kennwort stimmt mit Datenbank �berein, aber nur im Klartext.
00800                          // Das Kennwort muss ge�ndert werden
00801                          $this->mustChangePassword = true;
00802                          
00803                          // Login nicht erfolgreich
00804                          return false;
00805                     }
00806                     elseif   ( $row_user['password'] == md5( $password ) )
00807                     {
00808                          // Die Kennwort-Pr�fsumme stimmt mit dem aus der Datenbank �berein.
00809                          // Juchuu, Login ist erfolgreich.
00810                          return true;
00811                     }
00812                     else
00813                     {
00814                          // Kennwort stimmt garnicht �berein.
00815                          return false;
00816                     }
00817                }
00818                elseif( $authType == 'authdb' )
00819                {
00820                     $authdb = new DB( $conf['security']['authdb'] );
00821                     $sql = new Sql( $conf['security']['authdb']['sql'] );
00822                     $sql->setString('username',$this->name);
00823                     $sql->setString('password',$password);
00824                     $res = $authdb->query($sql->query);
00825                     $ok = ($res->numRows() >= 1);
00826 
00827                     if   ( $ok && $autoAdd )
00828                     {
00829                          // Falls die Authentifizierung geklappt hat, wird der
00830                          // Benutzername in der eigenen Datenbank eingetragen.
00831                          $this->fullname = $this->name;
00832                          $this->add();
00833                          $this->save();
00834                     }
00835                     // noch nicht implementiert: $authdb->close();
00836                     
00837                     return $ok;
00838                }
00839                elseif( $authType == 'http' )
00840                {
00841                     $http = new Http( $conf['security']['http']['url'] );
00842                     $http->method = 'HEAD';
00843                     $http->setBasicAuthentication( $this->name, $password );
00844                     
00845                     $ok = $http->request();
00846                     
00847                     return $ok; 
00848                }
00849                else
00850                {
00851                     die( 'unknown authentication-type in configuration: '.$authType );
00852                }
00853           }
00854 
00855           // Benutzername nicht in Datenbank.
00856           return false;
00857      }
00858      
00859      
00866      function setPassword( $password, $always=true )
00867      {
00868           $db = db_connection();
00869 
00870           $sql = new Sql( 'UPDATE {t_user} SET password={password}'.
00871                           'WHERE id={userid}' );
00872                           
00873           if   ( $always )
00874                $sql->setString('password',md5($password) );
00875           else
00876                $sql->setString('password',$password      );
00877                
00878           $sql->setInt   ('userid'  ,$this->userid  );
00879 
00880           $db->query( $sql->query );
00881      }
00882 
00883 
00889      function getGroups()
00890      {
00891           if   ( !is_array($this->groups) )
00892           {
00893                $db = db_connection();
00894      
00895                $sql = new Sql( 'SELECT {t_group}.id,{t_group}.name FROM {t_group} '.
00896                                'LEFT JOIN {t_usergroup} ON {t_usergroup}.groupid={t_group}.id '.
00897                                'WHERE {t_usergroup}.userid={userid}' );
00898                $sql->setInt('userid',$this->userid );
00899                $this->groups = $db->getAssoc( $sql->query );
00900           }
00901           
00902           return $this->groups;
00903      }
00904      
00905 
00906      // Gruppen ermitteln, in denen der Benutzer Mitglied ist
00907      function getGroupIds()
00908      {
00909           return array_keys( $this->getGroups() );
00910 
00911           /*
00912           $db = db_connection();
00913 
00914           $sql = new Sql( 'SELECT groupid FROM {t_usergroup} '.
00915                           'WHERE userid={userid}' );
00916           $sql->setInt('userid',$this->userid );
00917 
00918           return $db->getCol( $sql->query );
00919           */
00920      }
00921      
00922 
00923      // Gruppen ermitteln, in denen der Benutzer *nicht* Mitglied ist
00924      function getOtherGroups()
00925      {
00926           $db = db_connection();
00927 
00928           $sql = new Sql( 'SELECT {t_group}.id,{t_group}.name FROM {t_group}'.
00929                           '   LEFT JOIN {t_usergroup} ON {t_usergroup}.groupid={t_group}.id AND {t_usergroup}.userid={userid}'.
00930                           '   WHERE {t_usergroup}.userid IS NULL' );
00931           $sql->setInt('userid'  ,$this->userid );
00932 
00933           return $db->getAssoc( $sql->query );
00934      }
00935 
00936 
00937      // Benutzer einer Gruppe hinzufuegen
00938      function addGroup( $groupid )
00939      {
00940           $db = db_connection();
00941 
00942           $sql = new Sql('SELECT MAX(id) FROM {t_usergroup}');
00943           $usergroupid = intval($db->getOne($sql->query))+1;
00944 
00945           $sql = new Sql( 'INSERT INTO {t_usergroup} '.
00946                           '       (id,userid,groupid) '.
00947                           '       VALUES( {usergroupid},{userid},{groupid} )' );
00948           $sql->setInt('usergroupid',$usergroupid  );
00949           $sql->setInt('userid'     ,$this->userid );
00950           $sql->setInt('groupid'    ,$groupid      );
00951 
00952           $db->query( $sql->query );
00953      
00954      }
00955 
00956 
00957      // Benutzer aus Gruppe entfernen
00958      function delGroup( $groupid )
00959      {
00960           $db = db_connection();
00961 
00962           $sql = new Sql( 'DELETE FROM {t_usergroup} '.
00963                           '  WHERE userid={userid} AND groupid={groupid}' );
00964           $sql->setInt   ('userid'  ,$this->userid );
00965           $sql->setInt   ('groupid' ,$groupid      );
00966 
00967           $db->query( $sql->query );
00968      }
00969      
00970 
00977      function loadRights( $projectid,$languageid )
00978      {
00979      }
00980 
00981 
00988      function getAllAcls()
00989      {
00990 
00991           $this->delRights();
00992 
00993           $db = db_connection();
00994 
00995           $group_clause = $this->getGroupClause();
00996 
00997           $sql = new Sql( 'SELECT {t_acl}.*,{t_object}.projectid,{t_language}.name AS languagename FROM {t_acl}'.
00998                           '  LEFT JOIN {t_object} '.
00999                           '         ON {t_object}.id={t_acl}.objectid '.
01000                           '  LEFT JOIN {t_language} '.
01001                           '         ON {t_language}.id={t_acl}.languageid '.
01002                           '  WHERE ( {t_acl}.userid={userid} OR '.$group_clause.
01003                                                            ' OR ({t_acl}.userid IS NULL AND {t_acl}.groupid IS NULL) )'.
01004                           '  ORDER BY {t_object}.projectid,{t_acl}.languageid' );
01005           $sql->setInt  ( 'userid'    ,$this->userid );
01006 
01007           $aclList = array();
01008 
01009           foreach( $db->getAll( $sql->query ) as $row )
01010           {
01011                $acl = new Acl();
01012                $acl->setDatabaseRow( $row );
01013                $acl->projectid    = $row['projectid'   ];
01014                if   ( intval($acl->languageid) == 0 )
01015                     $acl->languagename = lang('GLOBAL_ALL_LANGUAGES');
01016                else
01017                     $acl->languagename = $row['languagename'];
01018                $aclList[] = $acl;
01019           }
01020           
01021           return $aclList;
01022      }
01023 
01024 
01029      function getRights()
01030      {
01031           die('User.class::getRights()');
01032           
01033 //        $db = db_connection();
01034 //        $var = array();
01035 //
01036 //        // Alle Projekte lesen
01037 //        $sql = new Sql( 'SELECT id,name FROM {t_project}' );
01038 //        $projects = $db->getAssoc( $sql->query );    
01039 //
01040 //        foreach( $projects as $projectid=>$projectname )
01041 //        {
01042 //             $var[$projectid] = array();
01043 //             $var[$projectid]['name'] = $projectname;
01044 //             $var[$projectid]['folders'] = array();
01045 //             $var[$projectid]['rights'] = array();
01046 //
01047 //             $sql = new Sql( 'SELECT {t_acl}.* FROM {t_acl}'.
01048 //                             '  LEFT JOIN {t_folder} ON {t_acl}.folderid = {t_folder}.id'.
01049 //                             '  WHERE {t_folder}.projectid={projectid}'.
01050 //                             '    AND {t_acl}.userid={userid}' );
01051 //             $sql->setInt('projectid',$projectid    );
01052 //             $sql->setInt('userid'   ,$this->userid );
01053 //             
01054 //             $acls = $db->getAll( $sql->query );
01055 //
01056 //             foreach( $acls as $acl )
01057 //             {
01058 //                  $aclid = $acl['id'];
01059 //                  $folder = new Folder( $acl['folderid'] );
01060 //                  $folder->load();
01061 //                  $var[$projectid]['rights'][$aclid] = $acl;
01062 //                  $var[$projectid]['rights'][$aclid]['foldername'] = implode(' &raquo; ',$folder->parentfolder( false,true ));
01063 //                  $var[$projectid]['rights'][$aclid]['delete_url'] = Html::url(array('action'=>'user','subaction'=>'delright','aclid'=>$aclid));
01064 //             }
01065 //             
01066 //             $sql = new Sql( 'SELECT id FROM {t_folder}'.
01067 //                             '  WHERE projectid={projectid}' );
01068 //             $sql->setInt('projectid',$projectid);
01069 //             $folders = $db->getCol( $sql->query );
01070 //
01071 //             $var[$projectid]['folders'] = array();
01072 //
01073 //             foreach( $folders as $folderid )
01074 //             {
01075 //                  $folder = new Folder( $folderid );
01076 //                  $folder->load();
01077 //                  $var[$projectid]['folders'][$folderid] = implode(' &raquo; ',$folder->parentfolder( false,true ));
01078 //             }
01079 //
01080 //             asort( $var[$projectid]['folders'] );
01081 //        }
01082 //        
01083 //        return $var;
01084      }
01085 
01086 
01090      function delRights()
01091      {
01092           $this->rights = array();
01093      }
01094 
01095 
01102      function hasRight( $objectid,$type )
01103      {
01104           global $conf;
01105           if   ( $this->isAdmin && !$conf['security']['readonly'] )
01106                return true;
01107 
01108           if   ( $this->isAdmin && $type & ACL_READ )
01109                return true;
01110                
01111           if   ( !isset($this->rights[$objectid]) )
01112                return false;
01113 
01114           return $this->rights[$objectid] & $type;
01115      }
01116 
01117 
01124      function addRight( $objectid,$type )
01125      {
01126           global $conf;
01127           
01128           if   ( $conf['security']['readonly'] )
01129                if   ( $type & ACL_READ )
01130                     $type = ACL_READ;
01131                else
01132                     $type = 0;
01133 
01134           if   ( $type & ACL_PUBLISH && $conf['security']['nopublish'] )
01135                $type -= ACL_PUBLISH;
01136 
01137 
01138           if   ( !isset($this->rights[$objectid]) )
01139                $this->rights[$objectid] = 0;
01140 
01141           $this->rights[$objectid] = $this->rights[$objectid] | $type;
01142      }
01143 
01144 
01148      function getAvailableStyles()
01149      {
01150           global $conf_themedir;
01151           
01152           $allstyles = array();
01153           
01154           // Theme-Verzeichnis nach "*.css"-Dateien durchsuchen.
01155           $dir = $conf_themedir.'/css';
01156           $handle = @opendir( $dir ); 
01157 
01158           if   ( !is_resource($handle) )
01159                Http::serverError('Cannot open CSS dir: '.$dir);
01160                
01161           while ($file = readdir ($handle))
01162           { 
01163                if ( eregi('\.css$',$file) )
01164                { 
01165                     $file = eregi_replace('\.css$','',$file);
01166                     $allstyles[$file] = ucwords($file);
01167                }
01168           }
01169           closedir($handle);
01170 
01171           asort($allstyles); // Alphabetisch sortieren.     
01172           return $allstyles;  
01173      }
01174      
01175      
01183      function createPassword()
01184      {
01185           global $conf;
01186           
01187           $pw = '';
01188           $c  = 'bcdfghjklmnprstvwz'; //consonants except hard to speak ones
01189           $v  = 'aeiou';              //vowels
01190           $a  = $c.$v;                //both
01191            
01192           //use two syllables...
01193           for ( $i=0; $i < intval($conf['security']['password']['min_length'])/3; $i++ )
01194           {
01195                $pw .= $c[rand(0, strlen($c)-1)];
01196                $pw .= $v[rand(0, strlen($v)-1)];
01197                $pw .= $a[rand(0, strlen($a)-1)];
01198           }
01199           //... and add a nice number
01200           $pw .= rand(10,99);
01201            
01202           return $pw;
01203      }
01204 }
01205 
01206 ?>

Erzeugt am Thu May 14 00:55:49 2009 für OpenRat von  doxygen 1.5.8