My users are required to confirm their email addresses before they have access to the application.
I have a specific route that they are sent to if they log in and their email is not confirmed: "customer/register-landing" this will send an email and the view will explain what they need to do.
I am using bootstrap for brevity.
This is what I have so far and the last bit I am struggling to work out (the re-direct part)
//I run console related queries and this breaks if run
if ( $e->getRequest() instanceof \ZF\ContentNegotiation\Request )
{
//Get a user object
$authService = $sm->get( AuthorizationService::class );
$userObject = $authService->getIdentity();
if (!$userObject instanceof User ) {
return;
}
if ($userObject->getIsEmailConfirmed() == 1) {
return;
}
//So we have a logged in user who needs to confirm their email
$redirect = $em->attach(MvcEvent::EVENT_DISPATCH,
function($e){
$route = $e->getRouteMatch();
if ($route->getMatchedRouteName() != 'customer/register-landing')
{
//Redirect to the route: customer/register-landing
}
}
);
}
What do I need to do to re-direct to the actual page? I had a look around and I found this code:
$em->getSharedManager()->attach('Zend\Mvc\Controller\AbstractActionController', 'dispatch', function($e) {
$controller = $e->getTarget();
$controller->plugin('redirect')->toRoute('customer/register-landing');
}, 100);
However when I add it to the class it does not work:
$redirect = $em->attach(MvcEvent::EVENT_DISPATCH,
function($e){
$route = $e->getRouteMatch();
if ($route->getMatchedRouteName() != 'customer/register-landing')
{
$em->getSharedManager()->attach('Zend\Mvc\Controller\AbstractActionController', 'dispatch', function($e) {
$controller = $e->getTarget();
$controller->plugin('redirect')->toRoute('customer/register-landing');
}, 100);
}
}
);
What is the correct way to do this?
i solved this with next code. if you need all code go to https://github.com/Gimalca/piderapido/blob/master/module/Admin/Module.php
class Module {
public function onBootstrap(MvcEvent $e) {
}
public function init(ModuleManager $moduleManager) {
$moduleName = $moduleManager->getEvent()->getModuleName();
if ($moduleName == 'Admin') {
$events = $moduleManager->getEventManager();
$sharedEvents = $events->getSharedManager();
// This define modules need Login
$sharedEvents->attach(array(__NAMESPACE__, 'Admin', 'Account'), 'dispatch', array($this, 'initAuth'), 100);
}
}
public function initAuth(MvcEvent $e) {
//This get router strings
$routerMatch = $e->getRouteMatch();
$module = $routerMatch->getMatchedRouteName();
$controller = $routerMatch->getParam('controller');
$action = $routerMatch->getParam('action');
//This get Authenticate Class
$app = $e->getApplication();
$sm = $app->getServiceManager();
$auth = $sm->get('Admin\Model\LoginAdmin');
// This redirect all. but is login interface not
if ($controller != 'Admin\Controller\Login' && !$auth->isLoggedIn()) {
$controller = $e->getTarget();
return $controller->redirect()->toRoute('admin',array('controller'=>'login','action' => 'index'));
}
if ($auth->isLoggedIn()) {
$viewModel = $e->getViewModel();
$viewModel->userIdentity = $auth->getIdentity();
}
}
Related
I am currently in the development of a module prestashop and I meet a problem. I created a new hook for my module and I add an admin controller in my module that execute this hook I add the function that calls the hook and that uses a template. Except that the template does not appear I have an empty page. Here is the code I did:
My module:
<?php
if (!defined('_PS_VERSION_'))
exit;
/* Checking compatibility with older PrestaShop and fixing it */
if (!defined('_MYSQL_ENGINE_'))
define('_MYSQL_ENGINE_', 'MyISAM');
require_once(_PS_MODULE_DIR_.'blockobjectif/classes/Objectif.php');
class blockobjectif extends Module
{
public function __construct()
{
$this->name = 'blockobjectif';
$this->tab = 'front_office_features';
$this->version = '1.0';
$this->author = 'Athor Athor';
$this->bootstrap = true;
$this->need_instance = 0;
$this->ps_versions_compliancy['min'] = '1.5';
$this->ps_versions_compliancy['max'] = '1.6';
parent::__construct();
$this->displayName = $this->l('Objectifs');
$this->description = $this->l('Définie des objectifs aux clients');
}
public function install()
{
$sql = array();
include(dirname(__FILE__).'/sql/install.php');
foreach ($sql as $s)
if (!Db::getInstance()->execute($s))
return false;
$class = 'AdminObjectif';
$tab = new Tab();
$tab->class_name = $class;
$tab->module = $this->name;
$tab->id_parent = (int) Tab::getIdFromClassName('AdminParentCustomer');
$langs = Language::getLanguages(false);
foreach ($langs as $l) {
$tab->name[$l['id_lang']] = $this->l('Objectifs');
}
$tab->save();
return parent::install()
&& $this->registerHook('displayCustomerAccount')
&& $this->registerHook('displayAdminObjectifs');
}
public function uninstall($delete_params = true)
{
$sql = array();
include(dirname(__FILE__).'/sql/uninstall.php');
foreach ($sql as $s)
if (!Db::getInstance()->execute($s))
return false;
$moduleTabs = Tab::getCollectionFromModule($this->name);
if (!empty($moduleTabs)) {
foreach ($moduleTabs as $moduleTab) {
$moduleTab->delete();
}
}
if (!parent::uninstall())
return false;
return true;
}
public function hookDisplayCustomerAccount($params)
{
ddd($params);
}
public function hookDisplayAdminObjectifs($params)
{
return $this->display(__FILE__, 'admin-obj.tpl');
}
}
My AdminObjectifController:
<?php
class AdminObjectifController extends ModuleAdminController
{
public function __construct()
{
$this->bootstrap = true;
$this->table = 'objectifs';
$this->className = 'Objectif';
parent::__construct();
Hook::exec('displayAdminProductsExtra');
}
}
Result:
I do not see where the problem comes from ...
Thank you for your help
This is not the right method to display a backoffice controller template.
Here is what you should try:
<?php
class AdminObjectifController extends ModuleAdminController
{
public function __construct()
{
$this->bootstrap = true;
$this->table = 'objectifs';
$this->className = 'Objectif';
parent::__construct();
}
public function initContent()
{
$tpl = $this->context->smarty->createTemplate($this->getTemplatePath() . 'admin-obj.tpl', $this->context->smarty);
$tpl->assign(array(
'my_var' => "test"
));
$this->content .= $tpl->fetch();
parent::initContent();
}
}
Note that your admin-obj.tpl file should be placed under views/templates/admin/admin-obj.tpl inside your module.
I am working on Album Application in zf3.I added acl functionality to the application like this:
AlbumController.php
class AlbumController extends AbstractActionController
{
protected $role;
public function onDispatch(\Zend\Mvc\MvcEvent $e)
{
$userSession = new Container('user');
if (!isset($userSession->email)) {
return $this->redirect()->toRoute('login');
}
else {
$this->role = $userSession->role;
parent::onDispatch($e);
}
}
public function checkPermission($role,$action)
{
if($role == 'admin'){
$acl = new Acl();
if ($acl->isAllowed('admin', 'AlbumController', $action)) {
return true;
}
}
return false;
}
public function editAction()
{
$action = 'edit';
$permission = $this->checkPermission($this->role,$action);
if (!$permission) {
$this->flashMessenger()->addMessage('<div class="alert alert- danger" role="alert"><b>You dont have the privilege to edit!!</b></div>');
return $this->redirect()->toRoute('album');
}
$id = (int) $this->params()->fromRoute('id', 0);
if (0 === $id) {
return $this->redirect()->toRoute('album', ['action' => 'add']);
}
try {
$album = $this->table->getAlbum($id);
} catch (\Exception $e) {
return $this->redirect()->toRoute('album', ['action' => 'index']);
}
$form = new AlbumForm();
$form->bind($album);
$form->get('submit')->setAttribute('value', 'Edit');
$request = $this->getRequest();
$viewData = ['id' => $id, 'form' => $form];
if (! $request->isPost()) {
return $viewData;
}
$form->setInputFilter($album->getInputFilter());
$form->setData($request->getPost());
$edit = $request->getPost('submit', 'Cancel');
if($edit == 'Cancel'){
$this->flashMessenger()->addMessage('<div class="alert alert-danger" role="alert"><b>Cancelled by User...!!</b></div>');
return $this->redirect()->toRoute('album');
}
if (! $form->isValid()) {
$this->flashMessenger()->addMessage('<div class="alert alert-danger" role="alert"><b>Failed to Update...!!</b></div>');
return $viewData;
}else{
$this->table->saveAlbum($album);
$this->flashMessenger()->addMessage('<div class="alert alert-success" role="alert"><b>Record Updated Successfully...!!</b></div>');
}
// Redirect to album list
return $this->redirect()->toRoute('album', ['action' => 'index']);
}
}
This is working perfectly fine,now i want to move the onDispatch function to Module.php but don't know how to implement it.Can someone please help me
Module.php
<?php
namespace Album;
use Album\Controller\AlbumController;
use Album\Model\Album;
use Album\Model\AlbumTable;
use Zend\Db\Adapter\AdapterInterface;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Album\Model\LoginTable;
class Module implements ConfigProviderInterface
{
public function getConfig()
{
return include __DIR__ . '/../config/module.config.php';
}
public function getServiceConfig()
{
return [
'factories' => [
AlbumTable::class => function($container) {
$tableGateway = $container->get(Model\AlbumTableGateway::class);
return new AlbumTable($tableGateway);
},
Model\AlbumTableGateway::class => function ($container) {
$dbAdapter = $container->get(AdapterInterface::class);
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Album());
return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
},
Model\LoginTable::class => function($container) {
$tableGateway = $container->get(Model\LoginTableGateway::class);
$table = new LoginTable($tableGateway);
return $table;
},
Model\LoginTableGateway::class => function ($container){
$dbAdapter = $container->get(AdapterInterface::class);
$resultSetPrototype = new ResultSet();
return new TableGateway('login', $dbAdapter, null, $resultSetPrototype);
}
],
];
}
public function getControllerConfig()
{
return [
'factories' => [
Controller\AlbumController::class => function($container) {
return new Controller\AlbumController($container->get(Model\AlbumTable::class));
},
Controller\LoginController::class => function($container) {
return new Controller\LoginController($container->get(Model\LoginTable::class));
},
Controller\LogoutController::class => function($container){
return new Controller\LogoutController($container->get(Model\LoginTable::class));
},
],
];
}
}
This is how I implemented it. In your Module.php, add a listener on EVENT_DISPATCH, with a closure as callback that will call your middleware class authorization :
class Module implements ConfigProviderInterface
{
public function getConfig()
{
return include __DIR__ . '/../config/module.config.php';
}
public function onBootstrap(MvcEvent $e)
{
$app = $e->getApplication();
$eventManager = $app->getEventManager();
$serviceManager = $app->getServiceManager();
// Register closure on event DISPATCH, call your checkProtectedRoutes() method
$eventManager->attach(MvcEvent::EVENT_DISPATCH, function (MvcEvent $e) use ($serviceManager) {
$match = $e->getRouteMatch();
$auth = $serviceManager->get(Middleware\AuthorizationMiddleware::class);
$res = $auth->checkProtectedRoutes($match);
if ($res instanceof Response) {
return $res;
}
}, 1);
// Init ACL : could be improved
$this->initAcl($e);
}
You should have an AuthorizationMiddlewareFactory (call it as you want):
<?php
namespace MyModule\Factory;
use Interop\Container\ContainerInterface;
use MyModule\Middleware\AuthorizationMiddleware;
use Zend\Authentication\AuthenticationService;
use Zend\ServiceManager\Factory\FactoryInterface;
class AuthorizationMiddlewareFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$authService = $container->get(AuthenticationService::class);
$acl = $container->get('Acl'); // II init it in bootstrap(), could be improved
$response = $container->get('Response');
$baseUrl = $container->get('Request')->getBaseUrl();
$authorization = new AuthorizationMiddleware($authService, $acl, $response, $baseUrl);
return $authorization ;
}
}
And your AuthorizationMiddleware class:
<?php
namespace MyModule\Middleware;
use Symfony\Component\VarDumper\VarDumper;
use Zend\Authentication\AuthenticationService;
use Zend\Http\PhpEnvironment\Response;
use Zend\Permissions\Acl\Acl;
use Zend\Router\RouteMatch;
class AuthorizationMiddleware
{
private $authService ;
private $acl;
private $response;
private $baseUrl;
/**
* AuthorizationMiddleware constructor.
* #param AuthenticationService $authService
* #param Acl $acl
* #param Response $response
* #param $baseUrl
*/
public function __construct(AuthenticationService $authService, Acl $acl, Response $response, $baseUrl)
{
$this->authService = $authService;
$this->acl = $acl;
$this->response = $response;
$this->baseUrl = $baseUrl;
}
public function checkProtectedRoutes(RouteMatch $match)
{
if (! $match) {
// Nothing without a route
return null ;
}
// Do your checks...
}
It can be improved, but you have the idea... See also this Question and the answers: ZF3 redirection after ACL authorization failed
I am using this code to authenticate user with Facebook and laravel-oauth2 package. But my Auth::atempt keeps failing.(Ecos FLAG) Can someone help me see the problem ? Here is my controller function and code for my route:
Route::get('oauth/{provider}', 'HomeController#provide');
public function provide($provider)
{
$init_p = $provider;
$provider = OAuth2::provider($provider, array(
'id' => 'MY ID',
'secret' => 'MY SECRET',
));
if ( ! isset($_GET['code']))
{
return $provider->authorize();
}
else
{
try
{
$params = $provider->access($_GET['code']);
$token = new Token_Access(array(
'access_token' => $params->access_token
));
$provided_user = $provider->get_user_info($token);
if($init_p == 'facebook')
{
if (User::whereFacebookUid($provided_user['uid'])->first())
{
if(Auth::check())
{
return Redirect::to('/testovi');
}
else
{
if (Auth::attempt(array('password' => $provided_user['uid'])))
{
return Redirect::to('/testovi');
}
else
{
echo("FLAG");
}
}
}
else
{
$user = new User;
$user->name = $provided_user['name'];
$user->email = $provided_user['email'];
$user->password = $provided_user['uid'];
$user->facebook_uid = $provided_user['uid'];
$user->save();
Auth::login($user);
return Redirect::to('testovi');
}
}
}
catch (OAuth2_Exception $e)
{
show_error('That didnt work: '.$e);
}
}
}
Solution was to HASH uid befor saving id to db. Details from:
Details
$user = new User;
$user->username = $provided_user['name'];
$user->email = $provided_user['email'];
$user->password = Hash::make($provided_user['uid']);
$user->facebook_uid = $provided_user['uid'];
$user->save();
Auth::login($user);
return Redirect::to('testovi');
I want make possible for user change the language of the webside. In the Module.php I wrote this:
public function onBootstrap(MvcEvent $e)
{
$e->getApplication()->getServiceManager('translator');
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$eventManager->attach(\Zend\Mvc\MvcEvent::EVENT_DISPATCH, array($this, 'bootstrapSession'), 10);
$config = $this->getConfig();
\Locale::setDefault('de');
\Zend\Validator\AbstractValidator::setDefaultTranslator(
$e->getApplication()
->getServiceManager()
->get('translator')
);
if ($session->language !== NULL)
{
$e->getApplication()->getServiceManager()->get('translator')->setLocale($session->language);
}
public function bootstrapSession()
{
$config = $this->getConfig();
$sessionConfig = new Session\Config\SessionConfig();
$sessionConfig->setOptions($config['session']);
$sessionManager = new Session\SessionManager($sessionConfig);
$sessionManager->start();
var_dump($sessionManager);
Session\Container::setDefaultManager($sessionManager);
}
public function getServiceConfig()
{
var_dump('halloo');
return array(
'factories' => array(
'session' => function() {
$session = Session\Container::getDefaultManager()->getStorage();
return $session;
},
),
);
}
In the IndexController.php I want to change the language and get it after in the module. So that the language changes.
Here is my action:
public function enAction()
{
$session = $this->getServiceLocator()->get('session');
$session->language = 'en';
return $this->redirect()->toRoute('home');
}
The browser shows no error but the language doesn't change. Does someone see a error and can help me?
Module.php
public function onBootstrap(MvcEvent $e){
// session container
$sessionContainer = new \Zend\Session\Container('locale');
// test if session language exists
if(!$sessionContainer->offsetExists('mylocale')){
// if not use the browser locale
if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
$sessionContainer->offsetSet('mylocale', \Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']));
}else{
$sessionContainer->offsetSet('mylocale', 'en_US');
}
}
// translating system
$translator = $serviceManager->get('translator');
$translator ->setLocale($sessionContainer->mylocale)
->setFallbackLocale('en_US');
$mylocale = $sessionContainer->mylocale;
Controller
Just change the language from controller
/**
*
* #return \Zend\View\Model\ViewModel
*/
public function changelocaleAction(){
// disable layout
$result = new ViewModel();
$result->setTerminal(true);
// variables
$event = $this->getEvent();
$matches = $event->getRouteMatch();
$myLocale = $matches->getParam('locale');
$redirect = $matches->getParam('redirecturl', '');
// translate
$sessionContainer = new Container('locale');
switch ($myLocale){
case 'fr_FR':
break;
case 'en_US':
break;
default :
$myLocale = 'en_US';
}
$sessionContainer->offsetSet('mylocale', $myLocale);
// redirect
switch ($redirect){
case '':
$this->redirect()->toRoute('home');
break;
default :
$this->redirect()->toUrl(urldecode($redirect));
}
return $result;
}
From Zend Framework Multi Language Integration Steps
I am trying to implement Custom authentication via SOAP webservice
as per Refering links
http://it-cook-de.blogspot.in/2013/07/zend-framework-2-custom-authentication-with-soap-webservice-part-1.html
http://samsonasik.wordpress.com/2012/10/23/zend-framework-2-create-login-authentication-using-authenticationservice-with-rememberme/#comment-6117
Now getting error as
Code :
<?php
namespace Application\Adapter;
use Zend\Authentication\Adapter\AdapterInterface;
use Zend\Authentication\Result;
use Zend\Soap\Client;
use Zend\Soap\Client\DotNet as DotNet;
class SoapAuthenticationAdapter implements AdapterInterface
{
//:TODO: move to configuration
//private $module = 'my_application_name_in_auth_system';
private $module = 'student';
//:TODO: move to configuration
// private $uri = 'full_url_to_soap_webservice_of_auth_system';
private $uri = 'http://ip/Service/student.svc?wsdl';
//:TODO: move to configuration
//private $location = 'full_url_to_soap_webservice_of_auth_system';
private $location = 'http://ip/Service/student.svc?wsdl';
protected $username;
protected $password;
protected $customercode;
function __construct()
{
}
public function authenticate()
{
$client = new DotNet($this->uri);
$param=array("customercode"=>$this->customercode,"username"=>$this->username,"password"=>$this->password);
$result = $client->call('ValidateUser',array($param));
if ($result) {
$this->permissions = $param;
return $this->getResult(Result::SUCCESS, $this->permissions, 'SUCCESS');
} else {
return $this->getResult(Result::FAILURE_CREDENTIAL_INVALID, null, 'FAILURE_CREDENTIAL_INVALID');
}
} catch (\SoapFault $e) {
// switch ($e->getMessage()) {
return $this->getResult(Result::FAILURE_UNCATEGORIZED, null, $e->getMessage());
// }
}
}
public function setIdentity($username)
{
$this->username = $username;
}
public function setCredential($password)
{
$this->password = $password;
}
public function setCustomerCode($customercode)
{
$this->customercode =$customercode;
}
private function getResult($type, $identity, $message)
{
return new Result($type, $identity, array(
$message
));
}
}
/////In IndexController
public function getAuthService()
{
if (! $this->authservice) {
$this->authservice = $this->getServiceLocator()
->get('SoapAuthenticationService');
}
return $this->authservice;
}
public function someAction()
{
$request = $this->getRequest();
if ($request->isPost()){
$this->getAuthService()->getAdapter()
->setIdentity($request->getPost('username'))
->setCredential($request->getPost('password'))
->setCustomerCode($request->getPost('customercode'));
$result = $this->getAuthService()->authenticate();
foreach($result->getMessages() as $message)
{
//save message temporary into flashmessenger
$this->flashmessenger()->addMessage($message);
}
if ($result->isValid()) {
$redirect = 'home';
//check if it has rememberMe :
if ($request->getPost('rememberme') == 1 ) {
$this->getSessionStorage()
->setRememberMe(1);
//set storage again
$this->getAuthService()->setStorage($this->getSessionStorage());
}
$this->getAuthService()->setStorage($this->getSessionStorage());
$this->getAuthService()->getStorage()->write($request->getPost('username'));
}
else
{
$redirect ='login';
}
return $this->redirect()->toRoute($redirect);
}
in module.php
public function getServiceConfig()
{
return array(
'factories' => array(
//Add the following lines
'Application\Model\SoapAuthenticationStorage' => function($sm){
return new SoapAuthenticationStorage('student');
},
'SoapAuthenticationService' => function($sm) {
$authAdapter = new SoapAuthenticationAdapter();
$authService = new AuthenticationService();
$authService->setAdapter($authAdapter);
$authService->setStorage($sm->get('Application\Model\SoapAuthenticationStorage'));
return $authService;
}
)
);
}
Any help to solve this
Each of the set methods in your adapter need to return $this in order to allow you to chain method calls together (this is called a 'fluent interface'). E.g.:
public function setIdentity($username)
{
$this->username = $username;
return $this;
}
That should fix the error you're getting.