<?php


/////////////////////////////////////////////////////////////////
//                                                             //
//  EISPortal, das Eisfair-Webfrontend                         //
//  Filename: User.php                                         //
//  Created on 18.11.2006                                      //
//  Author: Yves Schumann <eisportal(at)hiluxumbau(dot)de>     //
//                                                             //
/////////////////////////////////////////////////////////////////
//  $Id$

class User {

    private $m_iID = 0;
    private $m_sLoginname;
    private $m_iUserrole;
    private $m_sFirstname;
    private $m_sLastname;
    private $m_sPassword;
    private $m_AuthObject;
    private $m_Logger;
    private $m_MDB2;
    private $m_aUserRights = array ();

    private $m_bUserdataExtracted = false;

    function __construct($authObject) {
        $this->m_AuthObject = $authObject;
        $this->m_sLoginname = $this->m_AuthObject->getUsername();
        $this->getUserdata();
        $this->getUserRights();
        $this->m_Logger = Logger::singleton();
        $this->m_MDB2 =& MDB2::singleton(DSN);
    }

    function set_attribute($attr) {
        $this->attribute = $attr;
    }

    function getUsername() {
        return $this->m_sLoginname;
    }

    function getUserdata() {
        if (!$this->m_bUserdataExtracted) {
            // alle Eintraege fuer diesen User aus Usertabelle holen
            $sql = "SELECT ID, name, fname, lname, role ";
            $sql .= "FROM eisportal_user ";
            $sql .= "WHERE name = '";
            $sql .= $this->m_sLoginname;
            $sql .= "' ";

            $row = $this->m_MDB2->getOne($sql);

            // Resultat extrahieren
            $this->m_sFirstname = $row['fname'];
            $this->m_sLastname = $row['lname'];
            $this->m_iUserrole = $row['role'];
            $this->m_iID = $row['ID'];

            $this->m_bUserdataExtracted = true;

            $this->m_Logger->logDebug("User-Daten:");
            $this->m_Logger->logDebug("ID:        " . $this->m_iID );
            $this->m_Logger->logDebug("Username:  " . $this->m_sLoginname);
            $this->m_Logger->logDebug("Firstname: " . $this->m_sFirstname);
            $this->m_Logger->logDebug("Lastname:  " . $this->m_sLastname);
            $this->m_Logger->logDebug("Role:      " . $this->m_iUserrole);
        }
    }

    /*
     * Liefert ein Array mit den Rechten eines User
     * Parameter: Array mit Array mit ID des Users als Value. Das kann direkt der 
     * Rueckgabewert von z. B. getUserData($username) sein. Es wird nur Element 0 
     * des uebergebenen Arrays ausgewertet.
     * Array
     * (
     *     [0] => Array
     *         (
     *             [ID] => 2
     *             [name] => a              // wird
     *             [fname] => Peter         // hier
     *             [lname] => Pan           // nicht
     *             [role] => 3              // ausgewertet
     *         )
     * )
     * 
     * Returns: Array mit Arrays mit Userdaten (Aufbau der Rueckgabewerte ist bei 
     * allen getUserRights-Funktionen gleich).
     * Format: Array[user_ID][modul_ID] => [modul_right]
     * Array
     * (
     *     [1] => Array
     *         (
     *             [1] => 3
     *             [2] => 3
     *             [3] => 2
     *             [4] => 1
     *         )
     * ) 
     */
    function getUserRights() {
        if (count($this->m_aUserRights) > 0) {
            return $this->m_aUserRights;
        }
        // alle Eintraege fuer diesen User aus Berechtigungstabelle holen
        $sql = "SELECT ID, user_ID, modul_ID, modul_role ";
        $sql .= "FROM eisportal_rights ";
        $sql .= "WHERE user_ID = '";
        $sql .= $this->m_iID;
        $sql .= "' ";

        $res = $this->m_MDB2->query($sql);

        // Resultat extrahieren
        while ($row = $res->getRow()) {
            $this->m_aUserRights[$row['user_ID']][$row['modul_ID']] = $row['modul_role'];
        }
        $this->m_Logger->logDebug( _("User rights") . ":");
        $this->m_Logger->logDebug($this->m_aUserRights);
    }
}
?>
