|
1
|
|
|
<?php namespace VojtaSvoboda\CodeLogin\Components; |
|
2
|
|
|
|
|
3
|
|
|
use Auth; |
|
4
|
|
|
use Event; |
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Flash; |
|
7
|
|
|
use Input; |
|
8
|
|
|
use Redirect; |
|
9
|
|
|
use Request; |
|
10
|
|
|
use Validator; |
|
11
|
|
|
use ValidationException; |
|
12
|
|
|
use ApplicationException; |
|
13
|
|
|
use Cms\Classes\Page; |
|
14
|
|
|
use Cms\Classes\ComponentBase; |
|
15
|
|
|
use VojtaSvoboda\CodeLogin\Repositories\UserRepository; |
|
16
|
|
|
|
|
17
|
|
|
class CodeLogin extends ComponentBase |
|
18
|
|
|
{ |
|
19
|
|
|
public function componentDetails() |
|
20
|
|
|
{ |
|
21
|
|
|
return [ |
|
22
|
|
|
'name' => 'vojtasvoboda.codelogin::lang.logincomponent.name', |
|
23
|
|
|
'description' => 'vojtasvoboda.codelogin::lang.logincomponent.description' |
|
24
|
|
|
]; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function defineProperties() |
|
28
|
|
|
{ |
|
29
|
|
|
return [ |
|
30
|
|
|
'redirect' => [ |
|
31
|
|
|
'title' => 'vojtasvoboda.codelogin::lang.logincomponent.redirect.title', |
|
32
|
|
|
'description' => 'vojtasvoboda.codelogin::lang.logincomponent.redirect.description', |
|
33
|
|
|
'type' => 'dropdown', |
|
34
|
|
|
'default' => '' |
|
35
|
|
|
], |
|
36
|
|
|
'visible' => [ |
|
37
|
|
|
'title' => 'vojtasvoboda.codelogin::lang.logincomponent.visible.title', |
|
38
|
|
|
'description' => 'vojtasvoboda.codelogin::lang.logincomponent.visible.description', |
|
39
|
|
|
'type' => 'checkbox', |
|
40
|
|
|
'default' => false |
|
41
|
|
|
], |
|
42
|
|
|
'button' => [ |
|
43
|
|
|
'title' => 'vojtasvoboda.codelogin::lang.logincomponent.button.title', |
|
44
|
|
|
'description' => 'vojtasvoboda.codelogin::lang.logincomponent.button.description', |
|
45
|
|
|
'type' => 'string', |
|
46
|
|
|
'default' => 'enter' |
|
47
|
|
|
] |
|
48
|
|
|
]; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function getRedirectOptions() |
|
52
|
|
|
{ |
|
53
|
|
|
$default = [ |
|
54
|
|
|
'' => '- none -' |
|
55
|
|
|
]; |
|
56
|
|
|
$pages = Page::sortBy('baseFileName')->lists('baseFileName', 'baseFileName'); |
|
57
|
|
|
|
|
58
|
|
|
return $default + $pages; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Executed when this component is bound to a page or layout. |
|
63
|
|
|
*/ |
|
64
|
|
|
public function onRun() |
|
65
|
|
|
{ |
|
66
|
|
|
$this->page['visible'] = $this->property('visible'); |
|
67
|
|
|
$this->page['button'] = $this->property('button'); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Sign in the user |
|
72
|
|
|
*/ |
|
73
|
|
|
public function onCodesignin() |
|
74
|
|
|
{ |
|
75
|
|
|
try { |
|
76
|
|
|
return $this->doSignin(); |
|
77
|
|
|
|
|
78
|
|
|
} catch (Exception $ex) { |
|
79
|
|
|
if (Request::ajax()) { |
|
80
|
|
|
throw $ex; |
|
81
|
|
|
} |
|
82
|
|
|
Flash::error($ex->getMessage()); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
private function doSignin() |
|
87
|
|
|
{ |
|
88
|
|
|
/* |
|
89
|
|
|
* Validate input |
|
90
|
|
|
*/ |
|
91
|
|
|
$data = post(); |
|
92
|
|
|
|
|
93
|
|
|
$rules = []; |
|
94
|
|
|
$rules['code'] = 'required|min:2'; |
|
95
|
|
|
|
|
96
|
|
|
$messages = []; |
|
97
|
|
|
$messages['required'] = trans('vojtasvoboda.codelogin::lang.form.required'); |
|
98
|
|
|
|
|
99
|
|
|
$validation = Validator::make($data, $rules, $messages); |
|
100
|
|
|
if ($validation->fails()) { |
|
101
|
|
|
throw new ValidationException($validation); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/* |
|
105
|
|
|
* Find user by password |
|
106
|
|
|
*/ |
|
107
|
|
|
$users = new UserRepository(); |
|
108
|
|
|
$userToLog = $users->getUserByPassword(array_get($data, 'code')); |
|
109
|
|
|
if ($userToLog === null) { |
|
110
|
|
|
$exception = new ValidationException([ 'code' => trans('vojtasvoboda.codelogin::lang.form.wrong_code') ]); |
|
111
|
|
|
throw $exception; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/* |
|
115
|
|
|
* Authenticate user |
|
116
|
|
|
*/ |
|
117
|
|
|
$remember = true; |
|
118
|
|
|
$user = Auth::authenticate([ |
|
119
|
|
|
'login' => $userToLog->email, |
|
120
|
|
|
'password' => array_get($data, 'code') |
|
121
|
|
|
], $remember); |
|
122
|
|
|
|
|
123
|
|
|
/* |
|
124
|
|
|
* Event just for backward compatibility. It's more then recommand use |
|
125
|
|
|
* standard rainlab.user.login event - see readme. |
|
126
|
|
|
*/ |
|
127
|
|
|
Event::fire('vojtasvoboda.codelogin.afterlogin', [$user]); |
|
128
|
|
|
|
|
129
|
|
|
/* |
|
130
|
|
|
* Redirect to the intended page after successful sign in |
|
131
|
|
|
*/ |
|
132
|
|
|
$redirectUrl = $this->pageUrl($this->property('redirect')); |
|
133
|
|
|
|
|
134
|
|
|
if ($redirectUrl = post('redirect', $redirectUrl)) { |
|
135
|
|
|
return Redirect::intended($redirectUrl); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|