'25', 'default_sort' => '0', 'order' => 'asc' ]; // not to be altered if user_settings // datatable is not altered at the same time! /** * @inheritdoc */ public static function tableName() { return '{{%user}}'; } /** * @inheritdoc */ public function behaviors() { return [ TimestampBehavior::className () ]; } /** * @inheritdoc */ public function rules() { return [ [ 'status', 'default', 'value' => self::STATUS_INACTIVE ], [ 'status', 'in', 'range' => [ self::STATUS_ACTIVE, self::STATUS_INACTIVE, self::STATUS_REJECTED ] ] ]; } /** * @inheritdoc */ public static function findIdentity($id) { return static::findOne ( [ 'id' => $id, 'status' => self::STATUS_ACTIVE ] ); } /** * @inheritdoc */ public static function findIdentityByAccessToken($token, $type = null) { throw new NotSupportedException ( '"findIdentityByAccessToken" is not implemented.' ); } /** * Finds user by username * * @param string $username * @return static|null */ public static function findByUsername($username) { { return static::findOne ( [ 'username' => $username ] ); // 'status' => self::STATUS_ACTIVE } } /** * Finds user by password reset token * * @param string $token * password reset token * @return static|null */ public static function findByPasswordResetToken($token) { if (! static::isPasswordResetTokenValid ( $token )) { return null; } return static::findOne ( [ 'password_reset_token' => $token, 'status' => self::STATUS_ACTIVE ] ); } public static function findByActivationKey($token) { if (empty ( $token )) { return false; } if (static::findOne ( [ 'activation_key' => $token, 'status' => self::STATUS_ACTIVE ] ) || static::findOne ( [ 'activation_key' => $token, 'status' => self::STATUS_REJECTED ] )) { return 'User active or rejected'; } return static::findOne ( [ 'activation_key' => $token ] ); // 'status' => self::STATUS_INACTIVE } /** * Finds out if password reset token is valid * * @param string $token * password reset token * @return boolean */ public static function isPasswordResetTokenValid($token) { if (empty ( $token )) { return false; } $expire = Yii::$app->params ['user.passwordResetTokenExpire']; $parts = explode ( '_', $token ); $timestamp = ( int ) end ( $parts ); return $timestamp + $expire >= time (); } /** * @inheritdoc */ public function getId() { return $this->getPrimaryKey (); } /** * @inheritdoc */ public function getAuthKey() { return $this->auth_key; } /** * @inheritdoc */ public function validateAuthKey($authKey) { return $this->getAuthKey () === $authKey; } /** * Validates password * * @param string $password * password to validate * @return boolean if password provided is valid for current user */ public function validatePassword($password) { return Yii::$app->security->validatePassword ( $password, $this->password_hash ); } /** * Generates password hash from password and sets it to the model * * @param string $password */ public function setPassword($password) { $this->password_hash = Yii::$app->security->generatePasswordHash ( $password ); } /** * Generates "remember me" authentication key */ public function generateAuthKey() { $this->auth_key = Yii::$app->security->generateRandomString (); } /** * Generates new password reset token */ public function generatePasswordResetToken() { $this->password_reset_token = Yii::$app->security->generateRandomString () . '_' . time (); } /** * Removes password reset token */ public function removePasswordResetToken() { $this->password_reset_token = null; } /** * Function to validate a user, by settings his status to "active". */ public function validateUser() { $this->status = self::STATUS_ACTIVE; $this->save (); } /** * Sets the status of a user to "rejected". */ public function rejectUser() { $this->status = self::STATUS_REJECTED; $this->save (); } public function generateActivationKey() { $this->activation_key = Yii::$app->security->generateRandomString () . '_' . time (); } public function addComment($com) { $this->comment = $com; $this->save (); } /** * This function gets form data as an Array and saves all fields that are filled * in to the user model = the database entry. * * @param array $formData: * an active from return array */ public function saveNewUserData($formData) { if (is_array ( $formData ) || is_object ( $formData )) { foreach ( $formData as $key => $arEl ) { try { if (! empty ( $arEl )) $this->$key = $arEl; } catch ( UnknownPropertyException $e ) { continue; } } } $this->save (); } /** * A general mail function, which serves to send a mail, to the user. * * @param String $mail: * The name of the email-model used * @param String $subject: * The mails subject */ public function sendMail($mail, $subject) { return \Yii::$app->mailer->compose ( [ 'html' => $mail . '-html', 'text' => $mail . '-text' ], [ 'user' => $this ] )->setFrom (Yii::$app->params ['noreplyMail'] )->setTo ( $this->email )->setSubject ( $subject )->send (); } /* * These methods serve as database interface for the settings and the safe-search-functionality. * They are not performing actions on the 'user'-table, which this active record instance is linked to. * Though they belong to the general user-functionalities, so, for instance the are located here. */ /** * * This method returns an array of user settings, which are stored in the database table called * 'user_settings'. * If the user never changed his personal settings no data will be stored in the * database, therfore a default-settings array is available and will be returned in this case. * The default array will also be returned for a user who is not logged-in. * * @return an array of user settings */ public function getUserSettings() { $connection = new \yii\db\Connection ( Yii::$app->db ); $connection->open (); $query = (new Query ())->select ( '*' )->from ( 'user_settings' )->where ( [ 'user_id' => $this->id ] )->all (); if (isset ( $query ) && isset ( $query [0] )) { return $query [0]; } return User::$_DEFAULT_SETTINGS; } /* * retrieve the email of all users have admin rights * */ public static function getAdmins() { $connection = new \yii\db\Connection ( Yii::$app->db ); $connection->open (); $query = (new Query ())->select ( 'user.email' )->from ( 'user' )->join ( 'join', 'auth_assignments', 'user_id=id' )->where ( [ 'item_name' => 'admin' ] )->all (); $mails = [ ]; if (isset ( $query )) foreach ( $query as $q ) { foreach ( $q as $qq ) $mails [] = $qq; } return $mails; } /** * * This method writes the user settings into the database table "user_settings". * * @param array $account_form: * form data in an array */ public function saveUserSettings($account_form) { $connection = new \yii\db\Connection ( Yii::$app->db ); $connection->open (); $post = $connection->createCommand ( 'SELECT * FROM user_settings WHERE user_id=' . $this->id )->queryOne (); if (empty ( $post )) { $connection->createCommand ()->insert ( 'user_settings', array_merge ( [ 'user_id' => $this->id ], $account_form ) )->execute (); } else { $connection->createCommand ()->update ( 'user_settings', array_merge ( [ 'user_id' => $this->id ], $account_form ) )->execute (); } $connection->close (); } /** * * * This method saves the user search parameter into the corresponding database table "user_searches" * * @param String $url: * The search url shown in the browser * @param int $number_records: * The number of records * @param bool $subscribe: * A boolean wether it was a subscription (1) or whether the search was just * saved (0) */ public function saveUserSearches($url, $number_records, $subscribe) { $connection = new \yii\db\Connection ( Yii::$app->db ); $connection->open (); $query = (new Query ())->select ( '*' )->from ( 'user_searches' )->where ( [ 'user_id' => $this->id, 'search_url' => $url ] )->all (); if (empty ( $query )) { $connection->createCommand ()->insert ( 'user_searches', [ 'user_id' => $this->id, 'search_url' => $url, 'nb_records_0' => $number_records, 'subscription' => $subscribe ] )->execute (); } else { $connection->createCommand ()->update ( 'user_searches', [ 'nb_records_0' => $number_records, 'subscription' => $subscribe ], [ 'user_id' => $this->id, 'search_url' => $url ] )->execute (); } $connection->close (); } /** * * Deletes a search given its search id. * * @param unknown $search_id */ public function deleteUserSearch($search_id) { $connection = new \yii\db\Connection ( Yii::$app->db ); $connection->open (); $connection->createCommand ()->delete ( 'user_searches', [ 'search_id' => $search_id ] )->execute (); $connection->close (); } /** * * * functions which allows subscription (1) and unsubscription (0) * * @param unknown $search_id: * The database id of the corresponding search * @param unknown $bool: * True if it is a subscription, False if it is an unsubscription */ public function subscribeUserSearch($search_id, $bool) { $connection = new \yii\db\Connection ( Yii::$app->db ); $connection->open (); $connection->createCommand ()->update ( 'user_searches', [ 'subscription' => $bool ], [ 'search_id' => $search_id ] )->execute (); $connection->close (); } }