final ParseUser user = getCurrentUser(); //TODO (grantland): convert to async synchronized (mutex) { final String sessionToken = user != null ? user.getSessionToken() : null; if (ParseTextUtils.isEmpty(getUsername())) { return Task.forError(new IllegalArgumentException("Username cannot be missing or blank")); if (ParseTextUtils.isEmpty(getPassword())) { return Task.forError(new IllegalArgumentException("Password cannot be missing or blank")); if (getObjectId() != null) { Map<String, Map<String, String>> authData = getAuthData(); if (authData.containsKey(ParseAnonymousUtils.AUTH_TYPE) && authData.get(ParseAnonymousUtils.AUTH_TYPE) == null) { return saveAsync(sessionToken, toAwait); boolean isLazy = user.isLazy(); final String oldUsername = user.getUsername(); final String oldPassword = user.getPassword(); final Map<String, String> anonymousData = user.getAuthData(ParseAnonymousUtils.AUTH_TYPE); user.copyChangesFrom(this); user.setUsername(getUsername()); user.setPassword(getPassword()); revert(); return user.saveAsync(sessionToken, isLazy, toAwait).continueWithTask(new Continuation<Void, Task<Void>>() { @Override public Task<Void> then(Task<Void> task) {
/** * Retrieves the username. */ public String getUsername() { return getString(KEY_USERNAME); }
/** * Whether the user is logged in anonymously. * * @param user User to check for anonymity. The user must be logged in on this device. * @return True if the user is anonymous. False if the user is not the current user or is not * anonymous. */ public static boolean isLinked(ParseUser user) { return user.isLinked(AUTH_TYPE); }
/** * This retrieves the currently logged in ParseUser with a valid session, either from memory or * disk if necessary. * * @return The currently logged in ParseUser */ public static ParseUser getCurrentUser() { return getCurrentUser(isAutomaticUserEnabled()); }
public static String getCurrentSessionToken() { ParseUser current = ParseUser.getCurrentUser(); return current != null ? current.getSessionToken() : null; }
private boolean isUnresolvedUser(ParseUser other) { // This might be a different instance, but if they have the same local id, assume it's correct. if (other == null || unresolvedUser == null) return false; return other == unresolvedUser || (other.getObjectId() == null && other.getOrCreateLocalId().equals(unresolvedUser.getOrCreateLocalId())); }
when(currentUser.getUsername()).thenReturn("oldUserName"); when(currentUser.getPassword()).thenReturn("oldPassword"); when(currentUser.isLazy()).thenReturn(false); when(currentUser.isLinked(ParseAnonymousUtils.AUTH_TYPE)).thenReturn(true); when(currentUser.getSessionToken()).thenReturn("oldSessionToken"); when(currentUser.getAuthData()).thenReturn(new HashMap<String, Map<String, String>>()); when(currentUser.saveAsync(anyString(), eq(false), Matchers.<Task<Void>>any())) .thenReturn(Task.<Void>forResult(null)); ParseUser.State state = new ParseUser.State.Builder() .put("oldKey", "oldValue") .build(); when(currentUser.getState()).thenReturn(state); ParseCurrentUserController currentUserController = mock(ParseCurrentUserController.class); when(currentUserController.getAsync(anyBoolean())).thenReturn(Task.forResult(currentUser)); ParseCorePlugins.getInstance().registerCurrentUserController(currentUserController); ParseUser user = new ParseUser(); user.setUsername("userName"); user.setPassword("password"); Map<String, String> anonymousAuthData = new HashMap<>(); anonymousAuthData.put("key", "token"); user.putAuthData(ParseAnonymousUtils.AUTH_TYPE, anonymousAuthData); Task<Void> signUpTask = user.signUpAsync(Task.<Void>forResult(null)); signUpTask.waitForCompletion(); verify(currentUser, times(1)).copyChangesFrom(user); verify(currentUser, times(1)).setUsername("userName");
public ParseACL get() { if (defaultACLUsesCurrentUser && defaultACL != null) { ParseUser currentUser = ParseUser.getCurrentUser(); if (currentUser != null) { // If the currentUser has changed, generate a new ACL from the defaultACL. ParseUser last = lastCurrentUser != null ? lastCurrentUser.get() : null; if (last != currentUser) { ParseACL newDefaultACLWithCurrentUser = defaultACL.copy(); newDefaultACLWithCurrentUser.setShared(true); newDefaultACLWithCurrentUser.setReadAccess(currentUser, true); newDefaultACLWithCurrentUser.setWriteAccess(currentUser, true); defaultACLWithCurrentUser = newDefaultACLWithCurrentUser; lastCurrentUser = new WeakReference<>(currentUser); } return defaultACLWithCurrentUser; } } return defaultACL; }
@Test public void testSignUpAsyncWithNoCurrentUserAndSignUpFailure() { // Register a mock currentUserController to make getCurrentUser work ParseCurrentUserController currentUserController = mock(ParseCurrentUserController.class); when(currentUserController.getAsync(anyBoolean())) .thenReturn(Task.<ParseUser>forResult(null)); ParseCorePlugins.getInstance().registerCurrentUserController(currentUserController); // Register a mock userController to make logIn work ParseUserController userController = mock(ParseUserController.class); ParseException signUpException = new ParseException(ParseException.OTHER_CAUSE, "test"); when(userController.signUpAsync( any(ParseUser.State.class), any(ParseOperationSet.class), anyString())) .thenReturn(Task.<ParseUser.State>forError(signUpException)); ParseCorePlugins.getInstance().registerUserController(userController); ParseUser user = new ParseUser(); user.put("key", "value"); user.setUsername("userName"); user.setPassword("password"); Task<Void> signUpTask = user.signUpAsync(Task.<Void>forResult(null)); // Make sure we sign up the user verify(userController, times(1)).signUpAsync( any(ParseUser.State.class), any(ParseOperationSet.class), anyString()); // Make sure user's data is correct assertEquals("value", user.getString("key")); // Make sure we never set the current user verify(currentUserController, never()).setAsync(user); // Make sure task is failed assertTrue(signUpTask.isFaulted()); assertSame(signUpException, signUpTask.getError()); }
ParseUser user = ParseUser.logIn("username", "password"); assertFalse(user.isDirty()); user.put("field", "data"); assertTrue(user.isDirty()); user.saveInBackground().continueWith(new Continuation<Void, Void>() { @Override public Void then(Task<Void> task) { assertTrue(saveCountDown1.await(5, TimeUnit.SECONDS)); assertNull(exceptionCapture.get()); assertFalse(user.isDirty()); user.put("field", "other data"); assertTrue(user.isDirty()); user.saveEventually().continueWith(new Continuation<Void, Void>() { @Override public Void then(Task<Void> task) { assertEquals(ParseException.SCRIPT_ERROR, ((ParseException) exceptionCapture.get()).getCode()); assertEquals("Save is not allowed", exceptionCapture.get().getMessage()); assertTrue(user.isDirty()); user = ParseUser.getCurrentUser(); assertTrue(user.isDirty()); assertEquals("other data", user.get("field"));
ParseCorePlugins.getInstance().registerUserController(userController); ParseUser user = new ParseUser(); user.setUsername("userName"); user.setPassword("password"); ParseTaskUtils.wait(user.signUpAsync(Task.<Void>forResult(null))); any(ParseUser.State.class), any(ParseOperationSet.class), anyString()); assertEquals("newSessionToken", user.getSessionToken()); assertEquals("newValue", user.getString("newKey")); assertFalse(user.isLazy());
when(callbacks.onRestore(Matchers.<Map<String, String>>any())) .thenReturn(true); ParseUser.registerAuthenticationCallback("facebook", callbacks); ParseUser user = new ParseUser(); user.setIsCurrentUser(true); user.setObjectId("objectId"); user.putAuthData(ParseAnonymousUtils.AUTH_TYPE, new HashMap<String, String>()); ParseUser partialMockUser = spy(user); doReturn(Task.<Void>forResult(null)) .when(partialMockUser) .saveAsync(anyString(), eq(false), Matchers.<Task<Void>>any()); doReturn("sessionTokenAgain") .when(partialMockUser) .getSessionToken(); Map<String, String> authData = new HashMap<>(); authData.put("token", "test"); ParseTaskUtils.wait(partialMockUser.linkWithInBackground("facebook", authData)); assertNull(partialMockUser.getAuthData().get(ParseAnonymousUtils.AUTH_TYPE)); assertSame(authData, partialMockUser.getAuthData().get("facebook")); .saveAsync(eq("sessionTokenAgain"), eq(false), Matchers.<Task<Void>>any());
@Override /* package */ void validateSave() { synchronized (mutex) { if (getObjectId() == null) { throw new IllegalArgumentException( "Cannot save a ParseUser until it has been signed up. Call signUp first."); } if (isAuthenticated() || !isDirty() || isCurrentUser()) { return; } } if (!Parse.isLocalDatastoreEnabled()) { // This might be a different of instance of the currentUser, so we need to check objectIds ParseUser current = ParseUser.getCurrentUser(); //TODO (grantland): possible blocking disk i/o if (current != null && getObjectId().equals(current.getObjectId())) { return; } } throw new IllegalArgumentException("Cannot save a ParseUser that is not authenticated."); }
/** * 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())); } }
@Test public void testLoginWithAsyncWithLinkedLazyUser() throws Exception { // Register a mock currentUserController to make getCurrentUser work ParseUser currentUser = new ParseUser(); currentUser.putAuthData(ParseAnonymousUtils.AUTH_TYPE, new HashMap<String, String>()); setLazy(currentUser); ParseUser partialMockCurrentUser = spy(currentUser); when(partialMockCurrentUser.getSessionToken()).thenReturn("oldSessionToken"); doReturn(Task.<ParseUser>forResult(null)) .when(partialMockCurrentUser) .resolveLazinessAsync(Matchers.<Task<Void>>any()); ParseCurrentUserController currentUserController = mock(ParseCurrentUserController.class); when(currentUserController.getAsync(false)).thenReturn(Task.forResult(partialMockCurrentUser)); ParseCorePlugins.getInstance().registerCurrentUserController(currentUserController); String authType = "facebook"; Map<String, String> authData = new HashMap<>(); authData.put("token", "123"); ParseUser userAfterLogin = ParseTaskUtils.wait(ParseUser.logInWithInBackground(authType, authData)); // Make sure we stripAnonymity assertNull(userAfterLogin.getAuthData().get(ParseAnonymousUtils.AUTH_TYPE)); // Make sure we update authData assertEquals(authData, userAfterLogin.getAuthData().get("facebook")); // Make sure we resolveLaziness verify(partialMockCurrentUser, times(1)).resolveLazinessAsync(Matchers.<Task<Void>>any()); }
ParseCorePlugins.getInstance().registerUserController(userController); ParseUser currentUser = new ParseUser(); currentUser.putAuthData(ParseAnonymousUtils.AUTH_TYPE, new HashMap<String, String>()); currentUser.setObjectId("objectId"); // Make it not lazy. ParseUser partialMockCurrentUser = spy(currentUser); when(partialMockCurrentUser.getSessionToken()).thenReturn("sessionToken"); ParseException linkException = new ParseException(ParseException.ACCOUNT_ALREADY_LINKED, "Account already linked"); doReturn(Task.<Void>forError(linkException)) .when(partialMockCurrentUser) .linkWithInBackground(anyString(), Matchers.<Map<String, String>>any()); ParseCurrentUserController currentUserController = mock(ParseCurrentUserController.class); when(currentUserController.getAsync(false)).thenReturn(Task.forResult(partialMockCurrentUser)); Map<String, String> authData = new HashMap<>(); authData.put("token", "123"); ParseUser userAfterLogin = ParseTaskUtils.wait(ParseUser.logInWithInBackground(authType, authData)); verify(partialMockCurrentUser, times(1)).linkWithInBackground(authType, authData); assertEquals("newValue", userAfterLogin.get("newKey")); assertEquals("newSessionToken", userAfterLogin.getSessionToken());
@Test public void testSetStateDoesNotAddNonExistentAuthData() { // Set user initial state ParseUser.State userState = new ParseUser.State.Builder() .objectId("test") .put("oldKey", "oldValue") .put("key", "value") .build(); ParseUser user = ParseObject.from(userState); user.setIsCurrentUser(true); // Build new state ParseUser.State newUserState = new ParseUser.State.Builder() .objectId("testAgain") .put("key", "valueAgain") .build(); user.setState(newUserState); // Make sure we do not add authData when it did not exist before assertFalse(user.keySet().contains("authData")); assertEquals(1, user.keySet().size()); assertEquals(0, user.getAuthData().size()); // Make sure old state is replaced assertFalse(user.has("oldKey")); // Make sure new state is set assertEquals("testAgain", user.getObjectId()); assertEquals("valueAgain", user.get("key")); }
user.setUsername(username); user.setPassword(password); user.setEmail(email); user.put(USER_OBJECT_NAME_FIELD, name); user.signUpInBackground(new SignUpCallback() {
ParseCorePlugins.getInstance().registerCurrentUserController(currentUserController); ParseUser user = new ParseUser(); Map<String, String> anonymousAuthData = new HashMap<>(); anonymousAuthData.put("anonymousToken", "anonymousTest"); user.setObjectId("objectId"); user.putAuthData(ParseAnonymousUtils.AUTH_TYPE, anonymousAuthData); ParseUser partialMockUser = spy(user); Exception saveException = new Exception(); doReturn(Task.<Void>forError(saveException)) .when(partialMockUser) .saveAsync(anyString(), eq(false), Matchers.<Task<Void>>any()); doReturn("sessionTokenAgain") .when(partialMockUser) .getSessionToken(); String authType = "facebook"; Map<String, String> authData = new HashMap<>(); partialMockUser.linkWithInBackground(authType, authData); linkTask.waitForCompletion(); .saveAsync(eq("sessionTokenAgain"), eq(false), Matchers.<Task<Void>>any()); assertSame(anonymousAuthData, partialMockUser.getAuthData().get(ParseAnonymousUtils.AUTH_TYPE)); assertNull(partialMockUser.getAuthData().get("facebook"));