Ivan - Implent NullDataStore pattern, refactor login controller, add get-setting and set-setting controllers

This commit is contained in:
ivan 2016-07-27 13:58:19 -03:00
parent 511098e608
commit eccc0b5a29
11 changed files with 134 additions and 47 deletions

View File

@ -0,0 +1,30 @@
<?php
use Respect\Validation\Validator as DataValidator;
class GetSettingController extends Controller {
const PATH = '/get-setting';
public function validations() {
return [
'permission' => 'any',
'requestData' => [
'name' => [
'validation' => DataValidator::length(4),
'error' => ERRORS::INVALID_SETTING
]
]
];
}
public function handler() {
$setting = Setting::getSetting(Controller::request('name'));
if (!$setting->isNull()) {
Response::respondSuccess([
'setting' => $setting->value
]);
} else {
Response::respondError(ERRORS::INVALID_SETTING);
}
}
}

View File

@ -0,0 +1,35 @@
<?php
use Respect\Validation\Validator as DataValidator;
class GetSettingController extends Controller {
const PATH = '/get-setting';
public function validations() {
return [
'permission' => 'any',
'requestData' => [
'name' => [
'validation' => DataValidator::length(4),
'error' => ERRORS::INVALID_SETTING
],
'value' => [
'validation' => DataValidator::length(4),
'error' => ERRORS::INVALID_SETTING
]
]
];
}
public function handler() {
$setting = Setting::getSetting(Controller::request('name'));
if (!$setting->isNull()) {
$setting->value = Controller::request('value');
$setting->store();
Response::respondSuccess();
} else {
Response::respondError(ERRORS::INVALID_SETTING);
}
}
}

View File

@ -1,5 +1,4 @@
<?php
use RedBeanPHP\Facade as RedBean;
use Respect\Validation\Validator as DataValidator;
class CreateController extends Controller {

View File

@ -4,7 +4,6 @@ class LoginController extends Controller {
const PATH = '/login';
private $userInstance;
private $session;
private $rememberToken;
public function validations() {
@ -20,7 +19,7 @@ class LoginController extends Controller {
return;
}
if ($this->areCredentialsValid() || $this->isRememberTokenValid()) {
if ($this->checkInputCredentials() || $this->checkRememberToken()) {
$this->createUserSession();
$this->createSessionCookie();
@ -31,61 +30,61 @@ class LoginController extends Controller {
}
private function isAlreadyLoggedIn() {
return $this->getSession()->sessionExists();
return Session::getInstance()->sessionExists();
}
private function areCredentialsValid() {
return ($this->getUserByInputCredentials() !== null);
private function checkInputCredentials() {
$this->userInstance = $this->getUserByInputCredentials();
return !$this->userInstance->isNull();
}
private function isRememberTokenValid() {
$rememberToken = Controller::request('rememberToken');
if ($rememberToken) {
$sessionCookie = SessionCookie::getDataStore($rememberToken, 'token');
$userid = Controller::request('userId');
if ($sessionCookie !== null && $userid === $sessionCookie->user->id) {
$this->userInstance = $sessionCookie->user;
return true;
}
}
private function checkRememberToken() {
$this->userInstance = $this->getUserByRememberToken();
return !$this->userInstance->isNull();
}
private function createUserSession() {
$this->getSession()->createSession($this->userInstance->id);
Session::getInstance()->createSession($this->userInstance->id);
}
private function getUserData() {
$userInstance = $this->getUserByInputCredentials();
$userInstance = $this->userInstance;
return array(
'userId' => $userInstance->id,
'userEmail' => $userInstance->email,
'token' => $this->getSession()->getToken(),
'token' => Session::getInstance()->getToken(),
'rememberToken' => $this->rememberToken
);
}
private function getUserByInputCredentials() {
if ($this->userInstance === null) {
$email = Controller::request('email');
$password = Controller::request('password');
$email = Controller::request('email');
$password = Controller::request('password');
$this->userInstance = User::authenticate($email, $password);
return User::authenticate($email, $password);
}
private function getUserByRememberToken() {
$rememberToken = Controller::request('rememberToken');
$userInstance = new NullDataStore();
if ($rememberToken) {
$sessionCookie = SessionCookie::getDataStore($rememberToken, 'token');
$userId = Controller::request('userId');
if (!$sessionCookie->isNull() && $userId === $sessionCookie->user->id) {
$userInstance = new User($sessionCookie->user);
$sessionCookie->trash();
}
}
return $this->userInstance;
return $userInstance;
}
private function getSession() {
if ($this->session === null) {
$this->session = Session::getInstance();
}
return $this->session;
}
private function createSessionCookie(){
private function createSessionCookie() {
$remember = Controller::request('remember');
if ($remember) {
$this->rememberToken = Hashing::generateRandomToken();

View File

@ -8,5 +8,6 @@ class ERRORS {
const INVALID_EMAIL = 'Invalid email';
const INVALID_PASSWORD = 'Invalid password';
const INVALID_NAME = 'Invalid name';
const INVALID_SETTING = 'Invalid setting';
const INIT_SETTINGS_DONE = 'Settings already initialized';
}

View File

@ -4,12 +4,12 @@ class MailSender {
private $mailOptions = [];
public function __construct() {
$this->mailOptions['from'] = Setting::getSetting('no-reply-email');
$this->mailOptions['from'] = Setting::getSetting('no-reply-email')->value;
$this->mailOptions['smtp-host'] = Setting::getSetting('smtp-host');
$this->mailOptions['smtp-port'] = Setting::getSetting('smtp-host');
$this->mailOptions['smtp-user'] = Setting::getSetting('smtp-host');
$this->mailOptions['smtp-pass'] = Setting::getSetting('smtp-host');
$this->mailOptions['smtp-host'] = Setting::getSetting('smtp-host')->value;
$this->mailOptions['smtp-port'] = Setting::getSetting('smtp-host')->value;
$this->mailOptions['smtp-user'] = Setting::getSetting('smtp-host')->value;
$this->mailOptions['smtp-pass'] = Setting::getSetting('smtp-host')->value;
}
public function setTemplate($type, $config) {

View File

@ -13,7 +13,7 @@ abstract class DataStore {
':value' => $value
));
return ($bean) ? new static($bean) : null;
return ($bean) ? new static($bean) : new NullDataStore();
}
public function __construct($beanInstance = null) {
@ -80,4 +80,8 @@ abstract class DataStore {
public function trash() {
RedBean::trash($this->_bean);
}
public function isNull() {
return false;
}
}

View File

@ -8,14 +8,14 @@ class MailTemplate extends DataStore {
const USER_PASSWORD = 'USER_PASSWORD';
public static function getTemplate($type) {
$globalLanguage = Setting::getSetting('language');
$globalLanguage = Setting::getSetting('language')->value;
$bean = RedBean::findOne(MailTemplate::TABLE, 'type = :type AND language = :language', array(
':type' => $type,
':language' => $globalLanguage
));
return ($bean) ? new MailTemplate($bean) : null;
return ($bean) ? new MailTemplate($bean) : new NullDataStore();
}
public static function getProps() {

View File

@ -0,0 +1,21 @@
<?php
class NullDataStore extends DataStore {
const TABLE = null;
public function __construct() {
$this->_bean = null;
}
public function isNull() {
return true;
}
public function getProps() {
return [];
}
public function store() {
return null;
}
}

View File

@ -4,9 +4,7 @@ class Setting extends DataStore {
const TABLE = 'setting';
public static function getSetting($name) {
$dataStore = parent::getDataStore($name, 'name');
return ($dataStore !== null) ? $dataStore->value : null;
return parent::getDataStore($name, 'name');
}
public static function getProps() {

View File

@ -7,7 +7,7 @@ class User extends DataStore {
public static function authenticate($userEmail, $userPassword) {
$user = User::getUser($userEmail, 'email');
return ($user && Hashing::verifyPassword($userPassword, $user->password)) ? $user : null;
return ($user && Hashing::verifyPassword($userPassword, $user->password)) ? $user : new NullDataStore();
}
public static function getProps() {