Skip to content

Commit

Permalink
1.添加后台操作日志
Browse files Browse the repository at this point in the history
2.修复部分BUG
  • Loading branch information
liulipeng committed Jul 12, 2016
1 parent 06fb4bb commit 9ae3175
Show file tree
Hide file tree
Showing 17 changed files with 730 additions and 234 deletions.
9 changes: 9 additions & 0 deletions components/AccessControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace izyue\admin\components;

use izyue\admin\models\Log;
use yii\web\ForbiddenHttpException;
use yii\base\Module;
use Yii;
Expand Down Expand Up @@ -81,6 +82,14 @@ public function beforeAction($action)
$this->denyAccess($user);
}

/**
* @inheritdoc
*/
public function afterAction($action, $result) {
Log::addLog($action);
return parent::afterAction($action, $result);
}

/**
* Denies the access of the user.
* The default implementation will redirect the user to the login page if he is a guest;
Expand Down
5 changes: 5 additions & 0 deletions components/Configs.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ class Configs extends \yii\base\Object
*/
public $adminTable = '{{%admin}}';

/**
* @var string Admin table name.
*/
public $adminLogTable = '{{%admin_log}}';

/**
* @var array
*/
Expand Down
6 changes: 3 additions & 3 deletions components/MenuHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Yii;
use yii\caching\TagDependency;
use izyue\admin\models\Menu;
use yii\helpers\ArrayHelper;

/**
* MenuHelper used to generate menu depend of user role.
Expand Down Expand Up @@ -129,6 +130,7 @@ public static function getAssignedMenu($userId, $root = null, $callback = null,
$key = [__METHOD__, $assigned, $root];
$cache = null;
if ($refresh || $callback !== null || $cache === null || (($result = $cache->get($key)) === false)) {

$result = static::normalizeMenu($assigned, $menus, $callback, $root);
if ($cache !== null && $callback === null) {
$cache->set($key, $result, $config->cacheDuration, new TagDependency([
Expand Down Expand Up @@ -252,9 +254,7 @@ private static function normalizeMenu(&$assigned, &$menus, $callback, $parent =
$result = [];
$order = [];
foreach ($assigned as $id) {
$menu = $menus[$id];
// print_r($menu);
// die;
$menu = ArrayHelper::getValue($menus, $id);
if ($menu['parent'] == $parent) {
$menu['children'] = static::normalizeMenu($assigned, $menus, $callback, $id);
if ($callback !== null) {
Expand Down
81 changes: 81 additions & 0 deletions controllers/LogController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace izyue\admin\controllers;

use izyue\admin\components\MenuHelper;
use Yii;
use izyue\admin\models\Log;
use izyue\admin\models\searchs\Log as LogSearch;
use yii\helpers\Url;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use izyue\admin\components\Helper;

/**
* LogController
*
* @author liulipeng
* @since 1.0
*/
class LogController extends Controller
{

/**
* @inheritdoc
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
];
}

/**
* Lists all Log models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new LogSearch;
$dataProvider = $searchModel->search(Yii::$app->request->getQueryParams());

return $this->render('index', [
'dataProvider' => $dataProvider,
'searchModel' => $searchModel,
]);
}

/**
* Displays a single Log model.
* @param integer $id
* @return mixed
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}

/**
* Finds the Menu model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Log the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Log::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}
6 changes: 6 additions & 0 deletions messages/zh-CN/rbac-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,11 @@
'New route(s)' => '新路由',
'Search for avaliable' => '搜索可用的',
'Search for assigned' => '搜索已经分配',
'Url' => '链接',
'Email' => '邮箱',
'Ip' => 'IP',
'User Agent' => '浏览器代理商',
'Updated At' => '修改时间',
'Created At' => '添加时间',

];
82 changes: 0 additions & 82 deletions migrations/m160608_111327_create_menu_table.php

This file was deleted.

52 changes: 52 additions & 0 deletions migrations/m160712_034501_create_admin_log.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php


use \izyue\admin\components\Configs;
use yii\db\Migration;

class m160712_034501_create_admin_log extends Migration
{
public function up()
{
$tableOptions = null;
if ($this->db->driverName === 'mysql') {
// http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci
$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
}
$table = Configs::instance()->adminLogTable;
// Check if the table exists
if ($this->db->schema->getTableSchema($table, true) === null) {
$this->createTable($table, [
'id' => $this->primaryKey(),
'route' => $this->string(255)->notNull(),
'url' => $this->string(255)->notNull(),
'user_agent' => $this->string(255)->notNull(),
'gets' => $this->text(),
'posts' => $this->text()->notNull(),
'admin_id' => $this->integer()->notNull(),
'admin_email' => $this->string(255)->notNull(),
'ip' => $this->string(255)->notNull(),
'created_at' => $this->integer()->notNull(),
'updated_at' => $this->integer()->notNull(),
], $tableOptions);
}
}
public function down()
{
$userTable = Configs::instance()->adminTable;
if ($this->db->schema->getTableSchema($userTable, true) !== null) {
$this->dropTable($userTable);
}
}

/*
// Use safeUp/safeDown to run migration code within a transaction
public function safeUp()
{
}
public function safeDown()
{
}
*/
}
Loading

0 comments on commit 9ae3175

Please sign in to comment.