/** * Creates inherited user account. */ public function init() { parent::init(); if (!Yii::$app->user->isGuest) { if (PodiumModule::getInstance()->userComponent == PodiumModule::USER_INHERIT) { $user = User::findMe(); if (empty($user)) { $new = new User(); $new->setScenario('installation'); $new->inherited_id = Yii::$app->user->id; $new->status = User::STATUS_ACTIVE; $new->role = User::ROLE_MEMBER; $new->timezone = User::DEFAULT_TIMEZONE; if ($new->save()) { $this->success(Yii::t('podium/flash', 'Hey! Your new forum account has just been automatically created! Go to {link} to complement it.', ['link' => Html::a(Yii::t('podium/view', 'Profile'))])); Cache::clearAfterActivate(); Log::info('Inherited account created', $new->id, __METHOD__); } else { throw new Exception(Yii::t('podium/view', 'There was an error while creating inherited user account. Podium can not run with the current configuration. Please contact administrator about this problem.')); } } elseif ($user->status == User::STATUS_BANNED) { return $this->redirect(['default/ban']); } } else { $user = Yii::$app->user->identity; } if ($user && !empty($user->timezone)) { Yii::$app->formatter->timeZone = $user->timezone; } } }
/** * Adds Administrator account. * @return string result message. */ protected function _addAdmin() { try { $podium = PodiumModule::getInstance(); if ($podium->userComponent == PodiumModule::USER_INHERIT) { if (!empty($podium->adminId)) { $this->authManager->assign($this->authManager->getRole('podiumAdmin'), $podium->adminId); return $this->outputSuccess(Yii::t('podium/flash', Messages::ADMINISTRATOR_PRIVILEGES_SET, ['id' => $podium->adminId])); } else { return $this->outputWarning(Yii::t('podium/flash', Messages::NO_ADMINISTRATOR_PRIVILEGES_SET)); } } else { $admin = new User(); $admin->setScenario('installation'); $admin->username = self::DEFAULT_USERNAME; $admin->email = self::DEFAULT_USER_EMAIL; $admin->status = User::STATUS_ACTIVE; $admin->role = User::ROLE_ADMIN; $admin->generateAuthKey(); $admin->setPassword(self::DEFAULT_USERNAME); if ($admin->save()) { $this->authManager->assign($this->authManager->getRole('podiumAdmin'), $admin->getId()); return $this->outputSuccess(Yii::t('podium/flash', Messages::ADMINISTRATOR_ACCOUNT_CREATED) . ' ' . Html::tag('strong', Yii::t('podium/flash', 'Login') . ':') . ' ' . Html::tag('kbd', self::DEFAULT_USERNAME) . ' ' . Html::tag('strong', Yii::t('podium/flash', 'Password') . ':') . ' ' . Html::tag('kbd', self::DEFAULT_USERNAME)); } else { $this->setError(true); return $this->outputDanger(Yii::t('podium/flash', Messages::ACCOUNT_CREATING_ERROR) . ': ' . Html::tag('pre', VarDumper::dumpAsString($admin->getErrors()))); } } } catch (Exception $e) { Yii::error([$e->getName(), $e->getMessage()], __METHOD__); $this->setError(true); return $this->outputDanger(Yii::t('podium/flash', Messages::ACCOUNT_CREATING_ERROR) . ': ' . Html::tag('pre', $e->getMessage())); } }
/** * Adds proper user ID for query. * @param integer $id */ public function loggedUser($id) { if (PodiumModule::getInstance()->userComponent == PodiumModule::USER_INHERIT) { return $this->andWhere(['inherited_id' => $id]); } return $this->andWhere(['id' => $id]); }
/** * Updating the profile details. * @return string|\yii\web\Response */ public function actionDetails() { $model = User::findMe(); if (empty($model)) { return $this->redirect(['account/login']); } $model->scenario = PodiumModule::getInstance()->userComponent == PodiumModule::USER_INHERIT ? 'accountInherit' : 'account'; $model->current_password = null; $previous_new_email = $model->new_email; if ($model->load(Yii::$app->request->post())) { if ($model->validate()) { if ($model->saveChanges()) { if ($previous_new_email != $model->new_email) { $forum = Config::getInstance()->get('name'); $email = Content::fill(Content::EMAIL_NEW); if ($email !== false && Email::queue($model->new_email, str_replace('{forum}', $forum, $email->topic), str_replace('{forum}', $forum, str_replace('{link}', Html::a(Url::to(['account/new-email', 'token' => $model->email_token], true), Url::to(['account/new-email', 'token' => $model->email_token], true)), $email->content)), !empty($model->id) ? $model->id : null)) { Log::info('New email activation link queued', $model->id, __METHOD__); $this->success(Yii::t('podium/flash', 'Your account has been updated but your new e-mail address is not active yet. Click the activation link that will be sent to your new e-mail address in few minutes.')); } else { Log::error('Error while queuing new email activation link', $model->id, __METHOD__); $this->warning(Yii::t('podium/flash', 'Your account has been updated but your new e-mail address is not active yet. Unfortunately there was some error while sending you the activation link. Contact administrator about this problem.')); } } else { Log::info('Details updated', $model->id, __METHOD__); $this->success(Yii::t('podium/flash', 'Your account has been updated.')); } return $this->refresh(); } } } $model->current_password = null; return $this->render('details', ['model' => $model]); }
/** * Creates inherited user account. */ public function init() { parent::init(); if (!Yii::$app->user->isGuest) { if (PodiumModule::getInstance()->userComponent == PodiumModule::USER_INHERIT) { $user = User::findMe(); if (empty($user)) { $new = new User(); $new->setScenario('installation'); $new->inherited_id = Yii::$app->user->id; $new->status = User::STATUS_ACTIVE; $new->role = User::ROLE_MEMBER; $new->timezone = User::DEFAULT_TIMEZONE; if ($new->save()) { $this->success(Yii::t('podium/flash', Messages::ACCOUNT_INHERITED, ['link' => Html::a(Yii::t('podium/layout', 'Profile'))])); Cache::clearAfterActivate(); Log::info('Inherited account created', $new->id, __METHOD__); } else { throw new Exception(Yii::t('podium/view', Messages::ACCOUNT_INHERITED_ERROR)); } } elseif ($user->status == User::STATUS_BANNED) { return $this->redirect(['default/ban']); } } else { $user = Yii::$app->user->identity; } if ($user && !empty($user->timezone)) { Yii::$app->formatter->timeZone = $user->timezone; } } }
/** * Adds Administrator account. * @return string result message. */ protected function _addAdmin() { try { $podium = PodiumModule::getInstance(); if ($podium->userComponent == PodiumModule::USER_INHERIT) { if (!empty($podium->adminId)) { $admin = new User(); $admin->setScenario('installation'); $admin->inherited_id = $podium->adminId; $admin->username = self::DEFAULT_USERNAME; $admin->status = User::STATUS_ACTIVE; $admin->role = User::ROLE_ADMIN; $admin->timezone = User::DEFAULT_TIMEZONE; if ($admin->save()) { $this->authManager->assign($this->authManager->getRole(Rbac::ROLE_ADMIN), $podium->adminId); return $this->outputSuccess(Yii::t('podium/flash', 'Administrator privileges have been set for the user of ID {id}.', ['id' => $podium->adminId])); } else { $this->setError(true); return $this->outputDanger(Yii::t('podium/flash', 'Error during account creating') . ': ' . Html::tag('pre', VarDumper::dumpAsString($admin->getErrors()))); } } else { return $this->outputWarning(Yii::t('podium/flash', 'No administrator privileges have been set.')); } } else { $admin = new User(); $admin->setScenario('installation'); $admin->username = self::DEFAULT_USERNAME; $admin->status = User::STATUS_ACTIVE; $admin->role = User::ROLE_ADMIN; $admin->timezone = User::DEFAULT_TIMEZONE; $admin->generateAuthKey(); $admin->setPassword(self::DEFAULT_USERNAME); if ($admin->save()) { $this->authManager->assign($this->authManager->getRole(Rbac::ROLE_ADMIN), $admin->getId()); return $this->outputSuccess(Yii::t('podium/flash', 'Administrator account has been created.') . ' ' . Html::tag('strong', Yii::t('podium/flash', 'Login') . ':') . ' ' . Html::tag('kbd', self::DEFAULT_USERNAME) . ' ' . Html::tag('strong', Yii::t('podium/flash', 'Password') . ':') . ' ' . Html::tag('kbd', self::DEFAULT_USERNAME)); } else { $this->setError(true); return $this->outputDanger(Yii::t('podium/flash', 'Error during account creating') . ': ' . Html::tag('pre', VarDumper::dumpAsString($admin->getErrors()))); } } } catch (Exception $e) { Yii::error([$e->getName(), $e->getMessage()], __METHOD__); $this->setError(true); return $this->outputDanger(Yii::t('podium/flash', 'Error during account creating') . ': ' . Html::tag('pre', $e->getMessage())); } }
/** * Updating the profile details. * @return string|\yii\web\Response */ public function actionDetails() { $model = User::findMe(); if (empty($model)) { return $this->redirect(['account/login']); } if (PodiumModule::getInstance()->userComponent == PodiumModule::USER_INHERIT) { $model->setScenario('accountInherit'); } else { $model->setScenario('account'); } $model->current_password = null; $previous_new_email = $model->new_email; if ($model->load(Yii::$app->request->post())) { if ($model->validate()) { if ($model->saveChanges()) { if ($previous_new_email != $model->new_email) { $email = Content::find()->where(['name' => 'email-new'])->limit(1)->one(); if ($email) { $topic = $email->topic; $content = $email->content; } else { $topic = 'New e-mail activation link at {forum}'; $content = '<p>{forum} New E-mail Address Activation</p><p>To activate your new e-mail address open the following link in your Internet browser and follow the instructions on screen.</p><p>{link}</p><p>Thank you<br />{forum}</p>'; } $forum = Config::getInstance()->get('name'); if (Email::queue($model->new_email, str_replace('{forum}', $forum, $topic), str_replace('{forum}', $forum, str_replace('{link}', Html::a(Url::to(['account/new-email', 'token' => $model->email_token], true), Url::to(['account/new-email', 'token' => $model->email_token], true)), $content)), !empty($model->id) ? $model->id : null)) { Log::info('New email activation link queued', $model->id, __METHOD__); $this->success(Yii::t('podium/flash', 'Your account has been updated but your new e-mail address is not active yet. ' . 'Click the activation link that has been sent to your new e-mail address.')); } else { Log::error('Error while queuing new email activation link', $model->id, __METHOD__); $this->warning(Yii::t('podium/flash', 'Your account has been updated but your new e-mail address is not active yet. ' . 'Unfortunately there was some error while sending you the activation link. ' . 'Contact administrator about this problem.')); } } else { Log::info('Details updated', $model->id, __METHOD__); $this->success(Yii::t('podium/flash', 'Your account has been updated.')); } return $this->refresh(); } } else { $model->current_password = null; } } return $this->render('details', ['model' => $model]); }
/** * Implementation of \yii\web\User::can(). * @param string $permissionName the name of the permission (e.g. "edit post") that needs access check. * @param array $params name-value pairs that would be passed to the rules associated * with the roles and permissions assigned to the user. A param with name 'user' is added to * this array, which holds the value of [[id]]. * @param boolean $allowCaching whether to allow caching the result of access check. * When this parameter is true (default), if the access check of an operation was performed * before, its result will be directly returned when calling this method to check the same * operation. If this parameter is false, this method will always call * [[\yii\rbac\ManagerInterface::checkAccess()]] to obtain the up-to-date access result. Note that this * caching is effective only within the same request and only works when `$params = []`. * @return boolean whether the user can perform the operation as specified by the given permission. */ public static function can($permissionName, $params = [], $allowCaching = true) { if (PodiumModule::getInstance()->userComponent == PodiumModule::USER_INHERIT) { $user = static::findMe(); if ($allowCaching && empty($params) && isset($user->_access[$permissionName])) { return $user->_access[$permissionName]; } $access = Yii::$app->authManager->checkAccess($user->id, $permissionName, $params); if ($allowCaching && empty($params)) { $user->_access[$permissionName] = $access; } return $access; } else { return Yii::$app->user->can($permissionName, $params, $allowCaching); } }
</div> <div class="row"> <div class="col-sm-12"> <button class="btn btn-success" id="show-email"><span class="glyphicon glyphicon-envelope"></span> <?php echo Yii::t('podium/view', 'Click here to change your e-mail address'); ?> </button> <div id="details-email" class="hide"> <?php echo $form->field($model, 'new_email')->textInput(['placeholder' => Yii::t('podium/view', 'Leave empty if you don\'t want to change it'), 'data-container' => 'body', 'data-toggle' => 'popover', 'data-placement' => 'right', 'data-content' => Yii::t('podium/view', 'New e-mail has to be activated first. Activation link will be sent to the new address.'), 'data-trigger' => 'focus', 'autocomplete' => 'off', 'disabled' => true])->label(Yii::t('podium/view', 'New Podium e-mail')); ?> </div> </div> </div> <?php if (PodiumModule::getInstance()->userComponent == PodiumModule::USER_OWN) { ?> <div class="row"> <div class="col-sm-12"> <button class="btn btn-success" id="show-password"><span class="glyphicon glyphicon-lock"></span> <?php echo Yii::t('podium/view', 'Click here to change your password'); ?> </button> </div> </div> <div id="details-password" class="hide"> <div class="row"> <div class="col-sm-12"> <?php echo $form->field($model, 'password')->passwordInput(['placeholder' => Yii::t('podium/view', 'Leave empty if you don\'t want to change it'), 'data-container' => 'body', 'data-toggle' => 'popover', 'data-placement' => 'right', 'data-content' => Yii::t('podium/view', 'Password must contain uppercase and lowercase letter, digit, and be at least 6 characters long.'), 'data-trigger' => 'focus', 'autocomplete' => 'off', 'disabled' => true])->label(Yii::t('podium/view', 'New password')); ?>
/** * Sending the account password reset link. * @return string|\yii\web\Response */ public function actionReset() { if (PodiumModule::getInstance()->userComponent == PodiumModule::USER_INHERIT) { $this->info(Yii::t('podium/flash', 'Please contact the administrator to reset your account password.')); return $this->module->goPodium(); } $model = new ReForm(); if ($model->load(Yii::$app->request->post())) { if ($model->reset()) { $email = Content::find()->where(['name' => 'email-pass'])->limit(1)->one(); if ($email) { $topic = $email->topic; $content = $email->content; } else { $topic = Messages::EMAIL_PASS_TITLE; $content = Messages::EMAIL_PASS_BODY; } $forum = Config::getInstance()->get('name'); if (!empty($model->email)) { if (Email::queue($model->user->email, str_replace('{forum}', $forum, $topic), str_replace('{forum}', $forum, str_replace('{link}', Html::a(Url::to(['account/password', 'token' => $model->user->password_reset_token], true), Url::to(['account/password', 'token' => $model->user->password_reset_token], true)), $content)), !empty($model->user->id) ? $model->user->id : null)) { Log::info('Password reset link queued', $model->user->id, __METHOD__); $this->success(Yii::t('podium/flash', 'The password reset link has been sent to your e-mail address.')); } else { Log::error('Error while queuing password reset link', $model->user->id, __METHOD__); $this->error(Yii::t('podium/flash', 'Sorry! There was some error while sending you the password reset link. Contact administrator about this problem.')); } } else { Log::error('Error while queuing password reset link - no email set', $model->user->id, __METHOD__); $this->error(Yii::t('podium/flash', 'Sorry! There is no e-mail address saved with your account. Contact administrator about resetting password.')); } return $this->module->goPodium(); } else { $this->error(Yii::t('podium/flash', 'Sorry! We can not find the account with that user name or e-mail address.')); } } return $this->render('reset', ['model' => $model]); }
/** * Podium Module * Yii 2 Forum Module * @author Paweł Bizley Brzozowski <*****@*****.**> * @since 0.1 */ use bizley\podium\components\Config; use bizley\podium\models\User; use bizley\podium\Module as PodiumModule; use bizley\podium\rbac\Rbac; use yii\bootstrap\Nav; use yii\bootstrap\NavBar; use yii\helpers\Html; $items = [['label' => Yii::t('podium/layout', 'Home'), 'url' => ['default/index']]]; $podiumModule = PodiumModule::getInstance(); if (Yii::$app->user->isGuest) { if (Config::getInstance()->get('members_visible')) { $items[] = ['label' => Yii::t('podium/layout', 'Members'), 'url' => ['members/index'], 'active' => $this->context->id == 'members']; } if ($podiumModule->userComponent == PodiumModule::USER_OWN) { if (!empty($podiumModule->loginUrl)) { $items[] = ['label' => Yii::t('podium/layout', 'Sign in'), 'url' => $podiumModule->loginUrl]; } if (!empty($podiumModule->registerUrl)) { $items[] = ['label' => Yii::t('podium/layout', 'Register'), 'url' => $podiumModule->registerUrl]; } } } else { $podiumUser = User::findMe(); $messageCount = $podiumUser->newMessagesCount;
/** * Sending the account password reset link. * @return string|\yii\web\Response */ public function actionReset() { if (PodiumModule::getInstance()->userComponent == PodiumModule::USER_INHERIT) { $this->info(Yii::t('podium/flash', 'Please contact the administrator to reset your account password.')); return $this->module->goPodium(); } $model = new ReForm(); if ($model->load(Yii::$app->request->post())) { if ($model->reset()) { $email = Content::find()->where(['name' => 'email-pass'])->limit(1)->one(); if ($email) { $topic = $email->topic; $content = $email->content; } else { $topic = '{forum} password reset link'; $content = '<p>{forum} Password Reset</p><p>You are receiving this e-mail because someone has started the process of changing the account password at {forum}.<br>If this person is you open the following link in your Internet browser and follow the instructions on screen.</p><p>{link}</p><p>If it was not you just ignore this e-mail.</p><p>Thank you!<br>{forum}</p>'; } $forum = Config::getInstance()->get('name'); if (!empty($model->email)) { if (Email::queue($model->user->email, str_replace('{forum}', $forum, $topic), str_replace('{forum}', $forum, str_replace('{link}', Html::a(Url::to(['account/password', 'token' => $model->user->password_reset_token], true), Url::to(['account/password', 'token' => $model->user->password_reset_token], true)), $content)), !empty($model->user->id) ? $model->user->id : null)) { Log::info('Password reset link queued', $model->user->id, __METHOD__); $this->success(Yii::t('podium/flash', 'The password reset link has been sent to your e-mail address.')); } else { Log::error('Error while queuing password reset link', $model->user->id, __METHOD__); $this->error(Yii::t('podium/flash', 'Sorry! There was some error while sending you the password reset link. Contact administrator about this problem.')); } } else { Log::error('Error while queuing password reset link - no email set', $model->user->id, __METHOD__); $this->error(Yii::t('podium/flash', 'Sorry! There is no e-mail address saved with your account. Contact administrator about resetting password.')); } return $this->module->goPodium(); } else { $this->error(Yii::t('podium/flash', 'Sorry! We can not find the account with that user name or e-mail address.')); } } return $this->render('reset', ['model' => $model]); }
?> </strong> </div> </div> </div> <?php } ?> </div> </div> <div class="col-sm-3"> <?php if (!empty($model->meta->gravatar)) { ?> <?php echo Gravatar::widget(['email' => PodiumModule::getInstance()->userComponent == PodiumModule::USER_OWN ? $model->email : $model->getEmail(), 'defaultImage' => 'identicon', 'rating' => 'r', 'options' => ['alt' => Yii::t('podium/view', 'Your Gravatar image'), 'class' => 'img-circle img-responsive']]); } elseif (!empty($model->meta->avatar)) { ?> <img class="img-circle img-responsive" src="/avatars/<?php echo $model->meta->avatar; ?> " alt="<?php echo Yii::t('podium/view', 'Your avatar'); ?> "> <?php } else { ?> <img class="img-circle img-responsive" src="<?php echo Helper::defaultAvatar(); ?>
/** * Creates inherited user account. * @throws Exception */ public function init() { parent::init(); if (!Yii::$app->user->isGuest) { if (PodiumModule::getInstance()->userComponent == PodiumModule::USER_INHERIT) { $user = User::findMe(); if (empty($user)) { if (User::createInheritedAccount()) { $this->success(Yii::t('podium/flash', 'Hey! Your new forum account has just been automatically created! Go to {link} to complement it.', ['link' => Html::a(Yii::t('podium/view', 'Profile'))])); } else { throw new Exception(Yii::t('podium/view', 'There was an error while creating inherited user account. Podium can not run with the current configuration. Please contact administrator about this problem.')); } } } else { $user = Yii::$app->user->identity; } if ($user->status == User::STATUS_BANNED) { return $this->redirect(['default/ban']); } if ($user && !empty($user->timezone)) { Yii::$app->formatter->timeZone = $user->timezone; } } }