Skip to content

Commit 7ae84d2

Browse files
Merge pull request #1154 from Codeinwp/fix/sql_data_save
fix: sql for subscriber
2 parents 106789c + cedcd06 commit 7ae84d2

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

classes/Visualizer/Module/Chart.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1431,6 +1431,10 @@ public function getQueryData() {
14311431
wp_send_json_error( array( 'msg' => __( 'Action not allowed for this user.', 'visualizer' ) ) );
14321432
}
14331433

1434+
if ( ! Visualizer_Module::is_pro() ) {
1435+
wp_send_json_error( array( 'msg' => __( 'Feature is not available.', 'visualizer' ) ) );
1436+
}
1437+
14341438
$params = wp_parse_args( $_POST['params'] );
14351439
$chart_id = filter_var( $params['chart_id'], FILTER_VALIDATE_INT );
14361440
$query = trim( $params['query'], ';' );
@@ -1452,6 +1456,17 @@ public function getQueryData() {
14521456
public function saveQuery() {
14531457
check_ajax_referer( Visualizer_Plugin::ACTION_SAVE_DB_QUERY . Visualizer_Plugin::VERSION, 'security' );
14541458

1459+
if ( ! current_user_can( 'administrator' ) ) {
1460+
wp_send_json_error( array( 'msg' => __( 'Action not allowed for this user.', 'visualizer' ) ) );
1461+
}
1462+
if ( ! is_super_admin() ) {
1463+
wp_send_json_error( array( 'msg' => __( 'Action not allowed for this user.', 'visualizer' ) ) );
1464+
}
1465+
1466+
if ( ! Visualizer_Module::is_pro() ) {
1467+
wp_send_json_error( array( 'msg' => __( 'Feature is not available.', 'visualizer' ) ) );
1468+
}
1469+
14551470
$chart_id = filter_input(
14561471
INPUT_GET,
14571472
'chart',

tests/test-ajax.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ public function setUp() {
6666
public function test_ajax_response_get_query_data_valid_query() {
6767
$this->_setRole( 'administrator' );
6868

69+
$this->enable_pro();
70+
6971
$_GET['security'] = wp_create_nonce( Visualizer_Plugin::ACTION_FETCH_DB_DATA . Visualizer_Plugin::VERSION );
7072

7173
global $wpdb;
@@ -93,6 +95,8 @@ public function test_ajax_response_get_query_data_valid_query() {
9395
public function test_ajax_response_get_query_data_invalid_query() {
9496
$this->_setRole( 'administrator' );
9597

98+
$this->enable_pro();
99+
96100
$_GET['security'] = wp_create_nonce( Visualizer_Plugin::ACTION_FETCH_DB_DATA . Visualizer_Plugin::VERSION );
97101

98102
$_POST['params'] = array(
@@ -120,6 +124,8 @@ public function test_ajax_response_get_query_data_invalid_query() {
120124
public function test_ajax_response_get_query_data_valid_query_with_filtered_columns() {
121125
$this->_setRole( 'administrator' );
122126

127+
$this->enable_pro();
128+
123129
$_GET['security'] = wp_create_nonce( Visualizer_Plugin::ACTION_FETCH_DB_DATA . Visualizer_Plugin::VERSION );
124130

125131
$_POST['params'] = array(
@@ -203,6 +209,8 @@ public function test_ajax_response_get_query_data_subcriber_dissallow() {
203209
public function test_ajax_response_get_query_data_invalid_query_subquery() {
204210
$this->_setRole( 'administrator' );
205211

212+
$this->enable_pro();
213+
206214
$_GET['security'] = wp_create_nonce( Visualizer_Plugin::ACTION_FETCH_DB_DATA . Visualizer_Plugin::VERSION );
207215

208216
$_POST['params'] = array(
@@ -230,6 +238,8 @@ public function test_ajax_response_get_query_data_invalid_query_subquery() {
230238
public function test_ajax_response_get_query_data_invalid_query_comment() {
231239
$this->_setRole( 'administrator' );
232240

241+
$this->enable_pro();
242+
233243
$_GET['security'] = wp_create_nonce( Visualizer_Plugin::ACTION_FETCH_DB_DATA . Visualizer_Plugin::VERSION );
234244

235245
$_POST['params'] = array(
@@ -264,4 +274,66 @@ public function test_sql_comment_strip() {
264274
$source = new Visualizer_Source_Query( "/* SELECT */ DELETE * FROM test_table /* WHERE post_type = 'post' */");
265275
$this->assertEquals( 'DELETE * FROM test_table', $source->get_query() );
266276
}
277+
278+
/**
279+
* Test Save Query not allowed for subscriber.
280+
*/
281+
public function test_sql_save_chart_subscriber() {
282+
$this->_setRole( 'subscriber' );
283+
284+
$_GET['security'] = wp_create_nonce( Visualizer_Plugin::ACTION_SAVE_DB_QUERY . Visualizer_Plugin::VERSION );
285+
$_GET['chart'] = '1';
286+
287+
$_POST['params'] = array(
288+
'query' => 'SELECT * FROM wp_posts LIMIT 1',
289+
);
290+
try {
291+
// Trigger the AJAX action
292+
$this->_handleAjax( Visualizer_Plugin::ACTION_SAVE_DB_QUERY );
293+
} catch ( WPAjaxDieContinueException $e ) {
294+
// We expected this, do nothing.
295+
}
296+
297+
$response = json_decode( $this->_last_response );
298+
$this->assertIsObject( $response );
299+
$this->assertObjectHasAttribute( 'success', $response );
300+
$this->assertObjectHasAttribute( 'data', $response );
301+
$this->assertEquals( 'Action not allowed for this user.', $response->data->msg );
302+
$this->assertFalse( $response->success );
303+
}
304+
305+
/**
306+
* Test Save Query not allowed if not pro.
307+
*/
308+
public function test_sql_save_chart_admin() {
309+
wp_set_current_user( $this->admin_user_id );
310+
$this->_setRole( 'administrator' );
311+
312+
$_GET['security'] = wp_create_nonce( Visualizer_Plugin::ACTION_SAVE_DB_QUERY . Visualizer_Plugin::VERSION );
313+
$_GET['chart'] = '1';
314+
315+
$_POST['params'] = array(
316+
'query' => 'SELECT * FROM wp_posts LIMIT 1',
317+
);
318+
try {
319+
// Trigger the AJAX action
320+
$this->_handleAjax( Visualizer_Plugin::ACTION_SAVE_DB_QUERY );
321+
} catch ( WPAjaxDieContinueException $e ) {
322+
// We expected this, do nothing.
323+
}
324+
325+
$response = json_decode( $this->_last_response );
326+
$this->assertIsObject( $response );
327+
$this->assertObjectHasAttribute( 'success', $response );
328+
$this->assertObjectHasAttribute( 'data', $response );
329+
$this->assertEquals( 'Feature is not available.', $response->data->msg );
330+
$this->assertFalse( $response->success );
331+
}
332+
333+
/**
334+
* Utility method to mock pro version.
335+
*/
336+
private function enable_pro() {
337+
add_filter( 'visualizer_is_pro', '__return_true' );
338+
}
267339
}

0 commit comments

Comments
 (0)