Merged in OS141create-Log-Architectur (pull request #105)

Os141create log architectur
This commit is contained in:
Ivan Diaz 2016-12-29 23:56:20 -03:00
commit 805e78f0a2
47 changed files with 300 additions and 48 deletions

View File

@ -25,6 +25,10 @@ class AddTopicController extends Controller {
'iconColor' => Controller::request('iconColor')
]);
$staff = Controller::getLoggedUser();
Log::createLog('ADD_TOPIC', $topic->name);
Response::respondSuccess([
'topicId' => $topic->store()
]);

View File

@ -38,6 +38,10 @@ class AddArticleController extends Controller {
$topic->ownArticleList->add($article);
$topic->store();
$staff = Controller::getLoggedUser();
Log::createLog('ADD_ARTICLE', $article->title);
Response::respondSuccess([
'articleId' => $article->store()
]);

View File

@ -19,8 +19,10 @@ class DeleteTopicController extends Controller {
public function handler() {
$topic = Topic::getDataStore(Controller::request('topicId'));
$topic->delete();
Log::createLog('DELETE_TOPIC', $topic->name);
$topic->delete();
Response::respondSuccess();
}
}

View File

@ -19,6 +19,8 @@ class DeleteArticleController extends Controller {
public function handler() {
$article = Article::getDataStore(Controller::request('articleId'));
Log::createLog('DELETE_ARTICLE', $article->title);
$article->delete();
Response::respondSuccess();

View File

@ -46,6 +46,9 @@ class EditArticleController extends Controller {
$article->lastEdited = Date::getCurrentDate();
$article->store();
Log::createLog('EDIT_ARTICLE', $article->title);
Response::respondSuccess();
}
}

View File

@ -57,6 +57,8 @@ class AddStaffController extends Controller {
$this->addOwner();
Log::createLog('ADD_STAFF', $this->name);
Response::respondSuccess([
'id' => $staff->store()
]);

View File

@ -1,5 +1,7 @@
<?php
use Respect\Validation\Validator as DataValidator;
use RedBeanPHP\Facade as RedBean;
DataValidator::with('CustomValidations', true);
class DeleteStaffController extends Controller {
@ -36,7 +38,8 @@ class DeleteStaffController extends Controller {
$department->owners--;
$department->store();
}
RedBean::exec('DELETE FROM log WHERE author_staff_id = ?', [$staffId]);
$staff->delete();
Response::respondSuccess();
}

View File

@ -5,6 +5,7 @@ require_once 'system/edit-settings.php';
require_once 'system/add-department.php';
require_once 'system/edit-department.php';
require_once 'system/delete-department.php';
require_once 'system/get-logs.php';
$systemControllerGroup = new ControllerGroup();
$systemControllerGroup->setGroupPath('/system');
@ -15,6 +16,6 @@ $systemControllerGroup->addController(new EditSettingsController);
$systemControllerGroup->addController(new AddDepartmentController);
$systemControllerGroup->addController(new EditDepartmentController);
$systemControllerGroup->addController(new DeleteDepartmentController);
$systemControllerGroup->addController(new GetLogsController);
$systemControllerGroup->finalize();

View File

@ -23,11 +23,11 @@ class AddDepartmentController extends Controller {
$departmentInstance->setProperties([
'name' => $name,
]);
$departmentInstance->store();
Log::createLog('ADD_DEPARTMENT', $name);
Response::respondSuccess();
}

View File

@ -37,6 +37,8 @@ class DeleteDepartmentController extends Controller {
$departmentInstance = Department::getDataStore($this->departmentId);
$departmentInstance->delete();
Log::createLog('DELETE_DEPARTMENT', $departmentInstance->name);
Response::respondSuccess();
}

View File

@ -32,6 +32,8 @@ class EditDepartmentController extends Controller {
$departmentInstance->store();
Log::createLog('EDIT_DEPARTMENT', $departmentInstance->name);
Response::respondSuccess();
}

View File

@ -40,6 +40,9 @@ class EditSettingsController extends Controller {
if(Controller::request('allowedLanguages') || Controller::request('supportedLanguages')) {
$this->handleLanguages();
}
Log::createLog('EDIT_SETTINGS', null);
Response::respondSuccess();
}

View File

@ -0,0 +1,25 @@
<?php
use Respect\Validation\Validator as DataValidator;
class GetLogsController extends Controller {
const PATH = '/get-logs';
public function validations() {
return [
'permission' => 'staff_1',
'requestData' => [
'page' => [
'validation' => DataValidator::numeric(),
'error' => ERRORS::INVALID_PAGE
]
]
];
}
public function handler() {
$page =Controller::request('page');
$logList = Log::find('LIMIT ? OFFSET ?', [10, 10*($page-1)+1]);
Response::respondSuccess($logList->toArray());
}
}

View File

@ -34,6 +34,8 @@ class AddCustomResponseController extends Controller {
]);
$customResponse->store();
Log::createLog('ADD_CUSTOM_RESPONSE', null);
Response::respondSuccess();
}
}

View File

@ -43,6 +43,9 @@ class ChangeDepartmentController extends Controller {
$ticket->department = $department;
$ticket->unread = true;
$ticket->store();
Log::createLog('CHANGE_DEPARTMENT', $department);
Response::respondSuccess();
}
}

View File

@ -37,6 +37,8 @@ class ChangePriorityController extends Controller {
));
$ticket->addEvent($event);
$ticket->store();
Log::createLog('CHANGE_PRIORITY', $priority);
Response::respondSuccess();
} else {
Response::respondError(ERRORS::NO_PERMISSION);

View File

@ -32,6 +32,9 @@ class CloseController extends Controller {
$this->ticket->closed = true;
$this->ticket->store();
Log::createLog('CLOSE_TICKET', $this->ticket);
Response::respondSuccess();
}

View File

@ -30,6 +30,9 @@ class CommentController extends Controller {
if ($session->isLoggedWithId($this->ticket->author->id) || Controller::isStaffLogged()) {
$this->storeComment();
Log::createLog('COMMENT_TICKET', $this->ticket);
Response::respondSuccess();
} else {
Response::respondError(ERRORS::NO_PERMISSION);

View File

@ -43,6 +43,7 @@ class CreateController extends Controller {
$this->storeTicket();
Log::createLog('CREATE_TICKET', $this->title);
Response::respondSuccess([
'ticketNumber' => $this->ticketNumber
]);

View File

@ -21,6 +21,8 @@ class DeleteCustomResponseController extends Controller {
$customResponse = CustomResponse::getDataStore(Controller::request('id'));
$customResponse->delete();
Log::createLog('DELETE_CUSTOM_RESPONSE', null);
Response::respondSuccess();
}
}

View File

@ -34,6 +34,7 @@ class EditCustomResponseController extends Controller {
$customResponse->store();
Log::createLog('EDIT_CUSTOM_RESPONSE', null);
Response::respondSuccess();
}
}

View File

@ -31,6 +31,9 @@ class ReOpenController extends Controller {
$this->ticket->closed = false;
$this->ticket->store();
Log::createLog('RE_OPEN_TICKET', $this->ticket);
Response::respondSuccess();
}

View File

@ -29,6 +29,8 @@ class BanUserController extends Controller {
$ban->store();
Log::createLog('BAN_USER', $email);
Response::respondSuccess();
} else {
Response::respondError(ERRORS::ALREADY_BANNED);

View File

@ -1,5 +1,7 @@
<?php
use Respect\Validation\Validator as DataValidator;
use RedBeanPHP\Facade as RedBean;
DataValidator::with('CustomValidations', true);
class DeleteUserController extends Controller {
@ -21,7 +23,10 @@ class DeleteUserController extends Controller {
$userId = Controller::request('userId');
$user = User::getDataStore($userId);
Log::createLog('DELETE_USER', $user->name);
RedBean::exec('DELETE FROM log WHERE author_user_id = ?', [$userId]);
$user->delete();
Response::respondSuccess();
}
}

View File

@ -58,7 +58,8 @@ class SignUpController extends Controller {
'userId' => $userId,
'userEmail' => $this->userEmail
]);
Log::createLog('SIGNUP', null, User::getDataStore($userId));
}
public function storeRequestData() {

View File

@ -24,6 +24,9 @@ class UnBanUserController extends Controller {
Response::respondError(ERRORS::INVALID_EMAIL);
} else {
$banRow->delete();
Log::createLog('UN_BAN_USER', $email);
Response::respondSuccess();
}

43
server/models/Log.php Normal file
View File

@ -0,0 +1,43 @@
<?php
use RedBeanPHP\Facade as RedBean;
class Log extends DataStore {
const TABLE = 'log';
public static function getProps() {
return [
'type',
'authorUser',
'authorStaff',
'to'
];
}
public static function createLog($type,$to, $author = null) {
if($author === null) {
$author = Controller::getLoggedUser();
}
$log = new Log();
$log->setProperties(array(
'type' => $type,
'to' => $to
));
if($author instanceof User) {
$log->authorUser = $author;
} else {
$log->authorStaff = $author;
}
$log->store();
}
public function toArray() {
return [
'type' => $this->type,
'to' => $this->to,
'author' => ($this->authorUser instanceof User) ? $this->authorUser->toArray() : $this->authorStaff->toArray()
];
}
}

View File

@ -29,4 +29,12 @@ class User extends DataStore {
public static function getUser($value, $property = 'id') {
return parent::getDataStore($value, $property);
}
public function toArray() {
return [
'email' => $this->email,
'id' => $this->id,
'name' => $this->name
];
}
}

View File

@ -27,6 +27,9 @@ describe 'Article path' do
(article['content']).should.equal('This is an article about server management.')
(article['topic_id']).should.equal(@topic_id.to_s)
(article['position']).should.equal('1')
lastLog = $database.getLastRow('log')
(lastLog['type']).should.equal('ADD_ARTICLE')
end
it 'should edit article' do
@ -43,6 +46,9 @@ describe 'Article path' do
(article['content']).should.equal('This is an article about server management2.')
(article['topic_id']).should.equal(@topic_id.to_s)
(article['position']).should.equal('1')
lastLog = $database.getLastRow('log')
(lastLog['type']).should.equal('EDIT_ARTICLE')
end
it 'should edit article topic' do
@ -76,6 +82,9 @@ describe 'Article path' do
csrf_token: $csrf_token
})
(result['status']).should.equal('success')
lastLog = $database.getLastRow('log')
(lastLog['type']).should.equal('DELETE_ARTICLE')
end
it 'should retrieve all articles' do
@ -103,5 +112,6 @@ describe 'Article path' do
(result['data'][0]['articles'][0]['title']).should.equal('Some article')
(result['data'][0]['articles'][0]['content']).should.equal('This is an article about server management.')
(result['data'][0]['articles'][0]['position']).should.equal('1')
end
end
end

View File

@ -17,6 +17,9 @@ describe 'Topic paths' do
(topic['name']).should.equal('Server management')
(topic['icon_color']).should.equal('red')
(topic['icon']).should.equal('cogs')
lastLog = $database.getLastRow('log')
(lastLog['type']).should.equal('ADD_TOPIC')
end
it 'should edit topic correctly' do
@ -44,6 +47,9 @@ describe 'Topic paths' do
})
(result['status']).should.equal('success')
lastLog = $database.getLastRow('log')
(lastLog['type']).should.equal('DELETE_TOPIC')
end
it 'should deny permission if it is not logged as staff' do
@ -78,4 +84,4 @@ describe 'Topic paths' do
(result['status']).should.equal('fail')
(result['message']).should.equal('NO_PERMISSION')
end
end
end

View File

@ -23,6 +23,12 @@ class Database
return queryResponse.fetch_hash
end
def getLastRow(table)
queryResponse = @connection.query("select * from #{table} order by id desc limit 1")
return queryResponse.fetch_hash
end
end
$database = Database.new

View File

@ -7,7 +7,7 @@ class Scripts
})
if response['status'] === 'fail'
raise 'Could not create user'
raise response['message']
end
userRow = $database.getRow('user', email, 'email')
response = request('/user/verify', {

View File

@ -26,6 +26,9 @@ describe'/staff/add' do
row = $database.getRow('department', 1, 'id')
(row['owners']).should.equal('2')
lastLog = $database.getLastRow('log')
(lastLog['type']).should.equal('ADD_STAFF')
end
it 'should fail if staff member is alrady a staff' do
result= request('/staff/add', {

View File

@ -14,5 +14,8 @@ describe'system/add-department' do
row = $database.getRow('department', 4, 'id')
(row['name']).should.equal('new department')
lastLog = $database.getLastRow('log')
(lastLog['type']).should.equal('ADD_DEPARTMENT')
end
end
end

View File

@ -81,5 +81,8 @@ describe 'system/delete-department' do
(ticket3['department_id']).should.equal('2')
(ticket3['owner_id']).should.equal($csrf_userid)
lastLog = $database.getLastRow('log')
(lastLog['type']).should.equal('DELETE_DEPARTMENT')
end
end
end

View File

@ -15,5 +15,8 @@ describe'system/edit-department' do
row = $database.getRow('department', 4, 'id')
(row['name']).should.equal('second name')
lastLog = $database.getLastRow('log')
(lastLog['type']).should.equal('EDIT_DEPARTMENT')
end
end
end

View File

@ -1,40 +1,85 @@
describe'system/edit-settings' do
request('/user/logout')
Scripts.login($staff[:email], $staff[:password], true)
it 'should edit settings' do
result= request('/system/edit-settings', {
"csrf_userid" => $csrf_userid,
"csrf_token" => $csrf_token,
"maintenance-mode" => 1,
"time-zone" => -3,
"layout" => 'full-width',
"allow-attachments" => 1,
"max-size" => 2,
"language" => 'es',
"no-reply-email" => 'testemail@hotmail.com'
})
(result['status']).should.equal('success')
row = $database.getRow('setting', 'maintenance-mode', 'name')
(row['value']).should.equal('1')
row = $database.getRow('setting', 'time-zone', 'name')
(row['value']).should.equal('-3')
row = $database.getRow('setting', 'layout', 'name')
(row['value']).should.equal('full-width')
row = $database.getRow('setting', 'max-size', 'name')
(row['value']).should.equal('2')
row = $database.getRow('setting', 'language', 'name')
(row['value']).should.equal('es')
row = $database.getRow('setting', 'no-reply-email', 'name')
(row['value']).should.equal('testemail@hotmail.com')
request('/user/logout')
end
it 'should change allowed and supported languages' do
request('/user/logout')
Scripts.login($staff[:email], $staff[:password], true)
it 'should edit settings' do
result= request('/system/edit-settings', {
"csrf_userid" => $csrf_userid,
"csrf_token" => $csrf_token,
"maintenance-mode" => 1,
"time-zone" => -3,
"layout" => 'full-width',
"allow-attachments" => 1,
"max-size" => 2,
"language" => 'es',
"no-reply-email" => 'testemail@hotmail.com'
})
result= request('/system/edit-settings', {
"csrf_userid" => $csrf_userid,
"csrf_token" => $csrf_token,
"supportedLanguages" => '["en", "pr", "jp", "ru"]',
"allowedLanguages" => '["en","pr", "jp", "ru", "de"]'
})
(result['status']).should.equal('success')
(result['status']).should.equal('success')
row = $database.getRow('setting', 'maintenance-mode', 'name')
(row['value']).should.equal('1')
row = $database.getRow('language', 'en', 'code')
(row['supported']).should.equal('1')
row = $database.getRow('setting', 'time-zone', 'name')
(row['value']).should.equal('-3')
row = $database.getRow('language', 'pr', 'code')
(row['supported']).should.equal('1')
row = $database.getRow('setting', 'layout', 'name')
(row['value']).should.equal('full-width')
row = $database.getRow('language', 'jp', 'code')
(row['supported']).should.equal('1')
row = $database.getRow('setting', 'max-size', 'name')
(row['value']).should.equal('2')
row = $database.getRow('language', 'ru', 'code')
(row['supported']).should.equal('1')
row = $database.getRow('setting', 'language', 'name')
(row['value']).should.equal('es')
row = $database.getRow('language', 'en', 'code')
(row['allowed']).should.equal('1')
row = $database.getRow('setting', 'no-reply-email', 'name')
(row['value']).should.equal('testemail@hotmail.com')
row = $database.getRow('language', 'pr', 'code')
(row['allowed']).should.equal('1')
request('/user/logout')
end
end
row = $database.getRow('language', 'jp', 'code')
(row['allowed']).should.equal('1')
row = $database.getRow('language', 'ru', 'code')
(row['allowed']).should.equal('1')
row = $database.getRow('language', 'de', 'code')
(row['allowed']).should.equal('1')
lastLog = $database.getLastRow('log')
(lastLog['type']).should.equal('EDIT_SETTINGS')
request('/user/logout')
end
end

View File

@ -19,5 +19,8 @@ describe '/ticket/change-department' do
ticket = $database.getRow('ticket', 1 , 'id')
(ticket['unread']).should.equal('1')
(ticket['department_id']).should.equal('2')
lastLog = $database.getLastRow('log')
(lastLog['type']).should.equal('CHANGE_DEPARTMENT')
end
end
end

View File

@ -19,6 +19,9 @@ describe '/ticket/change-priority' do
ticket = $database.getRow('ticket', 1 , 'id')
(ticket['priority']).should.equal('high')
(ticket['unread']).should.equal('1')
lastLog = $database.getLastRow('log')
(lastLog['type']).should.equal('CHANGE_PRIORITY')
end
it 'should change priority to medium if everything is okey' do
@ -36,6 +39,9 @@ describe '/ticket/change-priority' do
ticket = $database.getRow('ticket', 1 , 'id')
(ticket['priority']).should.equal('medium')
(ticket['unread']).should.equal('1')
lastLog = $database.getLastRow('log')
(lastLog['type']).should.equal('CHANGE_PRIORITY')
end
it 'should change priority to low if everything is okey' do
@ -53,6 +59,9 @@ describe '/ticket/change-priority' do
ticket = $database.getRow('ticket', 1 , 'id')
(ticket['priority']).should.equal('low')
(ticket['unread']).should.equal('1')
lastLog = $database.getLastRow('log')
(lastLog['type']).should.equal('CHANGE_PRIORITY')
end
end
end

View File

@ -19,5 +19,7 @@ describe '/ticket/close' do
(ticket['closed']).should.equal('1')
(ticket['unread']).should.equal('1')
lastLog = $database.getLastRow('log')
(lastLog['type']).should.equal('CLOSE_TICKET')
end
end
end

View File

@ -73,6 +73,9 @@ describe '/ticket/comment/' do
(comment['type']).should.equal('COMMENT')
(comment['author_user_id']).should.equal($csrf_userid)
(ticket['unread_staff']).should.equal('1')
lastLog = $database.getLastRow('log')
(lastLog['type']).should.equal('COMMENT_TICKET')
end
it 'should fail if user is not the author nor owner' do

View File

@ -116,6 +116,9 @@ describe '/ticket/create' do
ticket_user_relation = $database.getRow('ticket_user', ticket['id'],'ticket_id')
(ticket_user_relation['user_id']).should.equal($csrf_userid)
lastLog = $database.getLastRow('log')
(lastLog['type']).should.equal('CREATE_TICKET')
end
it 'should set correct ticket number' do

View File

@ -18,6 +18,9 @@ describe 'CustomResponses' do
(customResponse['name']).should.equal('Some common problem')
(customResponse['content']).should.equal('this is the content of a custom response for a common problem')
(customResponse['language']).should.equal('en')
lastLog = $database.getLastRow('log')
(lastLog['type']).should.equal('ADD_CUSTOM_RESPONSE')
end
end
@ -36,6 +39,9 @@ describe 'CustomResponses' do
(customResponse['name']).should.equal('Some common problem')
(customResponse['content']).should.equal('this is the content of a custom response for a common problem 2')
(customResponse['language']).should.equal('en')
lastLog = $database.getLastRow('log')
(lastLog['type']).should.equal('EDIT_CUSTOM_RESPONSE')
end
end
@ -65,6 +71,9 @@ describe 'CustomResponses' do
(result['status']).should.equal('success')
customResponse = $database.getRow('customresponse', 1)
(customResponse).should.equal(nil)
lastLog = $database.getLastRow('log')
(lastLog['type']).should.equal('DELETE_CUSTOM_RESPONSE')
end
end
end
end

View File

@ -19,5 +19,7 @@ describe '/ticket/re-open' do
(ticket['closed']).should.equal('0')
(ticket['unread']).should.equal('1')
lastLog = $database.getLastRow('log')
(lastLog['type']).should.equal('RE_OPEN_TICKET')
end
end
end

View File

@ -22,6 +22,8 @@ describe '/user/ban' do
user = $database.getRow('ban', 1 , 'id')
(user['email']).should.equal('nothing@hotmail.com')
lastLog = $database.getLastRow('log')
(lastLog['type']).should.equal('BAN_USER')
end
it 'should get ban list' do
@ -58,6 +60,8 @@ describe '/user/ban' do
user = $database.getRow('ban', 1 , 'id')
(user).should.equal(nil)
lastLog = $database.getLastRow('log')
(lastLog['type']).should.equal('UN_BAN_USER')
end
it 'should not un-ban user if it is not banned' do
@ -72,4 +76,4 @@ describe '/user/ban' do
end
end
end

View File

@ -22,7 +22,7 @@ describe '/user/delete' do
user = $database.getRow('user', 4 , 'id')
(user).should.equal(nil)
lastLog = $database.getLastRow('log')
(lastLog['type']).should.equal('DELETE_USER')
end
end

View File

@ -15,6 +15,9 @@ describe '/user/signup' do
(userRow['email']).should.equal('steve@jobs.com')
(userRow['name']).should.equal('Steve Jobs')
lastLog = $database.getLastRow('log')
(lastLog['type']).should.equal('SIGNUP')
end
it 'should fail if name is invalid' do