/
UserController.php
executable file
·388 lines (372 loc) · 10.4 KB
/
UserController.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
<?php
namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Auth;
use Session;
use Illuminate\Http\Request;
use Illuminate\Http\RedirectResponse;
use Redirect;
use Socialize;
use App\User;
use App\Role;
use Hash;
use Input;
use Mail;
use URL;
use App\Post;
use App\Like;
use App\Category;
use Validator;
use App\Library\LibraryPublic;
use File;
class UserController extends Controller {
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct ()
{
$this->middleware('auth', ['only' => 'getProfile']);
$this->middleware('admin', ['only' => ['getCreate', 'postCreate']]);
}
/**
* get Login function
*
* @author Tran Van Moi <[moitran92@gmail.com]>
* @since 2015/05/12
* @return Response
*/
public function getLogin ()
{
/*
* Delete all session
* Session::flush();
*/
if (Auth::check())
return redirect('/');
return view('user/login');
}
/**
* post Login function
*
* @author Tran Van Moi <[moitran92@gmail.com]>
* @since 2015/05/15
* @return Response
*/
public function postLogin ()
{
$data = Input::all();
$validator = User::validate($data);
if ($validator->fails()) {
// If validation failed redirect back to login.
return Redirect::to('user/login')->withInput()->withErrors($validator);
} else {
$userdata = array(
'email' => Input::get('email'),
'password' => Input::get('password'));
/*
* login by email or name
* $field = filter_var($usernameinput, FILTER_VALIDATE_EMAIL) ?
* 'email' : 'name';
* if (Auth::attempt(array($field => $usernameinput, 'password' =>
* $password))){
*/
if (Auth::attempt($userdata)) {
return redirect('/');
} else {
// Session::flash('error', 'Username or password is not
// correct');
return redirect('user/login')->withInput()->with('error', 'Username or password is not correct');
}
}
}
/**
* Facebook redirect function
*
* @author Tran Van Moi <[moitran92@gmail.com]>
* @since 2015/05/13
* @return redirect to login
*/
public function getFacebookredirect ()
{
return Socialize::with('facebook')->redirect();
}
/**
* Google redirect function
*
* @author Tran Van Moi <[moitran92@gmail.com]>
* @since 2015/05/13
* @return redirect to login
*/
public function getGoogleredirect ()
{
return Socialize::with('google')->redirect();
}
/**
* Login with facebook function
*
* @author Tran Van Moi <[moitran92@gmail.com]>
* @since 2015/05/12
* @return Response
*/
public function getLoginfb ()
{
try {
$user_fb = Socialize::with('facebook')->user();
$user = User::whereEmail($user_fb['email'])->first();
// Create account, login and send new account to user
if (! $user) {
$user_fb['image'] = $user_fb->avatar;
$password = str_random(8);
$user_fb['password'] = $password;
$user = User::create_user($user_fb, env('USER'));
$data = array('email' => $user->email, 'password' => $password);
LibraryPublic::send_mail_res($data);
}
// Create session
Auth::login($user);
if (filter_var($user->image, FILTER_VALIDATE_URL) === true){
$user->image = $user_fb->avatar;
$user->save();
}
return redirect('/');
} catch (Exception $e) {
return redirect('user/login')->withInput()->with('error', 'Login with facebook failed! Please try again!');
}
}
/**
* Login with google function
*
* @author Tran Van Moi <[moitran92@gmail.com]>
* @since 2015/05/12
* @return Response
*/
public function getLogingg ()
{
try {
$user_gg = Socialize::with('google')->user();
$user = User::whereEmail($user_gg->email)->first();
// Create account, login and send new account to user
if (! $user) {
$password = str_random(8);
$user_data = [
'id' => $user_gg->id,
'name' => $user_gg->name,
'email' => $user_gg->email,
'password' => $password,
'image' => $user_gg->avatar];
$user = User::create_user($user_data, env('USER'));
LibraryPublic::send_mail_res($user_data);
}
// Create session
Auth::login($user);
if (filter_var($user->image, FILTER_VALIDATE_URL) === true){
$user->image = $user_gg->avatar;
$user->save();
}
return redirect('/');
} catch (Exception $e) {
return redirect('user/login')->withInput()->with('error', 'Login with google failed! Please try again!');
}
}
/**
* Register function
*
* @author Tran Van Moi <[moitran92@gmail.com]>
* @since 2015/05/12
* @return Response
*/
public function getRegister ()
{
return view('user/register');
}
/**
* post Register function
*
* @author Tran Van Moi <[moitran92@gmail.com]>
* @since 2015/05/12
* @return Response
*/
public function postRegister ()
{
$data = Input::all();
$validator = User::validate($data, env('FACTORY_REGISTER'));
if ($validator->fails()) {
// If validation failed redirect back to login.
return redirect('user/register')->withInput()->withErrors($validator);
} else {
$userdata = array(
'name' => Input::get('name'),
'email' => Input::get('email'),
'password' => Input::get('password'),
'image' => 'default.jpg');
$role_name = Input::get('is_owner') === 'on' ? env('OWNER') : env('USER');
$user = User::create_user($userdata, $role_name);
LibraryPublic::send_mail_res($userdata);
if (count($user)) {
return redirect('user/register')->withInput()->with('register_status',
[
'status' => 'success',
'message' => 'Register user is success, You can login now!']);
} else
return redirect('user/register')->withInput()->with('register_status',
[
'status' => 'danger',
'message' => 'Register user is failed, Please try again!']);
}
}
/**
* Logout function
*
* @author Tran Van Moi <[moitran92@gmail.com]>
* @since 2015/05/12
* @return Response
*/
public function getLogout ()
{
Auth::logout();
return redirect('home');
}
/**
* Profile function
*
* @author Tran Van Moi <[moitran92@gmail.com]>
* @since 2015/05/12
* @return Response
*/
public function getProfile ($category_id = null)
{
$category_id = trim($category_id);
$url_image = "";
if (filter_var(Auth::user()->image, FILTER_VALIDATE_URL) === false)
$url_image = URL::to('/') . "/public/images/avatar/" . Auth::user()->image;
else
$url_image = Auth::user()->image;
Session::put('url_image_auth', $url_image);
$keyword = trim(Input::get('keyword'));
$data['keyword'] = $keyword;
$rules = ['keyword' => 'max:150|min:1'];
$validator = Validator::make($data, $rules);
if ($validator->fails()){
$data['keyword'] = "";
$posts = Post::get_all_posts($category_id, "", Auth::user()->id);
}
else
$posts = Post::get_all_posts($category_id, $keyword, Auth::user()->id);
$data['categories'] = Category::all();
$data['posts'] = $posts;
$data['total_like'] = Like::total_like_user(Auth::user()->id);
$data['categories'] = Category::all();
return view('user/profile', $data);
}
/**
* Logout function
*
* @author Tran Van Moi <[moitran92@gmail.com]>
* @since 2015/05/12
* @return Response
*/
public function getForgot ()
{
return view('user/forgot');
}
/**
* View profile of other user function
*
* @author Tran Van Moi <[moitran92@gmail.com]>
* @since 2015/05/12
* @return Response
*/
public function getView($user_id = null, $category_id = null){
$keyword = trim(Input::get('keyword'));
$category_id = trim($category_id);
$data = ['user_id' => $user_id];
$rules = ['user_id' => 'required|exists:users,id'];
$validator = Validator::make($data, $rules);
if ($validator->fails()) {
Session::flash('view_user_error','User is not found!');
} else {
if($user_id == Auth::user()->id)
return redirect('user/profile');
$check_user_exist = User::whereDelete_status(0)->whereId($user_id)->first();
if ($check_user_exist) {
$data['keyword'] = $keyword;
$rules = ['keyword' => 'max:150|min:1'];
$validator = Validator::make($data, $rules);
if ($validator->fails()){
$data['keyword'] = "";
$data['posts'] = Post::get_all_posts($category_id, "", $user_id);
}
else
$data['posts'] = Post::get_all_posts($category_id, $keyword, $user_id);
$data['categories'] = Category::all();
if(filter_var($check_user_exist->image, FILTER_VALIDATE_URL) === false)
$check_user_exist->image = URL::to('/') . "/public/images/avatar/" . $check_user_exist->image;
$data['user_info'] = $check_user_exist;
$data['total_like'] = Like::total_like_user($user_id);
} else
Session::flash('view_user_error','User is not found!');
}
return view('user/view', $data);
}
/**
* Function setting account of user
* @method GET
* @author Tran Van Moi
* @since 2015/05/20
* @return response
*/
public function getSetting(){
return view('user/setting');
}
/**
* Function setting account of user
* @method POST
* @author Tran Van Moi
* @since 2015/05/20
* @return response
*/
public function postSetting(){
$data = Input::all();
$validator = User::validate($data, 'edit');
if($validator->fails()){
return redirect('user/setting')->withInput()->withErrors($validator);
}
else {
if(Hash::check($data['current_password'], Auth::user()->password)){
$user = Auth::user();
$user->name = $data['name'];
$user->address = $data['address'];
$user->birthday = $data['birthday'];
if(Input::file('image')){
$destination_path = './public/images/avatar/'; // upload path
$extension = Input::file('image')->getClientOriginalExtension(); // getting image extension
$file_name = str_random(8).'.'.$extension; // renameing image
if(Input::file('image')->move($destination_path, $file_name)){
if ($user->image != "default.jpg" && File::exists($destination_path.$user->image))
File::delete($destination_path.$user->image);
$user->image = $file_name;
}
}
if($data['password'] != "" && $data['password'] == $data['password_confirmation']){
$user->password = Hash::make($data['password']);
}
$user->save();
$url_image = LibraryPublic::get_url_image($user->image);
Session::put('url_image_auth', $url_image);
return redirect('user/setting')->withInput()->with('setting_status',
[
'status' => 'success',
'message' => 'Setting account is success!']);
}
else{
return redirect('user/setting')->withInput()->with('setting_status',
[
'status' => 'danger',
'message' => 'Password is not correct']);
}
}
}
}