Authored by wuxiao

后台非权限菜单隐藏

... ... @@ -64,6 +64,20 @@ class AccessControl extends \yii\filters\AccessControl
/**
* 首先判断具有强制通过验证权限
*/
if (self::forceAccess() === true){
return true;
}
return Yii::$app->authManager->checkAccess(Yii::$app->user->getId(), $permissionName);
}
/**
* 判断是否具有强制通过验证权限
* @staticvar type $forceAccess
* @return boolean
*/
static function forceAccess()
{
static $forceAccess;
if ($forceAccess === null)
{
... ... @@ -72,13 +86,11 @@ class AccessControl extends \yii\filters\AccessControl
$forceAccess = true;
}elseif (in_array(Yii::$app->user->getId(),Yii::$app->authManager->getUserIdsByRole('admin'))){
$forceAccess = true;
}else{
$forceAccess = false;
}
}
if ($forceAccess === true){
return true;
}
return Yii::$app->authManager->checkAccess(Yii::$app->user->getId(), $permissionName);
return $forceAccess;
}
public function beforeAction($action)
... ...
... ... @@ -2,7 +2,7 @@
return [
'adminEmail' => 'admin@example.com',
'menu'=>array(
'index'=>array('管理首页',''),
'index'=>array('管理首页','index/index'),
'live'=>array(
'直播管理',array(
'room'=>array('直播间','live/room'),
... ...
... ... @@ -165,14 +165,10 @@ class AdminController extends BaseController
$allRoles = $auth->getRoles();
if (Yii::$app->request->isPost){
$roles = Yii::$app->request->post('roles',[]);
$auth->revokeAll($user_id);
foreach ($roles as $roleName){
if (empty($allRoles[$roleName])){
continue;
}
$role = $allRoles[$roleName];
$auth->assign($role,$user_id);
$roleName = Yii::$app->request->post('roleName');
if (!empty($allRoles[$roleName])){
$auth->revokeAll($user_id);
$auth->assign($allRoles[$roleName],$user_id);
}
}
... ... @@ -239,24 +235,24 @@ class AdminController extends BaseController
}
if(Yii::$app->request->isPost){
$posts = Yii::$app->request->post();
$model->username = $posts['username'];
$model->email = $posts['email'];
if (!empty($posts['password'])){
$model->setPassword($posts['password']);
$post = Yii::$app->request->post();
$model->username = $post['username'];
$model->email = $post['email'];
if (!empty($post['password'])){
$model->setPassword($post['password']);
$model->generateAuthKey();
$model->generatePasswordResetToken();
}
if($model->save()){
if (!empty($posts['password'])){
Yii::$app->session->setFlash('success', '密码修改成功。');
if (!empty($post['password'])){
Yii::$app->session->setFlash('info', '密码修改成功。');
}
Yii::$app->session->setFlash('success', '编辑成功。');
}else{
Yii::$app->session->setFlash('warning', current($model->getFirstErrors()));
}
return $this->refresh();
//return $this->refresh();
}
return $this->render('adminedit',[
... ... @@ -281,9 +277,9 @@ class AdminController extends BaseController
}
if(Yii::$app->request->isPost){
$posts = Yii::$app->request->post();
$model->name = $posts['name'];
$model->description = $posts['description'];
$post = Yii::$app->request->post();
$model->name = $post['name'];
$model->description = $post['description'];
if($this->authManager->update($name,$model)){
Yii::$app->session->setFlash('success', '编辑成功。');
... ... @@ -315,9 +311,9 @@ class AdminController extends BaseController
}
if(Yii::$app->request->isPost){
$posts = Yii::$app->request->post();
$model->name = $posts['name'];
$model->description = $posts['description'];
$post = Yii::$app->request->post();
$model->name = $post['name'];
$model->description = $post['description'];
if($this->authManager->update($name,$model)){
Yii::$app->session->setFlash('success', '编辑成功。');
... ...
... ... @@ -60,8 +60,9 @@ class BaseController extends Controller
$view->params['main_id'] = $main_id;
$view->params['sub_id'] = $sub_id;
$view->params['main_title'] = $this->main_title ? : (@Yii::$app->params['menu'][$main_id][0] ? : '');
$view->params['sub_title'] = $this->sub_title ? : (@Yii::$app->params['menu'][$main_id][1][$sub_id][0] ? : '');
$view->params['sub_title'] = $this->sub_title ? :
(!is_array(Yii::$app->params['menu'][$main_id][1]) ? '' :
(@Yii::$app->params['menu'][$main_id][1][$sub_id][0] ? : ''));
return parent::beforeAction($action);
}
... ...
... ... @@ -21,8 +21,8 @@ $this->title = $this->params['main_title'] . '-' . $this->params['sub_title'];
<?php foreach ($allRoles as $roleName=>$role):?>
<li class="col-xs-6 col-sm-4 col-md-3">
<div class="ckbox ckbox-default">
<?php echo Html::checkbox('roles[]', array_key_exists($roleName, $userRoles), ['id'=>$roleName,'value'=>$roleName])?>
<div class="rdio rdio-default">
<?php echo Html::radio('roleName', array_key_exists($roleName, $userRoles), ['id'=>$roleName,'value'=>$roleName])?>
<label for="<?=$roleName?>"><?=$role->description?></label>
</div>
</li>
... ...
... ... @@ -8,6 +8,7 @@ use backend\assets\TemplateAsset;
use backend\assets\Ie9Asset;
use yii\web\View;
use backend\widgets\Alert;
use backend\widgets\LeftMenu;
TemplateAsset::register($this);
Ie9Asset::register($this);
... ... @@ -32,7 +33,33 @@ Ie9Asset::register($this);
<section>
<div class="mainwrapper">
<?php echo $this->render('leftmenu');?>
<div class="leftpanel">
<div class="media profile-left">
<a class="pull-left profile-thumb" href="profile.html">
<img class="img-circle" src="/images/photos/profile.png" alt="">
</a>
<div class="media-body">
<?php if (Yii::$app->user->isGuest):?>
<h4 class="media-heading">Guest</h4>
<small class="text-muted">游客</small>
<?php else:?>
<h4 class="media-heading"><?=Yii::$app->user->getIdentity()->username?></h4>
<?php if ($userRoles = Yii::$app->authManager->getRolesByUser(Yii::$app->user->getId())):?>
<small class="text-muted"><?=current($userRoles)->description?></small>
<?php endif;?>
<?php endif;?>
</div>
</div><!-- media -->
<h5 class="leftpanel-title">Navigation</h5>
<ul class="nav nav-pills nav-stacked">
<?php echo LeftMenu::widget() ?>
</ul>
</div><!-- leftpanel -->
<div class="mainpanel">
<div class="pageheader">
... ... @@ -56,6 +83,7 @@ Ie9Asset::register($this);
</div><!-- contentpanel -->
</div><!-- mainpanel -->
</div><!-- mainwrapper -->
</section>
... ...
<?php
namespace backend\widgets;
use Yii;
use backend\components\filters\AccessControl;
/**
* 管理后台左侧菜单
*/
class LeftMenu extends \yii\base\Widget
{
public function init()
{
parent::init();
}
/**
* Executes the widget.
* @return string the result of widget execution to be outputted.
*/
public function run()
{
if (AccessControl::forceAccess()){
$menu = Yii::$app->params['menu'];
}else{
$userPermissions = Yii::$app->authManager->getPermissionsByUser(Yii::$app->user->getId());
$menu = [];
foreach (Yii::$app->params['menu'] as $controller=>$main){
if (is_array($main[1])){
$subs = [];
foreach ($main[1] as $action=>$sub){
if (empty($sub[1])){
continue;
}
if (!empty($userPermissions[$sub[1]])){
$subs[$action] = $sub;
}
}
if (!empty($subs)){
$menu[$controller] = array($main[0],$subs);
}
}else{
if (empty($main[1])){
continue;
}
if (!empty($userPermissions[$main[1]])){
$menu[$controller] = $main;
}
}
}
}
return $this->render('leftmenu',[
'menu'=>$menu
]);
}
}
... ...
<div class="leftpanel">
<div class="media profile-left">
<a class="pull-left profile-thumb" href="profile.html">
<img class="img-circle" src="/images/photos/profile.png" alt="">
</a>
<div class="media-body">
<h4 class="media-heading"><?=Yii::$app->user->isGuest ? 'Guest' : Yii::$app->user->getIdentity()->username?></h4>
<small class="text-muted">超级管理员</small>
</div>
</div><!-- media -->
<h5 class="leftpanel-title">Navigation</h5>
<ul class="nav nav-pills nav-stacked">
<?php foreach (Yii::$app->params['menu'] as $controller=>$main):?>
<?php if (is_array($main[1])):?>
<li class="parent <?=($this->params['main_id'] == $controller) ? 'active' : ''?>"><a href="javascript:;"><i class="fa fa-suitcase"></i> <span><?=$main[0]?></span></a>
<ul class="children">
<?php foreach ($main[1] as $action=>$sub):?>
<li class="<?=($this->params['sub_id'] == $action) ? 'active' : ''?>"><a href="<?php echo Yii::getAlias('@web/'.$sub[1])?>"><?=$sub[0]?></a></li>
<?php endforeach;?>
</ul>
<?php else:?>
<li class="<?=($this->params['main_id'] == $controller) ? 'active' : ''?>"><a href="<?php echo Yii::getAlias('@web/'.$main[1])?>"><i class="fa fa-suitcase"></i> <span><?=$main[0]?></span></a>
<?php endif;?>
</li>
<?php endforeach;?>
<?php foreach ($menu as $controller=>$main):?>
<?php if (is_array($main[1])):?>
<li class="parent <?=($this->params['main_id'] == $controller) ? 'active' : ''?>"><a href="javascript:;"><i class="fa fa-suitcase"></i> <span><?=$main[0]?></span></a>
<ul class="children">
<?php foreach ($main[1] as $action=>$sub):?>
<li class="<?=($this->params['sub_id'] == $action) ? 'active' : ''?>"><a href="<?php echo Yii::getAlias('@web/'.$sub[1])?>"><?=$sub[0]?></a></li>
<?php endforeach;?>
</ul>
<?php else:?>
<li class="<?=($this->params['main_id'] == $controller) ? 'active' : ''?>"><a href="<?php echo Yii::getAlias('@web/'.$main[1])?>"><i class="fa fa-suitcase"></i> <span><?=$main[0]?></span></a>
<?php endif;?>
</li>
</div><!-- leftpanel -->
\ No newline at end of file
<?php endforeach;?>
\ No newline at end of file
... ...