Skip to content
This repository has been archived by the owner on Apr 13, 2022. It is now read-only.

Commit

Permalink
Admin Tool Logging
Browse files Browse the repository at this point in the history
Added functionality to log each create, update, delete action via admin tool with the corresponding
user in a database table.
  • Loading branch information
novazembla committed Sep 4, 2011
1 parent 0aba2a5 commit f07557a
Show file tree
Hide file tree
Showing 40 changed files with 649 additions and 147 deletions.
Binary file added documentation/mg_database_schema_v0.5.6.mwb
Binary file not shown.
6 changes: 6 additions & 0 deletions www/install_log.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
),

SAVE the file

6.1 configure the admin email address on the very bottom of the same file.

7. Goto the website and see it complain that the active record classes can't be found in the database (CHECK 1 system works)

Expand Down Expand Up @@ -66,3 +68,7 @@ PWD: player
16. Choose Zentag and play ;-)
Note: everytime the system shows a previously not shown image it has to create resized versions. Future responses will be faster.
Note: Yes, at the moment images can repeat. There are plans to omit that.

FURTHER INFO
you can now configure a second app environment in /www/protected/config/development.php. This can be access via /www/index_dev.php. This is
used by me to test the development branch. It will not make it into the master branch.
23 changes: 23 additions & 0 deletions www/protected/components/MGHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,27 @@ public static function createSharedSecretAndSession($user_id, $user_name) {
}
return Yii::app()->session[$api_id .'_SHARED_SECRET'];
}

/**
* Creates an entry in the log table
*
* @param string $category The category of the action to be logged (create, update, delte, batch_*)
* @param string $message The information to be logged
* @param int $user_id The id of the user. If $user_id is null this method will try to set the current user's id
*/
public static function log($category, $message, $user_id=null) {
if (is_null($user_id))
$user_id = Yii::app()->user->id;

$sql=" INSERT INTO {{log}}
(category, message, user_id, created) VALUES
(:category, :message, :userID, :created)";

$command=Yii::app()->db->createCommand($sql);
$command->bindValue(':category' ,$category);
$command->bindValue(':message', $message);
$command->bindValue(':created', date('Y-m-d H:i:s'));
$command->bindValue(':userID', $user_id);
$command->execute();
}
}
7 changes: 7 additions & 0 deletions www/protected/data/fbvsettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@
'url' => '/admin/badge',
'role' => 'editor',
),
'tool-logs' =>
array (
'name' => 'Admin Tool Log',
'description' => 'Some short description',
'url' => '/admin/log',
'role' => 'dbmanager',
),
),
'games' =>
array (
Expand Down
5 changes: 3 additions & 2 deletions www/protected/data/mg.mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,10 @@ DROP TABLE IF EXISTS `log` ;

CREATE TABLE IF NOT EXISTS `log` (
`id` INT(11) NOT NULL AUTO_INCREMENT ,
`level` VARCHAR(128) NOT NULL ,
`category` VARCHAR(128) NOT NULL ,
`logtime` INT UNSIGNED NOT NULL ,
`message` TEXT NOT NULL ,
`user_id` INT(11) NULL ,
`created` DATETIME NOT NULL ,
PRIMARY KEY (`id`) ,
CONSTRAINT `fk_log_users1`
FOREIGN KEY (`user_id` )
Expand Down Expand Up @@ -676,6 +675,8 @@ CREATE INDEX `fk_game_partner_session1` ON `game_partner` (`session_id` ASC) ;
CREATE INDEX `fk_game_partner_game1` ON `game_partner` (`game_id` ASC) ;


CREATE USER `metadatagames` IDENTIFIED BY 'metadatagames';


SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function actionCreate() {
<?php else: ?>
if ($model->save()) {
<?php endif; ?>
MGHelper::log('create', 'Created <?php echo $this->modelClass; ?> with ID(' . $model-><?php echo $this->tableSchema->primaryKey; ?> . ')');
Flash::add('success', Yii::t('app', "<?php echo $this->modelClass; ?> created"));
if (Yii::app()->getRequest()->getIsAjaxRequest())
Yii::app()->end();
Expand Down Expand Up @@ -69,6 +70,7 @@ public function actionUpdate($id) {
<?php else: ?>
if ($model->save()) {
<?php endif; ?>
MGHelper::log('update', 'Updated <?php echo $this->modelClass; ?> with ID(' . $id . ')');
Flash::add('success', Yii::t('app', "<?php echo $this->modelClass; ?> updated"));
$this->redirect(array('view', 'id' => $model-><?php echo $this->tableSchema->primaryKey; ?>));
}
Expand All @@ -85,7 +87,9 @@ public function actionDelete($id) {
if ($model->hasAttribute("locked") && $model->locked) {
throw new CHttpException(400, Yii::t('app', 'Your request is invalid.'));
} else {
$model->delete();
$model->delete();
MGHelper::log('delete', 'Deleted <?php echo $this->modelClass; ?> with ID(' . $id . ')');

Flash::add('success', Yii::t('app', "<?php echo $this->modelClass; ?> deleted"));

if (!Yii::app()->getRequest()->getIsAjaxRequest())
Expand Down Expand Up @@ -139,9 +143,11 @@ private function _batchDelete() {
$criteria=new CDbCriteria;
$criteria->addInCondition("id", $_POST['<?php echo $this->class2id($this->modelClass)?>-ids']);
<?php echo ($this->tableSchema->getColumn("locked") !== null)? "\$criteria->addInCondition(\"locked\", array(0));" : ""; ?>

MGHelper::log('batch-delete', 'Batch deleted <?php echo $this->modelClass; ?> with IDs(' . implode(',', $_POST['<?php echo $this->class2id($this->modelClass)?>-ids']) . ')');

$model = new <?php echo $this->modelClass; ?>;
$model->deleteAll($criteria);
$model->deleteAll($criteria);

}
}
}
9 changes: 6 additions & 3 deletions www/protected/models/_base/BaseImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* @property string $name
* @property integer $size
* @property string $mime_type
* @property string $batch_id
* @property string $last_access
* @property integer $locked
* @property string $created
Expand Down Expand Up @@ -44,10 +45,10 @@ public function rules() {
array('name, size, mime_type, created, modified', 'required'),
array('size, locked', 'numerical', 'integerOnly'=>true),
array('name', 'length', 'max'=>254),
array('mime_type', 'length', 'max'=>45),
array('mime_type, batch_id', 'length', 'max'=>45),
array('last_access', 'safe'),
array('last_access, locked', 'default', 'setOnEmpty' => true, 'value' => null),
array('id, name, size, mime_type, last_access, locked, created, modified', 'safe', 'on'=>'search'),
array('batch_id, last_access, locked', 'default', 'setOnEmpty' => true, 'value' => null),
array('id, name, size, mime_type, batch_id, last_access, locked, created, modified', 'safe', 'on'=>'search'),
);
}

Expand All @@ -70,6 +71,7 @@ public function attributeLabels() {
'name' => Yii::t('app', 'Name'),
'size' => Yii::t('app', 'Size'),
'mime_type' => Yii::t('app', 'Mime Type'),
'batch_id' => Yii::t('app', 'Batch'),
'last_access' => Yii::t('app', 'Last Access'),
'locked' => Yii::t('app', 'Locked'),
'created' => Yii::t('app', 'Created'),
Expand All @@ -86,6 +88,7 @@ public function search() {
$criteria->compare('name', $this->name, true);
$criteria->compare('size', $this->size);
$criteria->compare('mime_type', $this->mime_type, true);
$criteria->compare('batch_id', $this->batch_id, true);
$criteria->compare('last_access', $this->last_access, true);
$criteria->compare('locked', $this->locked);
$criteria->compare('created', $this->created, true);
Expand Down
18 changes: 7 additions & 11 deletions www/protected/models/_base/BaseLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
* followed by relations of table "log" available as properties of the model.
*
* @property integer $id
* @property string $level
* @property string $category
* @property string $logtime
* @property string $message
* @property integer $user_id
* @property string $created
*
* @property User $user
*/
Expand All @@ -33,17 +32,16 @@ public static function label($n = 1) {
}

public static function representingColumn() {
return 'level';
return 'category';
}

public function rules() {
return array(
array('level, category, logtime, message', 'required'),
array('category, message, created', 'required'),
array('user_id', 'numerical', 'integerOnly'=>true),
array('level, category', 'length', 'max'=>128),
array('logtime', 'length', 'max'=>10),
array('category', 'length', 'max'=>128),
array('user_id', 'default', 'setOnEmpty' => true, 'value' => null),
array('id, level, category, logtime, message, user_id', 'safe', 'on'=>'search'),
array('id, category, message, user_id, created', 'safe', 'on'=>'search'),
);
}

Expand All @@ -61,11 +59,10 @@ public function pivotModels() {
public function attributeLabels() {
return array(
'id' => Yii::t('app', 'ID'),
'level' => Yii::t('app', 'Level'),
'category' => Yii::t('app', 'Category'),
'logtime' => Yii::t('app', 'Logtime'),
'message' => Yii::t('app', 'Message'),
'user_id' => null,
'created' => Yii::t('app', 'Created'),
'user' => null,
);
}
Expand All @@ -74,11 +71,10 @@ public function search() {
$criteria = new CDbCriteria;

$criteria->compare('id', $this->id);
$criteria->compare('level', $this->level, true);
$criteria->compare('category', $this->category, true);
$criteria->compare('logtime', $this->logtime, true);
$criteria->compare('message', $this->message, true);
$criteria->compare('user_id', $this->user_id);
$criteria->compare('created', $this->created, true);

return new CActiveDataProvider($this, array(
'criteria' => $criteria,
Expand Down
12 changes: 9 additions & 3 deletions www/protected/modules/admin/controllers/BadgeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function actionCreate() {
$model->setAttributes($_POST['Badge']);

if ($model->save()) {
MGHelper::log('create', 'Created Badge with ID(' . $model->id . ')');
Flash::add('success', Yii::t('app', "Badge created"));
if (Yii::app()->getRequest()->getIsAjaxRequest())
Yii::app()->end();
Expand All @@ -60,6 +61,7 @@ public function actionUpdate($id) {
$model->setAttributes($_POST['Badge']);

if ($model->save()) {
MGHelper::log('update', 'Updated Badge with ID(' . $id . ')');
Flash::add('success', Yii::t('app', "Badge updated"));
$this->redirect(array('view', 'id' => $model->id));
}
Expand All @@ -76,7 +78,9 @@ public function actionDelete($id) {
if ($model->hasAttribute("locked") && $model->locked) {
throw new CHttpException(400, Yii::t('app', 'Your request is invalid.'));
} else {
$model->delete();
$model->delete();
MGHelper::log('delete', 'Deleted Badge with ID(' . $id . ')');

Flash::add('success', Yii::t('app', "Badge deleted"));

if (!Yii::app()->getRequest()->getIsAjaxRequest())
Expand Down Expand Up @@ -129,9 +133,11 @@ private function _batchDelete() {
if (isset($_POST['badge-ids'])) {
$criteria=new CDbCriteria;
$criteria->addInCondition("id", $_POST['badge-ids']);

MGHelper::log('batch-delete', 'Batch deleted Badge with IDs(' . implode(',', $_POST['badge-ids']) . ')');

$model = new Badge;
$model->deleteAll($criteria);
$model->deleteAll($criteria);

}
}
}
12 changes: 9 additions & 3 deletions www/protected/modules/admin/controllers/BlockedIpController.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function actionCreate() {
$model->setAttributes($_POST['BlockedIp']);

if ($model->save()) {
MGHelper::log('create', 'Created BlockedIp with ID(' . $model->id . ')');
Flash::add('success', Yii::t('app', "BlockedIp created"));
if (Yii::app()->getRequest()->getIsAjaxRequest())
Yii::app()->end();
Expand All @@ -61,6 +62,7 @@ public function actionUpdate($id) {
$model->setAttributes($_POST['BlockedIp']);

if ($model->save()) {
MGHelper::log('update', 'Updated BlockedIp with ID(' . $id . ')');
Flash::add('success', Yii::t('app', "BlockedIp updated"));
$this->redirect(array('view', 'id' => $model->id));
}
Expand All @@ -77,7 +79,9 @@ public function actionDelete($id) {
if ($model->hasAttribute("locked") && $model->locked) {
throw new CHttpException(400, Yii::t('app', 'Your request is invalid.'));
} else {
$model->delete();
$model->delete();
MGHelper::log('delete', 'Deleted BlockedIp with ID(' . $id . ')');

Flash::add('success', Yii::t('app', "BlockedIp deleted"));

if (!Yii::app()->getRequest()->getIsAjaxRequest())
Expand Down Expand Up @@ -130,9 +134,11 @@ private function _batchDelete() {
if (isset($_POST['blocked-ip-ids'])) {
$criteria=new CDbCriteria;
$criteria->addInCondition("id", $_POST['blocked-ip-ids']);

MGHelper::log('batch-delete', 'Batch deleted BlockedIp with IDs(' . implode(',', $_POST['blocked-ip-ids']) . ')');

$model = new BlockedIp;
$model->deleteAll($criteria);
$model->deleteAll($criteria);

}
}
}
12 changes: 9 additions & 3 deletions www/protected/modules/admin/controllers/ImageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function actionCreate() {
);

if ($model->saveWithRelated($relatedData)) {
MGHelper::log('create', 'Created Image with ID(' . $model->id . ')');
Flash::add('success', Yii::t('app', "Image created"));
if (Yii::app()->getRequest()->getIsAjaxRequest())
Yii::app()->end();
Expand All @@ -67,6 +68,7 @@ public function actionUpdate($id) {
);

if ($model->saveWithRelated($relatedData)) {
MGHelper::log('update', 'Updated Image with ID(' . $id . ')');
Flash::add('success', Yii::t('app', "Image updated"));
$this->redirect(array('view', 'id' => $model->id));
}
Expand All @@ -83,7 +85,9 @@ public function actionDelete($id) {
if ($model->hasAttribute("locked") && $model->locked) {
throw new CHttpException(400, Yii::t('app', 'Your request is invalid.'));
} else {
$model->delete();
$model->delete();
MGHelper::log('delete', 'Deleted Image with ID(' . $id . ')');

Flash::add('success', Yii::t('app', "Image deleted"));

if (!Yii::app()->getRequest()->getIsAjaxRequest())
Expand Down Expand Up @@ -136,9 +140,11 @@ private function _batchDelete() {
if (isset($_POST['image-ids'])) {
$criteria=new CDbCriteria;
$criteria->addInCondition("id", $_POST['image-ids']);
$criteria->addInCondition("locked", array(0));
$criteria->addInCondition("locked", array(0)); MGHelper::log('batch-delete', 'Batch deleted Image with IDs(' . implode(',', $_POST['image-ids']) . ')');

$model = new Image;
$model->deleteAll($criteria);
$model->deleteAll($criteria);

}
}
}
12 changes: 9 additions & 3 deletions www/protected/modules/admin/controllers/ImageSetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function actionCreate() {
);

if ($model->saveWithRelated($relatedData)) {
MGHelper::log('create', 'Created ImageSet with ID(' . $model->id . ')');
Flash::add('success', Yii::t('app', "ImageSet created"));
if (Yii::app()->getRequest()->getIsAjaxRequest())
Yii::app()->end();
Expand All @@ -71,6 +72,7 @@ public function actionUpdate($id) {
);

if ($model->saveWithRelated($relatedData)) {
MGHelper::log('update', 'Updated ImageSet with ID(' . $id . ')');
Flash::add('success', Yii::t('app', "ImageSet updated"));
$this->redirect(array('view', 'id' => $model->id));
}
Expand All @@ -87,7 +89,9 @@ public function actionDelete($id) {
if ($model->hasAttribute("locked") && $model->locked) {
throw new CHttpException(400, Yii::t('app', 'Your request is invalid.'));
} else {
$model->delete();
$model->delete();
MGHelper::log('delete', 'Deleted ImageSet with ID(' . $id . ')');

Flash::add('success', Yii::t('app', "ImageSet deleted"));

if (!Yii::app()->getRequest()->getIsAjaxRequest())
Expand Down Expand Up @@ -140,9 +144,11 @@ private function _batchDelete() {
if (isset($_POST['image-set-ids'])) {
$criteria=new CDbCriteria;
$criteria->addInCondition("id", $_POST['image-set-ids']);
$criteria->addInCondition("locked", array(0));
$criteria->addInCondition("locked", array(0)); MGHelper::log('batch-delete', 'Batch deleted ImageSet with IDs(' . implode(',', $_POST['image-set-ids']) . ')');

$model = new ImageSet;
$model->deleteAll($criteria);
$model->deleteAll($criteria);

}
}
}
Loading

0 comments on commit f07557a

Please sign in to comment.