public static function _validator($name) { $comment = array('field' => 'comment', 'validator' => 'notEmpty', 'min' => 12, 'message' => 'We need at least 12 characters from you, buddy!'); $requireds = array('field' => array('username', 'password'), 'validator' => 'notEmpty'); $login = array('validator' => function ($validator) { try { $user = User::one(array('username' => trim($validator->input['username']), 'password' => $validator->input['password'])); $validator->context['user'] = $user; $validator->output['author_id'] = $user->user_id; // SessionUser::user()->login($user); // Auto-login? return true; } catch (Exception $ex) { } $validator->setError(array('username', 'password'), 'I don\'t know that username/password combination...'); }); $removes = array('validator' => 'remove', 'field' => array('username', 'password')); $setUser = array('validator' => function ($validator) { $validator->output['author_id'] = SessionUser::user()->userID(); }); switch ($name) { case 'add': return new Validator(array($comment, $setUser), array('model' => get_called_class())); case 'add_anonymous': return new Validator(array($requireds, $comment, $login, $removes), array('model' => get_called_class())); case 'edit': return new Validator(array($comment), array('model' => get_called_class())); } }
protected function _init() { // Make the session user always available in every controller: $this->user = SessionUser::user(); // Might come in handy sometimes: direct access to the DBAL: $this->db = $GLOBALS['db']; // Initialize Output/Views (used in 90% of controller actions): $this->tpl = new Output($this); $this->tpl->viewLayout = '_layout'; $this->tpl->assign('app', $this); }
protected function _init() { // I don't want to load ROW's default _init, because it does unwanted stuff, so I don't: // parent::_init(); // Because I don't use ROW's _init, I have to do this myself: // Make the session user always available in every controller: $this->user = SessionUser::user(); // Might come in handy sometimes: direct access to the DBAL: $this->db = $GLOBALS['db']; // Initialize Output/Views (used in 90% of controller actions): $this->tpl = new Output($this); $this->tpl->viewLayout = '_blogLayout'; $this->tpl->assign('app', $this); // Blog `Email` context Email::context('blog', function ($class, $options) { $mailer = new $class(); $mailer->setFrom('*****@*****.**', 'Tha Blog'); $mailer->Sender = '*****@*****.**'; return $mailer; }); }
$this->logout(); } } } public function hasAccess($zone) { if ($zone === 'login') { return $this->isLoggedIn(); } if ($zone === 'not login') { return !$this->isLoggedIn(); } return false; } public function logout() { if (parent::logout()) { Session::success('You are now logged out.'); } } public function displayName() { return $this->isLoggedIn() ? (string) $this->user : '******'; } public function userID() { return $this->isLoggedIn() ? (int) $this->user->user_id : 0; } } SessionUser::$class = 'app\\specs\\SessionUser';
public function canEdit() { $sessionUser = SessionUser::user(); return $sessionUser->userID() === (int) $this->author_id || $sessionUser->hasAccess('blog edit posts'); }