Skip to content

Commit 8c17b2f

Browse files
Dump version 0.1.0
1 parent 9fdc1ef commit 8c17b2f

10 files changed

+452
-0
lines changed

Config/config.xml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
3+
<config xmlns="http://thelia.net/schema/dic/config"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://thelia.net/schema/dic/config http://thelia.net/schema/dic/config/thelia-1.0.xsd">
6+
7+
</config>

Config/module.xml

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module xmlns="http://thelia.net/schema/dic/module"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://thelia.net/schema/dic/module http://thelia.net/schema/dic/module/module-2_2.xsd">
5+
<fullnamespace>LoginWithPhone\LoginWithPhone</fullnamespace>
6+
<descriptive locale="en_US">
7+
<title>Allows customers to connect with their phone number</title>
8+
</descriptive>
9+
<descriptive locale="fr_FR">
10+
<title>Permet aux clients de se connecter avec leurs numéros de téléphone.</title>
11+
</descriptive>
12+
<languages>
13+
<language>en_US</language>
14+
<language>fr_FR</language>
15+
</languages>
16+
<version>0.1.0</version>
17+
<authors>
18+
<author>
19+
<name>Gilles Bourgeat</name>
20+
<email>gilles.bourgeat@gmail.com</email>
21+
</author>
22+
</authors>
23+
<type>classic</type>
24+
<thelia>2.2.0</thelia>
25+
<stability>beta</stability>
26+
<mandatory>0</mandatory>
27+
<hidden>0</hidden>
28+
</module>

Config/routing.xml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
3+
<routes xmlns="http://symfony.com/schema/routing"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
6+
7+
<route id="customer.login.process" path="/login" methods="post">
8+
<default key="_controller">LoginWithPhone\Controller\Front\CustomerController::loginAction</default>
9+
<default key="_view">login</default>
10+
</route>
11+
12+
</routes>

Config/schema.xml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<database defaultIdMethod="native" name="thelia"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="../../../vendor/propel/propel/resources/xsd/database.xsd" >
5+
<!--
6+
See propel documentation on http://propelorm.org for all information about schema file
7+
8+
<table name="product_rel" namespace="LoginWithPhone\Model">
9+
<column autoIncrement="true" name="id" primaryKey="true" required="true" type="INTEGER" />
10+
<column defaultValue="0" name="visible" required="true" type="TINYINT" />
11+
<column defaultValue="0" name="position" required="true" type="INTEGER" />
12+
<column name="title" size="255" type="VARCHAR" />
13+
<column name="description" type="CLOB" />
14+
<column name="chapo" type="LONGVARCHAR" />
15+
<column name="postscriptum" type="LONGVARCHAR" />
16+
<foreign-key foreignTable="product" name="fk_product_id" onDelete="CASCADE" onUpdate="RESTRICT">
17+
<reference foreign="id" local="product_id" />
18+
</foreign-key>
19+
<behavior name="timestampable" />
20+
<behavior name="i18n">
21+
<parameter name="i18n_columns" value="title, description, chapo, postscriptum" />
22+
</behavior>
23+
<behavior name="versionable">
24+
<parameter name="log_created_at" value="true" />
25+
<parameter name="log_created_by" value="true" />
26+
</behavior>
27+
</table>
28+
-->
29+
<external-schema filename="local/config/schema.xml" referenceOnly="true" />
30+
</database>
+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
3+
namespace LoginWithPhone\Controller\Front;
4+
5+
use Front\Front;
6+
use LoginWithPhone\Form\Front\CustomerLogin;
7+
use LoginWithPhone\Security\Authentication\CustomerUsernamePasswordFormAuthenticator;
8+
use Thelia\Core\Event\Customer\CustomerEvent;
9+
use Thelia\Core\Event\TheliaEvents;
10+
use Thelia\Core\Security\Exception\AuthenticationException;
11+
use Thelia\Core\Security\Exception\CustomerNotConfirmedException;
12+
use Thelia\Core\Security\Exception\UsernameNotFoundException;
13+
use Thelia\Core\Security\Exception\WrongPasswordException;
14+
use Thelia\Form\Exception\FormValidationException;
15+
use Thelia\Log\Tlog;
16+
use Thelia\Model\Customer;
17+
18+
class CustomerController extends \Front\Controller\CustomerController
19+
{
20+
/**
21+
* Perform user login. On a successful login, the user is redirected to the URL
22+
* found in the success_url form parameter, or / if none was found.
23+
*
24+
* If login is not successfull, the same view is displayed again.
25+
*
26+
*/
27+
public function loginAction()
28+
{
29+
if (!$this->getSecurityContext()->hasCustomerUser()) {
30+
$request = $this->getRequest();
31+
$customerLoginForm = new CustomerLogin($request);
32+
33+
try {
34+
$form = $this->validateForm($customerLoginForm, "post");
35+
36+
// If User is a new customer
37+
if ($form->get('account')->getData() == 0 && $form->get("email")->getErrors()->count() == 0) {
38+
return $this->generateRedirectFromRoute(
39+
"customer.create.process",
40+
["email" => $form->get("email")->getData()]
41+
);
42+
} else {
43+
try {
44+
$authenticator = new CustomerUsernamePasswordFormAuthenticator($request, $customerLoginForm, [
45+
'username_field_name' => 'email'
46+
]);
47+
48+
/** @var Customer $customer */
49+
$customer = $authenticator->getAuthentifiedUser();
50+
51+
$this->processLogin($customer);
52+
53+
if (intval($form->get('remember_me')->getData()) > 0) {
54+
// If a remember me field if present and set in the form, create
55+
// the cookie thant store "remember me" information
56+
$this->createRememberMeCookie(
57+
$customer,
58+
$this->getRememberMeCookieName(),
59+
$this->getRememberMeCookieExpiration()
60+
);
61+
}
62+
63+
return $this->generateSuccessRedirect($customerLoginForm);
64+
65+
} catch (UsernameNotFoundException $e) {
66+
$message = $this->getTranslator()->trans(
67+
"Wrong email or password. Please try again",
68+
[],
69+
Front::MESSAGE_DOMAIN
70+
);
71+
} catch (WrongPasswordException $e) {
72+
$message = $this->getTranslator()->trans(
73+
"Wrong email or password. Please try again",
74+
[],
75+
Front::MESSAGE_DOMAIN
76+
);
77+
} catch (CustomerNotConfirmedException $e) {
78+
if ($e->getUser() !== null) {
79+
// Send the confirmation email again
80+
$this->getDispatcher()->dispatch(
81+
TheliaEvents::SEND_ACCOUNT_CONFIRMATION_EMAIL,
82+
new CustomerEvent($e->getUser())
83+
);
84+
}
85+
$message = $this->getTranslator()->trans(
86+
"Your account is not yet confirmed. A confirmation email has been sent to your email address, please check your mailbox",
87+
[],
88+
Front::MESSAGE_DOMAIN
89+
);
90+
} catch (AuthenticationException $e) {
91+
$message = $this->getTranslator()->trans(
92+
"Wrong email or password. Please try again",
93+
[],
94+
Front::MESSAGE_DOMAIN
95+
);
96+
}
97+
98+
}
99+
} catch (FormValidationException $e) {
100+
$message = $this->getTranslator()->trans(
101+
"Please check your input: %s",
102+
['%s' => $e->getMessage()],
103+
Front::MESSAGE_DOMAIN
104+
);
105+
} catch (\Exception $e) {
106+
$message = $this->getTranslator()->trans(
107+
"Sorry, an error occured: %s",
108+
['%s' => $e->getMessage()],
109+
Front::MESSAGE_DOMAIN
110+
);
111+
}
112+
113+
Tlog::getInstance()->error(
114+
sprintf(
115+
"Error during customer login process : %s. Exception was %s",
116+
$message,
117+
$e->getMessage()
118+
)
119+
);
120+
121+
$customerLoginForm->setErrorMessage($message);
122+
123+
$this->getParserContext()->addForm($customerLoginForm);
124+
125+
if ($customerLoginForm->hasErrorUrl()) {
126+
return $this->generateErrorRedirect($customerLoginForm);
127+
}
128+
}
129+
}
130+
}

Form/Front/CustomerLogin.php

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
/*************************************************************************************/
3+
/* This file is part of the Thelia package. */
4+
/* */
5+
/* Copyright (c) OpenStudio */
6+
/* email : dev@thelia.net */
7+
/* web : http://www.thelia.net */
8+
/* */
9+
/* For the full copyright and license information, please view the LICENSE.txt */
10+
/* file that was distributed with this source code. */
11+
/*************************************************************************************/
12+
13+
namespace LoginWithPhone\Form\Front;
14+
15+
use Symfony\Component\Validator\Constraints;
16+
use Symfony\Component\Validator\Context\ExecutionContextInterface;
17+
use Thelia\Core\Translation\Translator;
18+
use Thelia\Model\CustomerQuery;
19+
20+
class CustomerLogin extends \Thelia\Form\CustomerLogin
21+
{
22+
protected function buildForm()
23+
{
24+
parent::buildForm();
25+
26+
$this->formBuilder->remove('email');
27+
28+
$this->formBuilder
29+
->add("email", "text", array(
30+
"constraints" => array(
31+
new Constraints\NotBlank(),
32+
new Constraints\Callback(array(
33+
"methods" => array(
34+
array($this, "verifyExistingAccount"),
35+
),
36+
)),
37+
),
38+
"label" => Translator::getInstance()->trans("Please enter your email address"),
39+
"label_attr" => array(
40+
"for" => "email",
41+
),
42+
))
43+
;
44+
}
45+
46+
/**
47+
* If the user select "I'am a new customer", we make sure is email address does not exit in the database.
48+
*/
49+
public function verifyExistingAccount($value, ExecutionContextInterface $context)
50+
{
51+
$data = $context->getRoot()->getData();
52+
if ($data["account"] == 0) {
53+
$customer = CustomerQuery::create()->findOneByEmail($value);
54+
55+
if (null === $customer) {
56+
$query = CustomerQuery::create();
57+
58+
$query->useAddressQuery()
59+
->filterByCellphone($value)
60+
->endUse();
61+
62+
$customer = $query->findOne();
63+
}
64+
65+
if (null === $customer) {
66+
$query = CustomerQuery::create();
67+
68+
$query->useAddressQuery()
69+
->filterByPhone($value)
70+
->endUse();
71+
72+
$customer = $query->findOne();
73+
}
74+
75+
if ($customer) {
76+
$context->addViolation(Translator::getInstance()->trans("A user already exists with this email address. Please login or if you've forgotten your password, go to Reset Your Password."));
77+
}
78+
}
79+
}
80+
}

LoginWithPhone.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace LoginWithPhone;
4+
5+
use Thelia\Module\BaseModule;
6+
7+
class LoginWithPhone extends BaseModule
8+
{
9+
/** @var string */
10+
const DOMAIN_NAME = 'loginwithphone';
11+
12+
/*
13+
* You may now override BaseModuleInterface methods, such as:
14+
* install, destroy, preActivation, postActivation, preDeactivation, postDeactivation
15+
*
16+
* Have fun !
17+
*/
18+
}

Readme.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Login With Phone
2+
3+
Allows customers to connect with their phone number.
4+
5+
### Manually
6+
7+
* Copy the module into ```<thelia_root>/local/modules/``` directory and be sure that the name of the module is LoginWithPhone.
8+
* Activate it in your thelia administration panel
9+
10+
### Composer
11+
12+
Add it in your main thelia composer.json file
13+
14+
```
15+
composer require thelia/login-with-phone-module:~0.1.0
16+
```
17+
18+
## Usage
19+
20+
You just have to activate this module

0 commit comments

Comments
 (0)