-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAuthController.php
130 lines (110 loc) · 4.51 KB
/
AuthController.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
<?php
namespace Matchappen\Http\Controllers\Auth;
use Illuminate\Http\Request;
use Matchappen\Http\Requests\StoreWorkplaceRequest;
use Matchappen\Occupation;
use Matchappen\User;
use Matchappen\Workplace;
use Validator;
use Matchappen\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
class AuthController extends Controller
{
/*
|--------------------------------------------------------------------------
| Registration & Login Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users, as well as the
| authentication of existing users. By default, this controller uses
| a simple trait to add these behaviors. Why don't you explore it?
|
*/
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
/**
* Create a new authentication controller instance.
*
* @return void
*/
public function __construct(Request $request)
{
$this->middleware('guest', ['except' => 'getLogout']);
$fields_to_trim = array_merge(
['user.name', 'user.email', 'user.phone'],
array_map(function ($value) {
return 'workplace.' . $value;
}, array_keys(StoreWorkplaceRequest::rulesForCreate()))
);
$this->middleware('reformulator.explode:workplace.occupations', ['only' => 'postRegister']);
$this->middleware('reformulator.trim:' . implode(',', $fields_to_trim), ['only' => 'postRegister']);
$this->middleware('reformulator.strip_repeats:workplace.occupations', ['only' => 'postRegister']);
}
//TODO: make postRegister() redirect to getLogin() withInput if the email exists
//TODO: make postRegister() redirect to getLogin() withInput if user has triggered login action
//TODO: if organization name is taken in postRegister(), link to the organisation for contact details
//TODO: make postLogin() redirect to getRegister() withInput if user has triggered register action
//TODO: make postLogin() redirect to Auth\PasswordController@getEmail withInput if user has triggered forgotten password action
//TODO: make postLogin() redirect to Auth\EmailTokenController@getEmail withInput if login failed and email matches supervisor email pattern
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
$validator = Validator::make($data, []);
foreach (StoreWorkplaceRequest::rulesForCreate() as $attribute => $rules) {
$validator->mergeRules('workplace.' . $attribute, $rules);
}
foreach (User::rulesForCreate() as $attribute => $rules) {
$validator->mergeRules('user.' . $attribute, $rules);
}
// Validates the occupations contains only two words
$validator->after(function ($validator) {
Occupation::validateMax2Words($validator, 'workplace.occupations');
});
return $validator;
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return User
*/
protected function create(array $data)
{
$workplace = Workplace::create(array_filter($data['workplace']));
$user_data = array_filter($data['user']);
$user_data['password'] = bcrypt($user_data['password']);
$user = new User($user_data);
$user->workplace()->associate($workplace);
$user->save();
$occupations = Occupation::getOrCreateFromNames($data['workplace']['occupations'], $user);
$workplace->occupations()->sync($occupations);
\Mail::queue('emails.workplace_registration_admin_notification', compact('workplace'),
function ($message) use ($workplace) {
$message->to(User::getAdminEmails());
$message->subject(trans('workplace.registration_admin_notification_mail_subject',
['workplace' => $workplace->name]));
});
return $user;
}
/**
* Get the path to the login route.
*
* @return string
*/
public function loginPath()
{
return action('Auth\AuthController@getLogin');
}
/**
* @return string url for redirect after successful login or registration
*/
public function redirectPath()
{
return route('dashboard');
}
}