Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. <?php $form = ActiveForm::begin([
  2. 'id' => 'ajax-twofactor-verify',
  3. 'validationUrl' => ['verify_google_code'],
  4. 'enableAjaxValidation' => true,
  5. ]); ?>
  6.  
  7. <?= $form->field($twoFactorModel, 'googleCode', [
  8. 'inputOptions' => [
  9. 'class' => 'form-control',
  10. 'placeholder' => Yii::t('app', 'Google Authenticator Code'),
  11. ],
  12. ])->label(false) ?>
  13.  
  14. <div class="form-group">
  15. <?= Html::submitButton('Verify', ['class' => 'btn btn-primary', 'id' => 'twofactor-submit']) ?>
  16. </div>
  17.  
  18. <?php ActiveForm::end() ?>
  19.  
  20. public function actionVerify_google_code()
  21. {
  22. $twoFactorModel = new GoogleFactorForm(User::findOne(Yii::$app->user->getId()));
  23. if (Yii::$app->request->isAjax && $twoFactorModel->load(Yii::$app->request->post())) {
  24. Yii::$app->response->format = Response::FORMAT_JSON;
  25. return ActiveForm::validate($twoFactorModel);
  26. }
  27. return $this->redirect(['index']);
  28. }
  29.  
  30. public function actionIndex($tab = null)
  31. {
  32. // check if user enabling or disabling two factor authentication
  33. if (Yii::$app->request->isPost && Yii::$app->request->post('GoogleFactorForm') !== null) {
  34. $twoFactorModel = new GoogleFactorForm(User::findOne(Yii::$app->user->getId()));
  35. $twoFactorModel->load(Yii::$app->request->post());
  36. if ($twoFactorModel->save()) {
  37. echo 'success';
  38. }
  39. echo 'fail';
  40. exit;
  41. }
  42.  
  43. public function rules()
  44. {
  45. return [
  46. [['googleCode'], 'required'],
  47. [['googleCode'], 'string'],
  48. ['googleCode', 'validateGoogleCode'],
  49. ];
  50. }
  51.  
  52. public function validateGoogleCode($attribute)
  53. {
  54. if (!$this->hasErrors()) {
  55. $g = new GoogleAuthenticator();
  56. $google_code = Setting::get(sprintf(Setting::USER_TWOFACTORAUTH_GOOGLE_CODE, $this->user->getId()));
  57. if (!$g->checkCode($google_code, $this->googleCode)) {
  58. $this->addError($attribute, Yii::t('app', 'Invalid google secret'));
  59. }
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement