nyroFwk  0.2
default.class.php
Go to the documentation of this file.
1 <?php
12 
18  protected $session;
19 
25  protected $table;
26 
32  protected $user;
33 
39  protected $form;
40 
46  protected $logged = false;
47 
53  protected $roles = array();
54 
55  protected function afterInit() {
56  parent::afterInit();
57  $this->session = session::getInstance(array(
58  'nameSpace'=>$this->cfg->sessionNameSpace
59  ));
60  $this->table = db::get('table', $this->cfg->table);
61  $this->autoLogin();
62  }
63 
67  protected function autoLogin() {
68  $fromSession = true;
69  if (!$cryptic = $this->session->cryptic) {
70  // Try to check the cookie
71  $cook = factory::get('http_cookie', $this->cfg->cookie);
72  $cryptic = $cook->get(true);
73  $fromSession = false;
74  }
75 
76  if ($cryptic) {
77  $this->user = $this->getUserFromCryptic($cryptic);
78  if ($this->user) {
79  $this->logged = true;
80  $this->hook('autoLogin'.($fromSession ? 'Session' : null));
81  $this->session->cryptic = $cryptic;
82  } else if (isset($cook))
83  $cook->del();
84  }
85  }
86 
93  public function getUserFromCryptic($cryptic) {
94  return $this->table->find(array_merge(array(
95  $this->table->getRawName().'.'.$this->cfg->getInArray('fields', 'cryptic')=>$cryptic
96  ), $this->cfg->where));
97  }
98 
99  public function isLogged() {
100  return $this->logged;
101  }
102 
108  public function getUser() {
109  if ($this->isLogged() && $this->user)
110  return $this->user;
111  return null;
112  }
113 
121  public function setUser(db_row $user, $saveLogin = true, $cookieStayConnected = false) {
122  $this->user = $user;
123  if ($saveLogin)
124  $this->saveLogin($cookieStayConnected);
125  }
126 
133  protected function saveLogin($cookieStayConnected = false) {
134  $crypticKey = $this->cfg->getInArray('fields', 'cryptic');
135  $cryptic = $this->cryptPass(uniqid(), 'Cryptic');
136  $this->user->set($crypticKey, $cryptic);
137  $this->user->save();
138  $this->logFromCryptic($cryptic, $cookieStayConnected);
139  }
140 
148  protected function getWhereLogin($login, $pass) {
149  $tableName = $this->table->getRawName();
150  $loginField = $this->cfg->getInArray('fields', 'login');
151  $passField = $this->cfg->getInArray('fields', 'pass');
152 
153  return array(
154  $tableName.'.'.$loginField=>$login,
155  $tableName.'.'.$passField=>$this->cryptPass($pass),
156  );
157  }
158 
167  public function login($prm = null, $page = null, $redirectIfLogged = true) {
168  $loginField = $this->cfg->getInArray('fields', 'login');
169  $passField = $this->cfg->getInArray('fields', 'pass');
170 
171  $form = $this->getLoginForm();
172  if (is_null($prm)) {
173  if (request::isPost()) {
174  $form->refill();
175  $form->isValid();
176  $prm = $form->getValues(true);
177  }
178  }
179 
180  if (is_array($prm)
181  && array_key_exists($loginField, $prm)
182  && array_key_exists($passField, $prm)) {
183  $this->user = $this->table->find(array_merge(
184  $this->cfg->where,
185  $this->getWhereLogin($prm[$loginField], $prm[$passField])
186  ));
187 
188  if ($this->user) {
189  $this->saveLogin(array_key_exists('stayConnected', $prm) && $prm['stayConnected']);
190  $this->hook('login');
191  } else
192  $form->addCustomError($loginField, $this->cfg->errorMsg);
193  if ($this->logged && $redirectIfLogged) {
194  if (is_null($page)) {
195  if ($this->session->pageFrom) {
196  $page = $this->session->pageFrom;
197  unset($this->session->pageFrom);
198  } else
199  $page = request::uri($this->getPage('logged'));
200  } else
201  $page = request::uri($page);
202  response::getInstance()->redirect($page);
203  }
204  }
205  return $this->logged;
206  }
207 
214  public function logFromCryptic($cryptic, $cookieStayConnected = false) {
215  $this->session->cryptic = $cryptic;
216  $this->logged = true;
217  if ($cookieStayConnected)
218  $this->saveCookieStayConnected();
219  }
220 
224  public function saveCookieStayConnected() {
225  $cook = factory::get('http_cookie', $this->cfg->cookie);
226  $cook->set($this->user->get($this->cfg->getInArray('fields', 'cryptic')));
227  $cook->save();
228  }
229 
237  public function cryptPass($str, $plus = 'Password') {
238  $crypt = $this->cfg->get('crypt'.$plus);
239  if ($crypt && function_exists($crypt))
240  $str = $crypt($str);
241  return $str;
242  }
243 
244  public function logout($prm = null) {
245  if ($this->isLogged()) {
246  $this->session->del('cryptic');
247  $this->logged = false;
248  // Clear the cookie
249  $cook = factory::get('http_cookie', $this->cfg->cookie);
250  $cook->del();
251  }
252  $this->hook('logout');
253  return $this->logged == false;
254  }
255 
256  public function addRole($role) {
257  $this->roles[$role] = true;
258  return true;
259  }
260 
261  public function hasRole($role = null) {
262  if (is_null($role))
263  return $this->roles;
264 
265  return array_key_exists($role, $this->roles);
266  }
267 
268  public function delRole($role = null) {
269  if (is_null($role)) {
270  $this->roles = array();
271  return true;
272  }
273  unset($this->roles[$rol]);
274  return true;
275  }
276 
277  public function check(array $url = null, $redirect = true) {
278  if (is_null($url))
279  $url = request::get();
280 
281  if ($this->isContained($url, $this->cfg->noSecurity))
282  return true;
283 
284  $hasRight = $this->cfg->default;
285  if ($this->isContained($url, $this->cfg->spec)) {
286  if ($hasRight) {
287  $hasRight = $this->isLogged();
288  } else {
289  $hasRight = true;
290  }
291  } else if ($this->isLogged()) {
292  if (!empty($this->cfg->rightRoles)) {
293  $checks = array();
294  foreach($this->hasRole() as $r=>$t) {
295  $tmp = $this->cfg->getInArray('rightRoles', $r);
296  if (is_array($tmp)) {
297  foreach($tmp as $c)
298  $checks[] = $c;
299  }
300  }
301  $hasRight = $this->isContained($url, $checks);
302  } else
303  $hasRight = true;
304  }
305 
306  if (!$hasRight && $redirect) {
307  $request = request::removeLangOutUrl('/'.request::get('request'));
308  if ($request != $this->getPage('forbidden') && $request != $this->getPage('login')) {
309  $this->session->pageFrom = request::get('localUri');
310  session::setFlash('nyroError', $this->cfg->errorText);
311  $this->hook('redirectError');
312  response::getInstance()->redirect($this->getPage('forbidden', true), 403);
313  }
314  }
315 
316  return $hasRight;
317  }
318 
319  public function getLoginForm(array $prm = array()) {
320  if (!$this->form) {
321  $this->form = $this->table->getRow()->getForm(array(
322  $this->cfg->getInArray('fields', 'login'),
323  $this->cfg->getInArray('fields', 'pass')
324  ), array_merge($this->cfg->formOptions, $prm, array(
325  'action'=>request::uri($this->getPage('login'))
326  )), false);
327  $this->form->get($this->cfg->getInArray('fields', 'login'))->getValid()->delRule('dbUnique');
328  if ($this->cfg->stayConnected) {
329  $this->form->add('checkbox', array(
330  'name'=>'stayConnected',
331  'label'=>false,
332  'uniqValue'=>true,
333  'valid'=>array('required'=>false),
334  'list'=>array(
335  1=>utils::htmlOut($this->cfg->labelStayConnected)
336  )
337  ));
338  }
339  }
340 
341  return $this->form;
342  }
343 
349  public function getSession() {
350  return $this->session;
351  }
352 
353 }
static get($get=null)
static htmlOut($val, $key=false)
login($prm=null, $page=null, $redirectIfLogged=true)
saveLogin($cookieStayConnected=false)
delRole($role=null)
isContained(array $url, array $checks)
getWhereLogin($login, $pass)
static setFlash($name, $val=null)
hasRole($role=null)
getLoginForm(array $prm=array())
static getInstance(array $cfg=array())
static removeLangOutUrl($request)
check(array $url=null, $redirect=true)
static getInstance()
getPage($type='login', $uri=false)
logFromCryptic($cryptic, $cookieStayConnected=false)
add($type, array $prm=array(), $isI18n=false)
Definition: form.class.php:343
static uri($prm=array())
static get($type, $table, array $prm=array())
Definition: db.class.php:87
static isPost()
getUserFromCryptic($cryptic)
cryptPass($str, $plus='Password')
get($name)
Definition: form.class.php:384
static get($className, array $cfg=array())
setUser(db_row $user, $saveLogin=true, $cookieStayConnected=false)
Generated on Sun Oct 15 2017 22:25:20 for nyroFwk by doxygen 1.8.13