Commit ecf033672981009aefbd2b91005d51bc787ed282
1 parent
393a55ab
TEst
Showing
14 changed files
with
278 additions
and
27 deletions
Show diff stats
backend/controllers/SiteController.php
| @@ -29,16 +29,6 @@ class SiteController extends Controller | @@ -29,16 +29,6 @@ class SiteController extends Controller | ||
| 29 | 'rules' => [ | 29 | 'rules' => [ |
| 30 | [ | 30 | [ |
| 31 | 'allow' => true, | 31 | 'allow' => true, |
| 32 | - 'actions' => ['test'], | ||
| 33 | - 'matchCallback' => function($rule, $action) { | ||
| 34 | - return (Yii::$app->user->identity->id == 1); | ||
| 35 | - }, | ||
| 36 | - 'denyCallback' => function($rule, $action) { | ||
| 37 | - var_dump(Yii::$app->user->identity->id); | ||
| 38 | - }, | ||
| 39 | - ], | ||
| 40 | - [ | ||
| 41 | - 'allow' => true, | ||
| 42 | 'roles' => ['@'] | 32 | 'roles' => ['@'] |
| 43 | ], | 33 | ], |
| 44 | ], | 34 | ], |
| @@ -157,6 +147,11 @@ class SiteController extends Controller | @@ -157,6 +147,11 @@ class SiteController extends Controller | ||
| 157 | 147 | ||
| 158 | public function actionTest() | 148 | public function actionTest() |
| 159 | { | 149 | { |
| 150 | + echo "<pre>"; | ||
| 151 | + //var_dump(Yii::$app->getAuthManager()->getRole('CHUVAK')); | ||
| 152 | + //var_dump(Yii::$app->getAuthManager()->assign(Yii::$app->getAuthManager()->getRole('CHUVAK'), Yii::$app->user->getId())); | ||
| 153 | + var_dump(Yii::$app->getAuthManager()->getRoles()); | ||
| 154 | + echo "</pre>"; | ||
| 160 | return $this->render('index'); | 155 | return $this->render('index'); |
| 161 | } | 156 | } |
| 162 | 157 |
| 1 | +<?php | ||
| 2 | + | ||
| 3 | + namespace common\components\rules; | ||
| 4 | + | ||
| 5 | + use yii\db\Query; | ||
| 6 | + use yii\rbac\Rule; | ||
| 7 | + | ||
| 8 | + class CommentRule extends Rule | ||
| 9 | + { | ||
| 10 | + public $name = 'canComment'; | ||
| 11 | + | ||
| 12 | + public function execute ($user, $item, $params) | ||
| 13 | + { | ||
| 14 | + $auth = \Yii::$app->authManager; | ||
| 15 | + $access = false; | ||
| 16 | + if($params['record']) { | ||
| 17 | + $roles = \Yii::$app->user->identity->getRoles(); | ||
| 18 | + $permissions = []; | ||
| 19 | + $queryRole = (new Query())->from('auth_table_access_group')->where(['in', 'role', $roles])->andWhere(['record_id' => $params['record']->primaryKey])->all(); | ||
| 20 | + $queryUser = (new Query())->from('auth_table_access_user')->where(['user_id' => $user])->andWhere(['record_id' => $params['record']->primaryKey])->all(); | ||
| 21 | + foreach($queryRole as $oneRole) | ||
| 22 | + { | ||
| 23 | + $permissions[] = $oneRole['permission']; | ||
| 24 | + $permissions = array_merge($permissions, array_keys($auth->getPermissionsByRole($oneRole['permission']))); | ||
| 25 | + } | ||
| 26 | + foreach($queryUser as $oneUser) | ||
| 27 | + { | ||
| 28 | + $permissions[] = $oneUser['permission']; | ||
| 29 | + $permissions = array_merge($permissions, array_keys($auth->getPermissionsByRole($oneUser['permission']))); | ||
| 30 | + } | ||
| 31 | + $access = in_array($item->name, array_unique($permissions)); | ||
| 32 | + } | ||
| 33 | + return $access; | ||
| 34 | + } | ||
| 35 | + | ||
| 36 | + } | ||
| 0 | \ No newline at end of file | 37 | \ No newline at end of file |
| 1 | +<?php | ||
| 2 | + | ||
| 3 | + namespace common\components\rules; | ||
| 4 | + | ||
| 5 | + use yii\db\Query; | ||
| 6 | + use yii\rbac\Rule; | ||
| 7 | + | ||
| 8 | + class DeleteRule extends Rule | ||
| 9 | + { | ||
| 10 | + public $name = 'canDelete'; | ||
| 11 | + | ||
| 12 | + public function execute ($user, $item, $params) | ||
| 13 | + { | ||
| 14 | + $auth = \Yii::$app->authManager; | ||
| 15 | + $access = false; | ||
| 16 | + if($params['record']) { | ||
| 17 | + $roles = \Yii::$app->user->identity->getRoles(); | ||
| 18 | + $permissions = []; | ||
| 19 | + $queryRole = (new Query())->from('auth_table_access_group')->where(['in', 'role', $roles])->andWhere(['record_id' => $params['record']->primaryKey])->all(); | ||
| 20 | + $queryUser = (new Query())->from('auth_table_access_user')->where(['user_id' => $user])->andWhere(['record_id' => $params['record']->primaryKey])->all(); | ||
| 21 | + foreach($queryRole as $oneRole) | ||
| 22 | + { | ||
| 23 | + $permissions[] = $oneRole['permission']; | ||
| 24 | + $permissions = array_merge($permissions, array_keys($auth->getPermissionsByRole($oneRole['permission']))); | ||
| 25 | + } | ||
| 26 | + foreach($queryUser as $oneUser) | ||
| 27 | + { | ||
| 28 | + $permissions[] = $oneUser['permission']; | ||
| 29 | + $permissions = array_merge($permissions, array_keys($auth->getPermissionsByRole($oneUser['permission']))); | ||
| 30 | + } | ||
| 31 | + $access = in_array($item->name, array_unique($permissions)); | ||
| 32 | + } | ||
| 33 | + return $access; | ||
| 34 | + } | ||
| 35 | + | ||
| 36 | + } | ||
| 0 | \ No newline at end of file | 37 | \ No newline at end of file |
| 1 | +<?php | ||
| 2 | + | ||
| 3 | + namespace common\components\rules; | ||
| 4 | + | ||
| 5 | + use yii\db\Query; | ||
| 6 | + use yii\rbac\Rule; | ||
| 7 | + | ||
| 8 | + class UpdateRule extends Rule | ||
| 9 | + { | ||
| 10 | + public $name = 'canUpdate'; | ||
| 11 | + | ||
| 12 | + public function execute ($user, $item, $params) | ||
| 13 | + { | ||
| 14 | + $auth = \Yii::$app->authManager; | ||
| 15 | + $access = false; | ||
| 16 | + if($params['record']) { | ||
| 17 | + $roles = \Yii::$app->user->identity->getRoles(); | ||
| 18 | + $permissions = []; | ||
| 19 | + $queryRole = (new Query())->from('auth_table_access_group')->where(['in', 'role', $roles])->andWhere(['record_id' => $params['record']->primaryKey])->all(); | ||
| 20 | + $queryUser = (new Query())->from('auth_table_access_user')->where(['user_id' => $user])->andWhere(['record_id' => $params['record']->primaryKey])->all(); | ||
| 21 | + foreach($queryRole as $oneRole) | ||
| 22 | + { | ||
| 23 | + $permissions[] = $oneRole['permission']; | ||
| 24 | + $permissions = array_merge($permissions, array_keys($auth->getPermissionsByRole($oneRole['permission']))); | ||
| 25 | + } | ||
| 26 | + foreach($queryUser as $oneUser) | ||
| 27 | + { | ||
| 28 | + $permissions[] = $oneUser['permission']; | ||
| 29 | + $permissions = array_merge($permissions, array_keys($auth->getPermissionsByRole($oneUser['permission']))); | ||
| 30 | + } | ||
| 31 | + $access = in_array($item->name, array_unique($permissions)); | ||
| 32 | + } | ||
| 33 | + return $access; | ||
| 34 | + } | ||
| 35 | + | ||
| 36 | + } | ||
| 0 | \ No newline at end of file | 37 | \ No newline at end of file |
| 1 | +<?php | ||
| 2 | + | ||
| 3 | + namespace common\components\rules; | ||
| 4 | + | ||
| 5 | + use yii\db\Query; | ||
| 6 | + use yii\rbac\Rule; | ||
| 7 | + | ||
| 8 | + class ViewRule extends Rule | ||
| 9 | + { | ||
| 10 | + public $name = 'canView'; | ||
| 11 | + | ||
| 12 | + public function execute ($user, $item, $params) | ||
| 13 | + { | ||
| 14 | + $auth = \Yii::$app->authManager; | ||
| 15 | + $access = false; | ||
| 16 | + if($params['record']) { | ||
| 17 | + $roles = \Yii::$app->user->identity->getRoles(); | ||
| 18 | + $permissions = []; | ||
| 19 | + $queryRole = (new Query())->from('auth_table_access_group')->where(['in', 'role', $roles])->andWhere(['record_id' => $params['record']->primaryKey])->all(); | ||
| 20 | + $queryUser = (new Query())->from('auth_table_access_user')->where(['user_id' => $user])->andWhere(['record_id' => $params['record']->primaryKey])->all(); | ||
| 21 | + foreach($queryRole as $oneRole) | ||
| 22 | + { | ||
| 23 | + $permissions[] = $oneRole['permission']; | ||
| 24 | + $permissions = array_merge($permissions, array_keys($auth->getPermissionsByRole($oneRole['permission']))); | ||
| 25 | + } | ||
| 26 | + foreach($queryUser as $oneUser) | ||
| 27 | + { | ||
| 28 | + $permissions[] = $oneUser['permission']; | ||
| 29 | + $permissions = array_merge($permissions, array_keys($auth->getPermissionsByRole($oneUser['permission']))); | ||
| 30 | + } | ||
| 31 | + $access = in_array($item->name, array_unique($permissions)); | ||
| 32 | + } | ||
| 33 | + return $access; | ||
| 34 | + } | ||
| 35 | + | ||
| 36 | + } | ||
| 0 | \ No newline at end of file | 37 | \ No newline at end of file |
common/config/main.php
| @@ -12,6 +12,9 @@ return [ | @@ -12,6 +12,9 @@ return [ | ||
| 12 | 'class' => 'common\modules\blog\Module', | 12 | 'class' => 'common\modules\blog\Module', |
| 13 | ], | 13 | ], |
| 14 | ], | 14 | ], |
| 15 | + 'bootstrap' => [ | ||
| 16 | + 'options', | ||
| 17 | + ], | ||
| 15 | 'components' => [ | 18 | 'components' => [ |
| 16 | 'cache' => [ | 19 | 'cache' => [ |
| 17 | 'class' => 'yii\caching\FileCache', | 20 | 'class' => 'yii\caching\FileCache', |
| @@ -169,7 +172,9 @@ return [ | @@ -169,7 +172,9 @@ return [ | ||
| 169 | /*========End======= | 172 | /*========End======= |
| 170 | *end api sicial | 173 | *end api sicial |
| 171 | * */ | 174 | * */ |
| 172 | - | 175 | + 'options' => [ |
| 176 | + 'class' => 'common\models\OptionHelper', | ||
| 177 | + ] | ||
| 173 | ], | 178 | ], |
| 174 | 'language' => 'ru-RU' | 179 | 'language' => 'ru-RU' |
| 175 | ]; | 180 | ]; |
| 1 | +<?php | ||
| 2 | + | ||
| 3 | + namespace common\models; | ||
| 4 | + | ||
| 5 | + use yii\db\ActiveRecord; | ||
| 6 | + use yii\web\ForbiddenHttpException; | ||
| 7 | + | ||
| 8 | + class ActiveRecordRule extends ActiveRecord | ||
| 9 | + { | ||
| 10 | + public static function find () | ||
| 11 | + { | ||
| 12 | + $table = self::tableName(); | ||
| 13 | + $pk = self::primaryKey()[0]; | ||
| 14 | + $query = parent::find (); | ||
| 15 | + if (\Yii::$app->authManager && \Yii::$app->options->rule) | ||
| 16 | + { | ||
| 17 | + $authManager = \Yii::$app->authManager; | ||
| 18 | + $roles = \Yii::$app->user->identity->roles; | ||
| 19 | + $query->leftJoin ('auth_table_access_group', $table.'.'.$pk.' = auth_table_access_group.record_id') | ||
| 20 | + ->leftJoin ('auth_table_access_user', $table.'.'.$pk.' = auth_table_access_user.record_id') | ||
| 21 | + ->orWhere (['auth_table_access_group.model_name' => self::className (), 'auth_table_access_group.role' => $roles]) | ||
| 22 | + ->orWhere (['auth_table_access_user.user_id' => \Yii::$app->user->getId(), 'auth_table_access_user.model_name' => self::className ()]); | ||
| 23 | + } | ||
| 24 | + return $query; | ||
| 25 | + } | ||
| 26 | + | ||
| 27 | + public function delete () | ||
| 28 | + { | ||
| 29 | + $id = $this->primaryKey; | ||
| 30 | + $result = parent::delete(); | ||
| 31 | + if(is_int($id)) { | ||
| 32 | + \Yii::$app->db->createCommand()->delete('auth_table_access_group', ['model_name' => self::className(), 'record_id' => $id])->execute(); | ||
| 33 | + \Yii::$app->db->createCommand()->delete('auth_table_access_user', ['model_name' => self::className(), 'record_id' => $id])->execute(); | ||
| 34 | + } | ||
| 35 | + return $result; | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + public function update ($runValidation = true, $attributeNames = null) | ||
| 39 | + { | ||
| 40 | + if(\Yii::$app->user->can('updateRecord', ['record' => $this])) { | ||
| 41 | + return parent::update ($runValidation, $attributeNames); | ||
| 42 | + } else { | ||
| 43 | + throw new ForbiddenHttpException(\Yii::t('app', 'Permission denied')); | ||
| 44 | + } | ||
| 45 | + } | ||
| 46 | + } | ||
| 0 | \ No newline at end of file | 47 | \ No newline at end of file |
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +namespace common\models; | ||
| 4 | + | ||
| 5 | +use frontend\models\Options; | ||
| 6 | +use Yii; | ||
| 7 | +use yii\base\InvalidParamException; | ||
| 8 | +use yii\base\Model; | ||
| 9 | + | ||
| 10 | +class OptionHelper extends Model | ||
| 11 | +{ | ||
| 12 | + const OPTION_OBJECT = 1; | ||
| 13 | + const OPTION_ARRAY = 2; | ||
| 14 | + const OPTION_VALUE = 3; | ||
| 15 | + public function getRule($return = 3) | ||
| 16 | + { | ||
| 17 | + $result = Options::find()->where(['name' => 'rules'])->with('value'); | ||
| 18 | + if($return == self::OPTION_OBJECT) { | ||
| 19 | + return $result->one(); | ||
| 20 | + } elseif($return == self::OPTION_ARRAY) { | ||
| 21 | + return $result->asArray()->one(); | ||
| 22 | + } elseif($return == self::OPTION_VALUE) { | ||
| 23 | + return $result->one()->value->value; | ||
| 24 | + } else { | ||
| 25 | + throw new InvalidParamException(Yii::t('app', 'Must be 1-3')); | ||
| 26 | + } | ||
| 27 | + } | ||
| 28 | +} |
common/models/Tools.php
common/models/User.php
| @@ -228,4 +228,29 @@ class User extends ActiveRecord implements IdentityInterface, UserRbacInterface | @@ -228,4 +228,29 @@ class User extends ActiveRecord implements IdentityInterface, UserRbacInterface | ||
| 228 | { | 228 | { |
| 229 | return $this->username; | 229 | return $this->username; |
| 230 | } | 230 | } |
| 231 | + | ||
| 232 | + public function getRoles() | ||
| 233 | + { | ||
| 234 | + $auth = \Yii::$app->authManager; | ||
| 235 | + $roles = $this->getRoleChildrenRecursive($auth->getRolesByUser($this->id), $auth); | ||
| 236 | + return $roles; | ||
| 237 | + } | ||
| 238 | + | ||
| 239 | + protected function getRoleChildrenRecursive($roles, $auth, $result = []) | ||
| 240 | + { | ||
| 241 | + if(is_array($roles) && !empty($roles)) | ||
| 242 | + { | ||
| 243 | + foreach($roles as $role => $item) | ||
| 244 | + { | ||
| 245 | + if(!($item instanceof \yii\rbac\Role)) { | ||
| 246 | + continue; | ||
| 247 | + } | ||
| 248 | + $result[] = $role; | ||
| 249 | + $result = self::getRoleChildrenRecursive($auth->getChildren($role), $auth, $result); | ||
| 250 | + } | ||
| 251 | + return $result; | ||
| 252 | + } else { | ||
| 253 | + return $result; | ||
| 254 | + } | ||
| 255 | + } | ||
| 231 | } | 256 | } |
common/modules/blog/controllers/ArticleController.php
| 1 | <?php | 1 | <?php |
| 2 | namespace common\modules\blog\controllers; | 2 | namespace common\modules\blog\controllers; |
| 3 | 3 | ||
| 4 | +use common\components\rules\CommentRule; | ||
| 5 | +use common\components\rules\DeleteRule; | ||
| 6 | +use common\components\rules\UpdateRule; | ||
| 7 | +use common\components\rules\ViewRule; | ||
| 4 | use common\models\Language; | 8 | use common\models\Language; |
| 5 | use common\modules\blog\models\Article; | 9 | use common\modules\blog\models\Article; |
| 6 | use common\modules\blog\models\ArticleLang; | 10 | use common\modules\blog\models\ArticleLang; |
| 7 | use common\modules\blog\models\ArticleMedia; | 11 | use common\modules\blog\models\ArticleMedia; |
| 8 | use common\modules\blog\models\ArticleToCategory; | 12 | use common\modules\blog\models\ArticleToCategory; |
| 9 | use yii\data\ActiveDataProvider; | 13 | use yii\data\ActiveDataProvider; |
| 14 | +use yii\rbac\DbManager; | ||
| 10 | use yii\web\Controller; | 15 | use yii\web\Controller; |
| 11 | use yii\web\UploadedFile; | 16 | use yii\web\UploadedFile; |
| 12 | 17 | ||
| @@ -15,6 +20,7 @@ class ArticleController extends Controller | @@ -15,6 +20,7 @@ class ArticleController extends Controller | ||
| 15 | 20 | ||
| 16 | public function actionIndex() | 21 | public function actionIndex() |
| 17 | { | 22 | { |
| 23 | + var_dump(\Yii::$app->options->rule); | ||
| 18 | $dataProvider = new ActiveDataProvider([ | 24 | $dataProvider = new ActiveDataProvider([ |
| 19 | 'query' => Article::find(), | 25 | 'query' => Article::find(), |
| 20 | 'pagination' => [ | 26 | 'pagination' => [ |
common/modules/blog/models/Article.php
| @@ -2,6 +2,7 @@ | @@ -2,6 +2,7 @@ | ||
| 2 | 2 | ||
| 3 | namespace common\modules\blog\models; | 3 | namespace common\modules\blog\models; |
| 4 | 4 | ||
| 5 | +use common\models\ActiveRecordRule; | ||
| 5 | use common\models\Media; | 6 | use common\models\Media; |
| 6 | use common\models\User; | 7 | use common\models\User; |
| 7 | use common\modules\blog\behaviors\Autocomplete; | 8 | use common\modules\blog\behaviors\Autocomplete; |
| @@ -31,7 +32,7 @@ use yii\db\Query; | @@ -31,7 +32,7 @@ use yii\db\Query; | ||
| 31 | * @property ArticleToCategory[] $articleToCategories | 32 | * @property ArticleToCategory[] $articleToCategories |
| 32 | * @property Media[] $media | 33 | * @property Media[] $media |
| 33 | */ | 34 | */ |
| 34 | -class Article extends \yii\db\ActiveRecord | 35 | +class Article extends ActiveRecordRule |
| 35 | { | 36 | { |
| 36 | /** | 37 | /** |
| 37 | * @inheritdoc | 38 | * @inheritdoc |
| @@ -158,4 +159,5 @@ class Article extends \yii\db\ActiveRecord | @@ -158,4 +159,5 @@ class Article extends \yii\db\ActiveRecord | ||
| 158 | { | 159 | { |
| 159 | return $this->getArticleToCategories()->select('article_category_id')->column(); | 160 | return $this->getArticleToCategories()->select('article_category_id')->column(); |
| 160 | } | 161 | } |
| 162 | + | ||
| 161 | } | 163 | } |
db-migration/yarik/all.backup
No preview for this file type
frontend/models/Options.php
| @@ -5,7 +5,7 @@ namespace frontend\models; | @@ -5,7 +5,7 @@ namespace frontend\models; | ||
| 5 | use Yii; | 5 | use Yii; |
| 6 | 6 | ||
| 7 | /** | 7 | /** |
| 8 | - * This is the model class for table "options". | 8 | + * This is the model class for table "option". |
| 9 | * | 9 | * |
| 10 | * @property integer $option_id | 10 | * @property integer $option_id |
| 11 | * @property string $option_key | 11 | * @property string $option_key |
| @@ -22,43 +22,42 @@ class Options extends \yii\db\ActiveRecord | @@ -22,43 +22,42 @@ class Options extends \yii\db\ActiveRecord | ||
| 22 | /** | 22 | /** |
| 23 | * @inheritdoc | 23 | * @inheritdoc |
| 24 | */ | 24 | */ |
| 25 | - public static function tableName() | 25 | + public static function tableName () |
| 26 | { | 26 | { |
| 27 | - return 'options'; | 27 | + return 'option'; |
| 28 | } | 28 | } |
| 29 | 29 | ||
| 30 | /** | 30 | /** |
| 31 | * @inheritdoc | 31 | * @inheritdoc |
| 32 | */ | 32 | */ |
| 33 | - public function rules() | 33 | + public function rules () |
| 34 | { | 34 | { |
| 35 | return [ | 35 | return [ |
| 36 | - [['option_key'], 'required'], | ||
| 37 | - [['option_parent', 'option_translatable'], 'integer'], | ||
| 38 | - [['option_key', 'option_format'], 'string', 'max' => 200] | 36 | + [['option_key'], 'required'], [['option_parent', 'option_translatable'], 'integer'], [['option_key', 'option_format'], 'string', 'max' => 200] |
| 39 | ]; | 37 | ]; |
| 40 | } | 38 | } |
| 41 | 39 | ||
| 42 | /** | 40 | /** |
| 43 | * @inheritdoc | 41 | * @inheritdoc |
| 44 | */ | 42 | */ |
| 45 | - public function attributeLabels() | 43 | + public function attributeLabels () |
| 46 | { | 44 | { |
| 47 | return [ | 45 | return [ |
| 48 | - 'option_id' => Yii::t('app', 'Option ID'), | ||
| 49 | - 'option_key' => Yii::t('app', 'Option Key'), | ||
| 50 | - 'option_parent' => Yii::t('app', 'Option Parent'), | ||
| 51 | - 'option_translatable' => Yii::t('app', 'Option Translatable'), | ||
| 52 | - 'option_format' => Yii::t('app', 'Option Format'), | 46 | + 'option_id' => Yii::t ('app', 'Option ID'), 'option_key' => Yii::t ('app', 'Option Key'), 'option_parent' => Yii::t ('app', 'Option Parent'), 'option_translatable' => Yii::t ('app', 'Option Translatable'), 'option_format' => Yii::t ('app', 'Option Format'), |
| 53 | ]; | 47 | ]; |
| 54 | } | 48 | } |
| 55 | 49 | ||
| 56 | /** | 50 | /** |
| 57 | * @return \yii\db\ActiveQuery | 51 | * @return \yii\db\ActiveQuery |
| 58 | */ | 52 | */ |
| 59 | - public function getOptionValues() | 53 | + public function getOptionLang () |
| 60 | { | 54 | { |
| 61 | - return $this->hasMany(OptionValues::className(), ['option_id' => 'option_id']); | 55 | + return $this->hasMany (OptionLang::className (), ['option_id' => 'option_id']); |
| 56 | + } | ||
| 57 | + | ||
| 58 | + public function getValue () | ||
| 59 | + { | ||
| 60 | + return $this->hasOne(OptionLang::className(), ['option_id' => 'option_id'])->where(['option_lang.language_id' => '0']); | ||
| 62 | } | 61 | } |
| 63 | 62 | ||
| 64 | /** | 63 | /** |