Update API.

This commit is contained in:
Hoang Huu
2019-10-22 17:07:45 +07:00
parent b724789907
commit b8c6530801
10 changed files with 743 additions and 36 deletions

View File

@@ -27,6 +27,43 @@ function opalestate_rest_check_post_permissions( $post_type, $context = 'read',
return apply_filters( 'opalestate_rest_check_permissions', $permission, $context, $object_id, $post_type );
}
/**
* Check permissions of users on REST API.
*
* @param string $context Request context.
* @param int $object_id Post ID.
* @return bool
*/
function opalestate_rest_check_user_permissions( $context = 'read', $object_id = 0 ) {
$contexts = [
'read' => 'list_users',
'create' => 'promote_users', // Check if current user can create users, opalestate managers are not allowed to create users.
'edit' => 'edit_users',
'delete' => 'delete_users',
'batch' => 'promote_users',
];
// Check to allow opalestate_managers to manage only agents or agencies.
if ( in_array( $context, [ 'edit', 'delete' ], true ) && opalestate_current_user_has_role( 'opalestate_manager' ) ) {
$permission = false;
$user_data = get_userdata( $object_id );
$opalestate_manager_editable_roles = apply_filters( 'opalestate_manager_editable_roles', [ 'opalestate_agent', 'opalestate_agency' ] );
if ( isset( $user_data->roles ) ) {
$can_manage_users = array_intersect( $user_data->roles, array_unique( $opalestate_manager_editable_roles ) );
// Check if Opalestate Manager can edit agent | agency or with the is same opalestate manager.
if ( 0 < count( $can_manage_users ) || intval( $object_id ) === intval( get_current_user_id() ) ) {
$permission = current_user_can( $contexts[ $context ], $object_id );
}
}
} else {
$permission = current_user_can( $contexts[ $context ], $object_id );
}
return apply_filters( 'opalestate_rest_check_permissions', $permission, $context, $object_id, 'user' );
}
/**
* Return the user data for the given consumer_key.
*
@@ -136,7 +173,7 @@ function opalestate_rand_hash() {
/**
* Opalestate API - Hash.
*
* @param string $data Message to be hashed.
* @param string $data Message to be hashed.
* @return string
*/
function opalestate_api_hash( $data ) {
@@ -155,5 +192,5 @@ function opalestate_rest_urlencode_rfc3986( $value ) {
return array_map( 'opalestate_rest_urlencode_rfc3986', $value );
}
return str_replace( array( '+', '%7E' ), array( ' ', '~' ), rawurlencode( $value ) );
return str_replace( [ '+', '%7E' ], [ ' ', '~' ], rawurlencode( $value ) );
}