setup register

This commit is contained in:
GME 2018-11-26 10:17:28 +01:00
parent 8d99c00bf4
commit 63e6a5b7dd
5 changed files with 115 additions and 33 deletions

View file

@ -1,6 +1,7 @@
package monnethic.mobile.homepage; package monnethic.mobile.homepage;
import android.content.Intent; import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; import android.os.Bundle;
import android.view.View; import android.view.View;
@ -8,6 +9,8 @@ import android.widget.Button;
import com.example.monnthic.monnethicmobile.R; import com.example.monnthic.monnethicmobile.R;
import monnethic.mobile.database.User;
public class HomepageActivity extends AppCompatActivity { public class HomepageActivity extends AppCompatActivity {
@Override @Override
@ -35,6 +38,7 @@ public class HomepageActivity extends AppCompatActivity {
Intent registerIntent = new Intent(HomepageActivity.this, RegisterActivity.class); Intent registerIntent = new Intent(HomepageActivity.this, RegisterActivity.class);
HomepageActivity.this.startActivity(registerIntent); HomepageActivity.this.startActivity(registerIntent);
} }
public void launchLoginActivity(){ public void launchLoginActivity(){
Intent loginIntent = new Intent(HomepageActivity.this, LoginActivity.class); Intent loginIntent = new Intent(HomepageActivity.this, LoginActivity.class);
HomepageActivity.this.startActivity(loginIntent); HomepageActivity.this.startActivity(loginIntent);

View file

@ -101,7 +101,7 @@ public class LoginActivity extends AppCompatActivity {
String url = Config.USER_LOGIN; String url = Config.USER_LOGIN;
String[] paramsList = {params[0],params[1]}; String[] paramsList = {params[0],params[1]};
HttpCallHandler httpCallHandler = new HttpCallHandler(); HttpCallHandler httpCallHandler = new HttpCallHandler();
return new JSONObject(httpCallHandler.executePostHttp(url,paramsList)); return new JSONObject(httpCallHandler.executeLoginHttp(url,paramsList));
}catch (Exception e){ }catch (Exception e){
e.printStackTrace(); e.printStackTrace();
return null; return null;

View file

@ -1,6 +1,8 @@
package monnethic.mobile.homepage; package monnethic.mobile.homepage;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; import android.os.Bundle;
import android.view.View; import android.view.View;
@ -10,7 +12,11 @@ import android.widget.Toast;
import com.example.monnthic.monnethicmobile.R; import com.example.monnthic.monnethicmobile.R;
import org.json.JSONObject;
import monnethic.mobile.database.User; import monnethic.mobile.database.User;
import monnethic.mobile.restApi.Config;
import monnethic.mobile.restApi.HttpCallHandler;
import monnethic.mobile.user.UserAccountActivity; import monnethic.mobile.user.UserAccountActivity;
public class RegisterActivity extends AppCompatActivity { public class RegisterActivity extends AppCompatActivity {
@ -41,7 +47,6 @@ public class RegisterActivity extends AppCompatActivity {
finish(); finish();
} }
}); });
buttonOk.setOnClickListener(new View.OnClickListener() { buttonOk.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
@ -50,46 +55,17 @@ public class RegisterActivity extends AppCompatActivity {
}); });
} }
//TODO VERIFY EACH EDIT TEXT
public void validateInput(){ public void validateInput(){
if(checkInputEmpty()){ if(checkInputEmpty()){
if(!InputController.passwordValidator(password.getText().toString())){ if(!InputController.passwordValidator(password.getText().toString())){
Toast.makeText(this, "Password must contains 6 to 20 characters, one lowercase, one uppercase and one digit", Toast.LENGTH_LONG).show(); Toast.makeText(this, "Password must contains 6 to 20 characters, one lowercase, one uppercase and one digit", Toast.LENGTH_LONG).show();
}else if(InputController.validEmail(email.getText().toString())){ }else if(InputController.validEmail(email.getText().toString())){
User inputUser = new User(name.getText().toString(),firstname.getText().toString(),email.getText().toString(),password.getText().toString()); User inputUser = new User(name.getText().toString(),firstname.getText().toString(),email.getText().toString(),password.getText().toString());
insertUserLdap(inputUser); new UserRegisterTask(this).execute(inputUser);
} }
} }
} }
//TODO INSERT VALIDE USER INTO LDAP
private void insertUserLdap(User u){
//CHECK IF USER ALREADY EXIST
//IF USER DOESN'T EXIST, INSERT USER INTO LDAP
//CHECK RESPONSE OR RESULT OF INSERT
//IF SUCCESSFULLY INSERTED INTO LDAP CALL LAUNCHWALLET
//TEMPORARY
if(InputController.checkUser(u.getEmail())){
Toast.makeText(this, "User already have an account, please log in", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(this, "INSERT", Toast.LENGTH_SHORT).show();
launchUserActivity(1);
}
}
//TODO LAUNCH WALLET ACTIVITY
private void launchUserActivity(int ldapId){
//LAUNCH APP WALLET WITH ID USER
Intent walletIntent = new Intent(RegisterActivity.this, UserAccountActivity.class);
walletIntent.putExtra("idUser", ldapId);
RegisterActivity.this.startActivity(walletIntent);
finish();
}
private boolean checkInputEmpty(){ private boolean checkInputEmpty(){
if(InputController.isEmptyEdit(name)){ if(InputController.isEmptyEdit(name)){
Toast.makeText(this, "You did not enter your name", Toast.LENGTH_SHORT).show(); Toast.makeText(this, "You did not enter your name", Toast.LENGTH_SHORT).show();
@ -127,4 +103,41 @@ public class RegisterActivity extends AppCompatActivity {
} }
} }
} }
private class UserRegisterTask extends AsyncTask<User,Void,JSONObject> {
Context mContext;
private UserRegisterTask(final Context context){mContext=context;}
@Override
protected void onPostExecute(JSONObject result){
try{
if(result!=null){
if(result.getInt("status") == 200){
Intent accountIntent = new Intent(RegisterActivity.this, UserAccountActivity.class);
accountIntent.putExtra("userHash", result.getString("userHash"));
RegisterActivity.this.startActivity(accountIntent);
finish();
}else{
Toast.makeText(mContext, result.getString("response"), Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(mContext, "AN ERROR OCCURED", Toast.LENGTH_SHORT).show();
}
}catch (Exception e){
e.printStackTrace();
}
}
@Override
protected JSONObject doInBackground(User... users) {
try{
String url = Config.USER_REGISTER;
HttpCallHandler httpCallHandler = new HttpCallHandler();
return new JSONObject(httpCallHandler.executeRegisterHttp(url,users[0]));
}catch (Exception e){
e.printStackTrace();
return null;
}
}
}
} }

View file

@ -10,6 +10,8 @@ import java.io.InputStreamReader;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import monnethic.mobile.database.User;
public class HttpCallHandler { public class HttpCallHandler {
public String executeGetHttp(String urlParam, String[] params){ public String executeGetHttp(String urlParam, String[] params){
@ -59,7 +61,7 @@ public class HttpCallHandler {
} }
} }
public String executePostHttp(String urlParam, String[] params){ public String executeLoginHttp(String urlParam, String[] params){
BufferedReader bufferedReader = null; BufferedReader bufferedReader = null;
HttpURLConnection connection = null; HttpURLConnection connection = null;
String res = null; String res = null;
@ -120,4 +122,67 @@ public class HttpCallHandler {
} }
} }
} }
public String executeRegisterHttp(String urlParam, User user){
BufferedReader bufferedReader = null;
HttpURLConnection connection = null;
String res = null;
try{
URL url = new URL(urlParam);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
connection.setRequestProperty("Accept","application/json");
connection.setDoOutput(true);
connection.setDoInput(true);
JSONObject jsonParam = new JSONObject();
jsonParam.put("name",user.getName());
jsonParam.put("firstname",user.getFirstname());
jsonParam.put("email",user.getEmail());
jsonParam.put("password",user.getPassword());
DataOutputStream os = new DataOutputStream(connection.getOutputStream());
os.writeBytes(jsonParam.toString());
int statusCode = connection.getResponseCode();
if(statusCode!=200){
JSONObject jsonReturn = new JSONObject();
System.out.println("Response code is : "+statusCode);
if(statusCode == 302){
jsonReturn.put("status",302);
jsonReturn.put("response","User Already Exist !");
return jsonReturn.toString();
}else {
return null;
}
}else {
bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = bufferedReader.readLine())!=null){
stringBuilder.append(line);
}
JSONObject jsonSuccess = new JSONObject(stringBuilder.toString());
jsonSuccess.put("status",200);
res = jsonSuccess.toString();
os.flush();
os.close();
}
return res;
}catch (Exception e){
e.printStackTrace();
return null;
} finally {
if(connection != null){
connection.disconnect();
}
if(bufferedReader != null){
try {
bufferedReader.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
}
} }