public function actionIndex($user = null, $id = null) { $parent = $id ? $this->findModel($id) : null; if (!Yii::$app->user->identity->isManager() && (!$parent && !$user)) { //(!$user && $parent && $parent->user_name != Yii::$app->user->identity->name)) { return $this->redirect(['index', 'user' => Yii::$app->user->identity->name]); } $query = Node::find()->orderBy(['time' => SORT_DESC, 'id' => SORT_DESC]); if ($user) { $query->andWhere(['user_name' => $user]); } elseif ($parent) { $query->andWhere('"time" < :time', [':time' => $parent->time])->andWhere(['type_id' => $parent->type_id]); } return $this->render('index', ['dataProvider' => new ActiveDataProvider(['query' => $query]), 'parent' => $parent]); }
public function canChargeBonus() { //@todo decouple return $this->ref_name && (Node::find()->where(['user_name' => $this->ref_name])->count() > 0 || Income::find()->where(['user_name' => $this->ref_name])->count() > 0); }
public function actionLogin() { $model = new Login(); if ($model->load(Yii::$app->request->post())) { $user = $model->getUser(); if ($user) { if (empty($user->hash)) { Yii::$app->session->setFlash('error', Yii::t('app', Yii::t('app', 'Your account is not activated. Check your email'))); } else { $can = $user->canLogin(); if ($can && $user->validatePassword($model->password)) { if ($user->status > 0) { if (empty($user->auth)) { $user->generateAuthKey(); $user->save(); } if (Yii::$app->user->login($user, $model->remember ? $user->duration * 60 : 0)) { $bundle = $user->getBundle(); if ($bundle && isset($bundle['node_id'])) { $node_id = (int) $bundle['node_id']; $user->setBundle(null); $user->save(); if (Node::find()->where(['id' => $node_id])->count() > 0) { Yii::$app->session->addFlash('success', Yii::t('app', 'Congratulation! You receive a gift')); return $this->redirect(['/pyramid/node/index', 'id' => $node_id]); } } return $this->redirect(['view']); // return $this->actionView($user->name); } else { Yii::$app->session->addFlash('error', Yii::t('app', 'Something wrong happened')); } } else { Yii::$app->session->setFlash('error', Yii::t('app', Yii::t('app', 'Your account is blocked'))); } } else { Journal::write('user', 'login_fail', $user->id); if ($can) { Yii::$app->session->setFlash('error', Yii::t('app', 'Invalid username or password')); } else { $record = Record::find()->where(['object_id' => $user->id, 'event' => 'login_fail'])->orderBy(['time' => SORT_DESC])->one(); Yii::$app->session->setFlash('error', Yii::t('app', 'You have exceeded the maximum number of login attempts, you will be able to enter after {time}', ['time' => $record->time])); } } } } else { Yii::$app->session->setFlash('error', Yii::t('app', 'Invalid username or password')); } } return $this->render('login', ['model' => $model]); }
public function open(Transaction $transaction) { try { $sum = (int) Node::find()->where(['user_name' => $this->user_name])->count(); $sum += (int) Income::find()->where(['user_name' => $this->user_name])->count(); if ($this->invest()) { do { $i = $this->rise(); } while ($i > 0); if (0 == $sum && $this->user->canChargeBonus()) { $referral = $this->user->referral; $referral->account += $this->type->bonus; $referral->update(true, ['account']); if (4 == $this->type->id) { $count = $referral->getSponsors()->select('user_name')->joinWith('nodes')->groupBy('user_name')->count(); if ($count > 0 && 0 == $count % 10) { $gift = new Gift(['user_name' => $referral->name]); $gift->save(); Yii::$app->session->setFlash('success', Yii::t('app', 'Your referral may receive a gift')); } } } $transaction->commit(); Yii::$app->session->setFlash('success', Yii::t('app', 'The plan is open')); return true; } else { Yii::$app->session->setFlash('error', $this->dump()); } } catch (\Exception $ex) { $transaction->rollBack(); Yii::$app->session->setFlash('error', $ex->getMessage()); } return false; }