<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity {

    private $isSSO;
    private $_id;

    /**
     * Authenticates a user.
     * @return boolean whether authentication succeeds.
     */
    public function __construct($username, $password, $isSSO = false) {
        $this->username = $username;
        $this->password = $password;
        $this->isSSO = $isSSO;
    }

    public function authenticate() {
        $username = strtolower($this->username);

        //$password = crypt($this->password, $this->password);
        //START Code modified on 08-04-2016 for secured password
        $salt = '$2a$10$1qAz2wSx3eDc4rFv5tGb5t';
        if ($this->isSSO) {
            $password = $this->password;
        } else {
            $password = crypt($this->password, $salt);
        }

        //echo $password;exit;
        //END Code modified on 08-04-2016 for secured password
        //$user = User::model()->find('LOWER(username)=? AND isActive=? ', array($username, 'Y'));
        //$user = User::model()->find('LOWER(username)=? AND isActive=? AND company_id=?', array($username, 'Y',$company_id));
        //echo $condition = 'level IN(0,1,2,4) AND LOWER(username)="' . $username . '" AND isActive="Y"';//Level-2 ESS,4-Admin for Permission Creation
        //$user = User::model()->find(array('condition' => $condition));
        
        $user = User::model()->find('LOWER(username)=? AND isActive=? ', array($username, 'Y'));
        
        //echo '<pre>';
        //print_r($user);
        //exit;
        if ($user == null)
            $this->errorCode = self::ERROR_USERNAME_INVALID;
        else if ($user->password != $password)
            $this->errorCode = self::ERROR_PASSWORD_INVALID;
        else {
            $this->_id = $user->id;
            $this->setState('fullname', $user->fullname);
            $this->setState('master_user_company_id', $user->company_id);

            $this->setState('user_branch', "");
            $this->setState('user_email_id', $user->email);

            $authorised_user = 0;
            if ($user->level == 0)
                $authorised_user = 1;

            $this->setState('authorised_user', $authorised_user);
            $arr_company = MasCompany::getMasCompanyDetails($user->company_id);
            $master_company_name = $arr_company->company_name;
//            $company_db_name = 'blskymine_nia_migration_main';
            $company_db_name = $arr_company->company_db_name;
            $company_head_user = $arr_company->crm_user_id;
            $company_logo = $arr_company->company_logo;
            $this->setState('master_company_name', $master_company_name);
            $this->setState('main_db_name', 'blskymine_nia_hr_main');
            $this->setState('company_db_name', trim($company_db_name));
            $this->setState('company_head_user', trim($company_head_user));
            $this->setState('company_logo', $company_logo);
            $this->setState('pdf_company_logo', "pdf_" . $company_logo);
            $this->setState('level', $user->level);

            $this->setState('company_general_email', $arr_company->genral_email);
            $this->setState('company_general_web', $arr_company->genral_web);

            $this->setState('company_general_phone_no', $arr_company->genral_phno);

            if ($company_db_name != "") {
                $this->setState('database', trim($company_db_name));
                Yii::app()->db->setActive(false);
                Yii::app()->db->connectionString = 'mysql:host=localhost;dbname=' . $company_db_name;
                Yii::app()->db->tablePrefix = 'crm_';
                Yii::app()->db->setActive(true);
                $this->username = $user->username;
                $this->errorCode = self::ERROR_NONE;
            } else {
                $this->errorCode = 'You are not authorized person';
            }
        }
        return $this->errorCode == self::ERROR_NONE;
    }

    public function getId() {
        return $this->_id;
    }

}
