|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace plunner\Http\Controllers\Employees\Auth; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
|
6
|
|
|
use plunner\Company; |
|
7
|
|
|
use plunner\employee; |
|
8
|
|
|
use plunner\Http\Controllers\Controller; |
|
9
|
|
|
use Tymon\JWTAuth\Support\auth\AuthenticatesAndRegistersUsers; |
|
10
|
|
|
use Tymon\JWTAuth\Support\auth\ThrottlesLogins; |
|
11
|
|
|
use Validator; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class AuthController |
|
15
|
|
|
* @package plunner\Http\Controllers\Employees\Auth |
|
16
|
|
|
* @author Claudio Cardinale <[email protected]> |
|
17
|
|
|
* @copyright 2015 Claudio Cardinale |
|
18
|
|
|
* @version 1.0.0 |
|
19
|
|
|
*/ |
|
20
|
|
|
class AuthController extends Controller |
|
21
|
|
|
{ |
|
22
|
|
|
/* |
|
23
|
|
|
|-------------------------------------------------------------------------- |
|
24
|
|
|
| Registration & Login Controller |
|
25
|
|
|
|-------------------------------------------------------------------------- |
|
26
|
|
|
| |
|
27
|
|
|
| This controller handles the registration of new users, as well as the |
|
28
|
|
|
| authentication of existing users. By default, this controller uses |
|
29
|
|
|
| a simple trait to add these behaviors. Why don't you explore it? |
|
30
|
|
|
| |
|
31
|
|
|
*/ |
|
32
|
|
|
|
|
33
|
|
|
use AuthenticatesAndRegistersUsers { |
|
34
|
|
|
postRegister as postRegisterOriginal; |
|
35
|
|
|
postLogin as postLoginOriginal; |
|
36
|
|
|
} |
|
37
|
|
|
use ThrottlesLogins; |
|
38
|
|
|
|
|
39
|
|
|
protected $redirectPath = "/"; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* en = employee normal |
|
43
|
|
|
* @var array |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $custom = ['mode' => 'en']; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* unique identifiers of the user |
|
49
|
|
|
* @var array |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $username = ['company_id', 'email']; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var company |
|
55
|
|
|
*/ |
|
56
|
|
|
private $company = null; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Create a new authentication controller instance. |
|
60
|
|
|
* |
|
61
|
|
|
*/ |
|
62
|
15 |
|
public function __construct() |
|
63
|
|
|
{ |
|
64
|
15 |
|
config(['auth.model' => \plunner\Employee::class]); |
|
65
|
15 |
|
config(['jwt.user' => \plunner\Employee::class]); |
|
66
|
15 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function postRegister(Request $request) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->validate($request, ['company' => 'required|exists:companies,name']); |
|
71
|
|
|
$this->company = Company::whereName($request->input('company'))->firstOrFail(); |
|
72
|
|
|
return $this->postRegisterOriginal($request); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
15 |
|
public function postLogin(Request $request) |
|
76
|
|
|
{ |
|
77
|
|
|
//get company ID and impiled it in the request |
|
78
|
15 |
|
$this->validate($request, ['company' => 'required|exists:companies,name']); |
|
79
|
15 |
|
$this->company = Company::whereName($request->input('company'))->firstOrFail(); |
|
80
|
15 |
|
$request->merge(['company_id' => $this->company->id]); |
|
81
|
|
|
|
|
82
|
|
|
//remember me |
|
83
|
15 |
|
$this->validate($request, ['remember' => 'required|boolean']); |
|
84
|
15 |
|
if ($request->input('remember', false)) { |
|
|
|
|
|
|
85
|
|
|
config(['jwt.ttl' => '43200']); //30 days |
|
86
|
|
|
$this->custom = array_merge($this->custom, ['remember' => 'true']); |
|
87
|
|
|
} else |
|
88
|
15 |
|
$this->custom = array_merge($this->custom, ['remember' => 'false']); |
|
89
|
|
|
|
|
90
|
15 |
|
return $this->postLoginOriginal($request); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Get a validator for an incoming registration request. |
|
95
|
|
|
* |
|
96
|
|
|
* @param array $data |
|
97
|
|
|
* @return \Illuminate\Contracts\Validation\Validator |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function validator(array $data) |
|
100
|
|
|
{ |
|
101
|
|
|
return Validator::make($data, [ |
|
102
|
|
|
'name' => 'required|min:1|max:255', |
|
103
|
|
|
'email' => 'required|email|max:255|unique:employees,email,NULL,id,company_id,' . $this->company->id, |
|
104
|
|
|
'password' => 'required|confirmed|min:6', |
|
105
|
|
|
'token' => 'sometimes|filled|max:255', |
|
106
|
|
|
]); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Create a new user instance after a valid registration. |
|
111
|
|
|
* |
|
112
|
|
|
* @param array $data |
|
113
|
|
|
* @return Company |
|
114
|
|
|
*/ |
|
115
|
|
|
protected function create(array $data) |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->company->save(new employee([ |
|
118
|
|
|
'name' => $data['name'], |
|
119
|
|
|
'email' => $data['email'], |
|
120
|
|
|
'password' => bcrypt($data['password']), |
|
121
|
|
|
])); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: