Code Duplication    Length = 15-16 lines in 5 locations

core/libraries/rest_api/calculations/Base.php 1 location

@@ 26-41 (lines=16) @@
23
     */
24
    protected function verifyCurrentUserCan($required_permission, $attempted_calculation)
25
    {
26
        if (! current_user_can($required_permission)) {
27
            throw new RestException(
28
                'permission_denied',
29
                sprintf(
30
                    __(
31
                    // @codingStandardsIgnoreStart
32
                        'Permission denied, you cannot calculate %1$s on %2$s because you do not have the capability "%3$s"',
33
                        // @codingStandardsIgnoreEnd
34
                        'event_espresso'
35
                    ),
36
                    $attempted_calculation,
37
                    EEH_Inflector::pluralize_and_lower($this->getResourceName()),
38
                    $required_permission
39
                )
40
            );
41
        }
42
    }
43
44

core/libraries/rest_api/controllers/model/Write.php 4 locations

@@ 135-149 (lines=15) @@
132
    {
133
        Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_edit, 'create');
134
        $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap();
135
        if (! current_user_can($default_cap_to_check_for)) {
136
            throw new RestException(
137
                'rest_cannot_create_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())),
138
                sprintf(
139
                    esc_html__(
140
                    // @codingStandardsIgnoreStart
141
                        'For now, only those with the admin capability to "%1$s" are allowed to use the REST API to insert data into Event Espresso.',
142
                        // @codingStandardsIgnoreEnd
143
                        'event_espresso'
144
                    ),
145
                    $default_cap_to_check_for
146
                ),
147
                array('status' => 403)
148
            );
149
        }
150
        $submitted_json_data = array_merge((array) $request->get_body_params(), (array) $request->get_json_params());
151
        $model_data = ModelDataTranslator::prepareConditionsQueryParamsForModels(
152
            $submitted_json_data,
@@ 187-201 (lines=15) @@
184
    {
185
        Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_edit, 'edit');
186
        $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap();
187
        if (! current_user_can($default_cap_to_check_for)) {
188
            throw new RestException(
189
                'rest_cannot_edit_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())),
190
                sprintf(
191
                    esc_html__(
192
                    // @codingStandardsIgnoreStart
193
                        'For now, only those with the admin capability to "%1$s" are allowed to use the REST API to update data into Event Espresso.',
194
                        // @codingStandardsIgnoreEnd
195
                        'event_espresso'
196
                    ),
197
                    $default_cap_to_check_for
198
                ),
199
                array('status' => 403)
200
            );
201
        }
202
        $obj_id = $request->get_param('id');
203
        if (! $obj_id) {
204
            throw new RestException(
@@ 241-255 (lines=15) @@
238
    {
239
        Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_delete, 'delete');
240
        $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap();
241
        if (! current_user_can($default_cap_to_check_for)) {
242
            throw new RestException(
243
                'rest_cannot_delete_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())),
244
                sprintf(
245
                    esc_html__(
246
                    // @codingStandardsIgnoreStart
247
                        'For now, only those with the admin capability to "%1$s" are allowed to use the REST API to delete data into Event Espresso.',
248
                        // @codingStandardsIgnoreEnd
249
                        'event_espresso'
250
                    ),
251
                    $default_cap_to_check_for
252
                ),
253
                array('status' => 403)
254
            );
255
        }
256
        $obj_id = $request->get_param('id');
257
        // this is where we would apply more fine-grained caps
258
        $model_obj = $model->get_one_by_ID($obj_id);
@@ 470-484 (lines=15) @@
467
        // Check generic caps. For now, we're only allowing access to this endpoint to full admins.
468
        Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_edit, 'create');
469
        $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap();
470
        if (! current_user_can($default_cap_to_check_for)) {
471
            throw new RestException(
472
                'rest_cannot_add_relation_from_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())),
473
                sprintf(
474
                    esc_html__(
475
                        // @codingStandardsIgnoreStart
476
                        'For now, only those with the admin capability to "%1$s" are allowed to use the REST API to add relations in Event Espresso.',
477
                        // @codingStandardsIgnoreEnd
478
                        'event_espresso'
479
                    ),
480
                    $default_cap_to_check_for
481
                ),
482
                array('status' => 403)
483
            );
484
        }
485
        // Get the main model object.
486
        $model_obj = $this->getOneOrThrowException($model, $request->get_param('id'));
487
        // For now, we require the other model object to exist too. This might be relaxed later.