forked from robwilkerson/CakePHP-Audit-Log-Plugin
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
change source_id to user_id, update tables
- Loading branch information
imsamurai
committed
Oct 27, 2014
1 parent
c0fa6bc
commit d830807
Showing
22 changed files
with
1,171 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,26 @@ | ||
-- phpMyAdmin SQL Dump | ||
-- version 3.4.5deb1 | ||
-- http://www.phpmyadmin.net | ||
-- | ||
-- Host: localhost | ||
-- Generation Time: Mar 11, 2012 at 10:10 PM | ||
-- Server version: 5.1.58 | ||
-- PHP Version: 5.3.6-13ubuntu3.6 | ||
|
||
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; | ||
SET time_zone = "+00:00"; | ||
|
||
|
||
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; | ||
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; | ||
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; | ||
/*!40101 SET NAMES utf8 */; | ||
|
||
-- | ||
-- Database: `42viral_default` | ||
-- | ||
|
||
-- -------------------------------------------------------- | ||
|
||
-- | ||
-- Table structure for table `audits` | ||
-- | ||
CREATE TABLE `audit_deltas` ( | ||
`id` varchar(36) NOT NULL, | ||
`audit_id` varchar(36) NOT NULL, | ||
`property_name` varchar(255) NOT NULL, | ||
`old_value` text, | ||
`new_value` text, | ||
PRIMARY KEY (`id`), | ||
KEY `audit_id` (`audit_id`) | ||
) ENGINE=MyISAM DEFAULT CHARSET=utf8; | ||
|
||
CREATE TABLE IF NOT EXISTS `audits` ( | ||
CREATE TABLE `audits` ( | ||
`id` varchar(36) NOT NULL, | ||
`event` varchar(255) NOT NULL, | ||
`model` varchar(255) NOT NULL, | ||
`entity_id` varchar(36) NOT NULL, | ||
`json_object` text NOT NULL, | ||
`description` text, | ||
`source_id` varchar(255) DEFAULT NULL, | ||
`user_id` bigint(20) unsigned DEFAULT '0', | ||
`created` datetime NOT NULL, | ||
PRIMARY KEY (`id`) | ||
) ENGINE=MyISAM DEFAULT CHARSET=utf8; | ||
|
||
-- -------------------------------------------------------- | ||
|
||
-- | ||
-- Table structure for table `audit_deltas` | ||
-- | ||
|
||
CREATE TABLE IF NOT EXISTS `audit_deltas` ( | ||
`id` varchar(36) NOT NULL, | ||
`audit_id` varchar(36) NOT NULL, | ||
`property_name` varchar(255) NOT NULL, | ||
`old_value` text, | ||
`new_value` text, | ||
PRIMARY KEY (`id`), | ||
KEY `audit_id` (`audit_id`) | ||
UNIQUE KEY `id_UNIQUE` (`id`), | ||
KEY `event` (`event`), | ||
KEY `model` (`model`), | ||
KEY `user_id` (`user_id`), | ||
KEY `created` (`created`) | ||
) ENGINE=MyISAM DEFAULT CHARSET=utf8; | ||
|
||
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; | ||
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; | ||
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
/** | ||
* Author: imsamurai <im.samuray@gmail.com> | ||
* Date: 27.10.2014 | ||
* Time: 12:51:06 | ||
*/ | ||
Configure::write('Pagination.pages', Configure::read('Pagination.pages') ? Configure::read('Pagination.pages') : 10); | ||
$config = (array)Configure::read('AuditLog'); | ||
$config = Hash::mergeDiff($config, array( | ||
'User' => array( | ||
'name' => 'email', | ||
'url' => array( | ||
'plugin' => false, | ||
'controller' => 'users', | ||
'action' => 'view', | ||
'___id___' | ||
) | ||
) | ||
)); | ||
Configure::write('AuditLog', $config); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
<?php | ||
|
||
/** | ||
* Author: imsamurai <im.samuray@gmail.com> | ||
* Date: 27.10.2014 | ||
* Time: 12:36:50 | ||
* Format: http://book.cakephp.org/2.0/en/controllers.html | ||
*/ | ||
App::uses('AuditLogAppController', 'AuditLog.Controller'); | ||
|
||
/** | ||
* AuditController | ||
* | ||
* @property Audit $Audit Audit model | ||
* @property User $User User model | ||
* | ||
* @package AuditLog | ||
* @subpackage Controller | ||
*/ | ||
class AuditController extends AuditLogAppController { | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @var array | ||
*/ | ||
public $uses = array( | ||
'User', 'AuditLog.Audit' | ||
); | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @var array | ||
*/ | ||
public $helpers = array( | ||
'AuditLog.Audit', 'Time' | ||
); | ||
|
||
/** | ||
* List of audit records | ||
*/ | ||
public function index() { | ||
$this->request->data('Audit', $this->request->query); | ||
$list = $this->request->data('Audit.list'); | ||
$count = $this->request->data('Audit.count'); | ||
$this->paginate = array( | ||
'Audit' => array( | ||
'limit' => ($list && $count) ? $count : Configure::read('Pagination.limit'), | ||
'fields' => array( | ||
'id', | ||
'event', | ||
'model', | ||
'created', | ||
'user_id', | ||
'entity_id' | ||
), | ||
'contain' => array( | ||
'User' => array( | ||
'fields' => array( | ||
'id', | ||
Configure::read('AuditLog.User.name') | ||
) | ||
) | ||
), | ||
'conditions' => $this->_paginationFilter(), | ||
'order' => array('created' => 'desc'), | ||
) | ||
); | ||
$this->set(array( | ||
'data' => $this->paginate("Audit"), | ||
'users' => $this->User->find('list', array( | ||
'fields' => array('id', Configure::read('AuditLog.User.name')) | ||
)), | ||
'_serialize' => array('data') | ||
)); | ||
if ($list) { | ||
$this->render('list'); | ||
} | ||
} | ||
|
||
/** | ||
* View audit details | ||
* | ||
* @param string $id | ||
* @throws NotFoundException | ||
*/ | ||
public function view($id) { | ||
$data = $this->Audit->find('first', array( | ||
'contain' => array( | ||
'Delta', | ||
'User' => array( | ||
'fields' => array( | ||
'id', | ||
Configure::read('AuditLog.User.name') | ||
) | ||
) | ||
), | ||
'conditions' => array( | ||
$this->Audit->alias . '.id' => $id | ||
) | ||
)); | ||
if (!$data) { | ||
throw new NotFoundException(__("Audit #%s does not exists!", $id)); | ||
} | ||
$this->set('data', $data); | ||
} | ||
|
||
/** | ||
* Builds pagination conditions from search form | ||
* | ||
* @return array | ||
*/ | ||
protected function _paginationFilter() { | ||
$conditions = array_filter($this->request->query, function($var) { | ||
return $var !== ''; | ||
}); | ||
unset($conditions['url']); | ||
foreach (array('created') as $dateRangeField) { | ||
if (empty($conditions[$dateRangeField])) { | ||
continue; | ||
} | ||
if (preg_match('/^(?P<start>.*)\s(-|to)\s(?P<end>.*)$/is', $conditions[$dateRangeField], $range)) { | ||
$conditions[$this->Audit->alias . '.' . $dateRangeField . ' BETWEEN ? AND ?'] = array( | ||
(new DateTime($range['start']))->format('Y-m-d H:i:s'), | ||
(new DateTime($range['end']))->format('Y-m-d H:i:s') | ||
); | ||
} | ||
unset($conditions[$dateRangeField]); | ||
} | ||
|
||
if (!empty($conditions['model'])) { | ||
$conditions['LOWER('.$this->Audit->alias.'.model) LIKE'] = "%" . mb_strtolower($conditions['model']) . "%"; | ||
} | ||
unset($conditions['model']); | ||
|
||
if (!empty($conditions['id'])) { | ||
$conditions[$this->Audit->alias . '.id'] = $conditions['id']; | ||
} | ||
unset($conditions['id']); | ||
unset($conditions['list']); | ||
unset($conditions['count']); | ||
|
||
return $conditions; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
/** | ||
* Author: imsamurai <im.samuray@gmail.com> | ||
* Date: 27.10.2014 | ||
* Time: 12:28:40 | ||
* Format: http://book.cakephp.org/2.0/en/controllers.html | ||
*/ | ||
App::uses('AppController', 'Controller'); | ||
|
||
/** | ||
* AuditLogAppController | ||
* | ||
* @package AuditLog | ||
* @subpackage Controller | ||
*/ | ||
class AuditLogAppController extends AppController { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
/** | ||
* Author: imsamurai <im.samuray@gmail.com> | ||
* Date: 27.10.2014 | ||
* Time: 12:27:44 | ||
* Format: http://book.cakephp.org/2.0/en/models.html | ||
*/ | ||
App::uses('AuditLogAppModel', 'AuditLog.Model'); | ||
|
||
/** | ||
* Audit Model | ||
* | ||
* @package AuditLog | ||
* @subpackage Model | ||
*/ | ||
class Audit extends AuditLogAppModel { | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @var string | ||
*/ | ||
public $name = 'Audit'; | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @var array | ||
*/ | ||
public $hasMany = array( | ||
'Delta' => array( | ||
'className' => 'AuditLog.AuditDelta' | ||
) | ||
); | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @var array | ||
*/ | ||
public $belongsTo = array( | ||
'User' | ||
); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
/** | ||
* Author: imsamurai <im.samuray@gmail.com> | ||
* Date: 27.10.2014 | ||
* Time: 12:27:44 | ||
* Format: http://book.cakephp.org/2.0/en/models.html | ||
*/ | ||
App::uses('AuditLogAppModel', 'AuditLog.Model'); | ||
|
||
/** | ||
* AuditDelta Model | ||
* | ||
* @package AuditLog | ||
* @subpackage Model | ||
*/ | ||
class AuditDelta extends AuditLogAppModel { | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @var string | ||
*/ | ||
public $name = 'AuditDelta'; | ||
|
||
} |
Oops, something went wrong.