All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.backendless.UserService Maven / Gradle / Ivy

There is a newer version: 3.1.0
Show newest version
/*
 * ********************************************************************************************************************
 *  

* BACKENDLESS.COM CONFIDENTIAL *

* ******************************************************************************************************************** *

* Copyright 2012 BACKENDLESS.COM. All Rights Reserved. *

* NOTICE: All information contained herein is, and remains the property of Backendless.com and its suppliers, * if any. The intellectual and technical concepts contained herein are proprietary to Backendless.com and its * suppliers and may be covered by U.S. and Foreign Patents, patents in process, and are protected by trade secret * or copyright law. Dissemination of this information or reproduction of this material is strictly forbidden * unless prior written permission is obtained from Backendless.com. *

* ******************************************************************************************************************** */ package com.backendless; import com.backendless.async.callback.AsyncCallback; import com.backendless.exceptions.BackendlessException; import com.backendless.exceptions.BackendlessFault; import com.backendless.exceptions.ExceptionMessage; import com.backendless.persistence.local.UserIdStorageFactory; import com.backendless.persistence.local.UserTokenStorageFactory; import com.backendless.property.AbstractProperty; import com.backendless.property.UserProperty; import weborb.types.Types; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; public final class UserService { final static String USER_MANAGER_SERVER_ALIAS = "com.backendless.services.users.UserService"; private final static String PREFS_NAME = "backendless_pref"; private final static BackendlessUser currentUser = new BackendlessUser(); private final static Object currentUserLock = new Object(); private static final UserService instance = new UserService(); static UserService getInstance() { return instance; } private UserService() { Types.addClientClassMapping( "com.backendless.services.users.property.AbstractProperty", AbstractProperty.class ); Types.addClientClassMapping( "com.backendless.services.users.property.UserProperty", UserProperty.class ); Types.addClientClassMapping( "Users", BackendlessUser.class ); } public BackendlessUser CurrentUser() { if( currentUser != null && currentUser.getProperties().isEmpty() ) return null; return currentUser; } private UserServiceAndroidExtra getUserServiceAndroidExtra() { if( !Backendless.isAndroid() ) throw new RuntimeException( ExceptionMessage.NOT_ANDROID ); return UserServiceAndroidExtra.getInstance(); } public BackendlessUser register( BackendlessUser user ) throws BackendlessException { checkUserToBeProper( user ); user.putProperties( (HashMap) Invoker.invokeSync( USER_MANAGER_SERVER_ALIAS, "register", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), user.getProperties() } ) ); return user; } public void register( final BackendlessUser user, final AsyncCallback responder ) { try { checkUserToBeProper( user ); Invoker.invokeAsync( USER_MANAGER_SERVER_ALIAS, "register", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), user.getProperties() }, new AsyncCallback>() { @Override public void handleResponse( HashMap response ) { user.putProperties( response ); if( responder != null ) responder.handleResponse( user ); } @Override public void handleFault( BackendlessFault fault ) { if( responder != null ) responder.handleFault( fault ); } } ); } catch( BackendlessException e ) { if( responder != null ) responder.handleFault( new BackendlessFault( e ) ); } } public BackendlessUser update( final BackendlessUser user ) throws BackendlessException { checkUserToBeProperForUpdate( user ); if( user.getUserId() != null && user.getUserId().equals( "" ) ) throw new IllegalArgumentException( ExceptionMessage.WRONG_USER_ID ); user.putProperties( (HashMap) Invoker.invokeSync( USER_MANAGER_SERVER_ALIAS, "update", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), user.getProperties() } ) ); return user; } public void update( final BackendlessUser user, final AsyncCallback responder ) { try { checkUserToBeProperForUpdate( user ); if( user.getUserId() != null && user.getUserId().equals( "" ) ) throw new IllegalArgumentException( ExceptionMessage.WRONG_USER_ID ); Invoker.invokeAsync( USER_MANAGER_SERVER_ALIAS, "update", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), user.getProperties() }, new AsyncCallback>() { @Override public void handleResponse( HashMap response ) { user.putProperties( response ); if( responder != null ) responder.handleResponse( user ); } @Override public void handleFault( BackendlessFault fault ) { if( responder != null ) responder.handleFault( fault ); } } ); } catch( Throwable e ) { if( responder != null ) responder.handleFault( new BackendlessFault( e ) ); } } public BackendlessUser login( final String login, final String password ) throws BackendlessException { return login( login, password, false ); } public BackendlessUser login( final String login, final String password, boolean stayLoggedIn ) throws BackendlessException { synchronized( currentUserLock ) { if( !currentUser.getProperties().isEmpty() ) logout(); if( login == null || login.equals( "" ) ) throw new IllegalArgumentException( ExceptionMessage.NULL_LOGIN ); if( password == null || password.equals( "" ) ) throw new IllegalArgumentException( ExceptionMessage.NULL_PASSWORD ); handleUserLogin( (BackendlessUser) Invoker.invokeSync( USER_MANAGER_SERVER_ALIAS, "login", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), login, password } ), stayLoggedIn ); return currentUser; } } public void login( final String login, final String password, final AsyncCallback responder ) { login( login, password, responder, false ); } public void login( final String login, final String password, final AsyncCallback responder, boolean stayLoggedIn ) { if( !currentUser.getProperties().isEmpty() ) logout( new AsyncCallback() { @Override public void handleResponse( Void response ) { login( login, password, responder ); } @Override public void handleFault( BackendlessFault fault ) { if( responder != null ) responder.handleFault( fault ); } } ); else try { synchronized( currentUserLock ) { if( login == null || login.equals( "" ) ) throw new IllegalArgumentException( ExceptionMessage.NULL_LOGIN ); if( password == null || password.equals( "" ) ) throw new IllegalArgumentException( ExceptionMessage.NULL_PASSWORD ); else Invoker.invokeAsync( USER_MANAGER_SERVER_ALIAS, "login", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), login, password }, getUserLoginAsyncHandler( responder, stayLoggedIn ) ); } } catch( Throwable e ) { if( responder != null ) responder.handleFault( new BackendlessFault( e ) ); } } public BackendlessUser loginWithFacebookSession( com.facebook.Session facebookSession, com.facebook.model.GraphUser facebookUser ) { return loginWithFacebookSession( facebookSession, facebookUser, new HashMap() ); } public BackendlessUser loginWithFacebookSession( com.facebook.Session facebookSession, com.facebook.model.GraphUser facebookUser, boolean stayLoggedIn ) { return loginWithFacebookSession( facebookSession, facebookUser, new HashMap(), stayLoggedIn ); } public BackendlessUser loginWithFacebookSession( com.facebook.Session facebookSession, com.facebook.model.GraphUser facebookUser, Map facebookFieldsMappings ) { return loginWithFacebookSession( facebookSession, facebookUser, facebookFieldsMappings, false ); } public BackendlessUser loginWithFacebookSession( com.facebook.Session facebookSession, com.facebook.model.GraphUser facebookUser, Map facebookFieldsMappings, boolean stayLoggedIn ) { synchronized( currentUserLock ) { if( !currentUser.getProperties().isEmpty() ) logout(); handleUserLogin( getUserServiceAndroidExtra().loginWithFacebookSession( facebookSession, facebookUser, facebookFieldsMappings ), stayLoggedIn ); return currentUser; } } public void loginWithFacebookSession( final com.facebook.Session facebookSession, final com.facebook.model.GraphUser facebookUser, final AsyncCallback responder ) { loginWithFacebookSession( facebookSession, facebookUser, null, responder ); } public void loginWithFacebookSession( final com.facebook.Session facebookSession, final com.facebook.model.GraphUser facebookUser, final AsyncCallback responder, boolean stayLoggedIn ) { loginWithFacebookSession( facebookSession, facebookUser, null, responder, stayLoggedIn ); } public void loginWithFacebookSession( final com.facebook.Session facebookSession, final com.facebook.model.GraphUser facebookUser, final Map facebookFieldsMappings, final AsyncCallback responder ) { loginWithFacebookSession( facebookSession, facebookUser, facebookFieldsMappings, responder, false ); } public void loginWithFacebookSession( final com.facebook.Session facebookSession, final com.facebook.model.GraphUser facebookUser, final Map facebookFieldsMappings, final AsyncCallback responder, boolean stayLoggedIn ) { if( !currentUser.getProperties().isEmpty() ) logout( new AsyncCallback() { @Override public void handleResponse( Void response ) { loginWithFacebookSession( facebookSession, facebookUser, facebookFieldsMappings, responder ); } @Override public void handleFault( BackendlessFault fault ) { if( responder != null ) responder.handleFault( fault ); } } ); else try { synchronized( currentUserLock ) { getUserServiceAndroidExtra().loginWithFacebookSession( facebookSession, facebookUser, facebookFieldsMappings, getUserLoginAsyncHandler( responder, stayLoggedIn ) ); } } catch( Throwable e ) { if( responder != null ) responder.handleFault( new BackendlessFault( e ) ); } } public void loginWithFacebookSdk( android.app.Activity context, final AsyncCallback responder ) { loginWithFacebookSdk( context, null, null, responder ); } public void loginWithFacebookSdk( android.app.Activity context, final Map facebookFieldsMappings, List permissions, final AsyncCallback responder ) { getUserServiceAndroidExtra().loginWithFacebookSdk( context, facebookFieldsMappings, permissions, responder ); } public void loginWithFacebook( android.app.Activity context, final AsyncCallback responder ) { loginWithFacebook( context, null, null, null, responder ); } public void loginWithFacebook( android.app.Activity context, android.webkit.WebView webView, final AsyncCallback responder ) { loginWithFacebook( context, webView, null, null, responder ); } public void loginWithFacebook( android.app.Activity context, android.webkit.WebView webView, final AsyncCallback responder, boolean stayLoggedIn ) { loginWithFacebook( context, webView, null, null, responder, stayLoggedIn ); } public void loginWithFacebook( android.app.Activity context, android.webkit.WebView webView, Map facebookFieldsMappings, List permissions, final AsyncCallback responder ) { loginWithFacebook( context, webView, facebookFieldsMappings, permissions, responder, false ); } public void loginWithFacebook( android.app.Activity context, android.webkit.WebView webView, Map facebookFieldsMappings, List permissions, final AsyncCallback responder, boolean stayLoggedIn ) { getUserServiceAndroidExtra().loginWithFacebook( context, webView, facebookFieldsMappings, permissions, getUserLoginAsyncHandler( responder, stayLoggedIn ) ); } public void loginWithTwitter( android.app.Activity context, AsyncCallback responder ) { loginWithTwitter( context, null, null, responder, false ); } public void loginWithTwitter( android.app.Activity context, android.webkit.WebView webView, AsyncCallback responder ) { loginWithTwitter( context, webView, null, responder, false ); } public void loginWithTwitter( android.app.Activity context, Map twitterFieldsMappings, AsyncCallback responder ) { loginWithTwitter( context, null, twitterFieldsMappings, responder, false ); } public void loginWithTwitter( android.app.Activity context, AsyncCallback responder, boolean stayLoggedIn ) { loginWithTwitter( context, null, null, responder, stayLoggedIn ); } public void loginWithTwitter( android.app.Activity context, android.webkit.WebView webView, Map twitterFieldsMappings, AsyncCallback responder ) { loginWithTwitter( context, webView, twitterFieldsMappings, responder, false ); } public void loginWithTwitter( android.app.Activity context, android.webkit.WebView webView, AsyncCallback responder, boolean stayLoggedIn ) { loginWithTwitter( context, webView, null, responder, stayLoggedIn ); } public void loginWithTwitter( android.app.Activity context, Map twitterFieldsMappings, AsyncCallback responder, boolean stayLoggedIn ) { loginWithTwitter( context, null, twitterFieldsMappings, responder, stayLoggedIn ); } public void loginWithTwitter( android.app.Activity context, android.webkit.WebView webView, Map twitterFieldsMappings, AsyncCallback responder, boolean stayLoggedIn ) { getUserServiceAndroidExtra().loginWithTwitter( context, webView, twitterFieldsMappings, getUserLoginAsyncHandler( responder, stayLoggedIn ) ); } public void logout() throws BackendlessException { synchronized( currentUserLock ) { try { Invoker.invokeSync( USER_MANAGER_SERVER_ALIAS, "logout", new Object[] { Backendless.getApplicationId(), Backendless.getVersion() } ); } catch( BackendlessException fault ) { if( !isLogoutFaultAllowed( fault.getCode() ) ) throw fault; //else everything is OK } handleLogout(); } } private void handleLogout() { currentUser.clearProperties(); HeadersManager.getInstance().removeHeader( HeadersManager.HeadersEnum.USER_TOKEN_KEY ); UserTokenStorageFactory.instance().getStorage().set( "" ); UserIdStorageFactory.instance().getStorage().set( "" ); } public void logout( final AsyncCallback responder ) { synchronized( currentUserLock ) { Invoker.invokeAsync( USER_MANAGER_SERVER_ALIAS, "logout", new Object[] { Backendless.getApplicationId(), Backendless.getVersion() }, new AsyncCallback() { @Override public void handleResponse( Void response ) { try { handleLogout(); } catch( Throwable e ) { if( responder != null ) responder.handleFault( new BackendlessFault( e ) ); } if( responder != null ) responder.handleResponse( response ); } @Override public void handleFault( BackendlessFault fault ) { if( isLogoutFaultAllowed( fault.getCode() ) ) { handleResponse( null ); return; } if( responder != null ) responder.handleFault( fault ); } } ); } } public void restorePassword( String identity ) throws BackendlessException { if( identity == null ) throw new IllegalArgumentException( ExceptionMessage.NULL_IDENTITY ); Invoker.invokeSync( USER_MANAGER_SERVER_ALIAS, "restorePassword", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), identity } ); } public void restorePassword( final String identity, final AsyncCallback responder ) { try { if( identity == null ) throw new IllegalArgumentException( ExceptionMessage.NULL_IDENTITY ); Invoker.invokeAsync( USER_MANAGER_SERVER_ALIAS, "restorePassword", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), identity }, responder ); } catch( Throwable e ) { if( responder != null ) responder.handleFault( new BackendlessFault( e ) ); } } public BackendlessUser findById( String id ) throws BackendlessException { if( id == null ) throw new IllegalArgumentException( ExceptionMessage.NULL_IDENTITY ); BackendlessUser result = new BackendlessUser(); result.putProperties( (HashMap) Invoker.invokeSync( USER_MANAGER_SERVER_ALIAS, "findById", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), id, new ArrayList() } ) ); return result; } public void findById( final String id, final AsyncCallback responder ) { try { if( id == null ) throw new IllegalArgumentException( ExceptionMessage.NULL_IDENTITY ); Invoker.invokeAsync( USER_MANAGER_SERVER_ALIAS, "findById", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), id, new ArrayList() }, new AsyncCallback>() { @Override public void handleResponse( HashMap response ) { BackendlessUser result = new BackendlessUser(); result.putProperties( response ); if( responder != null ) responder.handleResponse( result ); } @Override public void handleFault( BackendlessFault fault ) { if( responder != null ) responder.handleFault( fault ); } } ); } catch( Throwable e ) { if( responder != null ) responder.handleFault( new BackendlessFault( e ) ); } } public void assignRole( String identity, String roleName ) throws BackendlessException { if( identity == null ) throw new IllegalArgumentException( ExceptionMessage.NULL_IDENTITY ); if( roleName == null || roleName.equals( "" ) ) throw new IllegalArgumentException( ExceptionMessage.NULL_ROLE_NAME ); Invoker.invokeSync( USER_MANAGER_SERVER_ALIAS, "assignRole", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), identity, roleName } ); } public void assignRole( final String identity, final String roleName, final AsyncCallback responder ) { try { if( identity == null ) throw new IllegalArgumentException( ExceptionMessage.NULL_IDENTITY ); if( roleName == null || roleName.equals( "" ) ) throw new IllegalArgumentException( ExceptionMessage.NULL_ROLE_NAME ); Invoker.invokeAsync( USER_MANAGER_SERVER_ALIAS, "assignRole", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), identity, roleName }, responder ); } catch( Throwable e ) { if( responder != null ) responder.handleFault( new BackendlessFault( e ) ); } } public void unassignRole( String identity, String roleName ) throws BackendlessException { if( identity == null ) throw new IllegalArgumentException( ExceptionMessage.NULL_IDENTITY ); if( roleName == null || roleName.equals( "" ) ) throw new IllegalArgumentException( ExceptionMessage.NULL_ROLE_NAME ); Invoker.invokeSync( USER_MANAGER_SERVER_ALIAS, "unassignRole", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), identity, roleName } ); } public void unassignRole( final String identity, final String roleName, final AsyncCallback responder ) { try { if( identity == null ) throw new IllegalArgumentException( ExceptionMessage.NULL_IDENTITY ); if( roleName == null || roleName.equals( "" ) ) throw new IllegalArgumentException( ExceptionMessage.NULL_ROLE_NAME ); Invoker.invokeAsync( USER_MANAGER_SERVER_ALIAS, "unassignRole", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), identity, roleName }, responder ); } catch( Throwable e ) { if( responder != null ) responder.handleFault( new BackendlessFault( e ) ); } } public List getUserRoles() throws BackendlessException { return Arrays.asList( (String[]) Invoker.invokeSync( USER_MANAGER_SERVER_ALIAS, "getUserRoles", new Object[] { Backendless.getApplicationId(), Backendless.getVersion() } ) ); } public void getUserRoles( final AsyncCallback> responder ) { try { AsyncCallback callback = new AsyncCallback() { @Override public void handleResponse( String[] response ) { if( responder != null ) responder.handleResponse( Arrays.asList( response ) ); } @Override public void handleFault( BackendlessFault fault ) { if( responder != null ) responder.handleFault( fault ); } }; Invoker.invokeAsync( USER_MANAGER_SERVER_ALIAS, "getUserRoles", new Object[] { Backendless.getApplicationId(), Backendless.getVersion() }, callback ); } catch( Throwable e ) { if( responder != null ) responder.handleFault( new BackendlessFault( e ) ); } } public List describeUserClass() throws BackendlessException { UserProperty[] response = Invoker.invokeSync( USER_MANAGER_SERVER_ALIAS, "describeUserClass", new Object[] { Backendless.getApplicationId(), Backendless.getVersion() } ); return Arrays.asList( response ); } public void describeUserClass( final AsyncCallback> responder ) { Invoker.invokeAsync( USER_MANAGER_SERVER_ALIAS, "describeUserClass", new Object[] { Backendless.getApplicationId(), Backendless.getVersion() }, new AsyncCallback() { @Override public void handleResponse( UserProperty[] response ) { if( responder != null ) responder.handleResponse( Arrays.asList( response ) ); } @Override public void handleFault( BackendlessFault fault ) { if( responder != null ) responder.handleFault( fault ); } } ); } public void getFacebookServiceAuthorizationUrlLink( Map facebookFieldsMappings, List permissions, AsyncCallback responder ) throws BackendlessException { if( facebookFieldsMappings == null ) facebookFieldsMappings = new HashMap(); if( permissions == null ) permissions = new ArrayList(); Invoker.invokeAsync( USER_MANAGER_SERVER_ALIAS, "getFacebookServiceAuthorizationUrlLink", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), HeadersManager.getInstance().getHeader( HeadersManager.HeadersEnum.APP_TYPE_NAME ), facebookFieldsMappings, permissions }, responder ); } public void getTwitterServiceAuthorizationUrlLink( Map twitterFieldsMapping, AsyncCallback responder ) { if( twitterFieldsMapping == null ) twitterFieldsMapping = new HashMap(); Invoker.invokeAsync( USER_MANAGER_SERVER_ALIAS, "getTwitterServiceAuthorizationUrlLink", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), HeadersManager.getInstance().getHeader( HeadersManager.HeadersEnum.APP_TYPE_NAME ), twitterFieldsMapping }, responder ); } private static void checkUserToBeProper( BackendlessUser user ) throws BackendlessException { checkUserToBeProperForUpdate( user ); if( user.getPassword() == null || user.getPassword().equals( "" ) ) throw new IllegalArgumentException( ExceptionMessage.NULL_PASSWORD ); } private static void checkUserToBeProperForUpdate( BackendlessUser user ) { if( user == null ) throw new IllegalArgumentException( ExceptionMessage.NULL_USER ); } /** * Returns user ID of the logged in user or empty string if user is not logged in. * * @return user id, if the user is logged in; else empty string */ public String loggedInUser() { return UserIdStorageFactory.instance().getStorage().get(); } /** * Sets the properties of the given user to current one. * * @param user a user from which properties should be taken */ public void setCurrentUser( BackendlessUser user ) { currentUser.setProperties( user.getProperties() ); } private void handleUserLogin( BackendlessUser invokeResult, boolean stayLoggedIn ) { String userToken = (String) invokeResult.getProperty( HeadersManager.HeadersEnum.USER_TOKEN_KEY.getHeader() ); HeadersManager.getInstance().addHeader( HeadersManager.HeadersEnum.USER_TOKEN_KEY, userToken ); final Map properties = invokeResult.getProperties(); for( String key : properties.keySet() ) if( !key.equals( HeadersManager.HeadersEnum.USER_TOKEN_KEY.getHeader() ) ) currentUser.setProperty( key, properties.get( key ) ); if( stayLoggedIn ) { UserTokenStorageFactory.instance().getStorage().set( userToken ); UserIdStorageFactory.instance().getStorage().set( Backendless.UserService.CurrentUser().getUserId() ); } } private AsyncCallback getUserLoginAsyncHandler( final AsyncCallback responder, final boolean stayLoggedIn ) { return new AsyncCallback() { @Override public void handleResponse( BackendlessUser response ) { try { handleUserLogin( response, stayLoggedIn ); } catch( Throwable e ) { if( responder != null ) responder.handleFault( new BackendlessFault( e ) ); } if( responder != null ) responder.handleResponse( currentUser ); } @Override public void handleFault( BackendlessFault fault ) { if( responder != null ) responder.handleFault( fault ); } }; } /** * if user token has been saved from previous logins, the method checks if the user token is still valid; * if user token is not saved, but user logged in, the method checks if the user session is still valid * * @return true, if user token is valid or user is logged in, else false */ public boolean isValidLogin() { String userToken = UserTokenStorageFactory.instance().getStorage().get(); if( userToken != null && !userToken.equals( "" ) ) { return Invoker.invokeSync( USER_MANAGER_SERVER_ALIAS, "isValidUserToken", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), userToken } ); } else { return CurrentUser() != null; } } /** * if user token has been saved from previous logins, the method checks if the user token is still valid; * if user token is not saved, but user logged in, the method checks if the user session is still valid */ public void isValidLogin( AsyncCallback responder ) { String userToken = UserTokenStorageFactory.instance().getStorage().get(); if( userToken != null && !userToken.equals( "" ) ) { Invoker.invokeAsync( USER_MANAGER_SERVER_ALIAS, "isValidUserToken", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), userToken }, responder ); } else { responder.handleResponse( CurrentUser() != null ); } } private boolean isLogoutFaultAllowed( String errorCode ) { return errorCode.equals( "3064" ) || errorCode.equals( "3091" ) || errorCode.equals( "3090" ) || errorCode.equals( "3023" ); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy