Commit 9755cb5953e69fec2468bc5f2235b51838793fd0
1 parent
07a132f4
fixed permissions (rules) in controllers
Showing
9 changed files
with
88 additions
and
112 deletions
Show diff stats
backend/controllers/CartController.php
@@ -8,7 +8,6 @@ use common\models\CartBillsSearch; | @@ -8,7 +8,6 @@ use common\models\CartBillsSearch; | ||
8 | use backend\components\base\BaseController; | 8 | use backend\components\base\BaseController; |
9 | use yii\web\NotFoundHttpException; | 9 | use yii\web\NotFoundHttpException; |
10 | use yii\filters\VerbFilter; | 10 | use yii\filters\VerbFilter; |
11 | -use yii\filters\AccessControl; | ||
12 | 11 | ||
13 | /** | 12 | /** |
14 | * CartController implements the CRUD actions for CartBills model. | 13 | * CartController implements the CRUD actions for CartBills model. |
@@ -16,34 +15,7 @@ use yii\filters\AccessControl; | @@ -16,34 +15,7 @@ use yii\filters\AccessControl; | ||
16 | class CartController extends BaseController | 15 | class CartController extends BaseController |
17 | { | 16 | { |
18 | public $layout = "/column"; | 17 | public $layout = "/column"; |
19 | - /** | ||
20 | - * @inheritdoc | ||
21 | - */ | ||
22 | - public function behaviors() | ||
23 | - { | ||
24 | - return [ | ||
25 | - 'access' => [ | ||
26 | - 'class' => AccessControl::className(), | ||
27 | - 'rules' => [ | ||
28 | - [ | ||
29 | - 'actions' => ['login', 'error', 'download-photo','delete-image' ], | ||
30 | - 'allow' => true, | ||
31 | - ], | ||
32 | - [ | ||
33 | - 'actions' => ['logout', 'index','create','update','view','delete',], | ||
34 | - 'allow' => true, | ||
35 | - 'roles' => ['@'], | ||
36 | - ], | ||
37 | - ], | ||
38 | - ], | ||
39 | - 'verbs' => [ | ||
40 | - 'class' => VerbFilter::className(), | ||
41 | - 'actions' => [ | ||
42 | - 'logout' => ['post'], | ||
43 | - ], | ||
44 | - ], | ||
45 | - ]; | ||
46 | - } | 18 | + |
47 | /** | 19 | /** |
48 | * Lists all CartBills models. | 20 | * Lists all CartBills models. |
49 | * @return mixed | 21 | * @return mixed |
backend/controllers/CrossingUploadController.php
@@ -8,10 +8,13 @@ | @@ -8,10 +8,13 @@ | ||
8 | 8 | ||
9 | namespace backend\controllers; | 9 | namespace backend\controllers; |
10 | 10 | ||
11 | +use backend\components\base\BaseActiveRecord; | ||
11 | use backend\components\base\BaseController; | 12 | use backend\components\base\BaseController; |
12 | use common\components\CustomArrayHelper; | 13 | use common\components\CustomArrayHelper; |
13 | use common\components\CustomVarDamp; | 14 | use common\components\CustomVarDamp; |
15 | +use yii\base\Model; | ||
14 | use yii\data\ArrayDataProvider; | 16 | use yii\data\ArrayDataProvider; |
17 | +use yii\db\ActiveRecord; | ||
15 | use yii\filters\VerbFilter; | 18 | use yii\filters\VerbFilter; |
16 | use yii\filters\AccessControl; | 19 | use yii\filters\AccessControl; |
17 | use backend\models\UploadFileCrossingForm; | 20 | use backend\models\UploadFileCrossingForm; |
@@ -82,7 +85,7 @@ class CrossingUploadController extends BaseController | @@ -82,7 +85,7 @@ class CrossingUploadController extends BaseController | ||
82 | //запускаем парсинг | 85 | //запускаем парсинг |
83 | $data = $model->readFile(); | 86 | $data = $model->readFile(); |
84 | // сохраняем в кеш отпарсенные даные | 87 | // сохраняем в кеш отпарсенные даные |
85 | - $this->cacheHandler( true, $data, $model ); | 88 | + $this->cacheHandler( 1, $data, $model ); |
86 | } else if (Yii::$app->getCache()->get('parser_data')) { | 89 | } else if (Yii::$app->getCache()->get('parser_data')) { |
87 | $data = json_decode(Yii::$app->getCache()->get('parser_data'), true); | 90 | $data = json_decode(Yii::$app->getCache()->get('parser_data'), true); |
88 | } | 91 | } |
@@ -128,7 +131,7 @@ class CrossingUploadController extends BaseController | @@ -128,7 +131,7 @@ class CrossingUploadController extends BaseController | ||
128 | $arr = $model->toArray(); | 131 | $arr = $model->toArray(); |
129 | 132 | ||
130 | // получим данные из кеша | 133 | // получим данные из кеша |
131 | - $this->cacheHandler( false, $data, $configuration ); | 134 | + $this->cacheHandler( 0, $data, $configuration ); |
132 | 135 | ||
133 | // соотнесем отпарсенные данные с соответствием полученным от пользователя | 136 | // соотнесем отпарсенные данные с соответствием полученным от пользователя |
134 | // для этого преобразуем массив отпарсенных данных - назначим ключи согласно соответствию | 137 | // для этого преобразуем массив отпарсенных данных - назначим ключи согласно соответствию |
@@ -138,16 +141,20 @@ class CrossingUploadController extends BaseController | @@ -138,16 +141,20 @@ class CrossingUploadController extends BaseController | ||
138 | $data = $this->convertDataByConfiguration( $data, $configuration ); | 141 | $data = $this->convertDataByConfiguration( $data, $configuration ); |
139 | 142 | ||
140 | $crosses_model = new DetailsCrosses(); | 143 | $crosses_model = new DetailsCrosses(); |
141 | - $crosses_model->ManualInsertWithIgnore($data); | ||
142 | 144 | ||
143 | - Yii::$app->session->setFlash('success', 'Файл кроссов успешно загружен'); | ||
144 | - // все прошло успешно - очищаем кеш | ||
145 | - Yii::$app->getCache()->delete('parser_data'); | ||
146 | - Yii::$app->getCache()->delete('parser_configuration'); | 145 | + if ( $this->validateModel( $crosses_model , $data ) && $crosses_model->ManualInsertWithIgnore( $data ) ) { |
146 | + | ||
147 | + Yii::$app->session->setFlash('success', 'Файл кроссов успешно загружен'); | ||
148 | + | ||
149 | + // очистим кеш | ||
150 | + $this->cacheHandler( 2 ); | ||
151 | + | ||
152 | + if (file_exists($configuration['file_path'])) | ||
153 | + unlink($configuration['file_path']); | ||
154 | + return $this->render('index', ['model' => $configuration]); | ||
155 | + | ||
156 | + } | ||
147 | 157 | ||
148 | - if (file_exists($configuration['file_path'])) | ||
149 | - unlink($configuration['file_path']); | ||
150 | - return $this->render('index', ['model' => $configuration]); | ||
151 | 158 | ||
152 | } else { | 159 | } else { |
153 | // не прошла валидация форма загрузки файлов | 160 | // не прошла валидация форма загрузки файлов |
@@ -188,8 +195,8 @@ class CrossingUploadController extends BaseController | @@ -188,8 +195,8 @@ class CrossingUploadController extends BaseController | ||
188 | $options ['configuration'] = ["string" => ['ARTICLE', 'CROSS_ARTICLE'],]; | 195 | $options ['configuration'] = ["string" => ['ARTICLE', 'CROSS_ARTICLE'],]; |
189 | } | 196 | } |
190 | 197 | ||
191 | - foreach ($data as &$row) { | ||
192 | - $row = Yii::$app->converter->convertByConfiguration($row, $options); | 198 | + foreach ( $data as &$row ) { |
199 | + $row = Yii::$app->converter->convertByConfiguration( $row, $options ); | ||
193 | } | 200 | } |
194 | 201 | ||
195 | return $data; | 202 | return $data; |
@@ -197,30 +204,47 @@ class CrossingUploadController extends BaseController | @@ -197,30 +204,47 @@ class CrossingUploadController extends BaseController | ||
197 | } | 204 | } |
198 | 205 | ||
199 | /** | 206 | /** |
200 | - * @param $mode - bool - true - put in cache, otherwise - fetch from cache | 207 | + * @param $mode - int: 0 - fetch from cache, - 1 - put in cache, <2 - delete from cache |
201 | * @param $data - array | 208 | * @param $data - array |
202 | * @param $configuration - array | 209 | * @param $configuration - array |
203 | * @throws \ErrorException | 210 | * @throws \ErrorException |
204 | */ | 211 | */ |
205 | - protected function cacheHandler( $mode, &$data, &$configuration ){ | ||
206 | - | ||
207 | - if ( $mode ) { | ||
208 | - | ||
209 | - Yii::$app->getCache()->set('parser_data', json_encode($data), 1800); | ||
210 | - // сохраняем в кеш модель - в ней настройки для дальнейшей обработки данных | ||
211 | - Yii::$app->getCache()->set('parser_configuration', serialize($configuration), 1800); | 212 | + protected function cacheHandler( $mode, &$data = [], &$configuration = [] ){ |
213 | + switch ( $mode ) { | ||
214 | + case 0: | ||
215 | + if (Yii::$app->getCache()->get('parser_data') && Yii::$app->getCache()->get('parser_configuration')) { | ||
216 | + $data = json_decode(Yii::$app->getCache()->get('parser_data'), true); | ||
217 | + $configuration = unserialize(Yii::$app->getCache()->get('parser_configuration')); | ||
218 | + } else { | ||
219 | + throw new \ErrorException('Ошибка кеша'); | ||
220 | + } | ||
221 | + break; | ||
222 | + | ||
223 | + case 1: | ||
224 | + Yii::$app->getCache()->set('parser_data', json_encode($data), 1800); | ||
225 | + // сохраняем в кеш модель - в ней настройки для дальнейшей обработки данных | ||
226 | + Yii::$app->getCache()->set('parser_configuration', serialize($configuration), 1800); | ||
227 | + break; | ||
228 | + | ||
229 | + default: | ||
230 | + if( Yii::$app->getCache()->exists('parser_data') ) | ||
231 | + Yii::$app->getCache()->delete('parser_data'); | ||
232 | + | ||
233 | + if( Yii::$app->getCache()->exists('parser_configuration') ) | ||
234 | + Yii::$app->getCache()->delete('parser_configuration'); | ||
235 | + } | ||
212 | 236 | ||
213 | - } else { | 237 | + } |
214 | 238 | ||
215 | - if (Yii::$app->getCache()->get('parser_data') && Yii::$app->getCache()->get('parser_configuration')) { | ||
216 | - $data = json_decode(Yii::$app->getCache()->get('parser_data'), true); | ||
217 | - $configuration = unserialize(Yii::$app->getCache()->get('parser_configuration')); | ||
218 | - } else { | ||
219 | - throw new \ErrorException('Ошибка кеша'); | ||
220 | - } | 239 | + protected function validateModel( BaseActiveRecord $model, array $data ){ |
221 | 240 | ||
241 | + foreach ( $data as $row ) { | ||
242 | + if ( !$model->validate( $row ) ) { | ||
243 | + $model->throwStringErrorException( key( $data ) ); | ||
244 | + }; | ||
222 | } | 245 | } |
223 | 246 | ||
247 | + return true; | ||
224 | 248 | ||
225 | } | 249 | } |
226 | } | 250 | } |
227 | \ No newline at end of file | 251 | \ No newline at end of file |
backend/controllers/DetailsDescriptionController.php
@@ -11,7 +11,6 @@ use yii\data\ActiveDataProvider; | @@ -11,7 +11,6 @@ use yii\data\ActiveDataProvider; | ||
11 | use yii\web\HttpException; | 11 | use yii\web\HttpException; |
12 | use yii\web\NotFoundHttpException; | 12 | use yii\web\NotFoundHttpException; |
13 | use yii\filters\VerbFilter; | 13 | use yii\filters\VerbFilter; |
14 | -use yii\filters\AccessControl; | ||
15 | 14 | ||
16 | /** | 15 | /** |
17 | * DetailsDescriptionController implements the CRUD actions for DetailsDescription model. | 16 | * DetailsDescriptionController implements the CRUD actions for DetailsDescription model. |
@@ -20,30 +19,13 @@ class DetailsDescriptionController extends BaseController | @@ -20,30 +19,13 @@ class DetailsDescriptionController extends BaseController | ||
20 | { | 19 | { |
21 | public $layout = "/column"; | 20 | public $layout = "/column"; |
22 | 21 | ||
23 | - /** | ||
24 | - * @inheritdoc | ||
25 | - */ | ||
26 | public function behaviors() | 22 | public function behaviors() |
27 | { | 23 | { |
28 | return [ | 24 | return [ |
29 | - 'access' => [ | ||
30 | - 'class' => AccessControl::className(), | ||
31 | - 'rules' => [ | ||
32 | - [ | ||
33 | - 'actions' => ['login', 'error', 'download-photo','delete-image' ], | ||
34 | - 'allow' => true, | ||
35 | - ], | ||
36 | - [ | ||
37 | - 'actions' => ['logout', 'index','create','update','view','delete',], | ||
38 | - 'allow' => true, | ||
39 | - 'roles' => ['@'], | ||
40 | - ], | ||
41 | - ], | ||
42 | - ], | ||
43 | 'verbs' => [ | 25 | 'verbs' => [ |
44 | 'class' => VerbFilter::className(), | 26 | 'class' => VerbFilter::className(), |
45 | 'actions' => [ | 27 | 'actions' => [ |
46 | - 'logout' => ['post'], | 28 | + 'delete' => ['post'], |
47 | ], | 29 | ], |
48 | ], | 30 | ], |
49 | ]; | 31 | ]; |
backend/controllers/ParserController.php
@@ -38,22 +38,17 @@ class ParserController extends BaseController | @@ -38,22 +38,17 @@ class ParserController extends BaseController | ||
38 | 'class' => AccessControl::className(), | 38 | 'class' => AccessControl::className(), |
39 | 'rules' => [ | 39 | 'rules' => [ |
40 | [ | 40 | [ |
41 | - 'actions' => ['login', 'error', 'download-photo','delete-image' ], | ||
42 | - 'allow' => true, | ||
43 | - ], | ||
44 | - [ | ||
45 | - 'actions' => ['logout', 'index','create','update','view','delete',], | ||
46 | 'allow' => true, | 41 | 'allow' => true, |
47 | 'roles' => ['@'], | 42 | 'roles' => ['@'], |
48 | ], | 43 | ], |
49 | ], | 44 | ], |
50 | ], | 45 | ], |
51 | - 'verbs' => [ | ||
52 | - 'class' => VerbFilter::className(), | ||
53 | - 'actions' => [ | ||
54 | - 'logout' => ['post'], | ||
55 | - ], | ||
56 | - ], | 46 | +// 'verbs' => [ |
47 | +// 'class' => VerbFilter::className(), | ||
48 | +// 'actions' => [ | ||
49 | +// 'logout' => ['post'], | ||
50 | +// ], | ||
51 | +// ], | ||
57 | ]; | 52 | ]; |
58 | } | 53 | } |
59 | 54 |
backend/controllers/RgGrupController.php
@@ -14,7 +14,6 @@ use common\components\CustomVarDamp; | @@ -14,7 +14,6 @@ use common\components\CustomVarDamp; | ||
14 | use common\components\parsers\MailAttachmentsSaver; | 14 | use common\components\parsers\MailAttachmentsSaver; |
15 | use common\models\Margins; | 15 | use common\models\Margins; |
16 | use common\models\MarginsGroups; | 16 | use common\models\MarginsGroups; |
17 | -use yii\filters\VerbFilter; | ||
18 | use yii\filters\AccessControl; | 17 | use yii\filters\AccessControl; |
19 | use Yii; | 18 | use Yii; |
20 | use yii\web\UploadedFile; | 19 | use yii\web\UploadedFile; |
@@ -36,22 +35,17 @@ class RgGrupController extends BaseController | @@ -36,22 +35,17 @@ class RgGrupController extends BaseController | ||
36 | 'class' => AccessControl::className(), | 35 | 'class' => AccessControl::className(), |
37 | 'rules' => [ | 36 | 'rules' => [ |
38 | [ | 37 | [ |
39 | - 'actions' => ['login', 'error', 'download-photo','delete-image' ], | ||
40 | - 'allow' => true, | ||
41 | - ], | ||
42 | - [ | ||
43 | - 'actions' => ['logout', 'index','create','update','view','delete',], | ||
44 | 'allow' => true, | 38 | 'allow' => true, |
45 | 'roles' => ['@'], | 39 | 'roles' => ['@'], |
46 | ], | 40 | ], |
47 | ], | 41 | ], |
48 | ], | 42 | ], |
49 | - 'verbs' => [ | ||
50 | - 'class' => VerbFilter::className(), | ||
51 | - 'actions' => [ | ||
52 | - 'logout' => ['post'], | ||
53 | - ], | ||
54 | - ], | 43 | +// 'verbs' => [ |
44 | +// 'class' => VerbFilter::className(), | ||
45 | +// 'actions' => [ | ||
46 | +// 'logout' => ['post'], | ||
47 | +// ], | ||
48 | +// ], | ||
55 | ]; | 49 | ]; |
56 | } | 50 | } |
57 | 51 |
backend/models/DetailsCrosses.php
@@ -78,8 +78,16 @@ class DetailsCrosses extends \backend\components\base\BaseActiveRecord | @@ -78,8 +78,16 @@ class DetailsCrosses extends \backend\components\base\BaseActiveRecord | ||
78 | $query = Yii::$app->db->createCommand()->batchInsert($table_name, $keys_arr, $current_batch_array)->sql; | 78 | $query = Yii::$app->db->createCommand()->batchInsert($table_name, $keys_arr, $current_batch_array)->sql; |
79 | // добавим ключевое слово - ignore | 79 | // добавим ключевое слово - ignore |
80 | $query = preg_replace('/INSERT/','INSERT IGNORE', $query); | 80 | $query = preg_replace('/INSERT/','INSERT IGNORE', $query); |
81 | - Yii::$app->db->createCommand($query)->execute(); | 81 | + $rows = Yii::$app->db->createCommand($query)->execute(); |
82 | + | ||
83 | + // если нет результата вернемся с ошибкой | ||
84 | + if ( $rows == 0 ) { | ||
85 | + return false; | ||
86 | + } | ||
82 | 87 | ||
83 | } | 88 | } |
89 | + | ||
90 | + return true; | ||
91 | + | ||
84 | } | 92 | } |
85 | } | 93 | } |
tests/_support/_generated/UnitTesterActions.php
tests/unit/BaseConverterTest.php
@@ -21,18 +21,19 @@ class BaseConverterTest extends \Codeception\TestCase\Test | @@ -21,18 +21,19 @@ class BaseConverterTest extends \Codeception\TestCase\Test | ||
21 | { | 21 | { |
22 | $this->converter = new Converter(); | 22 | $this->converter = new Converter(); |
23 | 23 | ||
24 | - $this->configuration = ['configuration' => | ||
25 | - ["encode" => 'encode', | ||
26 | - "string" => ['string1', 'string2' ], | ||
27 | - "float" => 'float', | ||
28 | - "integer" => ['integer1', 'integer2' ], | 24 | + $this->configuration = ['hasKey' => true, |
25 | + 'configuration' => | ||
26 | + ['encode' => 'encode', | ||
27 | + 'string' => ['string1', 'string2' ], | ||
28 | + 'float' => 'float', | ||
29 | + 'integer' => ['integer1', 'integer2' ], | ||
29 | ]]; | 30 | ]]; |
30 | 31 | ||
31 | $this->wrong_configuration = ['config' => | 32 | $this->wrong_configuration = ['config' => |
32 | - ["encode" => 'encode', | ||
33 | - "string" => 'string', | ||
34 | - "float" => 'float', | ||
35 | - "integer" => 'integer', | 33 | + ['encode' => 'encode', |
34 | + 'string' => 'string', | ||
35 | + 'float' => 'float', | ||
36 | + 'integer' => 'integer', | ||
36 | ]]; | 37 | ]]; |
37 | 38 | ||
38 | $this->data_in = [ | 39 | $this->data_in = [ |
@@ -49,7 +50,7 @@ class BaseConverterTest extends \Codeception\TestCase\Test | @@ -49,7 +50,7 @@ class BaseConverterTest extends \Codeception\TestCase\Test | ||
49 | 50 | ||
50 | public function testConvertByConfig(){ | 51 | public function testConvertByConfig(){ |
51 | 52 | ||
52 | - $this->data_out = $this->converter->convertByConfiguration($this->data_in, $this->configuration ); | 53 | + $this->data_out = $this->converter->convertByConfiguration( $this->data_in, $this->configuration ); |
53 | $this->assertEquals( $this->data_out['encode'], iconv( 'windows-1251', 'UTF-8', 'test encode string' ), 'Encoding failed' ); | 54 | $this->assertEquals( $this->data_out['encode'], iconv( 'windows-1251', 'UTF-8', 'test encode string' ), 'Encoding failed' ); |
54 | $this->assertInternalType( 'float', $this->data_out['float'], 'Convert to float is failed' ); | 55 | $this->assertInternalType( 'float', $this->data_out['float'], 'Convert to float is failed' ); |
55 | 56 | ||
@@ -58,7 +59,7 @@ class BaseConverterTest extends \Codeception\TestCase\Test | @@ -58,7 +59,7 @@ class BaseConverterTest extends \Codeception\TestCase\Test | ||
58 | public function testConvertToException(){ | 59 | public function testConvertToException(){ |
59 | 60 | ||
60 | $this->setExpectedException('\Exception'); | 61 | $this->setExpectedException('\Exception'); |
61 | - $this->data_out = $this->converter->convertByConfiguration($this->data_in, $this->wrong_configuration ); | 62 | + $this->data_out = $this->converter->convertByConfiguration( $this->data_in, $this->wrong_configuration ); |
62 | 63 | ||
63 | } | 64 | } |
64 | 65 |
tests/unit/CrossesParsingTest.php
@@ -17,7 +17,7 @@ class CrossesParsingTest extends \Codeception\TestCase\Test | @@ -17,7 +17,7 @@ class CrossesParsingTest extends \Codeception\TestCase\Test | ||
17 | public function _before() | 17 | public function _before() |
18 | { | 18 | { |
19 | $this->options[ 'mode' ] = 'crosses'; | 19 | $this->options[ 'mode' ] = 'crosses'; |
20 | - $this->options[ 'converter_conf' ] = [ 'configuration' => [ "string" => ['ARTICLE', 'CROSS_ARTICLE'], ] ]; | 20 | + $this->options[ 'converter_conf' ] = [ 'configuration' => [ "string" => ['ARTICLE', 'CROSS_ARTICLE'] ], 'hasKey' => 1 ]; |
21 | $this->file_path = Yii::getAlias('@data_parser') . '\crosses\test1.csv'; | 21 | $this->file_path = Yii::getAlias('@data_parser') . '\crosses\test1.csv'; |
22 | 22 | ||
23 | } | 23 | } |