1
|
|
|
<?php namespace Usman\Guardian\Http\Controllers; |
2
|
|
|
|
3
|
|
|
use View; |
4
|
|
|
use Input; |
5
|
|
|
use Redirect; |
6
|
|
|
use Request; |
7
|
|
|
use Usman\Guardian\Validators\UserValidator; |
8
|
|
|
use Usman\Guardian\Validators\Exceptions\ValidationException; |
9
|
|
|
use Usman\Guardian\Repositories\Interfaces\UserRepositoryInterface; |
10
|
|
|
|
11
|
|
|
class Users extends Base { |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* The repository instance |
15
|
|
|
* |
16
|
|
|
* @var Usman\Guardian\Repositories\UserRepository |
17
|
|
|
*/ |
18
|
|
|
protected $user; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The validator instance |
22
|
|
|
* |
23
|
|
|
* @var Usman\Guardian\Validators\UserValidator |
24
|
|
|
*/ |
25
|
|
|
protected $validator; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Creates a new User controller |
29
|
|
|
* |
30
|
|
|
* @param UserRepositoryInterface $user |
31
|
|
|
* @param UserValidator $validator |
32
|
|
|
*/ |
33
|
|
|
public function __construct(UserRepositoryInterface $user, UserValidator $validator) |
34
|
|
|
{ |
35
|
|
|
$this->user = $user; |
|
|
|
|
36
|
|
|
$this->validator = $validator; |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Lists ths users. |
41
|
|
|
* |
42
|
|
|
* @return Response |
43
|
|
|
*/ |
44
|
|
|
public function listUser() |
45
|
|
|
{ |
46
|
|
|
//we will check if a form is submitted with either |
47
|
|
|
//a role name or a username as a search term. |
48
|
|
|
if(Input::has('role') or Input::has('username')) |
49
|
|
|
{ |
50
|
|
|
extract(Input::only(['role','username'])); |
|
|
|
|
51
|
|
|
$users = $this->user->searchUserByRole($username,$role); |
52
|
|
|
$users->appends(compact('role','username')); |
53
|
|
|
return View::make('guardian::partials.user.list')->with('users',$users); |
54
|
|
|
}//otherwise we will display the default users list |
55
|
|
|
else |
56
|
|
|
{ |
57
|
|
|
$users = $this->user->getByPageWith('roles'); |
58
|
|
|
return View::make('guardian::partials.user.list',compact('users')); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Shows the add user form |
64
|
|
|
* |
65
|
|
|
* @return Response |
66
|
|
|
*/ |
67
|
|
|
public function addUser() |
68
|
|
|
{ |
69
|
|
|
return View::make('guardian::partials.user.add'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Saves the new user in the database. |
74
|
|
|
* |
75
|
|
|
* @return Response |
76
|
|
|
*/ |
77
|
|
|
public function createUser() |
78
|
|
|
{ |
79
|
|
|
try |
80
|
|
|
{ |
81
|
|
|
$this->validator->setFields(Input::all())->validate('create'); |
82
|
|
|
$id = $this->user->create(Input::all()); |
83
|
|
|
if(Input::has('roles')) |
84
|
|
|
{ |
85
|
|
|
$this->user->attach($id, Input::get('roles'),'roles'); |
86
|
|
|
} |
87
|
|
|
return Redirect::back()->withSuccess('User Created Successfully'); |
88
|
|
|
} |
89
|
|
|
catch (ValidationException $e) |
90
|
|
|
{ |
91
|
|
|
$errors = $e->getValidationErrors(); |
92
|
|
|
return Redirect::back()->withErrors($errors)->withInput(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Shows the user edit form. |
99
|
|
|
* |
100
|
|
|
* @param int $id |
101
|
|
|
* @return Response |
102
|
|
|
*/ |
103
|
|
|
public function editUser($id) |
104
|
|
|
{ |
105
|
|
|
$user = $this->user->findByIdWith($id,'roles'); |
106
|
|
|
return View::make('guardian::partials.user.edit')->with('user',$user); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Updates the database record for the user. |
111
|
|
|
* |
112
|
|
|
* @param int $id |
113
|
|
|
* @return Response |
114
|
|
|
*/ |
115
|
|
|
public function updateUser($id) |
116
|
|
|
{ |
117
|
|
|
try |
118
|
|
|
{ |
119
|
|
|
$this->validator->addRule('update','username','required|alpha_num|unique:users,username,'.$id); |
120
|
|
|
$this->validator->setFields(Input::all())->validate('update'); |
121
|
|
|
$this->user->update($id, Input::all()); |
122
|
|
|
$this->user->attach($id, Input::get('roles',[]),'roles'); |
123
|
|
|
return Redirect::route('user.list',Input::get('ref'))->withSuccess('User Updated Successfully'); |
124
|
|
|
} |
125
|
|
|
catch(ValidationException $e) |
126
|
|
|
{ |
127
|
|
|
$errors = $e->getValidationErrors(); |
128
|
|
|
return Redirect::back()->withErrors($errors)->withInput(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Deletes a user from the database. |
135
|
|
|
* |
136
|
|
|
* @param int $id |
137
|
|
|
* @return Response |
138
|
|
|
*/ |
139
|
|
|
public function deleteUser($id) |
140
|
|
|
{ |
141
|
|
|
$this->user->deleteWith($id,['roles']); |
142
|
|
|
return Redirect::back()->withSuccess('Deleted Successfully!'); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..