mirror of
https://github.com/opensupports/opensupports.git
synced 2025-07-31 01:35:15 +02:00
Merged in settings-options-backend (pull request #29)
Settings options backend
This commit is contained in:
commit
4d5ce7022e
30
server/controllers/system/get-setting.php
Normal file
30
server/controllers/system/get-setting.php
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
35
server/controllers/system/set-setting.php
Normal file
35
server/controllers/system/set-setting.php
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +1,4 @@
|
||||
<?php
|
||||
use RedBeanPHP\Facade as RedBean;
|
||||
use Respect\Validation\Validator as DataValidator;
|
||||
|
||||
class CreateController extends Controller {
|
||||
|
@ -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();
|
||||
|
@ -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';
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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() {
|
||||
|
21
server/models/NullDataStore.php
Normal file
21
server/models/NullDataStore.php
Normal 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;
|
||||
}
|
||||
}
|
@ -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() {
|
||||
|
@ -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() {
|
||||
|
41
server/tests/__mocks__/BeanMock.php
Normal file
41
server/tests/__mocks__/BeanMock.php
Normal file
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
class BeanMock implements ArrayAccess {
|
||||
private $properties;
|
||||
|
||||
public function __construct($array = []) {
|
||||
$this->properties = $array;
|
||||
}
|
||||
|
||||
public function offsetSet($offset, $value) {
|
||||
$this->__set($offset, $value);
|
||||
}
|
||||
|
||||
public function offsetExists($offset) {
|
||||
return $this->__isset($offset);
|
||||
}
|
||||
|
||||
public function offsetUnset($offset) {
|
||||
$this->__unset($offset);
|
||||
}
|
||||
|
||||
public function &offsetGet($offset) {
|
||||
return $this->__get($offset);
|
||||
}
|
||||
|
||||
public function &__get($property) {
|
||||
return $this->properties[$property];
|
||||
}
|
||||
|
||||
public function __set($property, $value) {
|
||||
$this->properties[$property] = $value;
|
||||
}
|
||||
|
||||
public function __isset($property) {
|
||||
return isset($this->properties[$property]);
|
||||
}
|
||||
|
||||
public function __unset($property) {
|
||||
unset($this->properties[$property]);
|
||||
}
|
||||
}
|
7
server/tests/__mocks__/NullDataStoreMock.php
Normal file
7
server/tests/__mocks__/NullDataStoreMock.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
class NullDataStore extends \Mock {
|
||||
|
||||
public function isNull() {
|
||||
return true;
|
||||
}
|
||||
}
|
@ -1,5 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace RedBeanPHP {
|
||||
include_once 'tests/__mocks__/BeanMock.php';
|
||||
|
||||
class Facade extends \Mock {
|
||||
public static $functionList = array();
|
||||
|
||||
@ -7,7 +10,7 @@ namespace RedBeanPHP {
|
||||
self::setStatics(array(
|
||||
'trash' => parent::stub(),
|
||||
'store' => parent::stub(),
|
||||
'dispense' => parent::stub()->returns(array())
|
||||
'dispense' => parent::stub()->returns(new \BeanMock())
|
||||
));
|
||||
}
|
||||
}
|
||||
|
26
server/tests/__mocks__/SettingMock.php
Normal file
26
server/tests/__mocks__/SettingMock.php
Normal file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
include_once 'tests/__mocks__/BeanMock.php';
|
||||
|
||||
class Setting extends \Mock {
|
||||
public static $functionList = array();
|
||||
|
||||
public static function initStubs() {
|
||||
parent::setStatics(array(
|
||||
'getSetting' => parent::stub()->returns(self::getSettingInstanceMock()),
|
||||
'setSetting' => parent::stub()
|
||||
));
|
||||
}
|
||||
|
||||
public function isNull() {
|
||||
return false;
|
||||
}
|
||||
|
||||
private static function getSettingInstanceMock() {
|
||||
$mockUserInstance = new BeanMock();
|
||||
|
||||
$mockUserInstance->name = 'MOCK_SETTING_NAME';
|
||||
$mockUserInstance->value = 'MOCK_SETTING_VALUE';
|
||||
|
||||
return $mockUserInstance;
|
||||
}
|
||||
}
|
@ -7,9 +7,13 @@ class User extends \Mock {
|
||||
'authenticate' => parent::stub()->returns(self::getUserInstanceMock()),
|
||||
));
|
||||
}
|
||||
|
||||
public function isNull() {
|
||||
return false;
|
||||
}
|
||||
|
||||
private static function getUserInstanceMock() {
|
||||
$mockUserInstance = new \stdClass();
|
||||
$mockUserInstance = new User();
|
||||
|
||||
$mockUserInstance->id = 'MOCK_ID';
|
||||
$mockUserInstance->email = 'MOCK_EMAIL';
|
||||
|
@ -1,6 +1,7 @@
|
||||
<?php
|
||||
// MOCKS
|
||||
include_once 'tests/__lib__/Mock.php';
|
||||
include_once 'tests/__mocks__/NullDataStoreMock.php';
|
||||
include_once 'tests/__mocks__/ResponseMock.php';
|
||||
include_once 'tests/__mocks__/ControllerMock.php';
|
||||
include_once 'tests/__mocks__/SessionMock.php';
|
||||
@ -45,7 +46,7 @@ class LoginControllerTest extends PHPUnit_Framework_TestCase {
|
||||
|
||||
public function testShouldRespondErrorIfCredentialsAreInvalid() {
|
||||
User::setStatics(array(
|
||||
'authenticate' => \Mock::stub()->returns(null)
|
||||
'authenticate' => \Mock::stub()->returns(new NullDataStore())
|
||||
));
|
||||
|
||||
$this->loginController->handler();
|
||||
|
@ -1,5 +1,6 @@
|
||||
<?php
|
||||
include_once 'tests/__lib__/Mock.php';
|
||||
include_once 'tests/__mocks__/BeanMock.php';
|
||||
include_once 'tests/__mocks__/SlimMock.php';
|
||||
include_once 'tests/__mocks__/RedBeanMock.php';
|
||||
include_once 'models/DataStore.php';
|
||||
@ -57,7 +58,7 @@ class DataStoreTest extends PHPUnit_Framework_TestCase {
|
||||
|
||||
public function testGetDataStore() {
|
||||
RedBean::setStatics(array(
|
||||
'findOne' => \Mock::stub()->returns(array('TEST_PROP' => 'TEST_VALUE'))
|
||||
'findOne' => \Mock::stub()->returns(new BeanMock(['TEST_PROP' => 'TEST_VALUE']))
|
||||
));
|
||||
|
||||
$dataStoreIntance = DataStoreMock::getDataStore('ID_VALUE');
|
||||
|
@ -1,12 +1,31 @@
|
||||
<?php
|
||||
include_once 'tests/__lib__/Mock.php';
|
||||
include_once 'tests/__mocks__/BeanMock.php';
|
||||
include_once 'tests/__mocks__/SettingMock.php';
|
||||
include_once 'tests/__mocks__/RedBeanMock.php';
|
||||
include_once 'models/MailTemplate.php';
|
||||
|
||||
use RedBeanPHP\Facade as RedBean;
|
||||
|
||||
class MailTemplateTest extends PHPUnit_Framework_TestCase {
|
||||
|
||||
protected function setUp() {
|
||||
RedBean::initStubs();
|
||||
Setting::initStubs();
|
||||
|
||||
RedBean::setStatics([
|
||||
'findOne' => \Mock::stub()->returns($this->getMockTemplateBean())
|
||||
]);
|
||||
}
|
||||
|
||||
public function testGetTemplateShouldReturnSpecifiedTemplate() {
|
||||
$mailTemplate = MailTemplate::getTemplate(MailTemplate::USER_SIGNUP);
|
||||
|
||||
$this->assertEquals(MailTemplate::USER_SIGNUP, $mailTemplate->type);
|
||||
$this->assertEquals('TEST_TYPE', $mailTemplate->type);
|
||||
$this->assertTrue(Redbean::get('findOne')->hasBeenCalledWithArgs('mailtemplate', 'type = :type AND language = :language', array(
|
||||
':type' => 'USER_SIGNUP',
|
||||
':language' => 'MOCK_SETTING_VALUE'
|
||||
)));
|
||||
}
|
||||
|
||||
public function testCompilation() {
|
||||
@ -24,4 +43,14 @@ class MailTemplateTest extends PHPUnit_Framework_TestCase {
|
||||
$this->assertEquals($result['subject'], 'Welcoming to cersei@opensupports.com');
|
||||
$this->assertEquals($result['body'], 'Welcome, Cersei Lannister to our team');
|
||||
}
|
||||
|
||||
private function getMockTemplateBean() {
|
||||
$mailTemplateBean = new BeanMock();
|
||||
$mailTemplateBean->type = 'TEST_TYPE';
|
||||
$mailTemplateBean->body = 'Some body';
|
||||
$mailTemplateBean->subject = 'Some subject';
|
||||
$mailTemplateBean->language = 'en';
|
||||
|
||||
return $mailTemplateBean;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user