/** * @return the session token for a user, if they are logged in. */ public String getSessionToken() { return getState().sessionToken(); }
/** * Indicates whether this {@code ParseUser} was created during this session through a call to * {@link #signUp()} or by logging in with a linked service such as Facebook. */ public boolean isNew() { return getState().isNew(); }
/* package for tests */ Task<Void> cleanUpAuthDataAsync() { ParseAuthenticationManager controller = getAuthenticationManager(); Map<String, Map<String, String>> authData; synchronized (mutex) { authData = getState().authData(); if (authData.size() == 0) { return Task.forResult(null); // Nothing to see or do here... } } List<Task<Void>> tasks = new ArrayList<>(); Iterator<Map.Entry<String, Map<String, String>>> i = authData.entrySet().iterator(); while (i.hasNext()) { Map.Entry<String, Map<String, String>> entry = i.next(); if (entry.getValue() == null) { i.remove(); tasks.add(controller.restoreAuthenticationAsync(entry.getKey(), null).makeVoid()); } } State newState = getState().newBuilder() .authData(authData) .build(); setState(newState); return Task.whenAll(tasks); }
/** * Whether the ParseUser has been authenticated on this device. This will be true if the ParseUser * was obtained via a logIn or signUp method. Only an authenticated ParseUser can be saved (with * altered attributes) and deleted. */ public boolean isAuthenticated() { synchronized (mutex) { ParseUser current = ParseUser.getCurrentUser(); return isLazy() || (getState().sessionToken() != null && current != null && getObjectId().equals(current.getObjectId())); } }
/* package */ Task<Void> logOutAsync(boolean revoke) { ParseAuthenticationManager controller = getAuthenticationManager(); List<Task<Void>> tasks = new ArrayList<>(); final String oldSessionToken; synchronized (mutex) { oldSessionToken = getState().sessionToken(); for (Map.Entry<String, Map<String, String>> entry : getAuthData().entrySet()) { tasks.add(controller.deauthenticateAsync(entry.getKey())); } State newState = getState().newBuilder() .sessionToken(null) .isNew(false) .build(); isCurrentUser = false; setState(newState); } if (revoke) { tasks.add(ParseSession.revokeAsync(oldSessionToken)); } return Task.whenAll(tasks); }
@Override public Task<Void> then(Task<Void> task) { return getUserController().signUpAsync( getState(), operations, sessionToken ).continueWithTask(new Continuation<ParseUser.State, Task<Void>>() { @Override public Task<Void> then(final Task<ParseUser.State> signUpTask) { ParseUser.State result = signUpTask.getResult(); return handleSaveResultAsync(result, operations).continueWithTask(new Continuation<Void, Task<Void>>() { @Override public Task<Void> then(Task<Void> task) { if (!signUpTask.isCancelled() && !signUpTask.isFaulted()) { return saveCurrentUserAsync(ParseUser.this); } return signUpTask.makeVoid(); } }); } }); } });
private Task<Void> setSessionTokenInBackground(String newSessionToken) { synchronized (mutex) { State state = getState(); if (newSessionToken.equals(state.sessionToken())) { return Task.forResult(null); } State.Builder builder = state.newBuilder() .sessionToken(newSessionToken); setState(builder.build()); return saveCurrentUserAsync(this); } }
@Override public Task<Void> then(Task<Void> task) { return getUserController().logInAsync(getState(), operations).onSuccessTask(new Continuation<ParseUser.State, Task<Void>>() { @Override public Task<Void> then(Task<ParseUser.State> task) {
@Test public void testToRest() throws Exception { ParseUser user = new ParseUser(); user.setUsername("userName"); user.setPassword("password"); JSONObject json = user.toRest(user.getState(), user.operationSetQueue, PointerEncoder.get()); // Make sure we delete password operations assertFalse(json.getJSONArray("__operations").getJSONObject(0).has("password")); // Make sure we have username operations assertEquals( "userName", json.getJSONArray("__operations").getJSONObject(0).getString("username")); }
@Test public void testSetAsync() throws Exception { File file = new File(temporaryFolder.getRoot(), "test"); ParseUser.State state = mock(ParseUser.State.class); JSONObject json = new JSONObject(); json.put("foo", "bar"); ParseUserCurrentCoder coder = mock(ParseUserCurrentCoder.class); when(coder.encode(eq(state), (ParseOperationSet) isNull(), any(PointerEncoder.class))) .thenReturn(json); FileObjectStore<ParseUser> store = new FileObjectStore<>(ParseUser.class, file, coder); ParseUser user = mock(ParseUser.class); when(user.getState()).thenReturn(state); ParseTaskUtils.wait(store.setAsync(user)); JSONObject jsonAgain = ParseFileUtils.readFileToJSONObject(file); assertEquals(json, jsonAgain, JSONCompareMode.STRICT); }
@Test public void testGetAsync() throws Exception { File file = new File(temporaryFolder.getRoot(), "test"); JSONObject json = new JSONObject(); ParseFileUtils.writeJSONObjectToFile(file, json); ParseUser.State.Builder builder = new ParseUser.State.Builder(); builder.put("foo", "bar"); ParseUserCurrentCoder coder = mock(ParseUserCurrentCoder.class); when(coder.decode(any(ParseUser.State.Builder.class), any(JSONObject.class), any(ParseDecoder.class))) .thenReturn(builder); FileObjectStore<ParseUser> store = new FileObjectStore<>(ParseUser.class, file, coder); ParseUser user = ParseTaskUtils.wait(store.getAsync()); assertEquals("bar", user.getState().get("foo")); }
@Test public void testLoginWithAsyncWithoutExistingLazyUser() throws ParseException { ParseCurrentUserController currentUserController = mock(ParseCurrentUserController.class); when(currentUserController.getAsync(false)).thenReturn(Task.<ParseUser>forResult(null)); when(currentUserController.setAsync(any(ParseUser.class))) .thenReturn(Task.<Void>forResult(null)); ParseUser.State userState = mock(ParseUser.State.class); when(userState.className()).thenReturn("_User"); when(userState.objectId()).thenReturn("1234"); when(userState.isComplete()).thenReturn(true); ParseUserController userController = mock(ParseUserController.class); when(userController.logInAsync(anyString(), anyMapOf(String.class, String.class))) .thenReturn(Task.forResult(userState)); ParseCorePlugins.getInstance().registerCurrentUserController(currentUserController); ParseCorePlugins.getInstance().registerUserController(userController); String authType = "facebook"; Map<String, String> authData = new HashMap<>(); authData.put("token", "123"); ParseUser user = ParseTaskUtils.wait(ParseUser.logInWithInBackground(authType, authData)); verify(currentUserController).getAsync(false); verify(userController).logInAsync(authType, authData); verify(currentUserController).setAsync(user); assertSame(userState, user.getState()); verifyNoMoreInteractions(currentUserController); verifyNoMoreInteractions(userController); }
.put("oldKey", "oldValue") .build(); when(currentUser.getState()).thenReturn(state); ParseCurrentUserController currentUserController = mock(ParseCurrentUserController.class); when(currentUserController.getAsync(anyBoolean())).thenReturn(Task.forResult(currentUser));