| Current Path : /var/www/html/mtdc/backend/models/ |
| Current File : /var/www/html/mtdc/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 $staff_company;
public $staff_designation;
public $staff_department;
public $staff_no;
public $staff_photo;
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 [
[['username', 'staff_name','roleCode','email','status','gender'], 'required', 'on' => 'manageUser'],
[['last_login', 'last_login_attempts', 'login_attempts', 'status', 'is_admin','gender'], 'integer'],
[['username', 'password_hash', 'confirmPassword', 'password_reset_token', 'email'], 'string', 'max' => 255],
[['auth_key'], 'string', 'max' => 32],
[['username'], 'unique'],
[['roleCode', 'staff_company', 'staff_designation', 'staff_department', 'staff_no', 'staff_photo', 'created_by',
'created_at', 'updated_by', 'updated_at', 'email_notification'], 'safe'],
[['email'], 'email'],
[['password_reset_token'], 'unique'],
];
}
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', 'User ID'),
'auth_key' => Yii::t('app', 'Auth Key'),
'password_hash' => Yii::t('app', 'Password'),
'password_reset_token' => Yii::t('app', 'Password Reset Token'),
'email' => Yii::t('app', 'Email'),
'last_login' => Yii::t('app', 'Last Login'),
'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', 'Staff Name'),
'location' => Yii::t('app', 'Location'),
'job_title' => Yii::t('app', 'Designation'),
'confirmPassword' => Yii::t('app', 'Repeat Password'),
'staff_name' => Yii::t('app', 'User Full Name'),
'roleCode' => Yii::t('app', 'User Role'),
'staff_company' => Yii::t('app', 'Company'),
'staff_designation' => Yii::t('app', 'Designation'),
'staff_department' => Yii::t('app', 'Department'),
'staff_no' => Yii::t('app', 'Staff No'),
'staff_photo' => Yii::t('app', 'User Image'),
'email_notification' => Yii::t('app', 'Receive corporate calendar change emails'),
'gender'=>'Gender'
];
}
}