summaryrefslogtreecommitdiff
blob: 10095bcb44e5cea32b8d6ec526e278ae4d4a2269 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName

new WPCOM_JSON_API_Site_User_Endpoint(
	array(
		'description'          => 'Get details of a user of a site by ID.',
		'group'                => '__do_not_document', // 'users'
		'stat'                 => 'sites:1:user',
		'method'               => 'GET',
		'path'                 => '/sites/%s/users/%d',
		'path_labels'          => array(
			'$site'    => '(int|string) Site ID or domain',
			'$user_id' => '(int) User ID',
		),
		'response_format'      => WPCOM_JSON_API_Site_User_Endpoint::$user_format,
		'example_request'      => 'https://public-api.wordpress.com/rest/v1/sites/30434183/user/23',
		'example_request_data' => array(
			'headers' => array(
				'authorization' => 'Bearer YOUR_API_TOKEN',
			),
		),
		'example_response'     => '{
		"ID": 18342963,
		"login": "binarysmash",
		"email": false,
		"name": "binarysmash",
		"URL": "http:\/\/binarysmash.wordpress.com",
		"avatar_URL": "http:\/\/0.gravatar.com\/avatar\/a178ebb1731d432338e6bb0158720fcc?s=96&d=identicon&r=G",
		"profile_URL": "http:\/\/en.gravatar.com\/binarysmash",
		"roles": [ "administrator" ]
	}',
	)
);

new WPCOM_JSON_API_Site_User_Endpoint(
	array(
		'description'          => 'Get details of a user of a site by login.',
		'group'                => 'users',
		'stat'                 => 'sites:1:user',
		'method'               => 'GET',
		'path'                 => '/sites/%s/users/login:%s',
		'path_labels'          => array(
			'$site'    => '(int|string) The site ID or domain.',
			'$user_id' => '(string) The user\'s login.',
		),
		'response_format'      => WPCOM_JSON_API_Site_User_Endpoint::$user_format,
		'example_request'      => 'https://public-api.wordpress.com/rest/v1/sites/30434183/user/login:binarysmash',
		'example_request_data' => array(
			'headers' => array(
				'authorization' => 'Bearer YOUR_API_TOKEN',
			),
		),
		'example_response'     => '{
		"ID": 18342963,
		"login": "binarysmash",
		"email": false,
		"name": "binarysmash",
		"URL": "http:\/\/binarysmash.wordpress.com",
		"avatar_URL": "http:\/\/0.gravatar.com\/avatar\/a178ebb1731d432338e6bb0158720fcc?s=96&d=identicon&r=G",
		"profile_URL": "http:\/\/en.gravatar.com\/binarysmash",
		"roles": [ "administrator" ]
	}',
	)
);

new WPCOM_JSON_API_Site_User_Endpoint(
	array(
		'description'          => 'Update details of a user of a site.',
		'group'                => 'users',
		'stat'                 => 'sites:1:user',
		'method'               => 'POST',
		'path'                 => '/sites/%s/users/%d',
		'path_labels'          => array(
			'$site'    => '(int|string) The site ID or domain.',
			'$user_id' => '(int) The user\'s ID.',
		),
		'request_format'       => WPCOM_JSON_API_Site_User_Endpoint::$user_format,
		'response_format'      => WPCOM_JSON_API_Site_User_Endpoint::$user_format,
		'example_request'      => 'https://public-api.wordpress.com/rest/v1/sites/30434183/user/23',
		'example_request_data' => array(
			'headers' => array(
				'authorization' => 'Bearer YOUR_API_TOKEN',
			),
			'body'    => array(
				'roles'      => array(
					array(
						'administrator',
					),
				),
				'first_name' => 'Rocco',
				'last_name'  => 'Tripaldi',
			),
		),
		'example_response'     => '{
		"ID": 18342963,
		"login": "binarysmash",
		"email": false,
		"name": "binarysmash",
		"URL": "http:\/\/binarysmash.wordpress.com",
		"avatar_URL": "http:\/\/0.gravatar.com\/avatar\/a178ebb1731d432338e6bb0158720fcc?s=96&d=identicon&r=G",
		"profile_URL": "http:\/\/en.gravatar.com\/binarysmash",
		"roles": [ "administrator" ]
	}',
	)
);

/**
 * Site user endpoint class.
 *
 * /sites/%s/users/%d -> $blog_id, $user_id
 */
class WPCOM_JSON_API_Site_User_Endpoint extends WPCOM_JSON_API_Endpoint {

	/**
	 * User format.
	 *
	 * @var array
	 */
	public static $user_format = array(
		'ID'          => '(int) The ID of the user',
		'login'       => '(string) The login username of the user',
		'email'       => '(string) The email of the user',
		'name'        => '(string) The name to display for the user',
		'first_name'  => '(string) The first name of the user',
		'last_name'   => '(string) The last name of the user',
		'nice_name'   => '(string) The nice_name to display for the user',
		'URL'         => '(string) The primary blog of the user',
		'avatar_URL'  => '(url) Gravatar image URL',
		'profile_URL' => '(url) Gravatar Profile URL',
		'site_ID'     => '(int) ID of the user\'s primary blog',
		'roles'       => '(array|string) The role or roles of the user',
	);

	/**
	 * API Callback.
	 *
	 * @param string $path - the path.
	 * @param int    $blog_id - the blog ID.
	 * @param int    $user_id - the user ID.
	 *
	 * @return array|WP_Error
	 */
	public function callback( $path = '', $blog_id = 0, $user_id = 0 ) {
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
		if ( is_wp_error( $blog_id ) ) {
			return $blog_id;
		}
		if ( ! current_user_can_for_blog( $blog_id, 'list_users' ) ) {
			return new WP_Error( 'unauthorized', 'User cannot view users for specified site', 403 );
		}

		// Get the user by ID or login
		$get_by = false !== strpos( $path, '/users/login:' ) ? 'login' : 'id';
		$user   = get_user_by( $get_by, $user_id );

		if ( ! $user ) {
			return new WP_Error( 'unknown_user', 'Unknown user', 404 );
		}

		if ( ! is_user_member_of_blog( $user->ID, $blog_id ) ) {
			return new WP_Error( 'unknown_user_for_site', 'Unknown user for site', 404 );
		}

		if ( 'GET' === $this->api->method ) {
			return $this->get_user( $user->ID );
		} elseif ( 'POST' === $this->api->method ) {
			if ( ! current_user_can_for_blog( $blog_id, 'promote_users' ) ) {
				return new WP_Error( 'unauthorized_no_promote_cap', 'User cannot promote users for specified site', 403 );
			}
			return $this->update_user( $user_id, $blog_id );
		} else {
			return new WP_Error( 'bad_request', 'An unsupported request method was used.' );
		}
	}

	/**
	 * Get the user.
	 *
	 * @param int $user_id - the user ID.
	 *
	 * @return object
	 */
	public function get_user( $user_id ) {
		$the_user = $this->get_author( $user_id, true );
		if ( $the_user && ! is_wp_error( $the_user ) ) {
			$userdata        = get_userdata( $user_id );
			$the_user->roles = ! is_wp_error( $userdata ) ? array_values( $userdata->roles ) : array();
		}

		return $the_user;
	}

	/**
	 * Updates user data.
	 *
	 * @param int $user_id - the user ID.
	 * @param int $blog_id - the blog ID.
	 *
	 * @return array|WP_Error
	 */
	public function update_user( $user_id, $blog_id ) {
		$user       = array();
		$input      = $this->input();
		$user['ID'] = $user_id;
		$is_wpcom   = defined( 'IS_WPCOM' ) && IS_WPCOM;

		if ( get_current_user_id() === (int) $user_id && isset( $input['roles'] ) ) {
			return new WP_Error( 'unauthorized', 'You cannot change your own role', 403 );
		}

		if ( $is_wpcom && $user_id !== get_current_user_id() && (int) $user_id === wpcom_get_blog_owner( $blog_id ) ) {
			return new WP_Error( 'unauthorized_edit_owner', 'Current user can not edit blog owner', 403 );
		}

		if ( ! $is_wpcom ) {
			foreach ( $input as $key => $value ) {
				if ( ! is_array( $value ) ) {
					$value = trim( $value );
				}
				$value = wp_unslash( $value );
				switch ( $key ) {
					case 'first_name':
					case 'last_name':
						$user[ $key ] = $value;
						break;
					case 'display_name':
					case 'name':
						$user['display_name'] = $value;
						break;
				}
			}
		}

		if ( isset( $input['roles'] ) ) {
			// For now, we only use the first role in the array.
			if ( is_array( $input['roles'] ) ) {
				$user['role'] = $input['roles'][0];
			} elseif ( is_string( $input['roles'] ) ) {
				$user['role'] = $input['roles'];
			} else {
				return new WP_Error( 'invalid_input', __( 'The roles property must be a string or an array.', 'jetpack' ), 400 );
			}

			$editable_roles = array_keys( get_editable_roles() );
			if ( ! in_array( $user['role'], $editable_roles, true ) ) {
				return new WP_Error(
					'invalid_input',
					sprintf(
						/* Translators: placeholder is an invalid role name */
						esc_html__( '%s is not a valid role.', 'jetpack' ),
						$editable_roles
					),
					400
				);
			}
		}

		$result = wp_update_user( $user );
		if ( is_wp_error( $result ) ) {
			return $result;
		}
		return $this->get_user( $user_id );
	}

}