1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TechWilk\Rota\Controller; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
7
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
8
|
|
|
use TechWilk\Rota\EmailAddress; |
9
|
|
|
use TechWilk\Rota\PendingUser; |
10
|
|
|
use TechWilk\Rota\PendingUserQuery; |
11
|
|
|
|
12
|
|
|
class PendingUserController extends BaseController |
13
|
|
|
{ |
14
|
|
|
public function getSignUpForm(ServerRequestInterface $request, ResponseInterface $response, $args) |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
$this->logger->info("Fetch sign-up GET '/signup'"); |
17
|
|
|
|
18
|
|
|
if (isset($_SESSION['userId'])) { |
19
|
|
|
return $response->withStatus(302)->withHeader('Location', $this->router->pathFor('home')); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
$auth = $this->auth; |
23
|
|
|
$slug = $auth->getAuthProviderSlug(); |
24
|
|
|
|
25
|
|
|
try { |
26
|
|
|
$auth->getSocialUserId(); |
27
|
|
|
} catch (Exception $e) { |
28
|
|
|
return $response->withStatus(302)->withHeader('Location', $this->router->pathFor('login')); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$existingPendingUser = PendingUserQuery::create() |
32
|
|
|
->filterBySocialId($auth->getSocialUserId()) |
33
|
|
|
->filterBySource($slug) |
34
|
|
|
->findOne(); |
35
|
|
|
|
36
|
|
|
if (!is_null($existingPendingUser)) { |
37
|
|
|
$firstName = $existingPendingUser->getFirstName(); |
38
|
|
|
|
39
|
|
|
// remove any fb auth tokens |
40
|
|
|
session_unset(); |
41
|
|
|
|
42
|
|
|
return $this->view->render($response, 'login-sign-up-complete.twig', [ |
43
|
|
|
'firstname' => $firstName, |
44
|
|
|
]); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$firstName = ''; |
48
|
|
|
$lastName = ''; |
49
|
|
|
$email = ''; |
50
|
|
|
|
51
|
|
|
switch ($slug) { |
52
|
|
|
case 'facebook': |
53
|
|
|
$meta = $auth->getMeta(); |
54
|
|
|
|
55
|
|
|
// Split first and last names from FB |
56
|
|
|
$names = explode(' ', $meta['name'], 2); |
57
|
|
|
|
58
|
|
|
$firstName = $names[0]; |
59
|
|
|
$lastName = $names[1]; |
60
|
|
|
$email = $meta['email']; |
61
|
|
|
break; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $this->view->render($response, 'login-sign-up.twig', [ |
65
|
|
|
'firstname' => $firstName, |
66
|
|
|
'lastname' => $lastName, |
67
|
|
|
'email' => $email, |
68
|
|
|
]); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function postSignUp(ServerRequestInterface $request, ResponseInterface $response, $args) |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
$this->logger->info("Submit sign-up POST '/signup'"); |
74
|
|
|
|
75
|
|
|
if (isset($_SESSION['userId'])) { |
76
|
|
|
return $response->withStatus(302)->withHeader('Location', $this->router->pathFor('home')); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$auth = $this->auth; |
80
|
|
|
$data = $request->getParsedBody(); |
81
|
|
|
|
82
|
|
|
$firstName = filter_var(trim($data['firstName']), FILTER_SANITIZE_STRING); |
83
|
|
|
$lastName = filter_var(trim($data['lastName']), FILTER_SANITIZE_STRING); |
84
|
|
|
$email = new EmailAddress(trim($data['email'])); |
85
|
|
|
|
86
|
|
|
$pendingUser = new PendingUser(); |
87
|
|
|
$pendingUser->setSocialId($auth->getSocialUserId()); |
88
|
|
|
$pendingUser->setFirstName($firstName); |
89
|
|
|
$pendingUser->setLastName($lastName); |
90
|
|
|
$pendingUser->setEmail($email); |
91
|
|
|
$pendingUser->setSource($auth->getAuthProviderSlug()); |
92
|
|
|
$pendingUser->save(); |
93
|
|
|
|
94
|
|
|
return $this->view->render($response, 'login-sign-up-complete.twig', [ |
95
|
|
|
'firstname' => $firstName, |
96
|
|
|
]); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function getSignUpCancel(ServerRequestInterface $request, ResponseInterface $response, $args) |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
session_unset(); |
102
|
|
|
|
103
|
|
|
return $response->withStatus(302)->withHeader('Location', $this->router->pathFor('login')); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.