This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathUsersSnippets.java
144 lines (126 loc) · 6.65 KB
/
UsersSnippets.java
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
/*
* Copyright (c) Microsoft. All rights reserved. Licensed under the MIT license.
* See LICENSE in the project root for license information.
*/
package com.microsoft.graph.snippets.snippet;
import android.content.SharedPreferences;
import com.google.gson.JsonObject;
import com.microsoft.graph.concurrency.ICallback;
import com.microsoft.graph.core.ClientException;
import com.microsoft.graph.models.extensions.PasswordProfile;
import com.microsoft.graph.models.extensions.User;
import com.microsoft.graph.options.Option;
import com.microsoft.graph.options.QueryOption;
import com.microsoft.graph.requests.extensions.IUserCollectionPage;
import com.microsoft.graph.snippets.util.SharedPrefsUtil;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import static com.microsoft.graph.snippets.R.array.get_organization_filtered_users;
import static com.microsoft.graph.snippets.R.array.get_organization_users;
import static com.microsoft.graph.snippets.R.array.insert_organization_user;
import static com.microsoft.graph.snippets.util.SharedPrefsUtil.PREF_USER_TENANT;
public abstract class UsersSnippets<Result> extends AbstractSnippet<Result> {
public UsersSnippets(Integer descriptionArray) {
super(SnippetCategory.userSnippetCategory, descriptionArray);
}
static UsersSnippets[] getUsersSnippets() {
return new UsersSnippets[]{
// Marker element
new UsersSnippets(null) {
@Override
public void request(ICallback callback) {
// Not implemented
}
},
/*
* Gets all of the users in your tenant\'s directory.
* HTTP GET https://graph.microsoft.com/{version}/myOrganization/users
* @see https://graph.microsoft.io/docs/api-reference/v1.0/api/user_list
*/
new UsersSnippets<JsonObject>(get_organization_users) {
@Override
public void request(final ICallback<JsonObject> callback) {
mGraphServiceClient
.users()
.buildRequest()
.get(new ICallback<IUserCollectionPage>() {
@Override
public void success(IUserCollectionPage iUserCollectionPage) {
callback.success(iUserCollectionPage.getRawObject());
}
@Override
public void failure(ClientException ex) {
callback.failure(ex);
}
});
}
},
/*
* Gets all of the users in your tenant's directory who are from the United States, using $filter.
* HTTP GET https://graph.microsoft.com/{version}/myOrganization/users?$filter=country eq \'United States\'
* @see http://graph.microsoft.io/docs/overview/query_parameters
*/
new UsersSnippets<JsonObject>(get_organization_filtered_users) {
@Override
public void request(final ICallback<JsonObject> callback) {
final List<Option> options = new LinkedList<>();
options.add(new QueryOption("$filter", "country eq 'United States'"));
mGraphServiceClient
.users()
.buildRequest(options)
.get(new ICallback<IUserCollectionPage>() {
@Override
public void success(IUserCollectionPage iUserCollectionPage) {
callback.success(iUserCollectionPage.getRawObject());
}
@Override
public void failure(ClientException ex) {
callback.failure(ex);
}
});
}
},
/*
* Adds a new user to the tenant's directory
* HTTP POST https://graph.microsoft.com/{version}/myOrganization/users
* @see https://graph.microsoft.io/docs/api-reference/v1.0/api/user_post_users
*/
new UsersSnippets<JsonObject>(insert_organization_user) {
@Override
public void request(final ICallback<JsonObject> callback) {
//Use a random UUID for the user name
String randomUserName = UUID.randomUUID().toString();
// create the user
User user = new User();
user.accountEnabled = true;
user.displayName = "SAMPLE " + randomUserName;
user.mailNickname = randomUserName;
// get the tenant from preferences
SharedPreferences prefs = SharedPrefsUtil.getSharedPreferences();
String tenant = prefs.getString(PREF_USER_TENANT, "");
user.userPrincipalName = randomUserName + "@" + tenant;
// initialize a password & say whether or not the user must change it
PasswordProfile password = new PasswordProfile();
password.password = UUID.randomUUID().toString().substring(0, 16);
password.forceChangePasswordNextSignIn = false;
user.passwordProfile = password;
mGraphServiceClient
.users()
.buildRequest()
.post(user, new ICallback<User>() {
@Override
public void success(User user) {
callback.success(user.getRawObject());
}
@Override
public void failure(ClientException ex) {
callback.failure(ex);
}
});
}
}
};
}
public abstract void request(ICallback<Result> callback);
}