From bda0317b9a568a6f163dfe3dcac4ddd9f85e5b18 Mon Sep 17 00:00:00 2001 From: BenoƮt Verhaeghe Date: Sun, 5 Feb 2017 16:45:24 +0100 Subject: [PATCH] LOGIN OK --- app/src/main/java/net/plil/clubinfo/etunicorn/app/LoginActivity.java | 38 ++++++++++++++++---------------------- app/src/main/java/net/plil/clubinfo/etunicorn/app/MainActivity.java | 20 ++++++-------------- app/src/main/java/net/plil/clubinfo/etunicorn/app/credit/Crediter.java | 2 +- app/src/main/java/net/plil/clubinfo/etunicorn/app/debit/Debiter.java | 2 +- app/src/main/java/net/plil/clubinfo/etunicorn/app/personne/CreatePersonne.java | 4 +--- app/src/main/java/net/plil/clubinfo/etunicorn/app/personne/DeletePersonne.java | 2 +- app/src/main/java/net/plil/clubinfo/etunicorn/app/personne/ModifyPersonne.java | 8 +++----- app/src/main/java/net/plil/clubinfo/etunicorn/app/personne/MyPersonneRecyclerViewAdapter.java | 6 +++--- app/src/main/java/net/plil/clubinfo/etunicorn/app/personne/PersonOverviewFragment.java | 6 +++--- app/src/main/java/net/plil/clubinfo/etunicorn/app/personne/PersonneFragment.java | 8 +++----- app/src/main/java/net/plil/clubinfo/etunicorn/data/Personne.java | 42 ++++++++++++++++++++---------------------- app/src/main/java/net/plil/clubinfo/etunicorn/data/Role.java | 12 ++++++------ app/src/main/java/net/plil/clubinfo/etunicorn/data/Session.java | 45 +++++++++++++++++++++++++++++++++++++++++++++ app/src/main/java/net/plil/clubinfo/etunicorn/data/User.java | 10 +++++----- app/src/main/java/net/plil/clubinfo/etunicorn/utils/ConvertBytesToString.java | 10 ++++++++++ app/src/main/java/net/plil/clubinfo/etunicorn/utils/JsonCoverter.java | 21 +++++++++++++++++++++ app/src/main/res/values-fr/strings.xml | 6 +++--- 17 files changed, 148 insertions(+), 94 deletions(-) create mode 100644 app/src/main/java/net/plil/clubinfo/etunicorn/data/Session.java create mode 100644 app/src/main/java/net/plil/clubinfo/etunicorn/utils/JsonCoverter.java diff --git a/app/src/main/java/net/plil/clubinfo/etunicorn/app/LoginActivity.java b/app/src/main/java/net/plil/clubinfo/etunicorn/app/LoginActivity.java index 83d8eb4..1982914 100644 --- a/app/src/main/java/net/plil/clubinfo/etunicorn/app/LoginActivity.java +++ b/app/src/main/java/net/plil/clubinfo/etunicorn/app/LoginActivity.java @@ -37,17 +37,27 @@ import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.JsonObjectRequest; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonParseException; import net.plil.clubinfo.etunicorn.R; import net.plil.clubinfo.etunicorn.data.Permission; import net.plil.clubinfo.etunicorn.data.Personne; +import net.plil.clubinfo.etunicorn.data.Session; import net.plil.clubinfo.etunicorn.data.User; +import net.plil.clubinfo.etunicorn.utils.ConvertBytesToString; +import net.plil.clubinfo.etunicorn.utils.JsonCoverter; import net.plil.clubinfo.etunicorn.utils.VolleyUtils; import org.json.JSONException; import org.json.JSONObject; +import java.lang.reflect.Type; import java.util.ArrayList; +import java.util.Date; import java.util.List; import static android.Manifest.permission.READ_CONTACTS; @@ -169,46 +179,30 @@ public class LoginActivity extends AppCompatActivity { private void tryLogin (String username, String password){ User user = new User(); user.setPassword(password); - user.setUsername(username); + user.setLogin(username); JsonObjectRequest jsonObjectRequest = null; try { - jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, VolleyUtils.baseUri + "/login" , new JSONObject(new Gson().toJson(user)), new Response.Listener() { + + jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, VolleyUtils.baseUri + "/login" , new JSONObject(JsonCoverter.getConverter().toJson(user)), new Response.Listener() { @Override public void onResponse(JSONObject response) { showProgress(false); - - Toast.makeText(getApplicationContext(), response.toString(), Toast.LENGTH_LONG).show(); Intent intent = new Intent(getApplicationContext(), MainActivity.class); - //TODO real interpretation when login work - //Personne p = new Gson().fromJson(String.valueOf(response), Personne.class); - Personne p = new Personne(); - Permission permission = new Permission(); - permission.setNom("ROLE_ADMIN"); - p.getRole().getPermissions().add(permission); - intent.putExtra("user", p); + Session s = JsonCoverter.getConverter().fromJson(String.valueOf(response), Session.class); + intent.putExtra("session", s); startActivity(intent); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { showProgress(false); - Toast.makeText(getApplicationContext(), "error : " + error.getCause(), Toast.LENGTH_SHORT).show(); - Intent intent = new Intent(getApplicationContext(), MainActivity.class); - - Personne p = new Personne(); - Permission permission = new Permission(); - permission.setNom("ROLE_ADMIN"); - p.getRole().getPermissions().add(permission); - intent.putExtra("user", p); - - startActivity(intent); + Toast.makeText(getApplicationContext(), "error : " + ConvertBytesToString.bytesToStringVolLey(error.networkResponse.data), Toast.LENGTH_SHORT).show(); } } ); } catch (JSONException e) { e.printStackTrace(); } - VolleyUtils.getInstance(getApplicationContext()).addToRequestQueue(jsonObjectRequest); } } diff --git a/app/src/main/java/net/plil/clubinfo/etunicorn/app/MainActivity.java b/app/src/main/java/net/plil/clubinfo/etunicorn/app/MainActivity.java index ac553b0..199210f 100644 --- a/app/src/main/java/net/plil/clubinfo/etunicorn/app/MainActivity.java +++ b/app/src/main/java/net/plil/clubinfo/etunicorn/app/MainActivity.java @@ -11,13 +11,11 @@ import android.support.design.widget.TabLayout; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; -import android.support.v4.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; -import android.widget.Toast; import net.plil.clubinfo.etunicorn.R; import net.plil.clubinfo.etunicorn.app.credit.Crediter; @@ -31,11 +29,8 @@ import net.plil.clubinfo.etunicorn.app.personne.PersonneFragment; import net.plil.clubinfo.etunicorn.data.Consommation; import net.plil.clubinfo.etunicorn.data.Event; import net.plil.clubinfo.etunicorn.data.Personne; +import net.plil.clubinfo.etunicorn.data.Session; import net.plil.clubinfo.etunicorn.utils.ConvertBytesToString; -import net.plil.clubinfo.etunicorn.utils.VolleyUtils; - -import java.util.ArrayList; -import java.util.List; public class MainActivity extends AppCompatActivity @@ -64,7 +59,7 @@ public class MainActivity extends AppCompatActivity private Toolbar toolbar; private AppBarLayout appBarLayout; - private Personne p; + private Session s; NfcAdapter mAdapter; PendingIntent mPendingIntent; @@ -73,22 +68,19 @@ public class MainActivity extends AppCompatActivity protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - // config tabbed pan Intent intent = this.getIntent(); - p = (Personne) intent.getSerializableExtra("user"); - //TODO remove this line - Toast.makeText(getApplicationContext(), p.getRole().getPermissions().get(0).getNom(), Toast.LENGTH_SHORT).show(); - if (p.getRole().getPermissions().get(0).getNom().equals("ROLE_ADMIN")){ + s = (Session) intent.getSerializableExtra("session"); + if (s.getPersonne().getRole().getNom().equals("admin")){ nbPages = 5; Action.CONSOMATION.setValue(2); Action.EVENT.setValue(3); Action.PERSONNE.setValue(4); - } else if (p.getRole().getPermissions().get(0).getNom().equals("ROLE_BDE")){ + } else if (s.getPersonne().getRole().getNom().equals("bde")){ nbPages = 4; Action.CONSOMATION.setValue(-1); Action.EVENT.setValue(2); Action.PERSONNE.setValue(3); - } else if (p.getRole().getPermissions().get(0).getNom().equals("ROLE_BAR")){ + } else if (s.getPersonne().getRole().getNom().equals("bar")){ nbPages = 3; Action.CONSOMATION.setValue(2); Action.EVENT.setValue(-1); diff --git a/app/src/main/java/net/plil/clubinfo/etunicorn/app/credit/Crediter.java b/app/src/main/java/net/plil/clubinfo/etunicorn/app/credit/Crediter.java index 9a84801..6a0d071 100644 --- a/app/src/main/java/net/plil/clubinfo/etunicorn/app/credit/Crediter.java +++ b/app/src/main/java/net/plil/clubinfo/etunicorn/app/credit/Crediter.java @@ -73,7 +73,7 @@ public class Crediter extends FragmentNFC { JSONObject jsonObject = new JSONObject(); try { Personne p = new Personne(); - p.setIdCarte(idCardUser); + p.setCarte(idCardUser); jsonObject.put("participant", p); jsonObject.put("prix", Double.parseDouble(mMoneyEditText.getText().toString())); } catch (JSONException e){ diff --git a/app/src/main/java/net/plil/clubinfo/etunicorn/app/debit/Debiter.java b/app/src/main/java/net/plil/clubinfo/etunicorn/app/debit/Debiter.java index b86fb80..92e28f1 100644 --- a/app/src/main/java/net/plil/clubinfo/etunicorn/app/debit/Debiter.java +++ b/app/src/main/java/net/plil/clubinfo/etunicorn/app/debit/Debiter.java @@ -78,7 +78,7 @@ public class Debiter extends FragmentNFC { //TODO verifier champ complet avant de construire la requete try { Personne p = new Personne(); - p.setIdCarte(iDCardUser); + p.setCarte(iDCardUser); jsonObject.put("participant", p); jsonObject.put("prix", -Double.parseDouble(mMoneyEditText.getText().toString())); mMoneyEditText.setError(null); diff --git a/app/src/main/java/net/plil/clubinfo/etunicorn/app/personne/CreatePersonne.java b/app/src/main/java/net/plil/clubinfo/etunicorn/app/personne/CreatePersonne.java index 0377c09..8689590 100644 --- a/app/src/main/java/net/plil/clubinfo/etunicorn/app/personne/CreatePersonne.java +++ b/app/src/main/java/net/plil/clubinfo/etunicorn/app/personne/CreatePersonne.java @@ -23,7 +23,6 @@ import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.JsonObjectRequest; import com.google.gson.Gson; -import com.google.gson.JsonArray; import net.plil.clubinfo.etunicorn.R; import net.plil.clubinfo.etunicorn.data.Role; @@ -32,7 +31,6 @@ import net.plil.clubinfo.etunicorn.utils.VolleyUtils; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; -import org.w3c.dom.Text; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -76,7 +74,7 @@ public class CreatePersonne extends DialogFragment { mRole = (Spinner) view.findViewById(R.id.create_personne_role); arraySpinnerRole = new ArrayList<>(); Role r = new Role(); - r.setName("Role"); + r.setNom("Role"); arraySpinnerRole.add(r); adapterRole = new ArrayAdapter<>(getActivity(), R.layout.simple_item_layout, arraySpinnerRole); diff --git a/app/src/main/java/net/plil/clubinfo/etunicorn/app/personne/DeletePersonne.java b/app/src/main/java/net/plil/clubinfo/etunicorn/app/personne/DeletePersonne.java index b72ebec..4fce793 100644 --- a/app/src/main/java/net/plil/clubinfo/etunicorn/app/personne/DeletePersonne.java +++ b/app/src/main/java/net/plil/clubinfo/etunicorn/app/personne/DeletePersonne.java @@ -75,7 +75,7 @@ public class DeletePersonne extends DialogFragment { public void onClick(View v) { mProgressBar.setVisibility(View.VISIBLE); - JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.DELETE, VolleyUtils.baseUri + "/personne/" + personne.getIdPersonne(), null, new Response.Listener() { + JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.DELETE, VolleyUtils.baseUri + "/personne/" + personne.getId(), null, new Response.Listener() { @Override public void onResponse(JSONObject response) { mProgressBar.setVisibility(View.GONE); diff --git a/app/src/main/java/net/plil/clubinfo/etunicorn/app/personne/ModifyPersonne.java b/app/src/main/java/net/plil/clubinfo/etunicorn/app/personne/ModifyPersonne.java index f07b227..da4fc8a 100644 --- a/app/src/main/java/net/plil/clubinfo/etunicorn/app/personne/ModifyPersonne.java +++ b/app/src/main/java/net/plil/clubinfo/etunicorn/app/personne/ModifyPersonne.java @@ -25,8 +25,6 @@ import com.android.volley.toolbox.JsonObjectRequest; import com.google.gson.Gson; import net.plil.clubinfo.etunicorn.R; -import net.plil.clubinfo.etunicorn.app.event.ModifyEvent; -import net.plil.clubinfo.etunicorn.data.Event; import net.plil.clubinfo.etunicorn.data.Personne; import net.plil.clubinfo.etunicorn.data.Role; import net.plil.clubinfo.etunicorn.utils.VolleyUtils; @@ -90,8 +88,8 @@ public class ModifyPersonne extends DialogFragment { final Personne personne = (Personne) getArguments().getSerializable("personne"); - mCarte.setText(personne.getIdCarte()); - mLogin.setText(personne.getLoginPoly()); + mCarte.setText(personne.getCarte()); + mLogin.setText(personne.getLogin()); arraySpinnerRole = new ArrayList<>(); arraySpinnerRole.add(personne.getRole()); @@ -148,7 +146,7 @@ public class ModifyPersonne extends DialogFragment { } catch (JSONException e){ e.printStackTrace(); } - JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.PUT, VolleyUtils.baseUri + "/personne/" + personne.getIdPersonne() ,jsonObject , new Response.Listener() { + JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.PUT, VolleyUtils.baseUri + "/personne/" + personne.getId() ,jsonObject , new Response.Listener() { @Override public void onResponse(JSONObject response) { mProgressBar.setVisibility(View.GONE); diff --git a/app/src/main/java/net/plil/clubinfo/etunicorn/app/personne/MyPersonneRecyclerViewAdapter.java b/app/src/main/java/net/plil/clubinfo/etunicorn/app/personne/MyPersonneRecyclerViewAdapter.java index 637a469..8821869 100644 --- a/app/src/main/java/net/plil/clubinfo/etunicorn/app/personne/MyPersonneRecyclerViewAdapter.java +++ b/app/src/main/java/net/plil/clubinfo/etunicorn/app/personne/MyPersonneRecyclerViewAdapter.java @@ -39,9 +39,9 @@ public class MyPersonneRecyclerViewAdapter extends RecyclerView.Adapter personneList = new ArrayList<>(); Role r = new Role(); - r.setName("ADMIN_TEST"); + r.setNom("ADMIN_TEST"); Permission p = new Permission(); p.setNom("Lire"); r.getPermissions().add(p); @@ -68,8 +66,8 @@ public class PersonneFragment extends FragmentNFC { r.getPermissions().add(p); for (int i = 0;i<150;++i){ Personne personne = new Personne(); - personne.setIdCarte("15245698547856"); - personne.setLoginPoly("bverhaeg"); + personne.setCarte("15245698547856"); + personne.setLogin("bverhaeg"); personne.setNaissance(new Date(System.currentTimeMillis())); personne.setSolde(45); personne.setRole(r); diff --git a/app/src/main/java/net/plil/clubinfo/etunicorn/data/Personne.java b/app/src/main/java/net/plil/clubinfo/etunicorn/data/Personne.java index c4ff69b..8edd43a 100644 --- a/app/src/main/java/net/plil/clubinfo/etunicorn/data/Personne.java +++ b/app/src/main/java/net/plil/clubinfo/etunicorn/data/Personne.java @@ -3,42 +3,40 @@ package net.plil.clubinfo.etunicorn.data; import java.io.Serializable; import java.sql.Date; -import java.util.ArrayList; -import java.util.List; public class Personne implements Serializable{ - private int idPersonne; - private String idCarte; + private int id; + private String carte; private Date naissance; private double solde = 0; - private String loginPoly; - private Role role = new Role(); + private String login; + private Role role; public Personne() { } - public Role getRole() { - return role; + public int getId() { + return id; } - public void setRole(Role role) { - this.role = role; + public void setId(int id) { + this.id = id; } - public int getIdPersonne() { - return idPersonne; + public Role getRole() { + return role; } - public void setIdPersonne(int idPersonne) { - this.idPersonne = idPersonne; + public void setRole(Role role) { + this.role = role; } - public String getIdCarte() { - return idCarte; + public String getCarte() { + return carte; } - public void setIdCarte(String idCarte) { - this.idCarte = idCarte; + public void setCarte(String carte) { + this.carte = carte; } public Date getNaissance() { @@ -57,11 +55,11 @@ public class Personne implements Serializable{ this.solde = solde; } - public String getLoginPoly() { - return loginPoly; + public String getLogin() { + return login; } - public void setLoginPoly(String loginPoly) { - this.loginPoly = loginPoly; + public void setLogin(String login) { + this.login = login; } } diff --git a/app/src/main/java/net/plil/clubinfo/etunicorn/data/Role.java b/app/src/main/java/net/plil/clubinfo/etunicorn/data/Role.java index 5ec3c57..2d25ec1 100644 --- a/app/src/main/java/net/plil/clubinfo/etunicorn/data/Role.java +++ b/app/src/main/java/net/plil/clubinfo/etunicorn/data/Role.java @@ -9,17 +9,17 @@ import java.util.List; */ public class Role implements Serializable{ private List permissions = new ArrayList<>(); - private String name; + private String nom; public Role() { } - public String getName() { - return name; + public String getNom() { + return nom; } - public void setName(String name) { - this.name = name; + public void setNom(String nom) { + this.nom = nom; } public List getPermissions() { @@ -32,6 +32,6 @@ public class Role implements Serializable{ @Override public String toString(){ - return getName(); + return getNom(); } } diff --git a/app/src/main/java/net/plil/clubinfo/etunicorn/data/Session.java b/app/src/main/java/net/plil/clubinfo/etunicorn/data/Session.java new file mode 100644 index 0000000..5bfa4d3 --- /dev/null +++ b/app/src/main/java/net/plil/clubinfo/etunicorn/data/Session.java @@ -0,0 +1,45 @@ +package net.plil.clubinfo.etunicorn.data; + +import java.io.Serializable; +import java.util.Date; + +/** + * Created by badetitou on 05/02/2017. + */ + +public class Session implements Serializable { + + private static final int SESSION_DURATION = 10 * 60; + private Personne personne; + private String token; + private Date validity; + + + public Session() { + } + + public Personne getPersonne() { + return personne; + } + + public void setPersonne(Personne personne) { + this.personne = personne; + } + + public String getToken() { + return token; + } + + public void setToken(String token) { + this.token = token; + } + + public Date getValidity() { + return validity; + } + + public void setValidity(Date validity) { + this.validity = validity; + } + +} diff --git a/app/src/main/java/net/plil/clubinfo/etunicorn/data/User.java b/app/src/main/java/net/plil/clubinfo/etunicorn/data/User.java index 1e76595..5be8d82 100644 --- a/app/src/main/java/net/plil/clubinfo/etunicorn/data/User.java +++ b/app/src/main/java/net/plil/clubinfo/etunicorn/data/User.java @@ -5,18 +5,18 @@ package net.plil.clubinfo.etunicorn.data; */ public class User { - private String username; + private String login; private String password; public User() { } - public String getUsername() { - return username; + public String getLogin() { + return login; } - public void setUsername(String username) { - this.username = username; + public void setLogin(String login) { + this.login = login; } public String getPassword() { diff --git a/app/src/main/java/net/plil/clubinfo/etunicorn/utils/ConvertBytesToString.java b/app/src/main/java/net/plil/clubinfo/etunicorn/utils/ConvertBytesToString.java index 24b9a25..f8fe042 100644 --- a/app/src/main/java/net/plil/clubinfo/etunicorn/utils/ConvertBytesToString.java +++ b/app/src/main/java/net/plil/clubinfo/etunicorn/utils/ConvertBytesToString.java @@ -1,5 +1,7 @@ package net.plil.clubinfo.etunicorn.utils; +import java.io.UnsupportedEncodingException; + /** * Created by badetitou on 04/02/2017. */ @@ -23,4 +25,12 @@ public class ConvertBytesToString { return stringBuilder.toString(); } + public static String bytesToStringVolLey(byte[] bytes){ + try { + return new String(bytes, "UTF-8"); + } catch (UnsupportedEncodingException e) { + return "Can't read error"; + } + } + } diff --git a/app/src/main/java/net/plil/clubinfo/etunicorn/utils/JsonCoverter.java b/app/src/main/java/net/plil/clubinfo/etunicorn/utils/JsonCoverter.java new file mode 100644 index 0000000..343ed1b --- /dev/null +++ b/app/src/main/java/net/plil/clubinfo/etunicorn/utils/JsonCoverter.java @@ -0,0 +1,21 @@ +package net.plil.clubinfo.etunicorn.utils; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +/** + * Created by badetitou on 05/02/2017. + */ + +public class JsonCoverter { + + private static Gson gson; + + public static Gson getConverter(){ + if (gson != null) + return gson; + + gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create(); + return gson; + } +} diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index 85826f9..370d286 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -41,13 +41,13 @@ Date de naissance Role Veuillez entrer une valeur avant d\'utiliser la carte NFC - Entrer le nom + Entrer le name Entrer le prix Date Entrer la date - Entrer le nom + Entrer le name Entrer le prix - Entrer le nouveau nom + Entrer le nouveau name Entrer le nouveau prix Nom Prix -- libgit2 0.21.2