-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathswad_ID.c
1003 lines (857 loc) · 33.7 KB
/
swad_ID.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// swad_ID.c: Users' IDs
/*
SWAD (Shared Workspace At a Distance),
is a web platform developed at the University of Granada (Spain),
and used to support university teaching.
This file is part of SWAD core.
Copyright (C) 1999-2025 Antonio Cañas Vargas
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*****************************************************************************/
/********************************* Headers ***********************************/
/*****************************************************************************/
#define _GNU_SOURCE // For asprintf
#include <ctype.h> // For isalnum, isdigit, etc.
#include <stdbool.h> // For boolean type
#include <stdio.h> // For asprintf
#include <stdlib.h> // For exit, system, malloc, free, rand, etc.
#include <string.h> // For string functions
#include "swad_account.h"
#include "swad_action_list.h"
#include "swad_alert.h"
#include "swad_box.h"
#include "swad_database.h"
#include "swad_error.h"
#include "swad_form.h"
#include "swad_global.h"
#include "swad_hierarchy_type.h"
#include "swad_HTML.h"
#include "swad_ID.h"
#include "swad_ID_database.h"
#include "swad_parameter.h"
#include "swad_QR.h"
#include "swad_user.h"
/*****************************************************************************/
/************** External global variables from others modules ****************/
/*****************************************************************************/
extern struct Globals Gbl;
/*****************************************************************************/
/***************************** Private constants *****************************/
/*****************************************************************************/
#define ID_MAX_IDS_PER_USER 3 // Maximum number of IDs per user
/*****************************************************************************/
/***************************** Private variables *****************************/
/*****************************************************************************/
const char *ID_ID_SECTION_ID = "id_section";
/*****************************************************************************/
/***************************** Private prototypes ****************************/
/*****************************************************************************/
static bool ID_CheckIfUsrIDIsValidUsingMinDigits (const char *UsrID,unsigned MinDigits);
static void ID_PutLinkToConfirmID (struct Usr_Data *UsrDat,unsigned NumID,
const char *Anchor);
static void ID_ShowFormChangeUsrID (Usr_MeOrOther_t MeOrOther,bool IShouldFillInID);
static void ID_PutParsRemoveMyID (void *ID);
static void ID_PutParsRemoveOtherID (void *ID);
static void ID_RemoveUsrID (const struct Usr_Data *UsrDat,Usr_MeOrOther_t MeOrOther);
static void ID_ChangeUsrID (const struct Usr_Data *UsrDat,Usr_MeOrOther_t MeOrOther);
/*****************************************************************************/
/********************** Get list of IDs of a user ****************************/
/*****************************************************************************/
void ID_GetListIDsFromUsrCod (struct Usr_Data *UsrDat)
{
MYSQL_RES *mysql_res;
MYSQL_ROW row;
unsigned NumIDs;
unsigned NumID;
/***** Initialize list of IDs to an empty list *****/
ID_FreeListIDs (UsrDat);
if (UsrDat->UsrCod > 0)
{
/***** Get user's IDs from database *****/
// First the confirmed (Confirmed == 'Y')
// Then the unconfirmed (Confirmed == 'N')
if ((NumIDs = ID_DB_GetIDsFromUsrCod (&mysql_res,UsrDat->UsrCod)))
{
/***** Allocate space for the list *****/
ID_ReallocateListIDs (UsrDat,NumIDs);
/***** Get list of IDs *****/
for (NumID = 0;
NumID < NumIDs;
NumID++)
{
row = mysql_fetch_row (mysql_res);
/* Get ID from row[0] */
Str_Copy (UsrDat->IDs.List[NumID].ID,row[0],
sizeof (UsrDat->IDs.List[NumID].ID) - 1);
/* Get if ID is confirmed from row[1] */
UsrDat->IDs.List[NumID].Confirmed = (row[1][0] == 'Y');
}
}
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
}
}
/*****************************************************************************/
/***************** Free memory allocated for list of IDs *********************/
/*****************************************************************************/
void ID_ReallocateListIDs (struct Usr_Data *UsrDat,unsigned NumIDs)
{
/***** Free list of IDs if used *****/
ID_FreeListIDs (UsrDat);
/***** Assign number of IDs in list *****/
UsrDat->IDs.Num = NumIDs;
/***** Allocate space for the list *****/
if ((UsrDat->IDs.List = malloc (NumIDs * sizeof (*UsrDat->IDs.List))) == NULL)
Err_NotEnoughMemoryExit ();
}
/*****************************************************************************/
/***************** Free memory allocated for list of IDs *********************/
/*****************************************************************************/
void ID_FreeListIDs (struct Usr_Data *UsrDat)
{
/***** Free list *****/
if (UsrDat->IDs.Num && UsrDat->IDs.List)
free (UsrDat->IDs.List);
/***** Reset list *****/
UsrDat->IDs.List = NULL;
UsrDat->IDs.Num = 0;
}
/*****************************************************************************/
/***************** Get list of user codes from user's IDs ********************/
/*****************************************************************************/
// Returns the number of users with any of these IDs
// The list of users' codes is allocated inside this function and should be freed by caller
unsigned ID_GetListUsrCodsFromUsrID (struct Usr_Data *UsrDat,
const char *EncryptedPassword, // If NULL or empty ==> do not check password
struct Usr_ListUsrCods *ListUsrCods,
bool OnlyConfirmedIDs)
{
MYSQL_RES *mysql_res;
unsigned NumUsr;
if (UsrDat->IDs.Num)
{
if ((ListUsrCods->NumUsrs = ID_DB_GetUsrCodsFromUsrID (&mysql_res,
UsrDat,
EncryptedPassword,
OnlyConfirmedIDs)))
{
/***** Allocate space for the list of users' codes *****/
Usr_AllocateListUsrCods (ListUsrCods);
// The list should be freed by caller
/***** Fill the list *****/
for (NumUsr = 0;
NumUsr < ListUsrCods->NumUsrs;
NumUsr++)
/* Get user's code */
ListUsrCods->Lst[NumUsr] = DB_GetNextCode (mysql_res);
UsrDat->UsrCod = ListUsrCods->Lst[0]; // The first user found
}
else // ListUsrCods->NumUsrs == 0
{
ListUsrCods->Lst = NULL;
UsrDat->UsrCod = -1L;
}
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
}
else // List of user's IDs is empty
{
ListUsrCods->NumUsrs = 0;
ListUsrCods->Lst = NULL;
UsrDat->UsrCod = -1L;
}
return ListUsrCods->NumUsrs;
}
/*****************************************************************************/
/******* Put hidden parameter with the plain user's ID of other user *********/
/*****************************************************************************/
void ID_PutParOtherUsrIDPlain (void)
{
Par_PutParString (NULL,"OtherUsrID",
(Gbl.Usrs.Other.UsrDat.IDs.Num &&
Gbl.Usrs.Other.UsrDat.IDs.List) ? Gbl.Usrs.Other.UsrDat.IDs.List[0].ID :
"");
}
/*****************************************************************************/
/********* Get parameter plain user's ID of other user from a form ***********/
/*****************************************************************************/
void ID_GetParOtherUsrIDPlain (void)
{
/***** Allocate space for the list *****/
ID_ReallocateListIDs (&Gbl.Usrs.Other.UsrDat,1);
/***** Get parameter *****/
Par_GetParText ("OtherUsrID",Gbl.Usrs.Other.UsrDat.IDs.List[0].ID,
ID_MAX_BYTES_USR_ID);
// Users' IDs are always stored internally in capitals and without leading zeros
Str_RemoveLeadingZeros (Gbl.Usrs.Other.UsrDat.IDs.List[0].ID);
Str_ConvertToUpperText (Gbl.Usrs.Other.UsrDat.IDs.List[0].ID);
Gbl.Usrs.Other.UsrDat.IDs.List[0].Confirmed = true;
}
/*****************************************************************************/
/****** Check whether a user's ID without the ending letter is valid *********/
/*****************************************************************************/
// Returns true if the user's ID string is valid, or false if not
// A valid user's ID must...:
// 1. Must be ID_MIN_BYTES_USR_ID <= characters <= ID_MAX_BYTES_USR_ID.
// 2. All characters must be digits or letters
// 3. Must have a minimum number of digits
// Wrapper function to avoid passing extra parameters
bool ID_CheckIfUsrIDIsValid (const char *UsrID)
{
if (UsrID)
if (UsrID[0])
return ID_CheckIfUsrIDIsValidUsingMinDigits (UsrID,ID_MIN_DIGITS_USR_ID);
return false;
}
// Wrapper function to avoid passing extra parameters
bool ID_CheckIfUsrIDSeemsAValidID (const char *UsrID)
{
if (UsrID)
if (UsrID[0])
return ID_CheckIfUsrIDIsValidUsingMinDigits (UsrID,ID_MIN_DIGITS_AUTOMATIC_DETECT_USR_ID);
return false;
}
static bool ID_CheckIfUsrIDIsValidUsingMinDigits (const char *UsrID,unsigned MinDigits)
{
const char *Ptr;
unsigned NumDigits = 0;
unsigned Length;
/***** Check length *****/
if (!UsrID)
return false;
if (!UsrID[0])
return false;
Length = strlen (UsrID);
if (Length < ID_MIN_BYTES_USR_ID ||
Length > ID_MAX_BYTES_USR_ID)
return false; // 1. Must be ID_MIN_BYTES_USR_ID <= characters <= ID_MAX_BYTES_USR_ID
/**** Loop through user's ID *****/
for (Ptr = UsrID;
*Ptr;
Ptr++)
if (isdigit ((int) *Ptr)) // If character is digit
NumDigits++;
else if (!((*Ptr >= 'A' && *Ptr <= 'Z') ||
(*Ptr >= 'a' && *Ptr <= 'z'))) // If character is not alpha
return false; // 2. All characters must be digits or letters
return (NumDigits >= MinDigits); // 3. Must have MinDigits digits at least
}
/*****************************************************************************/
/*************************** Write list of user's ID *************************/
/*****************************************************************************/
void ID_WriteUsrIDs (struct Usr_Data *UsrDat,const char *Anchor)
{
unsigned NumID;
Usr_Can_t ICanSeeUsrID = ID_ICanSeeOtherUsrIDs (UsrDat);
Usr_Can_t ICanConfirmUsrID = (ICanSeeUsrID == Usr_CAN &&
Usr_ItsMe (UsrDat->UsrCod) == Usr_OTHER && // Not me
!Frm_CheckIfInside () && // Not inside another form
Act_GetBrowserTab (Gbl.Action.Act) == Act_1ST) ? Usr_CAN : // Only in main browser tab
Usr_CAN_NOT;
for (NumID = 0;
NumID < UsrDat->IDs.Num;
NumID++)
{
/* If not the first ID ==> new line */
if (NumID)
HTM_BR ();
/* Write this ID */
HTM_SPAN_Begin ("class=\"%s_%s\"",
UsrDat->IDs.List[NumID].Confirmed ? "USR_ID_C" :
"USR_ID_NC",
The_GetSuffix ());
switch (ICanSeeUsrID)
{
case Usr_CAN:
HTM_Txt (UsrDat->IDs.List[NumID].ID);
break;
case Usr_CAN_NOT:
default:
HTM_Txt ("********");
break;
}
HTM_SPAN_End ();
/* Put link to confirm ID? */
if (ICanConfirmUsrID == Usr_CAN &&
!UsrDat->IDs.List[NumID].Confirmed)
ID_PutLinkToConfirmID (UsrDat,NumID,Anchor);
}
}
/*****************************************************************************/
/***************** Check if I can see another user's IDs *********************/
/*****************************************************************************/
Usr_Can_t ID_ICanSeeOtherUsrIDs (const struct Usr_Data *UsrDat)
{
/***** Fast check: It's me? *****/
if (Usr_ItsMe (UsrDat->UsrCod) == Usr_ME)
return Usr_CAN;
/***** Check if I have permission to see another user's IDs *****/
switch (Gbl.Usrs.Me.Role.Logged)
{
case Rol_NET:
case Rol_TCH:
/* Check 1: I can see the IDs of users who do not exist in database */
if (UsrDat->UsrCod <= 0) // User does not exist (when creating a new user)
return Usr_CAN;
/* Check 2: I can see the IDs of confirmed students */
if (UsrDat->Roles.InCurrentCrs == Rol_STD && // A student
UsrDat->Accepted) // who accepted registration
return Usr_CAN;
/* Check 3: I can see the IDs of users with user's data empty */
// This check is made to not view simultaneously:
// - an ID
// - a name or an email
if (!UsrDat->Password[0] && // User has no password (never logged)
!UsrDat->Surname1[0] && // and who has no surname 1 (nobody filled user's surname 1)
!UsrDat->Surname2[0] && // and who has no surname 2 (nobody filled user's surname 2)
!UsrDat->FrstName[0] && // and who has no first name (nobody filled user's first name)
!UsrDat->Email[0]) // and who has no email (nobody filled user's email)
return Usr_CAN;
return Usr_CAN_NOT;
case Rol_DEG_ADM:
case Rol_CTR_ADM:
case Rol_INS_ADM:
case Rol_SYS_ADM:
return Usr_CheckIfICanEditOtherUsr (UsrDat);
default:
return Usr_CAN_NOT;
}
}
/*****************************************************************************/
/****************** Put a link to confirm another user's ID ******************/
/*****************************************************************************/
static void ID_PutLinkToConfirmID (struct Usr_Data *UsrDat,unsigned NumID,
const char *Anchor)
{
extern const char *Txt_Confirm;
static Act_Action_t NextAction[Rol_NUM_ROLES] =
{
[Rol_UNK ] = ActCnfID_Oth,
[Rol_GST ] = ActCnfID_Oth,
[Rol_USR ] = ActCnfID_Oth,
[Rol_STD ] = ActCnfID_Std,
[Rol_NET ] = ActCnfID_Tch,
[Rol_TCH ] = ActCnfID_Tch,
[Rol_DEG_ADM] = ActCnfID_Oth,
[Rol_CTR_ADM] = ActCnfID_Oth,
[Rol_INS_ADM] = ActCnfID_Oth,
[Rol_SYS_ADM] = ActCnfID_Oth,
};
/***** Begin form *****/
Frm_BeginFormAnchor (NextAction[UsrDat->Roles.InCurrentCrs],Anchor);
if (Gbl.Action.Original != ActUnk)
{
ParCod_PutPar (ParCod_OrgAct,Act_GetActCod (Gbl.Action.Original)); // Original action, used to know where we came from
switch (Gbl.Action.Original)
{
case ActSeeRecSevGst:
case ActSeeRecSevStd:
case ActSeeRecSevTch:
Usr_PutParSelectedUsrsCods (&Gbl.Usrs.Selected);
break;
default:
break;
}
}
Usr_PutParUsrCodEncrypted (UsrDat->EnUsrCod);
Par_PutParString (NULL,"UsrID",UsrDat->IDs.List[NumID].ID);
/***** Put link *****/
HTM_BUTTON_Submit_Begin (Txt_Confirm,
"class=\"BT_LINK FORM_OUT_%s BOLD\"",
The_GetSuffix ());
Ico_PutIconTextLink ("check.svg",Ico_BLACK,Txt_Confirm);
HTM_BUTTON_End ();
/***** End form *****/
Frm_EndForm ();
}
/*****************************************************************************/
/*********************** Show form to change my user's ID ********************/
/*****************************************************************************/
void ID_ShowFormChangeMyID (bool IShouldFillInID)
{
extern const char *Hlp_PROFILE_Account;
extern const char *Txt_ID_identity_number;
/***** Begin section *****/
HTM_SECTION_Begin (ID_ID_SECTION_ID);
/***** Begin box *****/
Box_BoxBegin (Txt_ID_identity_number,Acc_PutLinkToRemoveMyAccount,NULL,
Hlp_PROFILE_Account,Box_NOT_CLOSABLE);
/***** Show form to change ID *****/
ID_ShowFormChangeUsrID (Usr_ME,IShouldFillInID);
/***** End box *****/
Box_BoxEnd ();
/***** End section *****/
HTM_SECTION_End ();
}
/*****************************************************************************/
/*********************** Show form to change my user's ID ********************/
/*****************************************************************************/
void ID_ShowFormChangeOtherUsrID (void)
{
extern const char *Hlp_PROFILE_Account;
extern const char *Txt_ID_identity_number;
/***** Begin section *****/
HTM_SECTION_Begin (ID_ID_SECTION_ID);
/***** Begin box *****/
Box_BoxBegin (Txt_ID_identity_number,NULL,NULL,
Hlp_PROFILE_Account,Box_NOT_CLOSABLE);
/***** Show form to change ID *****/
ID_ShowFormChangeUsrID (Usr_OTHER,
false); // IShouldFillInID
/***** End box *****/
Box_BoxEnd ();
/***** End section *****/
HTM_SECTION_End ();
}
/*****************************************************************************/
/*********************** Show form to change my user's ID ********************/
/*****************************************************************************/
static void ID_ShowFormChangeUsrID (Usr_MeOrOther_t MeOrOther,bool IShouldFillInID)
{
extern const char *Hlp_PROFILE_Account;
extern const char *Txt_Please_fill_in_your_ID;
extern const char *Txt_ID_X_confirmed;
extern const char *Txt_ID_X_not_confirmed;
extern const char *Txt_ID_identity_number;
extern const char *Txt_Another_ID;
extern const char *Txt_The_ID_is_used_in_order_to_facilitate_;
extern struct Usr_Data *Usr_UsrDat[Usr_NUM_ME_OR_OTHER];
unsigned NumID;
char *Title;
static struct
{
Act_Action_t Remove;
Act_Action_t Change;
} NextAction[Rol_NUM_ROLES] =
{
[Rol_UNK ] = {ActRemID_Oth,ActChgID_Oth},
[Rol_GST ] = {ActRemID_Oth,ActChgID_Oth},
[Rol_USR ] = {ActRemID_Oth,ActChgID_Oth},
[Rol_STD ] = {ActRemID_Std,ActChgID_Std},
[Rol_NET ] = {ActRemID_Tch,ActChgID_Tch},
[Rol_TCH ] = {ActRemID_Tch,ActChgID_Tch},
[Rol_DEG_ADM] = {ActRemID_Oth,ActChgID_Oth},
[Rol_CTR_ADM] = {ActRemID_Oth,ActChgID_Oth},
[Rol_INS_ADM] = {ActRemID_Oth,ActChgID_Oth},
[Rol_SYS_ADM] = {ActRemID_Oth,ActChgID_Oth},
};
static void (*FuncParsRemove[Usr_NUM_ME_OR_OTHER]) (void *ID) =
{
[Usr_ME ] = ID_PutParsRemoveMyID,
[Usr_OTHER] = ID_PutParsRemoveOtherID
};
struct
{
Act_Action_t Remove;
Act_Action_t Change;
} ActID[Rol_NUM_ROLES] =
{
[Usr_ME ] = {.Remove = ActRemMyID,
.Change = ActChgMyID},
[Usr_OTHER] = {.Remove = NextAction[Gbl.Usrs.Other.UsrDat.Roles.InCurrentCrs].Remove,
.Change = NextAction[Gbl.Usrs.Other.UsrDat.Roles.InCurrentCrs].Change}
};
/***** Show possible alerts *****/
Ale_ShowAlerts (ID_ID_SECTION_ID);
/***** Help message *****/
if (IShouldFillInID)
Ale_ShowAlert (Ale_WARNING,Txt_Please_fill_in_your_ID);
/***** Begin table *****/
HTM_TABLE_BeginPadding (2);
/***** List existing user's IDs *****/
for (NumID = 0;
NumID < Usr_UsrDat[MeOrOther]->IDs.Num;
NumID++)
{
if (NumID == 0)
{
HTM_TR_Begin (NULL);
/* Label */
Frm_LabelColumn ("Frm_C1 RT",NULL,Txt_ID_identity_number);
/* Data */
HTM_TD_Begin ("class=\"Frm_C2 LB DAT_STRONG_%s\"",The_GetSuffix ());
}
else // NumID >= 1
HTM_BR ();
if (Usr_UsrDat[MeOrOther]->IDs.Num > 1) // I have two or more IDs
{
if (MeOrOther == Usr_ME &&
Usr_UsrDat[MeOrOther]->IDs.List[NumID].Confirmed) // I can not remove my confirmed IDs
/* Put disabled icon to remove user's ID */
Ico_PutIconRemovalNotAllowed ();
else // I can remove
/* Form to remove user's ID */
Ico_PutContextualIconToRemove (ActID[MeOrOther].Remove,ID_ID_SECTION_ID,
FuncParsRemove[MeOrOther],Usr_UsrDat[MeOrOther]->IDs.List[NumID].ID);
}
/* User's ID */
if (asprintf (&Title,Usr_UsrDat[MeOrOther]->IDs.List[NumID].Confirmed ? Txt_ID_X_confirmed :
Txt_ID_X_not_confirmed,
Usr_UsrDat[MeOrOther]->IDs.List[NumID].ID) < 0)
Err_NotEnoughMemoryExit ();
HTM_SPAN_Begin ("class=\"%s\" title=\"%s\"",
Usr_UsrDat[MeOrOther]->IDs.List[NumID].Confirmed ? "USR_ID_C" :
"USR_ID_NC",
Title);
free (Title);
HTM_Txt (Usr_UsrDat[MeOrOther]->IDs.List[NumID].ID);
HTM_Txt (Usr_UsrDat[MeOrOther]->IDs.List[NumID].Confirmed ? "✓" :
"");
HTM_SPAN_End ();
if (NumID == Usr_UsrDat[MeOrOther]->IDs.Num - 1)
{
HTM_TD_End ();
HTM_TR_End ();
}
}
if (Usr_UsrDat[MeOrOther]->IDs.Num < ID_MAX_IDS_PER_USER)
{
/***** Write help text *****/
HTM_TR_Begin (NULL);
HTM_TD_Begin ("colspan=\"2\" class=\"CM DAT_%s\"",The_GetSuffix ());
Ale_ShowAlert (Ale_INFO,Txt_The_ID_is_used_in_order_to_facilitate_);
HTM_TD_End ();
HTM_TR_End ();
/***** Form to enter new user's ID *****/
HTM_TR_Begin (NULL);
/* Label */
Frm_LabelColumn ("Frm_C1 RT","NewID",
Usr_UsrDat[MeOrOther]->IDs.Num ? Txt_Another_ID : // A new user's ID
Txt_ID_identity_number); // The first user's ID
/* Data */
HTM_TD_Begin ("class=\"Frm_C2 LT DAT_%s\"",The_GetSuffix ());
Frm_BeginFormAnchor (ActID[MeOrOther].Change,ID_ID_SECTION_ID);
if (MeOrOther == Usr_OTHER)
Usr_PutParUsrCodEncrypted (Gbl.Usrs.Other.UsrDat.EnUsrCod);
HTM_INPUT_TEXT ("NewID",ID_MAX_BYTES_USR_ID,
Usr_UsrDat[MeOrOther]->IDs.Num ? Usr_UsrDat[MeOrOther]->IDs.List[Usr_UsrDat[MeOrOther]->IDs.Num - 1].ID :
"", // Show the most recent ID
HTM_NO_ATTR,
"id=\"NewID\" class=\"Frm_C2_INPUT INPUT_%s\""
" size=\"16\"",The_GetSuffix ());
HTM_BR ();
Btn_PutButtonInline (Btn_CREATE);
Frm_EndForm ();
HTM_TD_End ();
HTM_TR_End ();
}
/***** End table *****/
HTM_TABLE_End ();
}
static void ID_PutParsRemoveMyID (void *ID)
{
if (ID)
Par_PutParString (NULL,"UsrID",(char *) ID);
}
static void ID_PutParsRemoveOtherID (void *ID)
{
if (ID)
{
Usr_PutParUsrCodEncrypted (Gbl.Usrs.Other.UsrDat.EnUsrCod);
Par_PutParString (NULL,"UsrID",(char *) ID);
}
}
/*****************************************************************************/
/********************** Remove one of my user's IDs **************************/
/*****************************************************************************/
void ID_RemoveMyUsrID (void)
{
/***** Remove user's ID *****/
ID_RemoveUsrID (&Gbl.Usrs.Me.UsrDat,Usr_ME);
/***** Update list of IDs *****/
ID_GetListIDsFromUsrCod (&Gbl.Usrs.Me.UsrDat);
/***** Show my account again *****/
Acc_ShowFormChgMyAccount ();
}
/*****************************************************************************/
/**************** Remove one of the user's IDs of another user ***************/
/*****************************************************************************/
void ID_RemoveOtherUsrID (void)
{
/***** Get other user's code from form and get user's data *****/
if (Usr_GetParOtherUsrCodEncryptedAndGetUsrData ())
switch (Usr_CheckIfICanEditOtherUsr (&Gbl.Usrs.Other.UsrDat))
{
case Usr_CAN:
/***** Remove user's ID *****/
ID_RemoveUsrID (&Gbl.Usrs.Other.UsrDat,
Usr_ItsMe (Gbl.Usrs.Other.UsrDat.UsrCod));
/***** Update list of IDs *****/
ID_GetListIDsFromUsrCod (&Gbl.Usrs.Other.UsrDat);
/***** Show form again *****/
Acc_ShowFormChgOtherUsrAccount ();
break;
case Usr_CAN_NOT:
default:
Ale_ShowAlertUserNotFoundOrYouDoNotHavePermission ();
break;
}
else // User not found
Ale_ShowAlertUserNotFoundOrYouDoNotHavePermission ();
}
/*****************************************************************************/
/***************************** Remove user's ID ******************************/
/*****************************************************************************/
static void ID_RemoveUsrID (const struct Usr_Data *UsrDat,Usr_MeOrOther_t MeOrOther)
{
extern const char *Txt_ID_X_removed;
extern const char *Txt_You_can_not_delete_this_ID;
char UsrID[ID_MAX_BYTES_USR_ID + 1];
Usr_Can_t ICanRemove = Usr_CAN_NOT;
switch (Usr_CheckIfICanEditOtherUsr (UsrDat))
{
case Usr_CAN:
/***** Get user's ID from form *****/
Par_GetParText ("UsrID",UsrID,ID_MAX_BYTES_USR_ID);
// Users' IDs are always stored internally in capitals and without leading zeros
Str_RemoveLeadingZeros (UsrID);
Str_ConvertToUpperText (UsrID);
if (UsrDat->IDs.Num >= 2)
switch (MeOrOther)
{
case Usr_ME:
// I can remove my ID only if it is not confirmed
ICanRemove = ID_DB_CheckIfConfirmed (UsrDat->UsrCod,UsrID) ? Usr_CAN_NOT :
Usr_CAN;
break;
case Usr_OTHER:
ICanRemove = Usr_CAN;
break;
}
switch (ICanRemove)
{
case Usr_CAN:
/***** Remove one of the user's IDs *****/
ID_DB_RemoveUsrID (UsrDat->UsrCod,UsrID);
/***** Show message *****/
Ale_CreateAlert (Ale_SUCCESS,ID_ID_SECTION_ID,
Txt_ID_X_removed,UsrID);
break;
case Usr_CAN_NOT:
default:
Ale_CreateAlert (Ale_WARNING,ID_ID_SECTION_ID,
Txt_You_can_not_delete_this_ID);
break;
}
break;
case Usr_CAN_NOT:
default:
Ale_CreateAlertUserNotFoundOrYouDoNotHavePermission ();
break;
}
}
/*****************************************************************************/
/************************* New user's ID for me ******************************/
/*****************************************************************************/
void ID_NewMyUsrID (void)
{
/***** New user's ID *****/
ID_ChangeUsrID (&Gbl.Usrs.Me.UsrDat,Usr_ME);
/***** Update list of IDs *****/
ID_GetListIDsFromUsrCod (&Gbl.Usrs.Me.UsrDat);
/***** Show my account again *****/
Acc_ShowFormChgMyAccount ();
}
/*****************************************************************************/
/************************* Change another user's ID **************************/
/*****************************************************************************/
void ID_ChangeOtherUsrID (void)
{
/***** Get other user's code from form and get user's data *****/
if (Usr_GetParOtherUsrCodEncryptedAndGetUsrData ())
switch (Usr_CheckIfICanEditOtherUsr (&Gbl.Usrs.Other.UsrDat))
{
case Usr_CAN:
/***** Change user's ID *****/
ID_ChangeUsrID (&Gbl.Usrs.Other.UsrDat,
Usr_ItsMe (Gbl.Usrs.Other.UsrDat.UsrCod));
/***** Update list of IDs *****/
ID_GetListIDsFromUsrCod (&Gbl.Usrs.Other.UsrDat);
/***** Show form again *****/
Acc_ShowFormChgOtherUsrAccount ();
break;
case Usr_CAN_NOT:
default:
Ale_ShowAlertUserNotFoundOrYouDoNotHavePermission ();
break;
}
else // User not found
Ale_ShowAlertUserNotFoundOrYouDoNotHavePermission ();
}
/*****************************************************************************/
/***************************** Change user's ID ******************************/
/*****************************************************************************/
static void ID_ChangeUsrID (const struct Usr_Data *UsrDat,Usr_MeOrOther_t MeOrOther)
{
extern const char *Txt_The_ID_X_matches_one_of_the_existing;
extern const char *Txt_The_ID_X_has_been_confirmed;
extern const char *Txt_A_user_can_not_have_more_than_X_IDs;
extern const char *Txt_The_ID_X_has_been_registered_successfully;
extern const char *Txt_The_ID_X_is_not_valid;
char NewID[ID_MAX_BYTES_USR_ID + 1];
unsigned NumID;
bool AlreadyExists;
unsigned NumIDFound = 0; // Initialized to avoid warning
switch (Usr_CheckIfICanEditOtherUsr (UsrDat))
{
case Usr_CAN:
/***** Get new user's ID from form *****/
Par_GetParText ("NewID",NewID,ID_MAX_BYTES_USR_ID);
// Users' IDs are always stored internally in capitals and without leading zeros
Str_RemoveLeadingZeros (NewID);
Str_ConvertToUpperText (NewID);
if (ID_CheckIfUsrIDIsValid (NewID)) // If new ID is valid
{
/***** Check if the new ID matches any of the old IDs *****/
for (NumID = 0, AlreadyExists = false;
NumID < UsrDat->IDs.Num && !AlreadyExists;
NumID++)
if (!strcasecmp (UsrDat->IDs.List[NumID].ID,NewID))
{
AlreadyExists = true;
NumIDFound = NumID;
}
if (AlreadyExists) // This new ID was already associated to this user
{
if (MeOrOther == Usr_ME || UsrDat->IDs.List[NumIDFound].Confirmed)
Ale_CreateAlert (Ale_WARNING,ID_ID_SECTION_ID,
Txt_The_ID_X_matches_one_of_the_existing,
NewID);
else // It's not me && !Confirmed
{
/***** Mark this ID as confirmed *****/
ID_DB_ConfirmUsrID (UsrDat->UsrCod,NewID);
Ale_CreateAlert (Ale_SUCCESS,ID_ID_SECTION_ID,
Txt_The_ID_X_has_been_confirmed,
NewID);
}
}
else if (UsrDat->IDs.Num >= ID_MAX_IDS_PER_USER)
Ale_CreateAlert (Ale_WARNING,ID_ID_SECTION_ID,
Txt_A_user_can_not_have_more_than_X_IDs,
ID_MAX_IDS_PER_USER);
else // OK ==> add this new ID to my list of IDs
{
/***** Save this new ID *****/
// It's me ==> ID not confirmed
// Not me ==> ID confirmed
ID_DB_InsertANewUsrID (UsrDat->UsrCod,NewID,MeOrOther == Usr_OTHER);
Ale_CreateAlert (Ale_SUCCESS,ID_ID_SECTION_ID,
Txt_The_ID_X_has_been_registered_successfully,
NewID);
}
}
else // New ID is not valid
Ale_CreateAlert (Ale_WARNING,ID_ID_SECTION_ID,
Txt_The_ID_X_is_not_valid,
NewID);
break;
case Usr_CAN_NOT:
default:
Ale_CreateAlertUserNotFoundOrYouDoNotHavePermission ();
break;
}
}
/*****************************************************************************/
/************************ Confirm another user's ID **************************/
/*****************************************************************************/
void ID_ConfirmOtherUsrID (void)
{
extern const char *Txt_ID_X_had_already_been_confirmed;
extern const char *Txt_The_ID_X_has_been_confirmed;
char UsrID[ID_MAX_BYTES_USR_ID + 1];
Usr_Can_t ICanConfirm;
bool Found;
unsigned NumID;
unsigned NumIDFound = 0; // Initialized to avoid warning
/***** Get where we came from *****/
Gbl.Action.Original = Act_GetActionFromActCod (ParCod_GetPar (ParCod_OrgAct));
/***** Get other user's code from form and get user's data *****/
ICanConfirm = Usr_CAN_NOT;
if (Usr_GetParOtherUsrCodEncryptedAndGetUsrData ())
if (Usr_ItsMe (Gbl.Usrs.Other.UsrDat.UsrCod) == Usr_OTHER) // Not me
{
/* If user is a student in current course,
check if he/she has accepted */
if (Gbl.Hierarchy.Level == Hie_CRS)
if (Gbl.Usrs.Other.UsrDat.Roles.InCurrentCrs == Rol_STD)
Gbl.Usrs.Other.UsrDat.Accepted = Enr_CheckIfUsrHasAcceptedInCurrentCrs (&Gbl.Usrs.Other.UsrDat);
if (ID_ICanSeeOtherUsrIDs (&Gbl.Usrs.Other.UsrDat) == Usr_CAN)
ICanConfirm = Usr_CAN;
}
switch (ICanConfirm)
{
case Usr_CAN:
/***** Get user's ID from form *****/
Par_GetParText ("UsrID",UsrID,ID_MAX_BYTES_USR_ID);
// Users' IDs are always stored internally in capitals and without leading zeros
Str_RemoveLeadingZeros (UsrID);
Str_ConvertToUpperText (UsrID);
for (NumID = 0, Found = false;
NumID < Gbl.Usrs.Other.UsrDat.IDs.Num && !Found;
NumID++)
if (!strcasecmp (UsrID,Gbl.Usrs.Other.UsrDat.IDs.List[NumID].ID))
{
Found = true;
NumIDFound = NumID;
}
if (Found) // Found
{
if (Gbl.Usrs.Other.UsrDat.IDs.List[NumIDFound].Confirmed)
/***** ID found and already confirmed *****/
Ale_CreateAlert (Ale_INFO,ID_ID_SECTION_ID,
Txt_ID_X_had_already_been_confirmed,
Gbl.Usrs.Other.UsrDat.IDs.List[NumIDFound].ID);
else
{
/***** Mark this ID as confirmed *****/
ID_DB_ConfirmUsrID (Gbl.Usrs.Other.UsrDat.UsrCod,
Gbl.Usrs.Other.UsrDat.IDs.List[NumIDFound].ID);
Gbl.Usrs.Other.UsrDat.IDs.List[NumIDFound].Confirmed = true;
/***** Write success message *****/
Ale_CreateAlert (Ale_SUCCESS,ID_ID_SECTION_ID,
Txt_The_ID_X_has_been_confirmed,
Gbl.Usrs.Other.UsrDat.IDs.List[NumIDFound].ID);
}
}
else // User's ID not found
Ale_CreateAlertUserNotFoundOrYouDoNotHavePermission ();
break;
case Usr_CAN_NOT:
default:
Ale_CreateAlertUserNotFoundOrYouDoNotHavePermission ();
break;
}
/***** Show one or multiple records *****/
switch (Gbl.Action.Original)
{
case ActSeeRecSevGst:
/* Show multiple records of guests again (including the updated one) */
Rec_ListRecordsGstsShow ();
break;
case ActSeeRecSevStd:
/* Show multiple records of students again (including the updated one) */
Rec_ListRecordsStdsShow ();
break;
case ActSeeRecSevTch:
/* Show multiple records of teachers again (including the updated one) */
Rec_ListRecordsTchsShow ();
break;
default:
/* Show optional alert */
Ale_ShowAlerts (NULL);
/* Show only the updated record of this user */
Rec_ShowSharedUsrRecord (Rec_SHA_RECORD_LIST,
&Gbl.Usrs.Other.UsrDat,NULL);