Jump to content

[solved] Expiration date for registered users


Recommended Posts

Hello,

 

I would need to set up an expiration date for registered users. For example, the acces to the shop for all users from a certain group will expire X days after they registered.

 

Is there any way of getting this please?

 

Thank you in advance

Link to comment
Share on other sites

by default, customer table in database has got information, when the account was created: date_add - we will use it.

 

so, the most important thing in login process is AuthController located in controllers/front/AuthController.php

and the function for "login" process is:

 

protected function processSubmitLogin()

 

search for it in the code.

 

$customer = new Customer();
  $authentication = $customer->getByEmail(trim($email), trim($passwd));
  if (!$authentication || !$customer->id)
$this->errors[] = Tools::displayError('Authentication failed.');
  else
  {
$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;

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
{
 $this->context->cart->id_carrier = 0;
 $this->context->cart->setDeliveryOption(null);
 $this->context->cart->id_address_delivery = Address::getFirstCustomerAddressId((int)($customer->id));
 $this->context->cart->id_address_invoice = Address::getFirstCustomerAddressId((int)($customer->id));
}
$this->context->cart->id_customer = (int)$customer->id;
$this->context->cart->secure_key = $customer->secure_key;
$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');
// Login information have changed, so we check if the cart rules still apply
CartRule::autoRemoveFromCart($this->context);
CartRule::autoAddToCart($this->context);
if (!$this->ajax)
{
 if ($back = Tools::getValue('back'))
  Tools::redirect(html_entity_decode($back));
 Tools::redirect('index.php?controller='.(($this->authRedirection !== false) ? url_encode($this->authRedirection) : 'my-account'));
}
  }

 

and change it to:

 

if (!$authentication || !$customer->id)
$this->errors[] = Tools::displayError('Authentication failed.');
  else
  {
   $days=14;
			if ((strtotime($customer->date_add)+($days*60*60*24))<date("U")){
				$this->errors[] = Tools::displayError('Account expired.');
			} else {
	$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;

	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
	{
	 $this->context->cart->id_carrier = 0;
	 $this->context->cart->setDeliveryOption(null);
	 $this->context->cart->id_address_delivery = Address::getFirstCustomerAddressId((int)($customer->id));
	 $this->context->cart->id_address_invoice = Address::getFirstCustomerAddressId((int)($customer->id));
	}
	$this->context->cart->id_customer = (int)$customer->id;
	$this->context->cart->secure_key = $customer->secure_key;
	$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');

	// Login information have changed, so we check if the cart rules still apply
	CartRule::autoRemoveFromCart($this->context);
	CartRule::autoAddToCart($this->context);

	if (!$this->ajax)
	{
	 if ($back = Tools::getValue('back'))
	  Tools::redirect(html_entity_decode($back));
	 Tools::redirect('index.php?controller='.(($this->authRedirection !== false) ? url_encode($this->authRedirection) : 'my-account'));
	}
   }
		}

 

 

as you can see - i added this code:

 

$days=14;
if ((strtotime($customer->date_add)+($days*60*60*24))<date("U")){
$this->errors[] = Tools::displayError('Account expired.');
} else {
.. login code here...
}

 

the $days defines number of days, after this time account will be "expired"

Link to comment
Share on other sites

sure,

 

you can use $customer->id also customer group in if condition .

 

for example:

 

customer with id = 4

if ($customer->id == 4){

$days=15;

}

 

 

customer with id = 100

if ($customer->id==100) {

$days =9;

}

Link to comment
Share on other sites

Thank you vekia, I have not tried your code yet as komodityagro, but one question: Once expired how the customer appear in BO, as not activated once expired? It would be great if after expiration the user is not activated in BO so it can be manually activated again from BO.

 

Thanks a lot for your great help and support, vekia!!

Link to comment
Share on other sites

thanks

 

you may need to add the:

 

$customer->toggleStatus();

 

into the if condition which I created several post above:

 

$days=14;
if ((strtotime($customer->date_add)+($days*60*60*24))<date("U")){
$this->errors[] = Tools::displayError('Account expired.');
$customer->toggleStatus();
} else {
.. login code here...
}

Link to comment
Share on other sites

I added the code

 

$days=14;

if ((strtotime($customer->date_add)+($days*60*60*24))<date("U")){

$this->errors[] = Tools::displayError('Account expired.');

$customer->toggleStatus();

} else {

.. login code here...

}

 

to AuthController.php but still is not working.

Link to comment
Share on other sites

I understand all, function processSubmitLogin in AuthController.php is bellow. But when I click on Login appears only clear site.

 

protected function processSubmitLogin()
{
 Hook::exec('actionBeforeAuthentication');
 $passwd = trim(Tools::getValue('passwd'));
 $email = trim(Tools::getValue('email'));
 if (empty($email))
  $this->errors[] = Tools::displayError('An email address required.');
 elseif (!Validate::isEmail($email))
  $this->errors[] = Tools::displayError('Invalid email address.');
 elseif (empty($passwd))
  $this->errors[] = Tools::displayError('Password is required.');
 elseif (!Validate::isPasswd($passwd))
  $this->errors[] = Tools::displayError('Invalid password.');
 else
 {
  $customer = new Customer();
  $authentication = $customer->getByEmail(trim($email), trim($passwd));
  if (!$authentication || !$customer->id)
   $this->errors[] = Tools::displayError('Authentication failed.');
  else
  {
		    $days=14;
	    if ((strtotime($customer->date_add)+($days*60*60*24))<date("U")){
	    $this->errors[] = Tools::displayError('Account expired.');
	    $customer->toggleStatus();
		 } else {

   $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;

   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
   {
 $this->context->cart->id_carrier = 0;
 $this->context->cart->setDeliveryOption(null);
 $this->context->cart->id_address_delivery = Address::getFirstCustomerAddressId((int)($customer->id));
 $this->context->cart->id_address_invoice = Address::getFirstCustomerAddressId((int)($customer->id));
   }
   $this->context->cart->id_customer = (int)$customer->id;
   $this->context->cart->secure_key = $customer->secure_key;
   $this->context->cart->save();
   $this->context->cookie->id_cart = (int)$this->context->cart->id;
   $this->context->cookie->update();
   $this->context->cart->autosetProductAddress();
   Hook::exec('actionAuthentication');
   // Login information have changed, so we check if the cart rules still apply
   CartRule::autoRemoveFromCart($this->context);
   CartRule::autoAddToCart($this->context);
   if (!$this->ajax)
   {
 if ($back = Tools::getValue('back'))
  Tools::redirect(html_entity_decode($back));
 Tools::redirect('index.php?controller='.(($this->authRedirection !== false) ? url_encode($this->authRedirection) : 'my-account'));
   }
  }
 }
 if ($this->ajax)
 {
  $return = array(
   'hasError' => !empty($this->errors),
   'errors' => $this->errors,
   'token' => Tools::getToken(false)
  );
  die(Tools::jsonEncode($return));
 }
 else
  $this->context->smarty->assign('authentification_error', $this->errors);
}

Link to comment
Share on other sites

Hello, in back office you can active / deactive account. if you want additional information about expiry date - it needs additional modifications of controllers

 

I marked this thread as solved - but if you've got any other questions related to this case - feel free to continue discussion here

Link to comment
Share on other sites

Thank you for your help vekia.

 

Just one last question: Once the user is deactivated, if I activate him and the user logins afterwards he cannot and gets deactivated, as the login date remains the same. Is there anyway of activating the account again if needed?

 

Maybe using date_upd instead of date_add?

 

Thank you in advance!

Edited by Juandbbam (see edit history)
Link to comment
Share on other sites

Thank you for your help vekia.

 

Just one last question: Once the user is deactivated, if I activate him and the user logins afterwards he cannot and gets deactivated, as the login date remains the same. Is there anyway of activating the account again if needed?

 

Maybe using date_upd instead of date_add?

 

Thank you in advance!

 

yo've got right my friend. Try with date_upd instead date_add!

Link to comment
Share on other sites

Hi vekia,

 

can you more explain this below? Where have I add the code?

 

sure,
you can use $customer->id also customer group in if condition .
for example:
customer with id = 4
if ($customer->id == 4){
$days=15;
}

customer with id = 100
if ($customer->id==100) {
$days =9;
}

Link to comment
Share on other sites

×
×
  • Create New...