1
|
|
|
<?php namespace App\Http\Controllers; |
2
|
|
|
|
3
|
|
|
use App\Exceptions\RepositoryException; |
4
|
|
|
use App\Exceptions\ValidationException; |
5
|
|
|
use App\Repositories\ConnectRepository; |
6
|
|
|
use App\Repositories\UserRepository; |
7
|
|
|
use Input; |
8
|
|
|
|
9
|
|
|
class UsersController extends Controller { |
10
|
|
|
|
11
|
|
|
private $userRepository; |
12
|
|
|
private $connectRepository; |
13
|
|
|
|
14
|
|
|
public function __construct(UserRepository $repository, ConnectRepository $connectRepository) |
15
|
|
|
{ |
16
|
|
|
$this->userRepository = $repository; |
17
|
|
|
$this->connectRepository = $connectRepository; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
View Code Duplication |
public function getActiveUsers() |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
$allUsers = $this->userRepository->allByStatus(true); |
23
|
|
|
$otherUsersCount = count($this->userRepository->allByStatus(false)); |
24
|
|
|
|
25
|
|
|
return view('users.app')->with('users', $allUsers) |
26
|
|
|
->with('roles', $this->getRoleNames()) |
27
|
|
|
->with('isActive', true) |
28
|
|
|
->with('otherUsersCount', $otherUsersCount); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
View Code Duplication |
public function getDisabledUsers() |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
$allUsers = $this->userRepository->allByStatus(false); |
34
|
|
|
$otherUsersCount = count($this->userRepository->allByStatus(true)); |
35
|
|
|
|
36
|
|
|
return view('users.app')->with('users', $allUsers) |
37
|
|
|
->with('roles', $this->getRoleNames()) |
38
|
|
|
->with('isActive', false) |
39
|
|
|
->with('otherUsersCount', $otherUsersCount); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function showConnections($userId) |
43
|
|
|
{ |
44
|
|
|
$user = $this->userRepository->get($userId); |
45
|
|
|
$connections = $this->connectRepository->forUser($userId); |
46
|
|
|
|
47
|
|
|
return view('users.history')->with('user', $user) |
48
|
|
|
->with('connections', $connections); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function getRegisterForm() |
52
|
|
|
{ |
53
|
|
|
return view('users.register'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
View Code Duplication |
public function registerNewUser() |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
try { |
59
|
|
|
$user = $this->userRepository->store(Input::all()); |
60
|
|
|
} |
61
|
|
|
catch(ValidationException $e) { |
62
|
|
|
return redirect('app/users/register')->withInput() |
63
|
|
|
->with('error', $e->getMessageBag()->first()); |
64
|
|
|
} |
65
|
|
|
catch (RepositoryException $e) { |
66
|
|
|
return redirect('app/users/register')->with('error', 'Could not add new user: '.strtolower($e->getMessage())) |
67
|
|
|
->withInput(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return redirect('app/users')->with('success', $user->firstname.' account added'); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function changeAccountStatus($userId) |
74
|
|
|
{ |
75
|
|
|
try { |
76
|
|
|
$user = $this->userRepository->changeStatus($userId); |
77
|
|
|
} |
78
|
|
|
catch(RepositoryException $e) { |
79
|
|
|
return redirect('app/users')->with('error', 'Could not change account status'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if($user->is_active) |
83
|
|
|
return redirect('app/users/disabled')->with('success', 'User <b>'.$user->firstname.'</b> enabled'); |
84
|
|
|
else |
85
|
|
|
return redirect('app/users')->with('success', 'User <b>'.$user->firstname.'</b> disabled'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function changeAccountRole($userId) |
89
|
|
|
{ |
90
|
|
|
try { |
91
|
|
|
$user = $this->userRepository->changeRole($userId); |
92
|
|
|
} |
93
|
|
|
catch(RepositoryException $e) { |
94
|
|
|
return redirect('app/users')->with('error', 'Could not change user role'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$redirectUrl = 'app/users'; |
98
|
|
|
if( !$user->is_active ) |
99
|
|
|
$redirectUrl .= '/disabled'; |
100
|
|
|
|
101
|
|
|
$roleNames = ['ADMN'=>'administrator','MNGR'=>'manager','USER'=>'user']; |
102
|
|
|
$roleName = $roleNames[$user->role]; |
103
|
|
|
|
104
|
|
|
return redirect($redirectUrl)->with('success', 'User <b>'.$user->firstname.'</b> is now <i>'.$roleName.'</i>'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function deleteUser($userId) |
108
|
|
|
{ |
109
|
|
|
try { |
110
|
|
|
$user = $this->userRepository->softDelete($userId); |
111
|
|
|
} |
112
|
|
|
catch(RepositoryException $e) { |
113
|
|
|
return redirect('app/users')->with('error', 'User could not be deleted: '.$e->getMessage()); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return redirect('app/users')->with('success', 'User <b>'.$user->firstname.'</b> deleted'); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
private function getRoleNames() |
120
|
|
|
{ |
121
|
|
|
return ['ADMN'=>'Administrator','MNGR'=>'Manager','USER'=>'User']; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.