D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
var
/
www
/
html
/
epaji.com
/
public_html
/
hindiusa
/
models
/
Filename :
Users.php
back
Copy
<?php namespace app\models; use Yii; /** * This is the model class for table "users". * * @property int $id * @property string $email * @property string $password * @property string $firstname * @property string|null $lastname * @property int $role * @property int|null $school_id * @property string|null $authkey * @property string|null $secret * @property int $status * @property string $created * * @property Blogs[] $blogs * @property Schools $school * @property Schools[] $schools */ class Users extends \yii\db\ActiveRecord implements \yii\web\IdentityInterface { const ROLE_SUPER_ADMIN = 1; const ROLE_SCHOOL_MANAGER = 2; const ROLE_BLOG_EDITOR = 3; public $newpassword; /** * {@inheritdoc} */ public static function tableName() { return 'users'; } /** * {@inheritdoc} */ public function rules() { return [ [['email', 'password', 'firstname', 'role', 'status', 'created'], 'required'], [['role', 'school_id', 'status'], 'integer'], [['created'], 'safe'], [['email', 'firstname', 'lastname'], 'string', 'max' => 50], [['authKey', 'secret'], 'string', 'max' => 300], [['password'], 'string', 'max' => 300], [['newpassword'], 'string', 'min' => 6,'max' => 300], [['email'], 'unique'], [['school_id'], 'exist', 'skipOnError' => true, 'targetClass' => Schools::className(), 'targetAttribute' => ['school_id' => 'id']], ]; } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => Yii::t('app', 'ID'), 'email' => Yii::t('app', 'Email'), 'password' => Yii::t('app', 'Password'), 'newpassword' => Yii::t('app', 'Password'), 'firstname' => Yii::t('app', 'Firstname'), 'lastname' => Yii::t('app', 'Lastname'), 'role' => Yii::t('app', 'User Role'), 'school_id' => Yii::t('app', 'School'), 'authKey' => Yii::t('app', 'Auth Key'), 'secret' => Yii::t('app', 'Secret'), 'status' => Yii::t('app', 'Status'), 'created' => Yii::t('app', 'Created'), ]; } /** * Gets query for [[Blogs]]. * * @return \yii\db\ActiveQuery */ public function getBlogs() { return $this->hasMany(Blogs::className(), ['user_id' => 'id'])->inverseOf('user'); } /** * Gets query for [[School]]. * * @return \yii\db\ActiveQuery */ public function getSchool() { return $this->hasOne(Schools::className(), ['id' => 'school_id'])->inverseOf('users'); } /** * Gets query for [[Schools]]. * * @return \yii\db\ActiveQuery */ public function getSchools() { return $this->hasMany(Schools::className(), ['user_id' => 'id'])->inverseOf('user'); } public static function isUserSuperAdmin($id) { if (static::findOne(['id' => $id, 'role' => self::ROLE_SUPER_ADMIN])) { return true; } else { return false; } } public static function isUserSchoolManager($id) { if (static::findOne(['id' => $id, 'role' => self::ROLE_SCHOOL_MANAGER])) { return true; } else { return false; } } public static function isUserBlogEditor($id) { if (static::findOne(['id' => $id, 'role' => self::ROLE_BLOG_EDITOR])) { return true; } else { return false; } } /** * @inheritdoc */ public static function findIdentity($id) { return static::findOne($id); } /** * @inheritdoc */ public static function findIdentityByAccessToken($token, $type = null) { throw new yii\base\NotSupportedException(); } public static function findByEmail($email){ return self::findOne(['email'=>$email, 'status' => 1]); } /** * @inheritdoc * @return int|string current user ID */ public function getId() { return $this->id; } /** * @inheritdoc */ public function getAuthKey() { return $this->authKey; } /** * @inheritdoc */ public function validateAuthKey($authKey) { return $this->getAuthKey() === $authKey; } public function validatePassword($password){ return Yii::$app->getSecurity()->validatePassword($password, $this->password); } public function beforeSave($insert) { if (parent::beforeSave($insert)) { if ($this->isNewRecord) { $this->password = \Yii::$app->getSecurity()->generatePasswordHash($this->password); $this->authKey = \Yii::$app->security->generateRandomString(); } else if(!$this->isNewRecord){ if($this->newpassword) $this->password = \Yii::$app->getSecurity()->generatePasswordHash($this->newpassword); } return true; } } }