Skip to content

Commit 3b7eb1e

Browse files
committed
Ability to add and remove roles from a user
1 parent ce097be commit 3b7eb1e

File tree

8 files changed

+190
-15
lines changed

8 files changed

+190
-15
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## [3.1.0] - 2021-09-02
2+
3+
* Ability to add and remove roles from a user
4+
* Dependency updates
5+
16
## [3.0.0] - 2021-04-10
27

38
* Added null safety support

README.md

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
* Get Users Info
1212
* Update Users Info
1313
* Update Users Password
14+
* Add role to a user
15+
* Remove role from a user
1416

1517
**WooCommerce**
1618

@@ -25,7 +27,7 @@ In your flutter project add the dependency:
2527
``` dart
2628
dependencies:
2729
...
28-
wp_json_api: ^3.0.0
30+
wp_json_api: ^3.1.0
2931
```
3032

3133
### Usage example #
@@ -153,6 +155,30 @@ WPUserResetPasswordResponse wpUserResetPasswordResponse = await WPJsonAPI.instan
153155
));
154156
```
155157

158+
#### WordPress - Add a role to a user
159+
- Used to add a role to a user in WordPress
160+
- The first parameter is the **userToken** which is returned from the login/register response. You should have this saved somewhere e.g. shared_pref
161+
162+
``` dart
163+
WPUserAddRoleResponse wpUserAddRoleResponse = await WPJsonAPI.instance
164+
.api((request) => request.wpUserAddRole(
165+
userToken,
166+
role: "customer" // e.g. customer, subscriber
167+
));
168+
```
169+
170+
#### WordPress - Remove a role from a user
171+
- Used to remove a role from a user in WordPress
172+
- The first parameter is the **userToken** which is returned from the login/register response. You should have this saved somewhere e.g. shared_pref
173+
174+
``` dart
175+
WPUserRemoveRoleResponse wpUserAddRemoveResponse = await WPJsonAPI.instance
176+
.api((request) => request.wpUserRemoveRole(
177+
userToken,
178+
role: "customer" // e.g. customer, subscriber
179+
));
180+
```
181+
156182
#### WooCommerce - Get users info in WooCommerce
157183
- Used to get WooCommerce info for a given user
158184
- The first parameter is the **userToken** which is returned from the login/register response. You should have this saved somewhere e.g. shared_pref
@@ -208,6 +234,6 @@ For help getting started with WooSignal, view our
208234
To use this plugin, add `wp_json_api` as a [dependency in your pubspec.yaml file](https://flutter.io/platform-plugins/).
209235

210236
## Note
211-
Install WordPress plugin "WP JSON API" 3.0.x or later for version 2.0.0+
237+
Install WordPress plugin "WP JSON API" 3.1.x from [woosignal](https://woosignal.com/plugins/wordpress/wp-json-api) to use this flutter plugin `wp_json_api` (3.1.0).
212238

213239
Disclaimer: This plugin is not affiliated with or supported by Automattic, Inc. All logos and trademarks are the property of their respective owners.

lib/enums/wp_route_type.dart

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,31 @@
1414
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1515

1616
enum WPRouteType {
17-
/// Route type [WPRouteType.UserUpdatePassword] is used to update a WordPress users password.
17+
/// Route type [WPRouteType.UserUpdatePassword] is used to update a WordPress user's password.
1818
/// For WordPress
1919
UserUpdatePassword,
2020

21-
/// Route type [WPRouteType.UserUpdateInfo] is used to update a WordPress users account info.
21+
/// Route type [WPRouteType.UserUpdateInfo] is used to update a WordPress user's account info.
2222
/// For WordPress
2323
UserUpdateInfo,
2424

25-
/// Route type [WPRouteType.UserInfo] is used to return a WordPress users info.
25+
/// Route type [WPRouteType.UserAddRole] is used to add a role to a WordPress user's account.
26+
/// For WordPress
27+
UserAddRole,
28+
29+
/// Route type [WPRouteType.UserRemoveRole] is used to remove a role to a WordPress user's account.
30+
/// For WordPress
31+
UserRemoveRole,
32+
33+
/// Route type [WPRouteType.UserInfo] is used to return a WordPress user's info.
2634
/// For WordPress
2735
UserInfo,
2836

29-
/// Route type [WPRouteType.UserRegister] is used to Register a user on WordPress.
37+
/// Route type [WPRouteType.UserRegister] is used to Register a user's on WordPress.
3038
/// For WordPress
3139
UserRegister,
3240

33-
/// Route type [WPRouteType.UserLogin] is used to Login a user on WordPress.
41+
/// Route type [WPRouteType.UserLogin] is used to Login a user's on WordPress.
3442
/// For WordPress
3543
UserLogin,
3644

@@ -42,7 +50,7 @@ enum WPRouteType {
4250
/// For WordPress
4351
AuthVerify,
4452

45-
/// Route type [WPRouteType.WCCustomerInfo] is used to get a Customers info.
53+
/// Route type [WPRouteType.WCCustomerInfo] is used to get a Customer's info.
4654
/// For WooCommerce
4755
WCCustomerInfo,
4856

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) 2021, WooSignal Ltd.
2+
// All rights reserved.
3+
//
4+
// Redistribution and use in source and binary forms are permitted
5+
// provided that the above copyright notice and this paragraph are
6+
// duplicated in all such forms and that any documentation,
7+
// advertising materials, and other materials related to such
8+
// distribution and use acknowledge that the software was developed
9+
// by the WooSignal. The name of the
10+
// WooSignal may not be used to endorse or promote products derived
11+
// from this software without specific prior written permission.
12+
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
13+
// IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
14+
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
15+
16+
class WPUserAddRoleResponse {
17+
List<dynamic>? data;
18+
String? message;
19+
int? status;
20+
21+
WPUserAddRoleResponse({this.data, this.message, this.status});
22+
23+
WPUserAddRoleResponse.fromJson(Map<String, dynamic> json) {
24+
if (json['data'] != null) {
25+
data = json['data'];
26+
}
27+
message = json['message'];
28+
status = json['status'];
29+
}
30+
31+
Map<String, dynamic> toJson() {
32+
final Map<String, dynamic> data = new Map<String, dynamic>();
33+
if (this.data != null) {
34+
data['data'] = this.data!.map((v) => v.toJson()).toList();
35+
}
36+
data['message'] = this.message;
37+
data['status'] = this.status;
38+
return data;
39+
}
40+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) 2021, WooSignal Ltd.
2+
// All rights reserved.
3+
//
4+
// Redistribution and use in source and binary forms are permitted
5+
// provided that the above copyright notice and this paragraph are
6+
// duplicated in all such forms and that any documentation,
7+
// advertising materials, and other materials related to such
8+
// distribution and use acknowledge that the software was developed
9+
// by the WooSignal. The name of the
10+
// WooSignal may not be used to endorse or promote products derived
11+
// from this software without specific prior written permission.
12+
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
13+
// IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
14+
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
15+
16+
class WPUserRemoveRoleResponse {
17+
List<dynamic>? data;
18+
String? message;
19+
int? status;
20+
21+
WPUserRemoveRoleResponse({this.data, this.message, this.status});
22+
23+
WPUserRemoveRoleResponse.fromJson(Map<String, dynamic> json) {
24+
if (json['data'] != null) {
25+
data = json['data'];
26+
}
27+
message = json['message'];
28+
status = json['status'];
29+
}
30+
31+
Map<String, dynamic> toJson() {
32+
final Map<String, dynamic> data = new Map<String, dynamic>();
33+
if (this.data != null) {
34+
data['data'] = this.data!.map((v) => v.toJson()).toList();
35+
}
36+
data['message'] = this.message;
37+
data['status'] = this.status;
38+
return data;
39+
}
40+
}

lib/networking/network_manager.dart

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ import 'package:wp_json_api/models/responses/wc_customer_info_response.dart';
3434
import 'package:wp_json_api/models/responses/wc_customer_updated_response.dart';
3535
import 'package:wp_json_api/models/responses/wp_nonce_response.dart';
3636
import 'package:wp_json_api/models/responses/wp_nonce_verified_response.dart';
37+
import 'package:wp_json_api/models/responses/wp_user_add_role_response.dart';
3738
import 'package:wp_json_api/models/responses/wp_user_info_updated_response.dart';
3839
import 'package:wp_json_api/models/responses/wp_user_login_response.dart';
3940
import 'package:wp_json_api/models/responses/wp_user_register_response.dart';
41+
import 'package:wp_json_api/models/responses/wp_user_remove_role_response.dart';
4042
import 'package:wp_json_api/models/responses/wp_user_reset_password_response.dart';
4143
import 'package:wp_json_api/models/wp_meta_meta.dart';
4244
import 'package:wp_json_api/wp_json_api.dart';
@@ -235,6 +237,52 @@ class WPAppNetworkManager {
235237
: WPUserInfoUpdatedResponse.fromJson(json);
236238
}
237239

240+
/// Sends a request to add a role to a WordPress user. Include a valid
241+
/// [userToken] and [role] to send a successful request.
242+
///
243+
/// Returns a [WPUserInfoUpdatedResponse] future.
244+
/// Throws an [Exception] if fails.
245+
Future<WPUserAddRoleResponse> wpUserAddRole(userToken, {required String role}) async {
246+
Map<String, dynamic> payload = {};
247+
payload["role"] = role;
248+
249+
// send http request
250+
final json = await _http(
251+
method: "POST",
252+
url: _urlForRouteType(WPRouteType.UserAddRole),
253+
userToken: userToken,
254+
body: payload,
255+
);
256+
257+
// return response
258+
return _jsonHasBadStatus(json)
259+
? this._throwExceptionForStatusCode(json)
260+
: WPUserAddRoleResponse.fromJson(json);
261+
}
262+
263+
/// Sends a request to remove a role from a WordPress user. Include a valid
264+
/// [userToken] and [role] to send a successful request.
265+
///
266+
/// Returns a [WPUserInfoUpdatedResponse] future.
267+
/// Throws an [Exception] if fails.
268+
Future<WPUserRemoveRoleResponse> wpUserRemoveRole(userToken, {required String role}) async {
269+
Map<String, dynamic> payload = {};
270+
payload["role"] = role;
271+
272+
// send http request
273+
final json = await _http(
274+
method: "POST",
275+
url: _urlForRouteType(WPRouteType.UserRemoveRole),
276+
userToken: userToken,
277+
body: payload,
278+
);
279+
280+
// return response
281+
return _jsonHasBadStatus(json)
282+
? this._throwExceptionForStatusCode(json)
283+
: WPUserRemoveRoleResponse.fromJson(json);
284+
}
285+
238286
/// Reset a user password using the [userToken] and new [password] created.
239287
///
240288
/// Returns a [WCCustomerInfoResponse] future.
@@ -464,7 +512,7 @@ class WPAppNetworkManager {
464512
/// Returns [String] url path for request.
465513
String _getRouteUrlForType(
466514
WPRouteType wpRouteType, {
467-
String apiVersion = 'v2',
515+
String apiVersion = 'v3',
468516
}) {
469517
switch (wpRouteType) {
470518
// AUTH API
@@ -493,6 +541,14 @@ class WPAppNetworkManager {
493541
{
494542
return "/wpapp/api/$apiVersion/update/user/info";
495543
}
544+
case WPRouteType.UserAddRole:
545+
{
546+
return "/wpapp/api/$apiVersion/update/user/role/add";
547+
}
548+
case WPRouteType.UserRemoveRole:
549+
{
550+
return "/wpapp/api/$apiVersion/update/user/role/remove";
551+
}
496552
case WPRouteType.UserUpdatePassword:
497553
{
498554
return "/wpapp/api/$apiVersion/update/user/password";

pubspec.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ packages:
77
name: async
88
url: "https://pub.dartlang.org"
99
source: hosted
10-
version: "2.5.0"
10+
version: "2.6.1"
1111
boolean_selector:
1212
dependency: transitive
1313
description:
@@ -66,7 +66,7 @@ packages:
6666
name: http
6767
url: "https://pub.dartlang.org"
6868
source: hosted
69-
version: "0.13.1"
69+
version: "0.13.3"
7070
http_parser:
7171
dependency: transitive
7272
description:
@@ -113,7 +113,7 @@ packages:
113113
name: source_span
114114
url: "https://pub.dartlang.org"
115115
source: hosted
116-
version: "1.8.0"
116+
version: "1.8.1"
117117
stack_trace:
118118
dependency: transitive
119119
description:
@@ -148,7 +148,7 @@ packages:
148148
name: test_api
149149
url: "https://pub.dartlang.org"
150150
source: hosted
151-
version: "0.2.19"
151+
version: "0.3.0"
152152
typed_data:
153153
dependency: transitive
154154
description:

pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: wp_json_api
22
description: WordPress and WooCommerce JSON API for Flutter Mobile. API allows you to login, register new users, get users info and more.
3-
version: 3.0.0
3+
version: 3.1.0
44
homepage: https://woosignal.com
55
repository: https://github.com/woosignal/wp-json-api-flutter
66
issue_tracker: https://github.com/woosignal/wp-json-api-flutter/issues
@@ -10,7 +10,7 @@ environment:
1010
sdk: '>=2.12.0 <3.0.0'
1111

1212
dependencies:
13-
http: ^0.13.1
13+
http: ^0.13.3
1414
flutter:
1515
sdk: flutter
1616
collection: ^1.15.0

0 commit comments

Comments
 (0)