Today we are glad to introduce to our community a new free module: «Belvg_AutoLogin». In future this module will help us demonstrate how our modules work. The idea of this module is that it allows users to test modules, which are installed on our demo-shops, without creating accounts. Using the form, shown below, users can log into the demo-shop under a profile of some other user, who have already been registered in the system.
To make the user appear in the form, he should be recorded in the table belvg_autologin
and should also exist in the standard table ps_customer
. The user can be added to thebelvg_autologin
table both manually and automatically. The automatic mode is controlled by the property belvg_autologin::$default_customers:
1
2
3
|
protected $default_customers = array(
‘2’ //alexandr.simonchik@gmail.com
);
|
It contains an array of users’ IDs, whose profiles we want to use to allow free logging into the system.
The authorization is controlled by the controller Belvg_AutoLoginDefaultModuleFrontController and its method prосessLоgin( ):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
protected function processLogin()
{
$autologin_id = (int)Tools::getValue(‘autologin_id’); //getting the id of the record in the table belvg_autologin
$autologinObj = new Autologin($autologin_id); //creating an object
$customer = new Customer($autologinObj->id_customer);
if (!$autologinObj->id) {
die(Tools::displayError(‘This cuistomer is not exists’));
} //verifying if the user is related to this ID
if (!$customer->id) {
die(Tools::displayError(‘Authentication failed.’));
}
Hook::exec(‘actionBeforeAuthentication’); //implementing the standard hook to enable the logic of the modules, that are using this hook
// setting up data for the session
$this->context->cookie->id_compare = isset($this->context->cookie->id_compare) ? $this->context->cookie->id_compare: CompareProduct::getIdCompareByIdCustomer($customer->id);
$this->context->cookie->id_customer = (int)($customer->id);
$this->context->cookie->customer_lastname = $customer->lastname;
$this->context->cookie->customer_firstname = $customer->firstname;
$this->context->cookie->logged = 1;
$customer->logged = 1;
$this->context->cookie->is_guest = $customer->isGuest();
$this->context->cookie->passwd = $customer->passwd;
$this->context->cookie->email = $customer->email;
// Add customer to the context
$this->context->customer = $customer;
// Cart
if (Configuration::get(‘PS_CART_FOLLOWING’) && (empty($this->context->cookie->id_cart) || Cart::getNbProducts($this->context->cookie->id_cart) == 0) && $id_cart = (int)Cart::lastNoneOrderedCart($this->context->customer->id)) {
$this->context->cart = new Cart($id_cart);
} else {
$id_carrier = (int)$this->context->cart->id_carrier;
$this->context->cart->id_carrier = 0;
$this->context->cart->setDeliveryOption(null);
$this->context->cart->id_address_delivery = (int)Address::getFirstCustomerAddressId((int)($customer->id));
$this->context->cart->id_address_invoice = (int)Address::getFirstCustomerAddressId((int)($customer->id));
}
$this->context->cart->id_customer = (int)$customer->id;
$this->context->cart->secure_key = $customer->secure_key;
if ($this->ajax && isset($id_carrier) && $id_carrier && Configuration::get(‘PS_ORDER_PROCESS_TYPE’)) {
$delivery_option = array($this->context->cart->id_address_delivery => $id_carrier.‘,’);
$this->context->cart->setDeliveryOption($delivery_option);
}
$this->context->cart->save();
$this->context->cookie->id_cart = (int)$this->context->cart->id;
$this->context->cookie->write();
$this->context->cart->autosetProductAddress();
Hook::exec(‘actionAuthentication’); //invoke the standard hook again, for the same purpose as the previous one
Tools::redirect($this->context->link->getPageLink(‘order-opc’)); //in our case we want to redirect visitors to the checkout page, but you can use any other pages, see examples below
//Tools::redirect(‘index.php?controller=my-account’);
//Tools::redirect($_SERVER[‘HTTP_REFERER’]);
}
|