|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Controller\Users; |
|
4
|
|
|
|
|
5
|
|
|
use App\Facades\Router; |
|
6
|
|
|
use App\Facades\Security; |
|
7
|
|
|
use App\Facades\Session; |
|
8
|
|
|
use App\Facades\View; |
|
9
|
|
|
use App\Model\User; |
|
10
|
|
|
use Exception; |
|
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
12
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
13
|
|
|
use Ronanchilvers\Orm\Orm; |
|
14
|
|
|
use RuntimeException; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Controller for user invitations |
|
18
|
|
|
* |
|
19
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
class InvitationController |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Invite a user |
|
25
|
|
|
* |
|
26
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
27
|
|
|
*/ |
|
28
|
|
|
public function create( |
|
29
|
|
|
ServerRequestInterface $request, |
|
30
|
|
|
ResponseInterface $response, |
|
31
|
|
|
$args |
|
|
|
|
|
|
32
|
|
|
) { |
|
33
|
|
|
$user = new User(); |
|
34
|
|
|
if ('POST' == $request->getMethod()) { |
|
35
|
|
|
$data = $request->getParsedBody()['user']; |
|
36
|
|
|
$user->fromArray($data); |
|
37
|
|
|
if ($user->saveWithValidation('invitation')) { |
|
38
|
|
|
Session::flash([ |
|
39
|
|
|
'heading' => 'Invitation created' |
|
40
|
|
|
]); |
|
41
|
|
|
return $response->withRedirect( |
|
|
|
|
|
|
42
|
|
|
Router::pathFor('users.index') |
|
43
|
|
|
); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return View::render( |
|
48
|
|
|
$response, |
|
49
|
|
|
'users/invitations/create.html.twig', |
|
50
|
|
|
[ |
|
51
|
|
|
'user' => $user, |
|
52
|
|
|
] |
|
53
|
|
|
); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Action to accept an invitation |
|
58
|
|
|
* |
|
59
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
60
|
|
|
*/ |
|
61
|
|
|
public function accept( |
|
62
|
|
|
ServerRequestInterface $request, |
|
63
|
|
|
ResponseInterface $response, |
|
64
|
|
|
$args |
|
65
|
|
|
) { |
|
66
|
|
|
if (!isset($args['hash']) || 0 == strlen($args['hash'])) { |
|
67
|
|
|
return $response->withRedirect( |
|
68
|
|
|
Router::pathFor('user.login') |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
$hash = filter_var($args['hash'], FILTER_SANITIZE_STRING); |
|
|
|
|
|
|
72
|
|
|
$user = Orm::finder(User::class)->forHash($args['hash']); |
|
73
|
|
|
if (!$user instanceof User) { |
|
74
|
|
|
return $response->withRedirect( |
|
75
|
|
|
Router::pathFor('user.login') |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
if ('POST' == $request->getMethod()) { |
|
79
|
|
|
$data = $request->getParsedBody()['user']; |
|
80
|
|
|
$password = $request->getParsedBody()['password']; |
|
81
|
|
|
$user->fromArray($data); |
|
82
|
|
|
$user->setPassword( |
|
83
|
|
|
$password['value'], |
|
84
|
|
|
$password['confirm'], |
|
85
|
|
|
); |
|
86
|
|
|
$user->activate(); |
|
87
|
|
|
if ($user->validate() && $user->save()) { |
|
88
|
|
|
Session::flash([ |
|
89
|
|
|
'heading' => 'Saved' |
|
90
|
|
|
]); |
|
91
|
|
|
return $response->withRedirect( |
|
92
|
|
|
Router::pathFor('users.index') |
|
93
|
|
|
); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return View::render( |
|
98
|
|
|
$response, |
|
99
|
|
|
'users/invitations/accept.html.twig', |
|
100
|
|
|
[ |
|
101
|
|
|
'user' => $user, |
|
102
|
|
|
] |
|
103
|
|
|
); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.