Skip to content

Commit df57695

Browse files
committed
Limit Access to customers #86
1 parent e1c2f8f commit df57695

File tree

5 files changed

+22
-14
lines changed

5 files changed

+22
-14
lines changed

console/jobs/CreateReportJob.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ class CreateReportJob extends BaseObject implements RetryableJobInterface
2020
const TTR = 600; // in seconds
2121

2222
/**
23-
* @var int $customer
23+
* @var int[] $customer_ids array of customer IDs.
24+
* Limits the orders to one or more customers.
2425
*/
25-
public int $customer;
26+
public array $customer_ids;
2627

2728
/**
2829
* @var int $userId
@@ -50,9 +51,10 @@ class CreateReportJob extends BaseObject implements RetryableJobInterface
5051
public bool $items;
5152

5253
/**
53-
* @var array if set, the report will be generated on order numbers instead of date range.
54+
* @var array|null if set, the report will be generated on order numbers instead of date range.
5455
*/
55-
public array $order_nrs;
56+
public ?array $order_nrs = null;
57+
5658

5759
/**
5860
* @inheritDoc
@@ -97,9 +99,10 @@ protected function processReport(): string
9799
if (is_array($this->order_nrs)) {
98100
$ordersQuery->where(['customer_reference' => $this->order_nrs]);
99101
} else {
100-
$ordersQuery->where(['customer_id' => $this->customer]);
101-
$ordersQuery->andWhere(['between', 'created_date', $this->start_date, $this->end_date])
102+
$ordersQuery->andWhere(['between', 'created_date', $this->start_date, $this->end_date]);
102103
}
104+
// limit access by customer
105+
$ordersQuery->andWhere(['IN', 'customer_id', $this->customer_ids]);
103106

104107

105108
$dir = Yii::getAlias('@console') . '/runtime/reports/';

frontend/controllers/ReportController.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
namespace frontend\controllers;
44

55
use Yii;
6-
use console\jobs\CreateReportJob;
7-
use frontend\models\Customer;
86
use frontend\models\forms\ReportForm;
9-
use frontend\models\User;
7+
use yii\filters\AccessControl;
108

119
/**
1210
* Class ReportController
@@ -20,7 +18,7 @@ public function behaviors()
2018
{
2119
return [
2220
'access' => [
23-
'class' => 'yii\filters\AccessControl',
21+
'class' => AccessControl::class,
2422
'rules' => [
2523
[
2624
'allow' => true,
@@ -34,7 +32,6 @@ public function behaviors()
3432
/**
3533
* Index for creating CSV report
3634
*
37-
* @return string|void
3835
* @throws \yii\base\InvalidConfigException
3936
*/
4037
public function actionIndex($scenario = null)
@@ -61,7 +58,6 @@ public function actionIndex($scenario = null)
6158
}
6259
}
6360

64-
6561
return $this->render(
6662
'index',
6763
[

frontend/models/forms/ReportForm.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function pushReportQueueJob()
100100
switch ($this->scenario) {
101101
case self::SCENARIO_BY_DATE:
102102
$job = new CreateReportJob([
103-
'customer' => $this->customer,
103+
'customer_ids' => [$this->customer],
104104
'start_date' => $this->start_date,
105105
'end_date' => $this->end_date,
106106
'items' => $this->items,
@@ -109,6 +109,7 @@ public function pushReportQueueJob()
109109
case self::SCENARIO_BY_ORDER_NR:
110110
$job = new CreateReportJob([
111111
'order_nrs' => $this->getOrderNrs(),
112+
'customer_ids' => array_keys($this->getCustomerList()),
112113
'items' => $this->items,
113114
]);
114115
break;

tests/unit.suite.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ modules:
1212
- Yii2:
1313
configFile: 'common/config/test-local.php'
1414
transaction: false
15-
step_decorators: ~
15+
step_decorators: ~

tests/unit/frontend/models/ReportFormTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ public function _orderNrProvider()
6464
DEF
6565
6666
67+
CSV
68+
, ['ABC', '123', 'DEF']],
69+
[<<<CSV
70+
ABC,
71+
123 ,
72+
DEF
73+
74+
6775
CSV
6876
, ['ABC', '123', 'DEF']],
6977
];

0 commit comments

Comments
 (0)