Ho un problema come faccio a fare in modo che l'utetente non accede da amministratore?
Grazie in anticipo
Codice:
ListUtente.c
#include <stdlib.h>
#include "ListUtente.h"
#include <string.h>
#include <stdio.h>
ListUtente initListUtente(char name[50], char surname[50], char email[100],char password[7], bool isAdmin)
{
ListUtente L = (ListUtente)malloc(sizeof(struct Utente));
strcpy(L->name, name);
strcpy(L->surname, surname);
strcpy(L->email, email);
strcpy(L->password, password);
L->isAdmin =isAdmin;
L->next = NULL;
return L;
}
bool user_exist(ListUtente L, char name[50], char surname[50], char email[100], char password[7], bool isAdmin){
struct Utente *tmp;
tmp = L;
if (tmp != NULL)
{
if (strcmp(tmp->name,name)==0 && strcmp(tmp->surname,surname)==0 && strcmp(tmp->email,email)==0 && strcmp(tmp->password, password)==0 && tmp->isAdmin==isAdmin)
{
printf("\n Benvenuto %s %s \n", tmp->email, tmp->password);
return true;
}
user_exist(tmp->next,name,surname,email,password,isAdmin);
}
return false;
}
ListUtente user_push(ListUtente L, char name[50], char surname[50], char email[100], char password[7], bool isAdmin)
{
if (L != NULL){
L->next = user_push(L->next,name,surname,email,password,isAdmin);
} else{
L = initListUtente(name,surname,email, password,isAdmin);
}
return L;
}
Main.c
/*progetto gestione delle prenotazioni di voli.*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include <string.h>
#include <stdbool.h>
#include "Graph.h"
#include "List.h"
#include "Queue.h"
#include "Menu.h"
#include "ListPrenotazioni.h"
#include "ListUtente.h"
#define BUFFER_SIZE 256
#define randomize srand(((unsigned int) time(NULL)))
#define random(x) rand()%x
int main(int argc, char** argv){
bool access = false;
bool accesso;
bool Admin;
char name[50];
char surname[50];
char cityDestination[10];
char deleteCity[10];
char city[10];
char cityPartenza[10];
char cod_id[7];
char city1[10];
char cod_id1[7];
int m=1;
char email[100];
char pwd[7];
ListPrenotazioni L = NULL;//per listPrenotazioni
ListUtente l = NULL;//per listUtente
randomize;
Graph g = populateGraph();
char mostPopularDestination = random(g->nodes_count);
int loginChoice;
int adminChoice;
int userChoice;
int user1Choice;
int user2Choice;
bool continueLoop;
double price;
double distance;
int n;
do{
loginChoice = LoginMenu();
if(loginChoice == 0){ /*azioni amministratore*/
continueLoop = true;
accesso=&user_exist;
Admin=true;
if (accesso ==true){
printf("Insert name: ");
scanf("%s", name);
printf("Insert surname: ");
scanf("%s", surname);
printf("Insert e-mail: ");
scanf("%s", email);
printf("Insert password (max 7 characters): ");
scanf("%s", pwd);
l = user_push(l,name,surname,email,pwd,Admin);
user_exist(l,name,surname,email,pwd,Admin);
/*
Se l'admin non e' presente nella data structure degli admin --> continueLoop = false
In questo modo torna al menu di login e non vengono eseguite operazioni
*/
while(continueLoop){
adminChoice = AdminMenu();
switch (adminChoice){
case 0: // exit / logout
continueLoop = false;
break;
case 1: /* stampa destinazioni e tratte*/
printf("\nThe destinations are as follows:\n");
PrintGraph(g);
break;
case 2: /*inserisci destinazioni e tratte*/
addNode(g, getCityName("new destination(first uppercase character)"));
printf("\nInsert number of sections to be entered:\n");
scanf("%d",&n);
for(int v=0; v<n; v++){
printf("\nInsert price(es. 23.78):%d ",v);
scanf("%lf", &price);
printf("\nInsert distance(es. 345.98):%d",v);
scanf("%lf", &distance);
addEdge(g,getCityIndexByName(g,getCityName("new destination(first uppercase character)")), getCityIndexByName(g, getCityName("new destination(first uppercase character)")),price,distance);
}
printf("\n~Operation successfully performed~\n");
break;
case 3: /*cancella destinazioni e tratte*/
printf("Insert the city to be deleted:\n");
scanf("%s", deleteCity);
removeNodeByString(g,deleteCity);
printf("~\nOperation successfully performed~\n");
break;
default: /*Se l'utente sbaglia ad inserire il numero*/
printf("This button does not allow you to make choices! Try again!\n");
break;
}
}
}
}else if(loginChoice == 1){ /*azioni utenti*/
continueLoop = true;
accesso=&user_exist;
Admin=true;
if (accesso ==true){
Admin=false;
while(continueLoop){
user1Choice = UserMenu1();
switch (user1Choice){
case 0: // exit / logout
continueLoop = false;
break;
case 1: /*effettuo iscrizione*/
printf("\nInsert name:\n");
scanf("%s", name);
printf("\nInsert surname:\n");
scanf("%s", surname);
printf("\nInsert e-mail:\n");
scanf("%s",email);
printf("\nInsert password:\n");
scanf("%s",pwd);
l=user_push(l,name,surname,email,pwd,Admin);
break;
case 2: /*effettuo accesso*/
if(access == true){
printf("\nInsert e-mail:\n");
scanf("%s",email);
printf("\nInsert password:\n");
scanf("%s",pwd);
user_exist(l,name,surname,email,pwd,Admin);
printf("\n welcome %s %s \n", name, surname);
if(loginChoice == 1){ /*azioni utenti*/
continueLoop = true;
while (continueLoop){
userChoice = UserMenu();
switch (userChoice){
case 0:
continueLoop = false;
break;
case 1: /*vedi prenotazioni attive*/
printf("\nThe destinations are as follows:\n");
PrintGraph(g);
break;
case 2: /*effettua prenotazione*/
if(loginChoice == 1){ /*azioni utenti*/
continueLoop = true;
while (continueLoop){
user2Choice = UserMenu2();
switch (user2Choice){
case 0:
continueLoop = false;
break;
case 1: /*Prenotazione basata sul meno scambi inserendo la città di partenza e la città di destinazione*/
printf("\nInserisci città di partenza: ");
scanf("%s", cityPartenza);
printf("\nInserisci città di destinazione: ");
scanf("%s", cityDestination);
//inserire funzione per determinare tratta più economica
printf("Enter the number of reservations you wish to make\n");
scanf("%d",&m);
for (int i=0; i<m; i++){
printf("\nInsert citta:\n");
scanf("%s",city);
printf("\nInsert id:\n");
scanf("%s",cod_id);
L = insertTailPrenotazioni(L,city,cod_id,cod_id1,city1);
}
break;
case 2: /*Prenotazione basata sui percorsi più brevi inserendo la città di partenza e destinazione
funzione per percorsi brevi*/
printf("\nInserisci città di partenza: ");
scanf("%s", cityPartenza);
printf("\nInserisci città di destinazione: ");
scanf("%s", cityDestination);
//inserire funzione per determinare tratta più breve
printf("Enter the number of reservations you wish to make\n");
scanf("%d",&m);
for (int i=0; i<m; i++){
printf("\nInsert citta:\n");
scanf("%s",city);
printf("\nInsert id:\n");
scanf("%s",cod_id);
L = insertTailPrenotazioni(L,city,cod_id,cod_id1,city1);
}
break;
case 3: /*Prenotazione basata sulla destinazione più economica inserendo solo la città di partenza
funzione per destinazione piu economica*/
printf("\nInserisci città di partenza: ");
scanf("%s", cityPartenza);
//inserire funzione per la tratta più economica
printf("Enter the number of reservations you wish to make\n");
scanf("%d",&m);
for (int i=0; i<m; i++){
printf("\nInsert citta:\n");
scanf("%s",city);
printf("\nInsert id:\n");
scanf("%s",cod_id);
L = insertTailPrenotazioni(L,city,cod_id,cod_id1,city1);
}
break;
case 4: /*Prenotazione basata sulla destinazione più popolare inserendo solo la città di partenza*/
printf("\nEnter city of start: ");
scanf("%s", cityPartenza);
printf("\n The most popular destination is the following:\n");
mostPopularDestination = random(g->nodes_count);
char* cityName = g->cityNames[mostPopularDestination];
printf("id:%d city:%s\n",mostPopularDestination,cityName);
printf("Enter the number of reservations you wish to make\n");
scanf("%d",&m);
for (int i=0; i<m; i++){
printf("\nInsert citta partenza e id :\n");
scanf("%s%s", city,cod_id);
printf("\nInsert città arrivo id:\n");
scanf("%s%s",city1,cod_id1);
L = insertTailPrenotazioni(L,city,cod_id,cod_id1,city1);
}
break;
case 5: /*vedi prenotazioni attive*/
printListPrenotazioni(L);
break;
}
}
}
default: /*Se l'utente sbaglia ad inserire il numero*/
printf("This button does not allow you to make choices! Try again!\n");
break;
}
}
}
}
}
}
}
}
}while(loginChoice != 2);
printf("Thanks for choosing us, come back to visit us!\n");
return 0;
}
Menu.c
#include <stdio.h>
#include "Menu.h"
int LoginMenu(){
int choice = -1;
printf(" * * * * * * * * * * * * * * * * * * * * * * * * Welcome to flight booking management\n * * * * * * * * * * * * * * * * * * * * * * * * \n");
printf("Who are you?\n");
printf(" 0. Admin\n");
printf(" 1. User\n");
printf(" 2. Exit\n");
printf("Insert your choice:");
scanf("%d",&choice);
while (choice < 0 || choice > 2) {
printf("This button does not allow you to make choices! Try again!\n");
printf("Insert choice thanks: ");
scanf("%d", &choice);
return choice;
}
return choice;
}
int AdminMenu(){
int choice = -1;
printf(" **************** \n");
printf(" ADMINISTATOR \n");
printf(" **************** \n");
printf(" 0. Logout\n");
printf(" 1. Print destinations\n");
printf(" 2. Insert destinations\n");
printf(" 3. Delete destinations\n");
printf("Insert your choice:");
scanf("%d",&choice);
while (choice < 0 || choice > 3) {
printf("This button does not allow you to make choices! Try again!\n");
printf("Insert your choice:");
scanf("%d", &choice);
return choice;
}
return choice;
}
int UserMenu(){
int choice = -1;
printf(" \n **************** \n");
printf(" USER \n");
printf(" **************** \n");
printf(" 0. LogOut\n");
printf(" 1. List of available cities\n");
printf(" 2. Make reservation\n");
printf("Insert your choice:");
scanf("%d",&choice);
while (choice < 0 || choice > 3) {
printf("This button does not allow you to make choices! Try again!\n");
printf("Insert your choice:");
scanf("%d", &choice);
return choice;
}
return choice;
}
int UserMenu1(){
int choice = -1;
printf(" **************** \n");
printf(" USER \n");
printf(" **************** \n");
printf(" 0. LogOut\n");
printf(" 1. SignUp\n");
printf(" 2. SignIn\n");
printf("Insert your choice:");
scanf("%d",&choice);
while (choice < 0 || choice > 2) {
printf("This button does not allow you to make choices! Try again!\n");
printf("Insert your choice:");
scanf("%d", &choice);
return choice;
}
return choice;
}
int UserMenu2(){
int choice = -1;
printf("\n **************** \n");
printf(" USER \n");
printf(" **************** \n");
printf(" 0. LogOut\n");
printf(" 1. Booking based on the shortest route by entering the city and the departure destination\n");
printf(" 2. Booking based on the shortest routes by entering the city of departure and destination\n");
printf(" 3. Booking based on the cheapest destination by entering only the city of departure\n");
printf(" 4. Booking based on the most popular destination by entering only the city of departure\n");
printf(" 5. View prenotations\n");
printf("Insert your choice:");
scanf("%d",&choice);
while (choice < 0 || choice > 5) {
printf("This button does not allow you to make choices! Try again!\n");
printf("Insert your choice:");
scanf("%d", &choice);
return choice;
}
return choice;
}
Ultima modifica: