Completed
Branch BETA-4.9-messages-queue-fixed (941081)
by
unknown
548:45 queued 526:33
created

Capabilities   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 3
Bugs 2 Features 0
Metric Value
wmc 13
lcom 0
cbo 2
dl 0
loc 74
rs 10
c 3
b 2
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B current_user_has_partial_access_to() 0 14 5
A get_missing_permissions() 0 3 1
A get_missing_permissions_string() 0 3 1
B filter_out_inaccessible_entity_fields() 0 20 6
1
<?php
2
namespace EventEspresso\core\libraries\rest_api;
3
if ( !defined( 'EVENT_ESPRESSO_VERSION' ) ) {
4
	exit( 'No direct script access allowed' );
5
}
6
7
/**
8
 *
9
 * Capabilities
10
 *
11
 * @package			Event Espresso
12
 * @subpackage
13
 * @author				Mike Nelson
14
 *
15
 */
16
class Capabilities {
17
18
	/**
19
	 * The current user can see at least SOME of these entities.
20
	 * @param \EEM_Base $model
21
	 * @param string $model_context one of the return values from EEM_Base::valid_cap_contexts()
22
	 * @return boolean
23
	 */
24
	public static function current_user_has_partial_access_to( $model, $model_context = \EEM_Base::caps_read ) {
25
		if( apply_filters( 'FHEE__Capabilities__current_user_has_partial_access_to__override_begin', false, $model, $model ) ) {
26
			return true;
27
		}
28
		foreach( $model->caps_missing( $model_context ) as $capability_name => $restriction_obj ) {
29
			if( $restriction_obj instanceof \EE_Return_None_Where_Conditions ){
30
				return false;
31
			}
32
		}
33
		if( apply_filters( 'FHEE__Capabilities__current_user_has_partial_access_to__override_end', false, $model, $model ) ) {
34
			return false;
35
		}
36
		return true;
37
	}
38
	/**
39
	 * Gets an array of all the capabilities the current user is missing that affected
40
	 * the query
41
	 *
42
	 * @param \EEM_Base $model
43
	 * @param string $request_type one of the constants on WP_JSON_Server
44
	 * @return array
45
	 */
46
	public static function get_missing_permissions( $model, $request_type = \EEM_Base::caps_read ) {
47
		return $model->caps_missing( $request_type );
48
	}
49
	/**
50
	 * Gets a string of all the capabilities the current user is missing that affected
51
	 * the query
52
	 *
53
	 * @param \EEM_Base $model
54
	 * @param string $model_context one of the return values from EEM_Base::valid_cap_contexts()
55
	 * @return string
56
	 */
57
	public static function get_missing_permissions_string( $model, $model_context = \EEM_Base::caps_read ) {
58
		return implode(',', array_keys( self::get_missing_permissions( $model, $model_context ) ) );
59
	}
60
61
	/**
62
	 * Takes a entity that's ready to be returned and removes fields which the user shouldn't be able to access.
63
	 * @param array $entity
64
	 * @param \EEM_Base $model
65
	 * @param string $request_type one of the return values from EEM_Base::valid_cap_contexts()
66
	 * @param Model_Version_Info $model_version_info
67
	 * @return array ready for converting into json
68
	 */
69
	public static function filter_out_inaccessible_entity_fields( $entity,  $model, $request_type, $model_version_info ) {
70
		//we only care to do this for frontend reads and when the user can't edit the item
71
		if(  $request_type !== \EEM_Base::caps_read ||
72
				$model->exists( array(
73
					array( $model->primary_key_name() => $entity[ $model->primary_key_name() ] ),
74
					'default_where_conditions' => 'none',
75
					'caps' => \EEM_Base::caps_edit ) ) ) {
76
			return $entity;
77
		}
78
		foreach( $model->field_settings() as $field_name => $field_obj ){
79
			if( $model_version_info->field_has_rendered_format( $field_obj )
80
				&& isset( $entity[ $field_name ][ 'raw' ] )
81
			) {
82
				unset( $entity[ $field_name ][ 'raw' ] );
83
			}
84
		}
85
		//theoretically we may want to filter out specific fields for specific models
86
87
		return apply_filters( 'FHEE__Capabilities__filter_out_inaccessible_entity_fields', $entity, $model, $request_type );
88
	}
89
}
90
91
// End of file Capabilities.php