Skip to content
This repository has been archived by the owner on Sep 28, 2022. It is now read-only.

Commit

Permalink
Merge pull request #132 from kuzzleio/fix-33
Browse files Browse the repository at this point in the history
Rename getProfiles to getProfileIds and add a proper getProfiles method
  • Loading branch information
benoitvidis committed Jun 6, 2017
2 parents c90ff20 + 0cd37ef commit 35d807f
Show file tree
Hide file tree
Showing 3 changed files with 198 additions and 46 deletions.
20 changes: 6 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@
Official Kuzzle Android SDK
======

This SDK version requires Kuzzle v1.0.0-RC9 or higher.

## About Kuzzle

For UI and linked objects developers, Kuzzle is an open-source solution that handles all the data management (CRUD, real-time storage, search, high-level features, etc).
A backend software, self-hostable and ready to use to power modern apps.

You can access the Kuzzle repository on [Github](https://github.com/kuzzleio/kuzzle)


## SDK Documentation

The complete SDK documentation is available [here](http://kuzzle.io/sdk-documentation)
The complete SDK documentation is available [here](http://docs.kuzzle.io/sdk-reference)

## Report an issue

Expand Down Expand Up @@ -92,20 +89,15 @@ myCollection.subscribe(options);
## Prerequisite

To login using kuzzle you need at least one authentication plugin. You can refer [here](https://github.com/kuzzleio/kuzzle-plugin-auth-passport-local) for a local authentication plugin
or [here](https://github.com/kuzzleio/kuzzle-plugin-auth-github) to refer to an OAuth plugin with github.
or [here](https://github.com/kuzzleio/kuzzle-plugin-auth-passport-oauth) to refer to our OAuth2 plugin.

## Login with local strategy
To know more about how to log in with a Kuzzle SDK, please refer to our [documentation](http://docs.kuzzle.io/sdk-reference/kuzzle/login/)

If you have the kuzzle-plugin-auth-passport-local installed you can login using either the Kuzzle's constructor or the login method.

```java
KuzzleOptions options = new KuzzleOptions();
kuzzle = new Kuzzle("localhost", options);
```

## Login with an OAuth strategy
### Login with an OAuth strategy

If you have an OAUTH plugin like kuzzle-plugin-auth-passport-oauth you can login and use the KuzzleWebViewClient to make it easier to handle.
If you have an OAUTH plugin like kuzzle-plugin-auth-passport-oauth, you may use the KuzzleWebViewClient class to handle the second authentication phase:

```java
Handler handler = new Handler();
Expand Down
77 changes: 67 additions & 10 deletions src/main/java/io/kuzzle/sdk/security/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import org.json.JSONObject;

import java.util.Arrays;
import java.util.ArrayList;

import io.kuzzle.sdk.core.Kuzzle;
import io.kuzzle.sdk.core.Options;
import io.kuzzle.sdk.security.Profile;
import io.kuzzle.sdk.listeners.ResponseListener;
import io.kuzzle.sdk.listeners.OnQueryDoneListener;

Expand All @@ -20,7 +22,7 @@ public class User extends AbstractSecurityDocument {
/**
* The Profiles Ids List.
*/
private JSONArray profileIds = null;
private ArrayList<String> profileIds = new ArrayList<>();

private JSONObject credentials = new JSONObject();

Expand All @@ -41,7 +43,11 @@ public User(final Kuzzle kuzzle, @NonNull final String id, final JSONObject cont
this.content = new JSONObject(content.toString());

if (content.has("profileIds")) {
this.profileIds = content.getJSONArray("profileIds");
JSONArray profiles = content.getJSONArray("profileIds");

for (int i = 0; i < profiles.length(); i++) {
this.profileIds.add(profiles.getString(i));
}
}
}
}
Expand All @@ -57,7 +63,8 @@ public User setProfiles(@NonNull final String[] profileIds) {
throw new IllegalArgumentException("User.setProfiles: you must provide an array of profiles IDs strings");
}

this.profileIds = new JSONArray(Arrays.asList(profileIds));
this.profileIds.clear();
this.profileIds.addAll(Arrays.asList(profileIds));

return this;
}
Expand All @@ -73,7 +80,7 @@ public User addProfile(@NonNull final String profile) {
throw new IllegalArgumentException("User.addProfile: you must provide a string");
}

this.profileIds.put(profile);
this.profileIds.add(profile);

return this;
}
Expand Down Expand Up @@ -278,8 +285,8 @@ public JSONObject serialize() throws JSONException {
data = new JSONObject().put("_id", this.id),
content = new JSONObject(this.content.toString());

if (this.profileIds != null) {
content.put("profileIds", this.profileIds);
if (this.profileIds.size() > 0) {
content.put("profileIds", new JSONArray(this.profileIds));
}

data.put("body", content);
Expand All @@ -300,8 +307,8 @@ public JSONObject creationSerialize() throws JSONException {
content = new JSONObject(this.content.toString()),
credentials = new JSONObject(this.credentials.toString());

if (this.profileIds != null) {
content.put("profileIds", this.profileIds);
if (this.profileIds.size() > 0) {
content.put("profileIds", new JSONArray(this.profileIds));
}

body.put("content", content);
Expand All @@ -316,8 +323,58 @@ public JSONObject creationSerialize() throws JSONException {
*
* @return an array of strings
*/
public JSONArray getProfiles() {
return this.profileIds;
public String[] getProfileIds() {
return this.profileIds.toArray(new String[0]);
}

/**
* Resolves to the associated profiles as Profile objects
*/
public void getProfiles(final ResponseListener<Profile[]> listener) throws JSONException {
getProfiles(null, listener);
}

public void getProfiles(final Options options, final ResponseListener<Profile[]> listener) throws JSONException {
if (listener == null) {
throw new IllegalArgumentException("User.getProfiles: a valid ResponseListener object is required");
}

final Profile[] profiles = new Profile[this.profileIds.size()];

if (this.profileIds.size() == 0) {
listener.onSuccess(profiles);
return;
}

// using an array to allow these variables to be final
// while keeping the possibility to change their value
final int[] fetched = {0};
final boolean[] errored = {false};

for (int i = 0; i < this.profileIds.size(); i++) {
this.kuzzleSecurity.fetchProfile(this.profileIds.get(i), options, new ResponseListener<Profile>() {
@Override
public void onSuccess(Profile response) {
profiles[fetched[0]] = response;
fetched[0]++;

if (fetched[0] == User.this.profileIds.size()) {
listener.onSuccess(profiles);
}
}

@Override
public void onError(JSONObject error) {
// prevents triggering the listener multiple times
if (errored[0]) {
return;
}

errored[0] = true;
listener.onError(error);
}
});
}
}

/**
Expand Down
Loading

0 comments on commit 35d807f

Please sign in to comment.