1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Icybee\Modules\Users\NonceLogin;
13
14 use ICanBoogie\DateTime;
15 use ICanBoogie\PermissionRequired;
16 use ICanBoogie\HTTP\Request;
17
18 19 20 21 22 23 24
25 class NonceLoginRequestOperation extends \ICanBoogie\Operation
26 {
27 28 29
30 public function __construct($request=null)
31 {
32 global $core;
33
34 parent::__construct($request);
35
36 $this->module = $core->modules['users.noncelogin'];
37 }
38
39 40 41 42 43
44 protected function lazy_get_record()
45 {
46 global $core;
47
48 $email = $this->request['email'];
49
50 if (!$email)
51 {
52 return;
53 }
54
55
56
57 $record = $core->models['users']->filter_by_email($email)->one;
58
59 if ($record && $record->constructor != 'users')
60 {
61 $record = $core->models[$record->constructor][$record->uid];
62 }
63
64 return $record;
65 }
66
67 68 69 70 71
72 protected function get_user()
73 {
74 return $this->record;
75 }
76
77 private $ticket;
78
79 80 81 82 83
84 protected function get_ticket()
85 {
86 return $this->ticket;
87 }
88
89 protected function validate(\ICanboogie\Errors $errors)
90 {
91 global $core;
92
93 $email = $this->request['email'];
94
95 if (!$email)
96 {
97 $errors['email'] = $errors->format('The field %field is required!', array('%field' => 'Votre adresse E-Mail'));
98
99 return false;
100 }
101
102 if (!filter_var($email, FILTER_VALIDATE_EMAIL))
103 {
104 $errors['email'] = $errors->format("Invalid email address: %email.", array('%email' => $email));
105
106 return false;
107 }
108
109 $user = $this->record;
110
111 if (!$user)
112 {
113 $errors['email'] = $errors->format("Unknown email address.");
114
115 return false;
116 }
117
118 if ($user->language)
119 {
120 $core->locale = $user->language;
121 }
122
123 $expire_at = null;
124 $ticket = $this->module->model->filter_by_uid($user->uid)->one;
125
126 if ($ticket)
127 {
128 $expire_at = $ticket->expire_at;
129 }
130
131 if ($expire_at && (time() + Module::FRESH_PERIOD - $expire_at->timestamp < Module::COOLOFF_DELAY))
132 {
133 throw new PermissionRequired
134 (
135 $errors->format("nonce_login_request.operation.already_sent", array
136 (
137 ':time' => DateTime::from('@' . ($expire_at->timestamp - Module::FRESH_PERIOD + Module::COOLOFF_DELAY), 'utc')->local->format('H:i')
138 )),
139
140 403
141 );
142 }
143
144 return true;
145 }
146
147 148 149 150 151
152 protected function process()
153 {
154 global $core;
155
156 $user = $this->record;
157 $model = $this->module->model;
158
159
160
161 $model->filter_by_uid($user->uid)->delete();
162
163
164
165 $ticket = Ticket::from(array(
166
167 'uid' => $user->uid,
168 'token' => $model->generate_token(),
169 'expire_at' => '+' . Module::FRESH_PERIOD . ' seconds',
170 'ip' => $this->request->ip
171
172 ));
173
174 $ticket->save();
175
176 $this->ticket = $ticket;
177 $this->response->message = $errors->format('success', array('%email' => $user->email), array('scope' => \ICanBoogie\normalize($user->constructor, '_') . '.nonce_login_request.operation'));
178
179 return true;
180 }
181 }