forked from mdmsoft/yii2-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
mdmunirdeb
committed
Nov 25, 2013
1 parent
054a964
commit a0f3e8c
Showing
30 changed files
with
1,478 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
namespace mdm\auth\components; | ||
|
||
use yii\db\Query; | ||
use Yii; | ||
|
||
/** | ||
* Description of AccessControl | ||
* | ||
* @author MDMunir | ||
*/ | ||
class AccessControl extends \yii\base\Behavior | ||
{ | ||
|
||
public $tableAccess = 'm_access_route'; | ||
public $tableMenu = 'm_menu'; | ||
|
||
public function events() | ||
{ | ||
return[ | ||
//Application::EVENT_BEFORE_ACTION => 'beforeAction' | ||
]; | ||
} | ||
|
||
/** | ||
* | ||
* @param \yii\base\ActionEvent $event | ||
*/ | ||
public function beforeAction($event) | ||
{ | ||
$action = $event->action; | ||
if ($action->controller->hasMethod('allowAction') && in_array($action->id, $action->controller->allowAction())) { | ||
return; | ||
} | ||
$roles = AccessHelper::getItemsRole(); | ||
$names = array_keys($roles); | ||
|
||
$query = new Query; | ||
$count = $query->from($this->tableAccess) | ||
->where(['and', 'name' => $names, [ | ||
'or', 'route' => $action->uniqueId, 'route' => $action->controller->uniqueId . '/*' | ||
]]) | ||
->count(); | ||
if ($count == 0) { | ||
$this->denyAccess(Yii::$app->user); | ||
} | ||
} | ||
|
||
/** | ||
* Denies the access of the user. | ||
* The default implementation will redirect the user to the login page if he is a guest; | ||
* if the user is already logged, a 403 HTTP exception will be thrown. | ||
* @param yii\web\User $user the current user | ||
* @throws yii\web\HttpException if the user is already logged in. | ||
*/ | ||
protected function denyAccess($user) | ||
{ | ||
if ($user->getIsGuest()) { | ||
$user->loginRequired(); | ||
} else { | ||
throw new HttpException(403, Yii::t('yii', 'You are not allowed to perform this action.')); | ||
} | ||
} | ||
|
||
public function getMenu() | ||
{ | ||
// $roles = AccessHelper::getItemsRole(); | ||
// $names = array_keys($roles); | ||
|
||
$query = new Query; | ||
$names = ['c', 'b']; | ||
$items = $query->distinct() | ||
->select(['p.id as p_id', 'm.id', 'm.menu', 'm.route', 'm.priority']) | ||
->from($this->tableMenu . ' m') | ||
->innerJoin($this->tableAccess . ' a', ['or', | ||
'[[m.route]] = [[a.route]]', | ||
"[[m.route]] like concat([[a.route]],'%')"]) | ||
->leftJoin($this->tableMenu . ' p', '[[m.parent]]=[[p.id]]') | ||
->where(['name' => $names]) | ||
->orderBy('[[p.id]],[[m.priority]]') | ||
->createCommand() | ||
->queryAll(); | ||
return $this->buildMenuRecrusive($items); | ||
} | ||
|
||
protected function buildMenuRecrusive($items, $parent = null) | ||
{ | ||
$result = $priority = []; | ||
foreach ($items as $item) { | ||
if ($item['p_id'] === $parent) { | ||
$result[] = [ | ||
'label' => $item['menu'], | ||
'url' => [$item['route']], | ||
'items' => $this->buildMenuRecrusive($items, $item['id']) | ||
]; | ||
$priority[] = $item['priority']; | ||
} | ||
} | ||
array_multisort($priority, $result); | ||
return $result; | ||
} | ||
|
||
} |
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,132 @@ | ||
<?php | ||
|
||
namespace mdm\auth\components; | ||
|
||
use Yii; | ||
use yii\helpers\Inflector; | ||
|
||
/** | ||
* Description of AccessHelper | ||
* | ||
* @author MDMunir | ||
*/ | ||
class AccessHelper | ||
{ | ||
|
||
/** | ||
* | ||
* @return yii\rbac\Item[] | ||
*/ | ||
public static function getItemsRole() | ||
{ | ||
$manager = Yii::$app->getAuthManager(); | ||
if (Yii::$app->user->getIsGuest()) { | ||
$items = $manager->getItemChildren('guest'); | ||
} else { | ||
$items = $manager->getItems(Yii::$app->user->id); | ||
} | ||
if (count($items) > 0) { | ||
$key_cache = array_keys($items); | ||
$key_cache[] = '__ItemsRole__'; | ||
$cache = Yii::$app->getCache(); | ||
$result = $cache ? $cache->get($key_cache) : false; | ||
if ($result === false) { | ||
$result = self::getMenuItemRecrusive($items); | ||
$cache && $cache->set($key_cache, $result); | ||
} | ||
return $result; | ||
} | ||
return []; | ||
} | ||
|
||
/** | ||
* | ||
* @param yii\rbac\Item[] $items | ||
* @return yii\rbac\Item[] | ||
*/ | ||
protected static function getMenuItemRecrusive($items) | ||
{ | ||
$result = []; | ||
/* @var $item yii\rbac\Item */ | ||
foreach ($items as $name => $item) { | ||
$result[] = $name; | ||
if (($_items = $item->getChildren()) != [] && ($_result = self::getMenuItemRecrusive($_items)) != []) { | ||
foreach ($_result as $_item) { | ||
$result[] = $_item; | ||
} | ||
} | ||
} | ||
return $result; | ||
} | ||
|
||
/** | ||
* | ||
* @param \yii\base\Module $module | ||
* @return mixed List of all controller action. | ||
*/ | ||
public static function getRoutes($module = null) | ||
{ | ||
$result = []; | ||
if ($module === null) | ||
$module = Yii::$app; | ||
foreach ($module->getModules() as $id => $child) { | ||
if (($child = $module->getModule($id)) === null) { | ||
continue; | ||
} | ||
foreach (self::getRoutes($child) as $route) { | ||
$result[] = $route; | ||
} | ||
} | ||
/* @var $controller \yii\base\Controller */ | ||
foreach ($module->controllerMap as $id => $value) { | ||
$controller = Yii::createObject($value, $id, $module); | ||
$result[] = $controller->uniqueId . '/'; | ||
foreach (self::getActions($controller) as $route) { | ||
$result[] = $route; | ||
} | ||
} | ||
|
||
$path = $module->getControllerPath(); | ||
$namespace = $module->controllerNamespace . '\\'; | ||
$files = scandir($path); | ||
foreach ($files as $file) { | ||
if (strcmp(substr($file, -14), 'Controller.php') === 0) { | ||
$id = Inflector::camel2id(substr(basename($file), 0, -14)); | ||
$className = Inflector::id2camel($id) . 'Controller'; | ||
Yii::$classMap[$className] = $path . DIRECTORY_SEPARATOR . $className . '.php'; | ||
$className = ltrim($namespace . $className, '\\'); | ||
if (is_subclass_of($className, 'yii\base\Controller')) { | ||
$controller = new $className($id, $module); | ||
$result[] = $controller->uniqueId . '/'; | ||
foreach (self::getActions($controller) as $route) { | ||
$result[] = $route; | ||
} | ||
} | ||
} | ||
} | ||
return $result; | ||
} | ||
|
||
/** | ||
* | ||
* @param \yii\base\Controller $controller | ||
* @return mixed List of all controller action. | ||
*/ | ||
protected static function getActions($controller) | ||
{ | ||
$result = []; | ||
$prefix = $controller->uniqueId . '/'; | ||
foreach ($controller->actions() as $id => $value) { | ||
$result[] = $prefix . $id; | ||
} | ||
$class = new \ReflectionClass($controller); | ||
foreach ($class->getMethods() as $method) { | ||
$name = $method->getName(); | ||
if ($method->isPublic() && !$method->isStatic() && strpos($name, 'action') === 0 && $name !== 'actions') { | ||
$result[] = $prefix . Inflector::camel2id(substr($name, 6)); | ||
} | ||
} | ||
return $result; | ||
} | ||
|
||
} |
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,119 @@ | ||
<?php | ||
|
||
namespace mdm\auth\controllers; | ||
|
||
use mdm\auth\models\AuthItem; | ||
use mdm\auth\models\AuthItemSearch; | ||
use yii\web\Controller; | ||
use yii\web\HttpException; | ||
use yii\web\VerbFilter; | ||
|
||
/** | ||
* AuthItemController implements the CRUD actions for AuthItem model. | ||
*/ | ||
class AuthItemController extends Controller | ||
{ | ||
public function behaviors() | ||
{ | ||
return [ | ||
'verbs' => [ | ||
'class' => VerbFilter::className(), | ||
'actions' => [ | ||
'delete' => ['post'], | ||
], | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* Lists all AuthItem models. | ||
* @return mixed | ||
*/ | ||
public function actionIndex() | ||
{ | ||
$searchModel = new AuthItemSearch; | ||
$dataProvider = $searchModel->search($_GET); | ||
|
||
return $this->render('index', [ | ||
'dataProvider' => $dataProvider, | ||
'searchModel' => $searchModel, | ||
]); | ||
} | ||
|
||
/** | ||
* Displays a single AuthItem model. | ||
* @param string $id | ||
* @return mixed | ||
*/ | ||
public function actionView($id) | ||
{ | ||
return $this->render('view', [ | ||
'model' => $this->findModel($id), | ||
]); | ||
} | ||
|
||
/** | ||
* Creates a new AuthItem model. | ||
* If creation is successful, the browser will be redirected to the 'view' page. | ||
* @return mixed | ||
*/ | ||
public function actionCreate() | ||
{ | ||
$model = new AuthItem; | ||
|
||
if ($model->load($_POST) && $model->save()) { | ||
return $this->redirect(['view', 'id' => $model->name]); | ||
} else { | ||
return $this->render('create', [ | ||
'model' => $model, | ||
]); | ||
} | ||
} | ||
|
||
/** | ||
* Updates an existing AuthItem model. | ||
* If update is successful, the browser will be redirected to the 'view' page. | ||
* @param string $id | ||
* @return mixed | ||
*/ | ||
public function actionUpdate($id) | ||
{ | ||
$model = $this->findModel($id); | ||
|
||
if ($model->load($_POST) && $model->save()) { | ||
return $this->redirect(['view', 'id' => $model->name]); | ||
} else { | ||
return $this->render('update', [ | ||
'model' => $model, | ||
]); | ||
} | ||
} | ||
|
||
/** | ||
* Deletes an existing AuthItem model. | ||
* If deletion is successful, the browser will be redirected to the 'index' page. | ||
* @param string $id | ||
* @return mixed | ||
*/ | ||
public function actionDelete($id) | ||
{ | ||
$this->findModel($id)->delete(); | ||
return $this->redirect(['index']); | ||
} | ||
|
||
/** | ||
* Finds the AuthItem model based on its primary key value. | ||
* If the model is not found, a 404 HTTP exception will be thrown. | ||
* @param string $id | ||
* @return AuthItem the loaded model | ||
* @throws HttpException if the model cannot be found | ||
*/ | ||
protected function findModel($id) | ||
{ | ||
if (($model = AuthItem::find($id)) !== null) { | ||
return $model; | ||
} else { | ||
throw new HttpException(404, 'The requested page does not exist.'); | ||
} | ||
} | ||
} |
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,12 @@ | ||
<?php | ||
|
||
namespace mdm\auth\controllers; | ||
|
||
class DefaultController extends \yii\web\Controller | ||
{ | ||
public function actionIndex() | ||
{ | ||
return $this->render('index'); | ||
} | ||
|
||
} |
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,13 @@ | ||
<?php | ||
|
||
namespace mdm\auth\controllers; | ||
|
||
class RouteController extends \yii\web\Controller | ||
{ | ||
public function actionIndex() | ||
{ | ||
return $this->render('index'); | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.