Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
update
  • Loading branch information
peter-mw committed Oct 22, 2021
1 parent 3c1d402 commit 33a8d47
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/MicroweberPackages/App/Providers/AppServiceProvider.php
Expand Up @@ -78,7 +78,8 @@

use MicroweberPackages\Tag\TagsManagerServiceProvider;
use MicroweberPackages\Template\TemplateManagerServiceProvider;
use MicroweberPackages\Utils\Captcha\Providers\CaptchaManagerServiceProvider;
use MicroweberPackages\Utils\Captcha\Providers\CaptchaEventServiceProvider;
use MicroweberPackages\Utils\Captcha\Providers\CaptchaServiceProvider;
use MicroweberPackages\Utils\Http\Http;
use MicroweberPackages\Utils\System\ClassLoader;
use Spatie\Permission\PermissionServiceProvider;
Expand Down Expand Up @@ -257,7 +258,8 @@ public function register()
$this->app->register(FormServiceProvider::class);
$this->app->register(UserEventServiceProvider::class);
$this->app->register(CartEventServiceProvider::class);
$this->app->register(CaptchaManagerServiceProvider::class);
$this->app->register(CaptchaServiceProvider::class);
$this->app->register(CaptchaEventServiceProvider::class);
$this->app->register(OptionServiceProvider::class);
$this->app->register(BackupServiceProvider::class);
$this->app->register(CustomerServiceProvider::class);
Expand Down
13 changes: 12 additions & 1 deletion src/MicroweberPackages/Comment/tests/CommentsTest.php
Expand Up @@ -185,8 +185,19 @@ public function testPostCommentWithCaptcha()
route('api.comment.post'),
$req
);

$this->assertNotEmpty($commentData['data']);



//try to post again with the same captcha
$commentData = RequestRoute::postJson(
route('api.comment.post'),
$req
);
$this->assertEquals("captcha", $commentData['form_data_required']);
$this->assertEquals("captcha", $commentData['form_data_module']);


}


Expand Down
Expand Up @@ -53,6 +53,15 @@ public function validate($key, $captcha_id = null, $unset_if_found = true)
}
}

public function reset($captcha_id = null)
{
$old = app()->user_manager->session_set('captcha',[]);
$old = app()->user_manager->session_set('captcha_recent',[]);
if($captcha_id){
$old = app()->user_manager->session_set('captcha_' . $captcha_id,[]);
}
}

public function render($params = array())
{
sleep(1);
Expand Down
7 changes: 7 additions & 0 deletions src/MicroweberPackages/Utils/Captcha/CaptchaManager.php
Expand Up @@ -62,6 +62,13 @@ public function render($params = array())
return $this->adapter->render($params);
}

public function reset($captcha_id = null)
{
if (method_exists($this->adapter, 'reset')) {
return $this->adapter->reset($captcha_id);
}
}


public function setAdapter($adapter)
{
Expand Down
@@ -0,0 +1,23 @@
<?php

namespace MicroweberPackages\Utils\Captcha\Listeners;


use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class NewCommentListener
{

/**
* Handle the event.
*
* @param object $event
* @return void
*/
public function handle($event)
{
// reset the captcha
app()->captcha_manager->reset();
}
}
@@ -0,0 +1,28 @@
<?php
/*
* This file is part of the Microweber framework.
*
* (c) Microweber CMS LTD
*
* For full license information see
* https://github.com/microweber/microweber/blob/master/LICENSE
*
*/

namespace MicroweberPackages\Utils\Captcha\Providers;


use MicroweberPackages\App\Providers\EventServiceProvider;
use MicroweberPackages\Comment\Events\NewComment;
use MicroweberPackages\Utils\Captcha\Listeners\NewCommentListener;

class CaptchaEventServiceProvider extends EventServiceProvider
{

protected $listen = [
NewComment::class => [
NewCommentListener::class
],
];
}

Expand Up @@ -16,7 +16,7 @@
use MicroweberPackages\Utils\Captcha\Validators\CaptchaValidator;


class CaptchaManagerServiceProvider extends ServiceProvider
class CaptchaServiceProvider extends ServiceProvider
{

/**
Expand Down
Expand Up @@ -26,4 +26,9 @@ public function setAnswer($answer)
$this->answer = $answer;
}

public function reset($captcha_id = null)
{
$this->answer = null;
}

}

0 comments on commit 33a8d47

Please sign in to comment.