|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Bluz PHP Team |
|
4
|
|
|
* @link https://github.com/bluzphp/skeleton |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @namespace |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace Application\Users; |
|
11
|
|
|
|
|
12
|
|
|
use Application\Auth; |
|
13
|
|
|
use Application\Exception; |
|
14
|
|
|
use Application\UsersActions; |
|
15
|
|
|
use Bluz\Application\Application; |
|
16
|
|
|
use Bluz\Proxy\Logger; |
|
17
|
|
|
use Bluz\Proxy\Mailer; |
|
18
|
|
|
use Bluz\Proxy\Messages; |
|
19
|
|
|
use Bluz\Proxy\Router; |
|
20
|
|
|
use Bluz\Validator\Exception\ValidatorException; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Crud |
|
24
|
|
|
* |
|
25
|
|
|
* @package Application\Users |
|
26
|
|
|
* |
|
27
|
|
|
* @method Table getTable() |
|
28
|
|
|
* |
|
29
|
|
|
* @author Anton Shevchuk |
|
30
|
|
|
* @created 30.10.12 16:11 |
|
31
|
|
|
*/ |
|
32
|
|
|
class Crud extends \Bluz\Crud\Table |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* @param array $data |
|
36
|
|
|
* @throws Exception |
|
37
|
|
|
* @throws ValidatorException |
|
38
|
|
|
* @return integer |
|
39
|
|
|
*/ |
|
40
|
|
|
public function createOne($data) |
|
41
|
|
|
{ |
|
42
|
|
|
// password |
|
43
|
|
|
$password = isset($data['password']) ? $data['password'] : null; |
|
44
|
|
|
$password2 = isset($data['password2']) ? $data['password2'] : null; |
|
45
|
|
|
|
|
46
|
|
|
if (empty($password)) { |
|
47
|
|
|
throw ValidatorException::exception('password', __('Password can\'t be empty')); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
if ($password !== $password2) { |
|
51
|
|
|
throw ValidatorException::exception('password2', __('Password is not equal')); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
if ($data['id'] == '') { |
|
55
|
|
|
unset($data['id']); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** @var $row Row */ |
|
59
|
|
|
$row = $this->getTable()->create(); |
|
60
|
|
|
$row->setFromArray($data); |
|
61
|
|
|
$row->status = Table::STATUS_PENDING; |
|
62
|
|
|
$row->save(); |
|
63
|
|
|
|
|
64
|
|
|
$userId = $row->id; |
|
65
|
|
|
|
|
66
|
|
|
// create auth |
|
67
|
|
|
Auth\Table::getInstance()->generateEquals($row, $password); |
|
68
|
|
|
|
|
69
|
|
|
// create activation token |
|
70
|
|
|
// valid for 5 days |
|
71
|
|
|
$actionRow = UsersActions\Table::getInstance()->generate($userId, UsersActions\Table::ACTION_ACTIVATION, 5); |
|
72
|
|
|
|
|
73
|
|
|
// send activation email |
|
74
|
|
|
// generate activation URL |
|
75
|
|
|
$activationUrl = Router::getFullUrl( |
|
76
|
|
|
'users', |
|
77
|
|
|
'activation', |
|
78
|
|
|
['code' => $actionRow->code, 'id' => $userId] |
|
79
|
|
|
); |
|
80
|
|
|
|
|
81
|
|
|
$subject = "Activation"; |
|
82
|
|
|
|
|
83
|
|
|
$body = Application::getInstance()->dispatch( |
|
84
|
|
|
'users', |
|
85
|
|
|
'mail/template', |
|
86
|
|
|
[ |
|
87
|
|
|
'template' => 'registration', |
|
88
|
|
|
'vars' => ['user' => $row, 'activationUrl' => $activationUrl, 'password' => $password] |
|
89
|
|
|
] |
|
90
|
|
|
)->render(); |
|
91
|
|
|
|
|
92
|
|
|
try { |
|
93
|
|
|
$mail = Mailer::create(); |
|
94
|
|
|
$mail->Subject = $subject; |
|
95
|
|
|
$mail->msgHTML(nl2br($body)); |
|
96
|
|
|
$mail->addAddress($data['email']); |
|
97
|
|
|
Mailer::send($mail); |
|
98
|
|
|
} catch (\Exception $e) { |
|
99
|
|
|
Logger::log( |
|
100
|
|
|
'error', |
|
101
|
|
|
$e->getMessage(), |
|
102
|
|
|
['module' => 'users', 'controller' => 'change-email', 'userId' => $userId] |
|
103
|
|
|
); |
|
104
|
|
|
|
|
105
|
|
|
throw new Exception('Unable to send email. Please contact administrator.'); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
// show notification and redirect |
|
109
|
|
|
Messages::addSuccess( |
|
|
|
|
|
|
110
|
|
|
"Your account has been created and an activation link has" . |
|
111
|
|
|
"been sent to the e-mail address you entered.<br/>" . |
|
112
|
|
|
"Note that you must activate the account by clicking on the activation link" . |
|
113
|
|
|
"when you get the e-mail before you can login." |
|
114
|
|
|
); |
|
115
|
|
|
// wtf? |
|
116
|
|
|
// redirectTo('index', 'index'); |
|
|
|
|
|
|
117
|
|
|
|
|
118
|
|
|
return $userId; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
This check looks for function calls that miss required arguments.