-
Notifications
You must be signed in to change notification settings - Fork 22
/
Auth.php
191 lines (152 loc) · 4.83 KB
/
Auth.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
namespace atk4\login;
/**
* Authentication controller. Add this to your application somewhere
* and it will work wonders
*/
class Auth {
use \atk4\core\SessionTrait;
use \atk4\core\ContainerTrait;
use \atk4\core\FactoryTrait;
use \atk4\core\AppScopeTrait;
use \atk4\core\DIContainerTrait;
use \atk4\core\TrackableTrait;
use \atk4\core\InitializerTrait {
init as _init;
}
/**
* Contains information about a current user. Unlike Model this will
* contain a record loaded from session cache
*/
public $user = null;
/**
* Login Form. If you want to use a different LoginForm you can pass
* a seed or object here.
*/
public $form = '\atk4\login\LoginForm';
/**
* Which field to look up user by
*/
public $fieldLogin = 'email';
/**
* Password to be verified when authenticating
*/
public $fieldPassword = 'password';
/**
* Permorm check automatically and display a Login form when 'setModel' takes place.
*
* This is a transparent way to add authentication to an existing application
*/
public $check = true;
/**
* Which is the index page? This page should have auth / check.
*/
public $pageDashboard = null;
/**
* User will be sent to exit page when he logs out
*/
public $pageExit = 'index';
/**
* Should we add User Menu to Admin layout?
*/
public $hasUserMenu = true;
/**
* Should we display and handle preferences link
*/
public $hasPreferences = true;
function __construct($options = [])
{
$this->setDefaults($options);
}
function init()
{
$this->_init();
session_start();
}
function getSessionPersistence()
{
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
return new \atk4\data\Persistence_Array($_SESSION[$this->name]);
}
/**
* Specify a model for a user check here
*
*/
function setModel($model, $login_field = null, $password_field = null)
{
$this->user = $model;
if ($login_field) {
$this->fieldLogin = $login_field;
}
if ($password_field) {
$this->fieldPassword = $password_field;
}
$this->user->data = $this->getSessionPersistence()->tryLoad($this->user, 1);
$this->user->id = $this->user->data[$this->user->id_field];
$this->user->addHook('afterSave', function($m) {
$this->getSessionPersistence()->update($m, 1, $m->get());
// update persistence
});
if ($this->check) {
$this->check();
}
}
function logout()
{
$this->getSessionPersistence()->delete($this->user, 1);
}
/**
* Call this method to verify credentials
*/
function check($build_form_callback = null)
{
if ($this->user->loaded()) {
if ($this->hasUserMenu && $this->app->layout instanceof \atk4\ui\Layout\Admin) {
$m = $this->app->layout->menuRight->addMenu($this->user->getTitle());
if ($this->hasPreferences) {
$m->addItem(['Preferences', 'icon'=>'user'], [$this->pageDashboard, 'preferences'=>true]);
}
$m->addItem(['Logout', 'icon'=>'sign out'], [$this->pageDashboard, 'logout'=>true]);
}
if ($this->hasPreferences && $this->app->stickyGet('preferences')) {
$this->app->add(['Header', 'User Preferences', 'subHeader'=>$this->user->getTitle(), 'icon'=>'user']);
$this->app->add('Form')->setModel($this->user);
exit;
}
if (isset($_GET['logout'])) {
$this->logout();
$this->app->redirect([$this->pageExit]);
}
return;
}
$l = new \atk4\ui\App();
$this->app->catch_runaway_callbacks = false;
$this->app->run_called = true;
$l->catch_runaway_callbacks = false;
$l->initLayout(new \atk4\login\Layout\Narrow());
$form = $l->add([
$this->form,
'auth'=>$this,
'linkSuccess'=>[$this->pageDashboard],
'linkForgot'=>false,
]);
$l->layout->template->set('title', 'Log-in Required');
$l->run();
$this->app->terminate();
exit;
}
function tryLogin($email, $password) {
$user = $this->user->newInstance();
$user->tryLoadBy($this->fieldLogin, $email);
if ($user->loaded()) {
// verify if the password matches
if ($user->compare($this->fieldPassword, $password)) {
$this->getSessionPersistence()->update($user, 1, $user->get());
return true;
}
}
return false;
}
}