PluginProbe
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 1.1.4
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v1.1.4
4.7.2 4.7.1 4.7.0 4.6.6 4.6.5 4.6.4 4.6.3 4.6.2 4.6.1 4.6.0 4.5.1 4.5.0 4.4.2 4.4.1 4.4.0 4.3.3 4.3.2 4.3.1 4.3.0 4.2.3 4.2.2 4.2.1 1.0.3 1.0.4 1.0.5 All 281 releases
surecart / app / src / Permissions / Models / ModelPermissionsController.php
ModelPermissionsController.php
113 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace SureCart\Permissions\Models;
4
5 use SureCart\Models\User;
6
7 /**
8 * Model permissions abstract class.
9 */
10 abstract class ModelPermissionsController {
11 /**
12 * Get the customer id for the user
13 *
14 * @param int $user_id User ID.
15 * @return string Customer ID.
16 */
17 protected function getCustomerId( $user_id ) {
18 return User::find( $user_id )->customerId();
19 }
20
21 /**
22 * Meta caps for models
23 *
24 * @param bool[] $allcaps Array of key/value pairs where keys represent a capability name
25 * and boolean values represent whether the user has that capability.
26 * @param string[] $caps Required primitive capabilities for the requested capability.
27 * @param array $args {
28 * Arguments that accompany the requested capability check.
29 *
30 * @type string $0 Requested capability.
31 * @type int $1 Concerned user ID.
32 * @type mixed ...$2 Optional second and further parameters, typically object ID.
33 * }
34 * @param WP_User $user The user object.
35 * @return string[] Primitive capabilities required of the user.
36 */
37 public function handle( $allcaps, $caps, $args, $user ) {
38 $name = $caps[0] ?? false;
39 if ( $name && method_exists( $this, $name ) ) {
40 $user = User::find( $user->ID );
41 if ( ! $user ) {
42 return false;
43 }
44
45 // check permission.
46 $permission = $this->$name( $user, $args, $allcaps );
47 if ( $permission ) {
48 $allcaps[ $caps[0] ] = true;
49 return $allcaps;
50 }
51 }
52
53 return $allcaps;
54 }
55
56 /**
57 * Does the model belong to the user?
58 *
59 * @param string $model Model name.
60 * @param string $id Model ID.
61 * @param \SureCart\Models\User $user User model.
62 * @return boolean
63 */
64 public function belongsToUser( $model, $id, $user ) {
65 $model = $model::find( $id );
66 if ( is_wp_error( $model ) ) {
67 return $model;
68 }
69 return $model->belongsToUser( $user );
70 }
71
72 /**
73 * Is ths user listing their own customer ids.
74 *
75 * @param \SureCart\Models\User $user User model.
76 * @param array $customer_ids Array of customer ids.
77 * @return boolean
78 */
79 protected function isListingOwnCustomerIds( $user, $customer_ids ) {
80 // must have list.
81 if ( empty( $customer_ids ) ) {
82 return false;
83 }
84
85 // check each one.
86 foreach ( $customer_ids as $id ) {
87 if ( ! $id || ! in_array( $id, (array) $user->customerIds() ) ) {
88 return false; // this id does not belong to the user.
89 }
90 }
91
92 return true;
93 }
94
95 /**
96 * Check permissions for specific properties of the request.
97 *
98 * @param \WP_REST_Request $request Full details about the request.
99 * @param array $keys Keys to check.
100 *
101 * @return boolean
102 */
103 protected function requestOnlyHasKeys( $request, $keys ) {
104 $keys = array_merge( $keys, [ 'context', '_locale', 'rest_route', 'id', 'expand' ] );
105 foreach ( (array) $request as $key => $value ) {
106 if ( ! in_array( $key, $keys, true ) ) {
107 return false;
108 }
109 }
110 return true;
111 }
112 }
113