Authorization hook implemented.

This commit is contained in:
Geoff Taylor
2019-07-06 10:22:07 -04:00
parent bb4f5cbd50
commit fd80867ac0
4 changed files with 22 additions and 9 deletions
@@ -14,6 +14,25 @@ use GraphQL\Error\UserError;
* Class - Order_Mutation
*/
class Order_Mutation {
/**
* Filterable authentication function.
*
* @param string $mutation Mutation being executed.
*
* @return boolean
*/
public static function authorized( $mutation = 'create', $input, $context, $info ) {
$post_type_object = get_post_type_object( 'shop_order' );
return apply_filters(
"authorized_to_{$mutation}_orders",
! current_user_can( $post_type_object->cap->create_posts ),
$input,
$context,
$info
);
}
/**
* Create an order.
*
+1 -3
View File
@@ -133,9 +133,7 @@ class Order_Create {
*/
public static function mutate_and_get_payload() {
return function( $input, AppContext $context, ResolveInfo $info ) {
$post_type_object = get_post_type_object( 'shop_order' );
if ( ! current_user_can( $post_type_object->cap->create_posts ) ) {
if ( Order_Mutation::authorized( 'create', $input, $context, $info ) ) {
throw new UserError( __( 'User does not have the capabilities necessary to create an order.', 'wp-graphql-woocommerce' ) );
}
+1 -3
View File
@@ -84,9 +84,7 @@ class Order_Delete {
*/
public static function mutate_and_get_payload() {
return function( $input, AppContext $context, ResolveInfo $info ) {
$post_type_object = get_post_type_object( 'shop_order' );
if ( ! current_user_can( $post_type_object->cap->create_posts ) ) {
if ( Order_Mutation::authorized( 'delete', $input, $context, $info ) ) {
throw new UserError( __( 'User does not have the capabilities necessary to delete an order.', 'wp-graphql-woocommerce' ) );
}
+1 -3
View File
@@ -85,9 +85,7 @@ class Order_Update {
*/
public static function mutate_and_get_payload() {
return function( $input, AppContext $context, ResolveInfo $info ) {
$post_type_object = get_post_type_object( 'shop_order' );
if ( ! current_user_can( $post_type_object->cap->create_posts ) ) {
if ( Order_Mutation::authorized( 'update', $input, $context, $info ) ) {
throw new UserError( __( 'User does not have the capabilities necessary to update an order.', 'wp-graphql-woocommerce' ) );
}