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