-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.c
102 lines (86 loc) · 3.23 KB
/
auth.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "headers.h"
/*****************************************
* Comparar si es un inicio de sesion *
* valido, en caso de serlo, guardar sus *
* datos en un estructura. *
* @method logIn *
* @param user_name user_name *
* @param password contrasena *
* @param mysql pointer a DB *
*****************************************/
void logIn(char *user_name, char *password, MYSQL *mysql) {
char account[1024];
MYSQL_RES *res;
MYSQL_ROW row;
User usr;
sprintf(account, "SELECT name, l_name, typeof, id_user, id_institution FROM p1_users WHERE username = '%s' AND password = '%s'", user_name, password);
dbQuery(account,mysql,&res);
if (!(row = mysql_fetch_row(res))) {
printf("\n\n\n\tUsuario o contraseña incorrectos\n\tIntenta de nuevo!!!\n\n\n");
} else {
char cont[mysql_num_fields(res)][20];
notNUll(row,cont,mysql_num_fields(res));
setStruct(cont,&usr);
printf("\n\n\n\tBienvenido: %s %s (%s)\n\n",usr.name,usr.l_name,usr.type_of);
printf("\tPresione Enter para continuar...");
getchar();
setMenu(usr,mysql);
}
mysql_free_result(res);
}
/*****************************************
* Verifica si el valor regresado por el *
* query es nulo, de serlo regresa una *
* cadena vacia. *
* @method notNUll *
* @param row resultset *
* @param cont String array *
* @param x fields *
*****************************************/
void notNUll(MYSQL_ROW row, char cont[][20], int x) {
for (int i = 0; i < x; i++) {
if (row[i] != NULL) {
strcpy(cont[i],row[i]);
} else {
strcpy(cont[i],"");
}
}
}
/************************************
* Guarda los datos del query en la *
* estructura del usuario. *
* @method setStruct *
* @param cont String array *
* @param usr user struct *
************************************/
void setStruct(char cont[][20], User *usr) {
char str[20];
strcpy(usr->name,cont[0]);
strcpy(usr->l_name,cont[1]);
userType(cont[2]);
strcpy(usr->type_of,cont[2]);
sprintf(str,"%s\n",cont[3]);
usr->id_user = strInt(str);
if (strcmp(cont[4],"") == 0) {
usr->id_institution = -1;
} else {
sprintf(str,"%s\n",cont[4]);
usr->id_institution = strInt(str);
}
}
/*************************************
* Regresa un String del nombre del *
* tipo de uusuario, segun mascara *
* binaria. *
* @method userType *
* @param type pointer a String *
*************************************/
void userType(char *type) {
if (strcmp(type,"00") == 0) {
strcpy(type,"Superuser");
} else if (strcmp(type,"01") == 0) {
strcpy(type,"Administrador");
} else if (strcmp(type,"10") == 0) {
strcpy(type,"Cliente");
}
}