-
Notifications
You must be signed in to change notification settings - Fork 2
/
user.php
299 lines (264 loc) · 6.82 KB
/
user.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php
class Api_User_Controller extends Api_Controller {
/**
* Controller is RESTful.
*
* @var bool
*/
public $restful = true;
public function __construct()
{
parent::__construct();
$this->response = array();
$this->internal = false;
$this->validate_id = null;
}
/**
* Retrieve user data
*
* @param integer $id
* @return Response
*/
public function get_data($id = null)
{
if($id)
{
$user = User::find($id);
$this->response['user_data'] = $user;
}
else
{
$response['error'] = array(
'type' => 'missing_data',
'message' => 'No such id',
);
}
return Response::json($this->response, $this->status);
}
/**
* Register new user
*
* @return Response
*/
public function post_index()
{
$user = Input::all();
/**
* I need my precious device_id, can't live without it!
*/
if (Device::where('device_id', '=', $user['device_id'])->first())
{
/**
* Check if there isn't any existing user with that mail
*/
if ( ! User::where('email', '=', $user['email'])->first())
{
$new = new User;
$new->email = $user['email'];
$new->name = $user['name'];
/*
* Check for facebook token, and if not, for password
*/
if(isset($user['token_facebook']))
{
$new->token_facebook = $user['token_facebook'];
$new->password = null;
}
else
{
$new->password = Hash::make($user['password']);
}
/*
* Create user
*/
if($new->save())
{
$user['id'] = $new->attributes['id'];
$user['activation_hash'] = Str::random(30);
$this->response = array(
'user_id' => $user['id'],
);
if ( ! isset($user['token_facebook']))
{
$user_activation = new Activation;
$user_activation->user_id = $user['id'];
$user_activation->hash = $user['activation_hash'];
if ($user_activation->save())
{
if( ! Device::update($user['device_id'], array('user_id'=>$user['id'])))
{
$this->response['error'] = array(
'type' => 'db_insert_error',
'message' => 'Can\'t assign user_id to device',
);
}
Session::put('user', $user);
Message::send(function($mail)
{
$user = Session::get('user');
$mail->to($user['email']);
$mail->from(Config::get('lrapi_config.mail.email', null), Config::get('lrapi_config.mail.name', null));
$mail->subject('Confirm user registration');
$mail->body('view: response.default');
$mail->body->name = $user['name'];
$mail->body->link = URL::to("activate/user/{$user['id']}/{$user['activation_hash']}");
$mail->html(true);
Session::forget('user');
});
if( ! Message::was_sent())
{
$this->response['error'] = array(
'type' => 'internal',
'message' => 'Couldn\'t send the confirmation email'
);
}
}
else
{
$this->response['error'] = array(
'type' => 'db_insert_error',
'message' => 'Couldn\'t generate the confirmation parameters for the account',
);
}
}
else
{
if ( ! User::where_id($user['id'])->update(array('active'=>1)))
{
$this->response['error'] = array(
'type' => 'db_insert_error',
'message' => 'Account couldn\'t be activated, please try ',
);
}
}
}
else
{
$this->response['error'] = array(
'type' => 'db_insert_error',
'message' => 'There was an error while creating the account, please try again!',
);
}
}
else
{
$this->response['error'] = array(
'type' => 'register_failed',
'message' => 'There is already a user registered, with those credentials.',
);
}
}
else
{
$this->response['error'] = array(
'type' => 'register_failed',
'message' => 'No such device id registered',
);
}
return Response::json($this->response);
}
/**
* Check user's credentials on login
*
* @return Response
*/
public function post_login()
{
$user = Input::all();
if ($id = User::where_email($user['email'])->only('id'))
{
$this->internal = true;
$this->validate_id = $id;
if ($this->post_validate())
{
$this->response = array('user_id'=>$id);
if(isset($user['token_facebook']) && ! isset($user['password']))
{
$attempt = User::where('email', '=', $user['email'])
->where('token_facebook', '=', $user['token_facebook'])
->first();
}
else
{
$attempt = Auth::attempt(
array(
'username' => $user['email'],
'password' => $user['password'],
)
);
}
if( ! $attempt)
{
$this->response['error'] = array(
'type' => 'login_failed',
'message' => 'Password or Email, invalid!',
);
}
}
else
{
$this->response['error'] = array(
'type' => 'login_failed',
'message' => 'Please activate your account before trying to log in',
);
}
}
else
{
$this->response['error'] = array(
'type' => 'login_failed',
'message' => 'Password or Email, invalid!',
);
}
return Response::json($this->response);
}
public function post_validate()
{
$input = Input::all();
$id = isset($input['user_id']) ? $input['user_id'] : $this->validate_id;
if (( ! empty($id)) && (is_numeric($id)))
{
if( ! User::where_id($id)->only('active'))
{
if($this->internal)
return false;
else
$this->response['error'] = array(
'type' => 'login_failed',
'message' => 'Please activate your account before trying to log in',
);
return Response::json($this->response);
}
else
{
if($this->internal)
return true;
else
return Response::json($this->response, 200);
}
}
else
{
die(Response::json(array('message'=>'invalid id')));
}
}
public function post_logout()
{
$input = Input::all();
$device = Device::find($input['device_id']);
$device->api_key = Str::random(64);
$device->api_token = Str::random(128);
if ($device->save())
{
$this->response = array();
}
else
{
$this->response['error'] = array(
'type' => 'db_insert_error',
'message' => 'Error regenerating keys for the device',
);
}
return Response::json($this->response);
}
}