/** * Adds registered user activity. * @param string $ip * @param string $url * @return boolean */ protected static function _addUser($ip, $url) { $user = new PodiumUser(); $activity = self::findOne(['user_id' => $user->getId()]); if (!$activity) { $activity = new Activity(); $activity->user_id = $user->getId(); } $activity->username = $user->getName(); $activity->user_role = $user->getRole(); $activity->user_slug = $user->getSlug(); $activity->url = $url; $activity->ip = $ip; $activity->anonymous = $user->getAnonymous(); return $activity->save(); }
/** * Adds registered user activity. * @param string $ip * @param string $url * @return boolean */ protected static function _addUser($ip, $url) { $user = User::findMe(); if ($user) { $activity = self::find()->where(['user_id' => $user->id])->limit(1)->one(); if (!$activity) { $activity = new Activity(); $activity->user_id = $user->id; } $activity->username = $user->podiumName; $activity->user_role = $user->role; $activity->user_slug = $user->podiumSlug; $activity->url = $url; $activity->ip = $ip; $activity->anonymous = $user->anonymous; return $activity->save(); } return false; }
/** * Renders the list of users reading current section. * @return string */ public function run() { $url = Yii::$app->request->getUrl(); $out = ''; switch ($this->what) { case 'forum': $out .= Yii::t('podium/view', 'Browsing this forum') . ': '; break; case 'topic': $out .= Yii::t('podium/view', 'Reading this thread') . ': '; break; case 'unread': $out .= Yii::t('podium/view', 'Browsing unread threads') . ': '; break; case 'members': $out .= Yii::t('podium/view', 'Browsing the members') . ': '; break; } $conditions = ['and', [Activity::tableName() . '.anonymous' => 0], ['is not', 'user_id', null], new Expression('`url` LIKE :url'), ['>=', Activity::tableName() . '.updated_at', time() - 5 * 60]]; $guest = true; $anon = false; if (!Yii::$app->user->isGuest) { $guest = false; $me = User::findMe(); $conditions[] = ['not in', 'user_id', $me->id]; if ($me->anonymous == 0) { $out .= $me->podiumTag . ' '; } else { $anon = true; } } $users = Activity::find()->joinWith(['user'])->where($conditions)->params([':url' => $url . '%']); foreach ($users->each() as $user) { $out .= $user->user->podiumTag . ' '; } $conditions = ['and', ['anonymous' => 1], new Expression('`url` LIKE :url'), ['>=', 'updated_at', time() - 5 * 60]]; $anonymous = Activity::find()->where($conditions)->params([':url' => $url . '%'])->count('id'); if ($anon) { $anonymous += 1; } $conditions = ['and', ['user_id' => null], new Expression('`url` LIKE :url'), ['>=', 'updated_at', time() - 5 * 60]]; $guests = Activity::find()->where($conditions)->params([':url' => $url . '%'])->count('id'); if ($guest) { $guests += 1; } if ($anonymous) { $out .= Html::button(Yii::t('podium/view', '{n, plural, =1{# anonymous user} other{# anonymous users}}', ['n' => $anonymous]), ['class' => 'btn btn-xs btn-default disabled']) . ' '; } if ($guests) { $out .= Html::button(Yii::t('podium/view', '{n, plural, =1{# guest} other{# guests}}', ['n' => $guests]), ['class' => 'btn btn-xs btn-default disabled']); } return $out; }
/** * Saves user account details changes. * @return boolean */ public function saveChanges() { if ($this->password) { $this->setPassword($this->password); } if ($this->new_email) { $this->generateEmailToken(); } $updateActivityName = $this->isAttributeChanged('username'); if ($this->save()) { if ($updateActivityName) { Activity::updateName($this->id, $this->podiumName, $this->podiumSlug); } return true; } else { return false; } }
<?php } ?> </div> <div class="panel-footer small"> <ul class="list-inline"> <li><?php echo Yii::t('podium/view', 'Members'); ?> <span class="badge"><?php echo Activity::totalMembers(); ?> </span></li> <li><?php echo Yii::t('podium/view', 'Threads'); ?> <span class="badge"><?php echo Activity::totalThreads(); ?> </span></li> <li><?php echo Yii::t('podium/view', 'Posts'); ?> <span class="badge"><?php echo Activity::totalPosts(); ?> </span></li> </ul> </div> </div>
/** * Promoting the user of given ID. * @param integer $id * @return \yii\web\Response */ public function actionPromote($id = null) { if (User::can(Rbac::PERM_PROMOTE_USER)) { $model = User::findOne((int) $id); if (empty($model)) { $this->error(Yii::t('podium/flash', 'Sorry! We can not find User with this ID.')); } else { if ($model->role != User::ROLE_MEMBER) { $this->error(Yii::t('podium/flash', 'You can only promote Members to Moderators.')); } else { $transaction = User::getDb()->beginTransaction(); try { if ($model->promoteTo(User::ROLE_MODERATOR)) { if (Yii::$app->authManager->getRolesByUser($model->id)) { Yii::$app->authManager->revoke(Yii::$app->authManager->getRole(Rbac::ROLE_USER), $model->id); } if (Yii::$app->authManager->assign(Yii::$app->authManager->getRole(Rbac::ROLE_MODERATOR), $model->id)) { Activity::updateRole($model->id, User::ROLE_MODERATOR); $transaction->commit(); Log::info('User promoted', $model->id, __METHOD__); $this->success(Yii::t('podium/flash', 'User has been promoted.')); return $this->redirect(['admin/mods', 'id' => $model->id]); } } $this->error(Yii::t('podium/flash', 'Sorry! There was an error while promoting the user.')); } catch (Exception $e) { $transaction->rollBack(); Log::error($e->getMessage(), null, __METHOD__); $this->error(Yii::t('podium/flash', 'Sorry! There was an error while promoting the user.')); } } } } else { $this->error(Yii::t('podium/flash', 'You are not allowed to perform this action.')); } return $this->redirect(['members']); }
/** * Registers user activity after every action. * @see \bizley\podium\models\Activity * * @param \yii\base\Action $action the action just executed. * @param mixed $result the action return result. * @return mixed the processed action result. */ public function afterAction($action, $result) { $parentResult = parent::afterAction($action, $result); if (Yii::$app instanceof WebApplication && !in_array($action->id, ['import', 'run'])) { Activity::add(); } return $parentResult; }
/** * Deleting the user of given ID. * @param integer $id * @return \yii\web\Response */ public function actionDelete($id = null) { if (!User::can(Rbac::PERM_DELETE_USER)) { $this->error(Yii::t('podium/flash', 'You are not allowed to perform this action.')); } else { $model = User::find()->where(['id' => $id])->limit(1)->one(); if (empty($model)) { $this->error(Yii::t('podium/flash', 'Sorry! We can not find Member with this ID.')); } elseif ($model->id == User::loggedId()) { $this->error(Yii::t('podium/flash', 'Sorry! You can not delete your own account.')); } else { if ($model->delete()) { Cache::clearAfter('userDelete'); Activity::deleteUser($model->id); Log::info('User deleted', $model->id, __METHOD__); $this->success(Yii::t('podium/flash', 'User has been deleted.')); } else { Log::error('Error while deleting user', $model->id, __METHOD__); $this->error(Yii::t('podium/flash', 'Sorry! There was some error while deleting the user.')); } } } return $this->redirect(['admin/members']); }
/** * Sets relation with Activity. * @return \yii\db\ActiveQuery */ public function getActivity() { return $this->user->hasOne(Activity::className(), ['user_id' => $this->getId()]); }