Skip to content

Commit

Permalink
initial commit from yii2-advanced-funson86
Browse files Browse the repository at this point in the history
  • Loading branch information
funson86 committed Feb 2, 2015
1 parent c4500d2 commit 2e9e5eb
Show file tree
Hide file tree
Showing 216 changed files with 7,009 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .bowerrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"directory" : "vendor/bower"
}
31 changes: 31 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# yii console command
/yii

# phpstorm project files
.idea

# netbeans project files
nbproject

# zend studio for eclipse project files
.buildpath
.project
.settings

# windows thumbnail cache
Thumbs.db

# composer vendor dir
/vendor

# composer itself is not needed
composer.phar
composer.lock

# Mac DS_Store Files
.DS_Store

# phpunit itself is not needed
phpunit.phar
# local phpunit config
/phpunit.xml
32 changes: 32 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
The Yii framework is free software. It is released under the terms of
the following BSD License.

Copyright © 2008 by Yii Software LLC (http://www.yiisoft.com)
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name of Yii Software LLC nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
29 changes: 29 additions & 0 deletions backend/assets/AppAsset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/

namespace backend\assets;

use yii\web\AssetBundle;

/**
* @author Qiang Xue <[email protected]>
* @since 2.0
*/
class AppAsset extends AssetBundle
{
public $basePath = '@webroot';
public $baseUrl = '@web';
public $css = [
'css/site.css',
];
public $js = [
];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
];
}
2 changes: 2 additions & 0 deletions backend/config/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
main-local.php
params-local.php
1 change: 1 addition & 0 deletions backend/config/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
49 changes: 49 additions & 0 deletions backend/config/main.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
$params = array_merge(
require(__DIR__ . '/../../common/config/params.php'),
require(__DIR__ . '/../../common/config/params-local.php'),
require(__DIR__ . '/params.php'),
require(__DIR__ . '/params-local.php')
);

return [
'id' => 'app-backend',
'basePath' => dirname(__DIR__),
'controllerNamespace' => 'backend\controllers',
'bootstrap' => ['log'],
'modules' => [
'auth' => [
'class' => 'funson86\auth\Module',
'controllerNamespace' => 'funson86\auth\controllers'
],
],
'components' => [
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => true,
],
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
//'enableStrictParsing' => true,
'rules' => [
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
],
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'errorHandler' => [
'errorAction' => 'site/error',
],
],
'params' => $params,
];
4 changes: 4 additions & 0 deletions backend/config/params.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
return [
'adminEmail' => '[email protected]',
];
177 changes: 177 additions & 0 deletions backend/controllers/RoleController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
<?php
/**
* Created by JetBrains PhpStorm.
* User: funson
* Date: 14-9-9
* Time: 下午4:54
* To change this template use File | Settings | File Templates.
*/
namespace backend\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\ForbiddenHttpException;
use yii\web\HttpException;

use backend\models\Auth;
use backend\models\AuthSearch;
use yii\web\Controller;
use yii\filters\VerbFilter;

class RoleController extends Controller
{
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'allow' => true,
'roles' => ['@']
]
]
],
];
}

public function actionIndex()
{
//if(!Yii::$app->user->can('viewRole')) throw new ForbiddenHttpException(Yii::t('app', 'No Auth'));

$searchModel = new AuthSearch();
$dataProvider = $searchModel->search(Yii::$app->request->get(), Auth::TYPE_ROLE);
return $this->render('index', [
'dataProvider' => $dataProvider,
'searchModel' => $searchModel,
]);
}

public function actionCreate()
{
//if(!Yii::$app->user->can('createRole')) throw new ForbiddenHttpException(Yii::t('app', 'No Auth'));

$model = new Auth();
if ($model->load(Yii::$app->request->post())) {
$permissions = $this->preparePermissions(Yii::$app->request->post());
if($model->createRole($permissions)) {
Yii::$app->session->setFlash('success', " '$model->name' " . Yii::t('app', 'successfully saved'));
return $this->redirect(['view', 'name' => $model->name]);
}
else
{
$permissions = $this->getPermissions();
$model->_permissions = Yii::$app->request->post()['Auth']['_permissions'];
return $this->render('create', [
'model' => $model,
'permissions' => $permissions
]
);
}
} else {
$permissions = $this->getPermissions();
return $this->render('create', [
'model' => $model,
'permissions' => $permissions
]
);
}
}

public function actionUpdate($name)
{
//if(!Yii::$app->user->can('updateRole')) throw new ForbiddenHttpException(Yii::t('app', 'No Auth'));

if($name == 'admin') {
Yii::$app->session->setFlash('success', Yii::t('app', 'The Administrator has all permissions'));
return $this->redirect(['view', 'name' => $name]);
}
$model = $this->findModel($name);
if ($model->load(Yii::$app->request->post())) {
$permissions = $this->preparePermissions(Yii::$app->request->post());
if($model->updateRole($name, $permissions)) {
Yii::$app->session->setFlash('success', " '$model->name' " . Yii::t('app', 'successfully updated'));
return $this->redirect(['view', 'name' => $name]);
}
} else {
$permissions = $this->getPermissions();
$model->loadRolePermissions($name);
return $this->render('update', [
'model' => $model,
'permissions' => $permissions,
]
);
}
}

public function actionDelete($name)
{
//if(!Yii::$app->user->can('deleteRole')) throw new ForbiddenHttpException(Yii::t('app', 'No Auth'));

if ($name) {
if(!Auth::hasUsersByRole($name)) {
$auth = Yii::$app->getAuthManager();
$role = $auth->getRole($name);

// clear asset permissions
$permissions = $auth->getPermissionsByRole($name);
foreach($permissions as $permission) {
$auth->removeChild($role, $permission);
}
if($auth->remove($role)) {
Yii::$app->session->setFlash('success', " '$name' " . Yii::t('app', 'successfully removed'));
}
} else {
Yii::$app->session->setFlash('warning', " '$name' " . Yii::t('app', 'still used'));
}
}
return $this->redirect(['index']);
}

public function actionView($name)
{
$model = $this->findModel($name);
$model->loadRolePermissions($name);
$permissions = $this->getPermissions();
return $this->render('view', [
'model' => $model,
'permissions' => $permissions,
]);
}

protected function findModel($name)
{
if ($name) {
$auth = Yii::$app->getAuthManager();
$model = new Auth();
$role = $auth->getRole($name);
if ($role) {
$model->name = $role->name;
$model->description = $role->description;
$model->setIsNewRecord(false);
return $model;
}
}
throw new HttpException(404);
}

protected function getPermissions() {
$models = Auth::find()->where(['type' => Auth::TYPE_PERMISSION])->all();
$permissions = [];
foreach($models as $model) {
$permissions[$model->name] = $model->name . ' (' . $model->description . ')';
}
return $permissions;
}

protected function preparePermissions($post) {
return (isset($post['Auth']['_permissions']) &&
is_array($post['Auth']['_permissions'])) ? $post['Auth']['_permissions'] : [];
}
}
Loading

0 comments on commit 2e9e5eb

Please sign in to comment.