Commit e01168f2 authored by Nastuzzi Samy's avatar Nastuzzi Samy
Browse files

Rights fix

parent 5f80b369
......@@ -73,7 +73,8 @@ public abstract class BaseActivity extends NFCActivity {
});
}
protected void hasRights(final String titre, final String[] rightList, final Runnable runnable) {
protected void hasRights(final String title, final String[] rightList, final Runnable runnable) { hasRights(title, rightList, false, runnable);}
protected void hasRights(final String title, final String[] rightList, final boolean needToBeSuper, final Runnable runnable) {
dialog.startLoading(BaseActivity.this, getString(R.string.information_collection), getString(R.string.user_rights_list_collecting));
new Thread() {
@Override
......@@ -93,10 +94,12 @@ public abstract class BaseActivity extends NFCActivity {
}
}
if (myRightList.has(String.valueOf(nemopaySession.getFoundationId()))) {
for (JsonNode myRight : myRightList.get(String.valueOf(nemopaySession.getFoundationId()))) {
if (rights.contains(myRight.textValue()))
sameRights.add(myRight.textValue());
if (!needToBeSuper) {
for (JsonNode foundation : myRightList) {
for (JsonNode myRight : foundation) {
if (rights.contains(myRight.textValue()) && !sameRights.contains(myRight.textValue()))
sameRights.add(myRight.textValue());
}
}
}
......@@ -113,7 +116,7 @@ public abstract class BaseActivity extends NFCActivity {
@Override
public void run() {
dialog.stopLoading();
dialog.errorDialog(BaseActivity.this, titre, nemopaySession.forbidden(rightList));
dialog.errorDialog(BaseActivity.this, title, nemopaySession.forbidden(rightList, needToBeSuper));
}
});
}
......@@ -428,7 +431,7 @@ public abstract class BaseActivity extends NFCActivity {
}
protected void startCardManagementActivity(final Activity activity) {
hasRights(getString(R.string.user_rights_list_collecting), new String[]{"STAFF", "POSS3", "GESUSERS"}, new Runnable() {
hasRights(getString(R.string.user_rights_list_collecting), new String[]{"GESUSERS"}, true, new Runnable() {
@Override
public void run() {
activity.startActivity(new Intent(activity, CardManagementActivity.class));
......
......@@ -23,7 +23,7 @@ import java.util.Date;
*/
public class CardManagementActivity extends BaseActivity {
private static final String LOG_TAG = "CardManagementActivity";
private static final String LOG_TAG = "_CardManagementActivity";
TextView textUserId;
TextView textUsername;
......@@ -120,45 +120,50 @@ public class CardManagementActivity extends BaseActivity {
}.start();
}
else if (toRun == 2) {
dialog.dismiss();
final View view = getLayoutInflater().inflate(R.layout.dialog_contribute, null);
final RadioGroup radioGroup = view.findViewById(R.id.radio_type);
final RadioButton radioButton = view.findViewById(R.id.radio_student);
final EditText editText = view.findViewById(R.id.nbr_days);
radioButton.setChecked(true);
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(CardManagementActivity.this);
alertDialogBuilder
.setTitle(R.string.contribute)
.setView(view)
.setCancelable(false)
.setPositiveButton(R.string.contribute, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int id) {
dialog.startLoading(CardManagementActivity.this, getString(R.string.information_collection), getString(R.string.buyer_info_collecting));
new Thread(){
@Override
public void run() {
int id = radioGroup.indexOfChild(radioGroup.findViewById(radioGroup.getCheckedRadioButtonId()));
if (contributeCard(id == 3 ? new SimpleDateFormat("yyyy-MM-dd").format(new Date(new Date().getTime() + (Long.parseLong(String.valueOf(editText.getText())) * 86400000L))) : (Integer.parseInt(new SimpleDateFormat("MM").format(new Date())) > 8 ? Integer.toString(Integer.parseInt(new SimpleDateFormat("yyyy").format(new Date())) + 1) : new SimpleDateFormat("yyyy").format(new Date())) + "-08-31", id < 2 ? 20 : 1))
runOnUiThread(new Runnable() {
hasRights(getString(R.string.contribute), new String[]{"GESUSERS"}, true, new Runnable() {
@Override
public void run() {
dialog.dismiss();
final View view = getLayoutInflater().inflate(R.layout.dialog_contribute, null);
final RadioGroup radioGroup = view.findViewById(R.id.radio_type);
final RadioButton radioButton = view.findViewById(R.id.radio_student);
final EditText editText = view.findViewById(R.id.nbr_days);
radioButton.setChecked(true);
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(CardManagementActivity.this);
alertDialogBuilder
.setTitle(R.string.contribute)
.setView(view)
.setCancelable(false)
.setPositiveButton(R.string.contribute, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int id) {
dialog.startLoading(CardManagementActivity.this, getString(R.string.information_collection), getString(R.string.buyer_info_collecting));
new Thread(){
@Override
public void run() {
Toast.makeText(CardManagementActivity.this, R.string.contribute_now, Toast.LENGTH_LONG).show();
}
});
int id = radioGroup.indexOfChild(radioGroup.findViewById(radioGroup.getCheckedRadioButtonId()));
readCard();
}
}.start();
}
})
.setNegativeButton(R.string.cancel, null);
if (contributeCard(id == 3 ? new SimpleDateFormat("yyyy-MM-dd").format(new Date(new Date().getTime() + (Long.parseLong(String.valueOf(editText.getText())) * 86400000L))) : (Integer.parseInt(new SimpleDateFormat("MM").format(new Date())) > 8 ? Integer.toString(Integer.parseInt(new SimpleDateFormat("yyyy").format(new Date())) + 1) : new SimpleDateFormat("yyyy").format(new Date())) + "-08-31", id < 2 ? 20 : 1))
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(CardManagementActivity.this, R.string.contribute_now, Toast.LENGTH_LONG).show();
}
});
readCard();
}
}.start();
}
})
.setNegativeButton(R.string.cancel, null);
dialog.createDialog(alertDialogBuilder, editText);
dialog.createDialog(alertDialogBuilder, editText);
}
});
}
this.toRun = null;
......@@ -180,8 +185,8 @@ public class CardManagementActivity extends BaseActivity {
this.textTagId.setText(Integer.toString(tagInfo.get("id").intValue()));
this.textTag.setText(tagInfo.get("tag").textValue());
this.textShortTag.setText(tagInfo.get("short_tag").textValue() == null ? getString(R.string.none) : tagInfo.get("short_tag").textValue());
this.textSolde.setText(String.format("%.2f", new Float(this.solde) / 100.00f) + "€");
this.textCotisantGinger.setTextColor(this.solde == 0 ? Color.RED : Color.BLUE);
this.textSolde.setText(this.solde == null ? nemopaySession.forbidden(new String[]{"POSS3"}, false) : String.format("%.2f", new Float(this.solde) / 100.00f) + "€");
this.textSolde.setTextColor(this.solde == null || this.solde == 0 ? Color.RED : Color.BLUE);
this.textUsernameGinger.setText(gingerInfo.get("login").textValue());
this.textFirstnameGinger.setText(gingerInfo.get("prenom").textValue());
......@@ -205,49 +210,6 @@ public class CardManagementActivity extends BaseActivity {
}
protected Boolean contributeCard(final String fin, final Integer paid) {
try {
nemopaySession.getBuyerInfo(badgeId);
Thread.sleep(100);
// Toute une série de vérifications avant de lancer l'activité
final JsonNode buyerInfo = nemopaySession.getRequest().getJSONResponse();
if (!buyerInfo.has("lastname") || !buyerInfo.has("username") || !buyerInfo.has("firstname") || !buyerInfo.has("solde") || !buyerInfo.has("last_purchases") || !buyerInfo.get("last_purchases").isArray())
throw new Exception("Unexpected JSON");
username = nemopaySession.getRequest().getJSONResponse().get("username").textValue();
} catch (final Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage());
try {
if (nemopaySession.getRequest().getResponseCode() == 400)
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.errorDialog(CardManagementActivity.this, getString(R.string.information_collection), getString(R.string.badge_error_not_recognized));
}
});
else
throw new Exception("");
} catch (Exception e1) {
runOnUiThread(new Runnable() {
@Override
public void run() {
fatal(CardManagementActivity.this, getString(R.string.information_collection), e.getMessage());
}
});
}
return false;
}
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.changeLoading(getResources().getString(R.string.user_ginger_info_collecting));
}
});
try {
ginger.addCotisation(username, fin, paid);
Thread.sleep(100);
......@@ -297,19 +259,23 @@ public class CardManagementActivity extends BaseActivity {
if (!buyerInfo.has("lastname") || !buyerInfo.has("username") || !buyerInfo.has("firstname") || !buyerInfo.has("solde") || !buyerInfo.has("last_purchases") || !buyerInfo.get("last_purchases").isArray())
throw new Exception("Unexpected JSON");
username = nemopaySession.getRequest().getJSONResponse().get("username").textValue();
solde = buyerInfo.get("solde").intValue();
} catch (final Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage());
try {
if (nemopaySession.getRequest().getResponseCode() == 400)
if (nemopaySession.getRequest().getResponseCode() == 400) {
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.errorDialog(CardManagementActivity.this, getString(R.string.information_collection), getString(R.string.badge_error_not_recognized));
}
});
return;
}
if (nemopaySession.getRequest().getResponseCode() == 403)
solde = null;
else
throw new Exception("");
} catch (Exception e1) {
......@@ -319,26 +285,34 @@ public class CardManagementActivity extends BaseActivity {
fatal(CardManagementActivity.this, getString(R.string.information_collection), e.getMessage());
}
});
}
return;
return;
}
}
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.changeLoading(getResources().getString(R.string.user_searching));
dialog.changeLoading(getResources().getString(R.string.user_ginger_info_collecting));
}
});
try {
nemopaySession.foundUser(username);
ginger.getInfoFromBadge(badgeId);
Thread.sleep(100);
// Toute une série de vérifications avant de lancer l'activité
final JsonNode gingerInfo = new ObjectMapper().readTree(ginger.getRequest().getResponse()); // La réponse n'est pas considérée comme une réponse json.. (l'en-tête ne renvoie pas une application/json)
if (!gingerInfo.has("login") || !gingerInfo.has("nom") || !gingerInfo.has("prenom") || !gingerInfo.has("mail") || !gingerInfo.has("type") || !gingerInfo.has("is_adulte") || !gingerInfo.has("is_cotisant"))
throw new Exception("Unexpected JSON");
username = gingerInfo.get("login").textValue();
} catch (final Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage());
try {
if (nemopaySession.getRequest().getResponseCode() == 400)
if (ginger.getRequest().getResponseCode() == 404)
runOnUiThread(new Runnable() {
@Override
public void run() {
......@@ -362,24 +336,13 @@ public class CardManagementActivity extends BaseActivity {
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.changeLoading(getResources().getString(R.string.user_nemopay_info_collecting));
dialog.changeLoading(getResources().getString(R.string.information_collection));
}
});
try {
final JsonNode usersFounded = nemopaySession.getRequest().getJSONResponse();
if (!usersFounded.has(0) || !usersFounded.get(0).has("id"))
throw new Exception(getString(R.string.user_not_recognized));
nemopaySession.getUser(usersFounded.get(0).get("id").intValue());
nemopaySession.foundUser(username);
Thread.sleep(100);
// Toute une série de vérifications avant de lancer l'activité
final JsonNode userInfo = nemopaySession.getRequest().getJSONResponse();
if (!userInfo.has("user") || !userInfo.has("tag"))
throw new Exception("Unexpected JSON");
} catch (final Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage());
......@@ -388,7 +351,7 @@ public class CardManagementActivity extends BaseActivity {
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.errorDialog(CardManagementActivity.this, getString(R.string.information_collection), getString(R.string.user_error_collecting));
dialog.errorDialog(CardManagementActivity.this, getString(R.string.information_collection), getString(R.string.user_not_recognized));
}
});
else
......@@ -408,19 +371,24 @@ public class CardManagementActivity extends BaseActivity {
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.changeLoading(getResources().getString(R.string.user_ginger_info_collecting));
dialog.changeLoading(getResources().getString(R.string.user_nemopay_info_collecting));
}
});
try {
ginger.getInfoFromBadge(badgeId);
final JsonNode usersFounded = nemopaySession.getRequest().getJSONResponse();
final JsonNode gingerInfo = new ObjectMapper().readTree(ginger.getRequest().getResponse()); // La réponse n'est pas considérée comme une réponse json.. (l'en-tête ne renvoie pas une application/json)
if (!usersFounded.has(0) || !usersFounded.get(0).has("id"))
throw new Exception(getString(R.string.user_not_recognized));
nemopaySession.getUser(usersFounded.get(0).get("id").intValue());
Thread.sleep(100);
// Toute une série de vérifications avant de lancer l'activité
final JsonNode userInfo = nemopaySession.getRequest().getJSONResponse();
final JsonNode gingerInfo = new ObjectMapper().readTree(ginger.getRequest().getResponse()); // La réponse n'est pas considérée comme une réponse json.. (l'en-tête ne renvoie pas une application/json)
if (!gingerInfo.has("login") || !gingerInfo.has("nom") || !gingerInfo.has("prenom") || !gingerInfo.has("mail") || !gingerInfo.has("type") || !gingerInfo.has("is_adulte") || !gingerInfo.has("is_cotisant"))
if (!userInfo.has("user") || !userInfo.has("tag"))
throw new Exception("Unexpected JSON");
runOnUiThread(new Runnable() {
......@@ -434,11 +402,11 @@ public class CardManagementActivity extends BaseActivity {
Log.e(LOG_TAG, "error: " + e.getMessage());
try {
if (ginger.getRequest().getResponseCode() == 404)
if (nemopaySession.getRequest().getResponseCode() == 400)
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.errorDialog(CardManagementActivity.this, getString(R.string.information_collection), getString(R.string.user_not_recognized));
dialog.errorDialog(CardManagementActivity.this, getString(R.string.information_collection), getString(R.string.user_error_collecting));
}
});
else
......
......@@ -109,7 +109,7 @@ public class FoundationsOptionsActivity extends BaseActivity {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
if (isOption(position,0))
dialog.infoDialog(FoundationsOptionsActivity.this, "Non encore fait", "Pour la version 0.8");
dialog.infoDialog(FoundationsOptionsActivity.this, "Non encore fait", "Pour la version 0.9");
else if (isOption(position,1))
dialog.infoDialog(FoundationsOptionsActivity.this, "Non encore fait", "Pour la version 0.9");
else if (isOption(position,2))
......
......@@ -33,6 +33,8 @@ public class NemopaySession {
private String notLogged;
private String noRight;
private String noRights;
private String noSuperRight;
private String noSuperRights;
private String allRightsNeeded;
private String serviceText;
private String notFound;
......@@ -63,6 +65,8 @@ public class NemopaySession {
this.allRightsNeeded = activity.getString(R.string.all_rights_needed);
this.noRight = activity.getString(R.string.no_right);
this.noRights = activity.getString(R.string.no_rights);
this.noSuperRight = activity.getString(R.string.no_super_right);
this.noSuperRights = activity.getString(R.string.no_super_rights);
this.serviceText = activity.getString(R.string.service);
this.notFound = activity.getString(R.string.not_found);
this.badRequest = activity.getString(R.string.bad_request);
......@@ -218,7 +222,7 @@ public class NemopaySession {
put("badge_id", badgeId);
}},
new String[]{
"POSS3"
"POSS3"
}
);
}
......@@ -445,14 +449,14 @@ public class NemopaySession {
return reponseCode;
}
public String forbidden(final String[] rightsNeeded) {
public String forbidden(final String[] rightsNeeded, final boolean needToBeSuper) {
String result;
if (rightsNeeded.length == 0)
return this.allRightsNeeded;
else if (rightsNeeded.length == 1)
result = this.noRight;
result = needToBeSuper ? this.noSuperRight : this.noRight;
else
result = this.noRights;
result = needToBeSuper ? this.noSuperRights : this.noRights;
for (String right : rightsNeeded) {
if (allRights.containsKey(right))
......@@ -486,7 +490,7 @@ public class NemopaySession {
throw new Exception(this.notLogged);
}
throw new Exception(forbidden(rightsNeeded));
throw new Exception(forbidden(rightsNeeded, false));
}
else if (responseCode == 404)
throw new Exception(this.serviceText + " " + service + " " + this.notFound);
......
......@@ -4,6 +4,8 @@
<string name="ginger_no_rights">La clé donnée n\'a pas les droits</string>
<string name="no_right">Il est nécessaire d\'avoir au moins le droit suivant:</string>
<string name="no_rights">Il est nécessaire d\'avoir au moins les droits suivants:</string>
<string name="no_super_right">Il est nécessaire d\'avoir au moins le droit admin suivant:</string>
<string name="no_super_rights">Il est nécessaire d\'avoir au moins les droits admin suivants:</string>
<string name="all_rights_needed">Il est nécessaire d\'avoir tous les droits</string>
<string name="error_rights">Impossible de récupérer les droits de la personne</string>
<string name="not_found">n\'a pas réussi à trouver l\'élement souhaité</string>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment