|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Acacha\Users\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Acacha\Users\Events\UserCreated; |
|
6
|
|
|
use Acacha\Users\Events\UserInvited; |
|
7
|
|
|
use Acacha\Users\Http\Requests\CreateUserWithTokenRequest; |
|
8
|
|
|
use Acacha\Users\Http\Requests\SendInvitationRequest; |
|
9
|
|
|
use Acacha\Users\Http\Requests\UpdateInvitationRequest; |
|
10
|
|
|
use Acacha\Users\Models\UserInvitation; |
|
11
|
|
|
use App\User; |
|
12
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
|
13
|
|
|
use Illuminate\Http\Request; |
|
14
|
|
|
use Response; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class UserInvitationsController |
|
18
|
|
|
* |
|
19
|
|
|
* @package Acacha\Users\Http\Controllers |
|
20
|
|
|
*/ |
|
21
|
|
|
class UserInvitationsController extends Controller |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Send Invitation. |
|
25
|
|
|
*/ |
|
26
|
|
|
public function sendInvitation(SendInvitationRequest $request) |
|
27
|
|
|
{ |
|
28
|
|
|
return $this->store($request); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Store a newly created resource in storage. |
|
33
|
|
|
* |
|
34
|
|
|
* @param SendInvitationRequest $request |
|
35
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
36
|
|
|
*/ |
|
37
|
|
|
public function store(SendInvitationRequest $request) |
|
38
|
|
|
{ |
|
39
|
|
|
$invitation = UserInvitation::where(['email' => $request->input('email')])->first(); |
|
40
|
|
|
if (!$invitation) { |
|
41
|
|
|
$invitation = UserInvitation::create($request->only(['email','state','token'])); |
|
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
// NOTE : this method trigger method "created" in UserInvitationObserver. Fire also and event to enable hooking. |
|
45
|
|
|
event(new UserInvited($invitation)); |
|
46
|
|
|
|
|
47
|
|
|
return Response::json(['created' => true ]); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Remove the specified resource from storage. |
|
52
|
|
|
* |
|
53
|
|
|
* @param int $id |
|
54
|
|
|
* @return \Illuminate\Http\Response |
|
55
|
|
|
*/ |
|
56
|
|
|
public function destroy($id) |
|
57
|
|
|
{ |
|
58
|
|
|
$this->authorize('delete-user-invitations'); |
|
59
|
|
|
UserInvitation::destroy($id); |
|
60
|
|
|
|
|
61
|
|
|
// NOTE : this method trigger method "created" in UserInvitationObserver. Fire also and event to enable hooking. |
|
62
|
|
|
// event(new UserInvited($invitation)); |
|
63
|
|
|
|
|
64
|
|
|
return Response::json(['deleted' => true ]); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Display a listing of the resource. |
|
69
|
|
|
* |
|
70
|
|
|
* @return \Illuminate\Http\Response |
|
71
|
|
|
*/ |
|
72
|
|
|
public function index() |
|
73
|
|
|
{ |
|
74
|
|
|
$this->authorize('list-user-invitations'); |
|
75
|
|
|
return UserInvitation::paginate(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Display the specified resource. |
|
80
|
|
|
* |
|
81
|
|
|
* @param int $id |
|
82
|
|
|
* @return \Illuminate\Http\Response |
|
83
|
|
|
*/ |
|
84
|
|
|
public function show($id) |
|
85
|
|
|
{ |
|
86
|
|
|
$this->authorize('view-user-invitations'); |
|
87
|
|
|
return UserInvitation::find($id); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Update the specified resource in storage. |
|
92
|
|
|
* |
|
93
|
|
|
* @param UpdateInvitationRequest $request |
|
94
|
|
|
* @param $id |
|
95
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
96
|
|
|
*/ |
|
97
|
|
|
public function update(UpdateInvitationRequest $request, $id) |
|
98
|
|
|
{ |
|
99
|
|
|
$invitation = UserInvitation::find($id); |
|
100
|
|
|
$invitation->update($request->intersect(['email','state','token'])); |
|
|
|
|
|
|
101
|
|
|
return Response::json(['updated' => true ]); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Accept user invitation. |
|
106
|
|
|
* |
|
107
|
|
|
* @param Request $request |
|
108
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
|
109
|
|
|
*/ |
|
110
|
|
|
public function accept(Request $request) |
|
111
|
|
|
{ |
|
112
|
|
|
if (! $request->has('token')) abort(404); |
|
113
|
|
|
if (! $invitation = $this->validateToken($request->input('token'))) abort(404); |
|
114
|
|
|
return $this->showAcceptUserInvitationForm($invitation); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Process accept user invitation form. |
|
119
|
|
|
* |
|
120
|
|
|
* @param CreateUserWithTokenRequest $request |
|
121
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
122
|
|
|
*/ |
|
123
|
|
|
public function postAccept(CreateUserWithTokenRequest $request) |
|
124
|
|
|
{ |
|
125
|
|
|
if (! $request->has('token')) abort(403); |
|
126
|
|
|
if (! $invitation = $this->validateToken($request->input('token'))) abort(403); |
|
127
|
|
|
|
|
128
|
|
|
$user = User::create([ |
|
129
|
|
|
'name' => $request->input('name'), |
|
130
|
|
|
'email' => $request->input('email'), |
|
131
|
|
|
'password' => bcrypt($request->input('password')), |
|
|
|
|
|
|
132
|
|
|
]); |
|
133
|
|
|
|
|
134
|
|
|
event(new UserCreated($user)); |
|
135
|
|
|
|
|
136
|
|
|
$invitation = UserInvitation::where('token', $request->input('token'))->first(); |
|
137
|
|
|
$invitation->user()->associate($user); |
|
138
|
|
|
$invitation->accept(); |
|
139
|
|
|
|
|
140
|
|
|
return Response::json(['created' => true ]); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Validate token. |
|
145
|
|
|
* |
|
146
|
|
|
* @param $token |
|
147
|
|
|
* @return bool |
|
148
|
|
|
*/ |
|
149
|
|
|
protected function validateToken($token) |
|
150
|
|
|
{ |
|
151
|
|
|
if (!$token) return false; |
|
152
|
|
|
try { |
|
153
|
|
|
$invitation = UserInvitation::where('token', $token) |
|
154
|
|
|
->where('state', 'pending')->first(); |
|
155
|
|
|
if ($invitation) { |
|
156
|
|
|
if ( $invitation->token === $token) return $invitation; |
|
157
|
|
|
} |
|
158
|
|
|
} catch (ModelNotFoundException $e) { |
|
159
|
|
|
return false; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
return false; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Show accept user invitation form. |
|
167
|
|
|
* |
|
168
|
|
|
* @param $invitation |
|
169
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
|
170
|
|
|
*/ |
|
171
|
|
|
protected function showAcceptUserInvitationForm($invitation) |
|
172
|
|
|
{ |
|
173
|
|
|
$data = [ |
|
174
|
|
|
'email' => $invitation->email, |
|
175
|
|
|
'token' => $invitation->token |
|
176
|
|
|
]; |
|
177
|
|
|
return view('acacha_users::accept-invitation',$data); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Show the management user invitations page. |
|
182
|
|
|
* |
|
183
|
|
|
* @return Response |
|
184
|
|
|
*/ |
|
185
|
|
|
public function userInvitations() |
|
186
|
|
|
{ |
|
187
|
|
|
$this->authorize('see-manage-user-invitations-view'); |
|
188
|
|
|
return view('acacha_users::management-invitations'); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Show the user invitations public page. |
|
193
|
|
|
* |
|
194
|
|
|
* @return Response |
|
195
|
|
|
*/ |
|
196
|
|
|
public function invite() |
|
197
|
|
|
{ |
|
198
|
|
|
if (!config('users.users_can_invite_other_users')) abort(404); |
|
199
|
|
|
return view('acacha_users::invite-user'); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
} |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.