Your IP : 216.73.216.124


Current Path : /var/www/html/dvs/backend/models/
Upload File :
Current File : /var/www/html/dvs/backend/models/User.php

<?php

namespace backend\models;

use Yii;
use backend\models\Staff;
use backend\models\StaffDetail;
use backend\models\UserDetail;
use common\models\Dermaga;
use yii\base\Model;
use common\components\JWT;
use asinfotrack\yii2\audittrail\behaviors\AuditTrailBehavior;


/**
 * This is the model class for table "user".
 *
 * @property integer $id
 * @property string $username
 * @property string $auth_key
 * @property string $password_hash
 * @property string $password_reset_token
 * @property string $email
 * @property string $last_login
 * @property string $last_login_attempts
 * @property string $login_attempts
 * @property integer $status
 * @property integer $is_admin
 * @property integer $created_at
 * @property integer $updated_at
 */
class User extends \yii\db\ActiveRecord implements \yii\web\IdentityInterface
{
    /**
     * @inheritdoc
     */
    public static function tableName() {
        return 'dmg_user';
    }

    public $staff_name;
    public $company_email;
    public $confirmPassword;
    public $roleCode;
    public $sender;

    public function behaviors()
    {
        return [
            'audittrail'=>[
                'class'=>AuditTrailBehavior::className(),
                
                // some of the optional configurations
                'ignoredAttributes'=>['created_at','updated_at'],
                'consoleUserId'=>1, 
                'attributeOutput'=>[
                    'desktop_id'=>function ($value) {
                        $model = Desktop::findOne($value);
                        return sprintf('%s %s', $model->manufacturer, $model->device_name);
                    },
                    'last_checked'=>'datetime',
                ],
            ],
        ];
    }

    /**
     * @inheritdoc
     */

    public function rules()
    {
        return [
            [['email','username','staff_name','staff_division'], 'required', 'on' => 'manageUser'],              
            [['last_login', 'last_login_attempts', 'login_attempts', 'status', 'is_admin', 'created_at', 'updated_at','staff_division'], 'integer'],
            [['username', 'password_hash', 'confirmPassword', 'password_reset_token', 'email'], 'string', 'max' => 255],
            [['auth_key'], 'string', 'max' => 32],
            [['username'], 'unique'],
            [['email'], 'email'],
            [['password_reset_token'], 'unique'],
//            [['confirmPassword'], 'compare', 'compareAttribute' => 'password_hash'],
        ]; 
    }

     public static function findIdentity($id)
    {
        return isset(self::$users[$id]) ? new static(self::$users[$id]) : null;
    }

    /**
     * @inheritdoc
     */
    public static function findIdentityByAccessToken($token, $type = null)
    {
        foreach (self::$users as $user) {
            if ($user['accessToken'] === $token) {
                return new static($user);
            }
        }

        return null;
    }

     public static function findByUsername($username)
    {
        foreach (self::$users as $user) {
            if (strcasecmp($user['username'], $username) === 0) {
                return new static($user);
            }
        }

        return null;
    }

    /**
     * @inheritdoc
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @inheritdoc
     */
    public function getAuthKey()
    {
        return $this->authKey;
    }

    /**
     * @inheritdoc
     */
    public function validateAuthKey($auth_key)
    {
        return $this->auth_key === $auth_key;
    }

    /**
     * Validates password
     *
     * @param string $password password to validate
     * @return bool if password provided is valid for current user
     */
    public function validatePassword($password_hash)
    {
        return $this->password_hash === $password_hash;
    }
    
    /*
     * This method is required for SSO Client
     * 
     */

    public function getUserbyToken($token)
    {
        $claims = JWT::getVerifiedClaims($token);
        if ($claims){
            return new static($claims);
        }
    }

    public function getStaff()
    {
        return $this->hasOne(Staff::className(), ['userid' => 'id']);
    }
    
    public function getUserName() {
        return $this->username;
    }
    
    public function getSender()
    {
        return $this->hasOne(Message::className(), ['userid' => 'sender']);
    }

    
    
    public function attributeLabels() 
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'username' => Yii::t('app', 'Nama Pengguna'),
            'staff_division' => Yii::t('app', 'Bahagian Pegawai Bertugas'),
            'auth_key' => Yii::t('app', 'Auth Key'),
            'password_hash' => Yii::t('app', 'Kata Laluan'),
            'password_reset_token' => Yii::t('app', 'Password Reset Token'),
            'email' => Yii::t('app', 'Emel'),
            'last_login' => Yii::t('app', 'Log Masuk Kali Terakhir'),
            'last_login_attempts' => Yii::t('app', 'Last Login Attempts'),
            'login_attempts' => Yii::t('app', 'Login Attempts'),
            'status' => Yii::t('app', 'Status'),
            'is_admin' => Yii::t('app', 'Is Admin'),
            'created_at' => Yii::t('app', 'Created At'),
            'updated_at' => Yii::t('app', 'Updated At'),
            'name' => Yii::t('app', 'Nama Pekerja'),
            'location' => Yii::t('app', 'Lokasi'),
            'job_title' => Yii::t('app', 'Jawatan'),
            'confirmPassword' => Yii::t('app', 'Ulang Kata Laluan'),
        ];
    }
}