CrimsonCare
CrimsonCare is a C project designed to provide a robust solution for blood management.
 
Loading...
Searching...
No Matches
admin_manager.c
Go to the documentation of this file.
1
31#include "../include/misc.h"
32
38
54void saveAdminCredentials(void) {
55 errno = 0;
56 FILE* file = fopen("resources/db/admin_credentials.dat", "wb");
57 if (!file) {
58 if (errno != ENOENT) {
59 printf("Error opening admin credentials file: %s\n", strerror(errno));
60 return;
61 }
62 }
63
64 Admin* temp = adminHead;
65 while (temp != NULL) {
66 if (fwrite(temp, sizeof(Admin), 1, file)) {
67 temp = temp->next;
68 } else {
69 printf("Error writing admin credentials: %s\n", strerror(errno));
70 freeAdmin();
71 fclose(file);
72 return;
73 }
74 }
75 fclose(file);
76}
77
96void loadAdminCredentials(void) {
97 errno = 0;
98 FILE* file = fopen("resources/db/admin_credentials.dat", "rb");
99 if (!file) {
100 if (errno == ENOENT) {
101 Admin* newAdmin = (Admin*)malloc(sizeof(Admin));
102 if (newAdmin) {
103 strcpy(newAdmin->username, "admin");
104 strcpy(newAdmin->password, "1234");
105 newAdmin->next = NULL;
106 adminHead = newAdmin;
107 saveAdminCredentials();
108 } else {
109 printf("Error allocating memory for admin: %s\n", strerror(errno));
110 }
111 } else {
112 printf("Error opening admin credentials file: %s\n", strerror(errno));
113 }
114 return;
115 }
116
117 Admin tempAdmin;
118 while (fread(&tempAdmin, sizeof(Admin), 1, file)) {
119 Admin* newAdmin = (Admin*)malloc(sizeof(Admin));
120 if (newAdmin) {
121 *newAdmin = tempAdmin;
122 newAdmin->next = adminHead;
123 adminHead = newAdmin;
124 } else {
125 printf("Error allocating memory for admin: %s\n", strerror(errno));
126 freeAdmin();
127 fclose(file);
128 return;
129 }
130 }
131 fclose(file);
132}
133
151bool adminExists(const char* username) {
152 if (strcmp(username, "") == 0) {
153 printf("Error: Admin username cannot be empty.\n");
154 return false;
155 }
156
157 if (!checkUsername(username)) {
158 printf("Error: Invalid username. Username can only contain lowercase letters and digits.\n");
159 return false;
160 }
161
162 Admin* temp = adminHead;
163 while (temp != NULL) {
164 if (strcmp(temp->username, username) == 0) {
165 return true;
166 }
167 temp = temp->next;
168 }
169 return false;
170}
171
190bool validateAdmin(const char* username, const char* password) {
191 if (strcmp(username, "") == 0 || strcmp(password, "") == 0) {
192 printf("Error: Admin credentials cannot be empty.\n");
193 return false;
194 }
195
196 if (!checkUsername(username)) {
197 printf("Error: Invalid username. Username can only contain lowercase letters and digits.\n");
198 return false;
199 }
200
201 Admin* temp = adminHead;
202 while (temp != NULL) {
203 if (strcmp(username, temp->username) == 0 && strcmp(password, temp->password) == 0) {
204 return true;
205 }
206 temp = temp->next;
207 }
208 return false;
209}
210
240bool addAdmin(const char* username, const char* password, const char* currentAdminUsername, const char* currentAdminPassword) {
241 if (strcmp(currentAdminUsername, "") == 0 || strcmp(currentAdminPassword, "") == 0) {
242 printf("Error: Current admin credentials cannot be empty.\n");
243 return false;
244 }
245
246 if (!checkUsername(currentAdminUsername) || !checkUsername(username)) {
247 printf("Error: Invalid username. Username can only contain lowercase letters and digits.\n");
248 return false;
249 }
250
251 if (!validateAdmin(currentAdminUsername, currentAdminPassword)) {
252 printf("Error: Invalid current admin credentials.\n");
253 return false;
254 }
255
256 if (adminExists(username)) {
257 printf("Error: Admin already exists.\n");
258 return false;
259 }
260
261 if (strcmp(username, "") == 0 || strcmp(password, "") == 0) {
262 printf("Error: Admin credentials cannot be empty.\n");
263 return false;
264 }
265
266 Admin* newAdmin = (Admin*)malloc(sizeof(Admin));
267 if (!newAdmin) {
268 printf("Error allocating memory for admin: %s\n", strerror(errno));
269 return false;
270 }
271 strncpy(newAdmin->username, username, sizeof(newAdmin->username) - 1);
272 newAdmin->username[sizeof(newAdmin->username) - 1] = '\0';
273 strncpy(newAdmin->password, password, sizeof(newAdmin->password) - 1);
274 newAdmin->password[sizeof(newAdmin->password) - 1] = '\0';
275 newAdmin->next = adminHead;
276 adminHead = newAdmin;
277
278 saveAdminCredentials();
279 return true;
280}
281
306bool deleteAdmin(const char* username, const char* currentAdminUsername, const char* currentAdminPassword) {
307 if (strcmp(currentAdminUsername, "") == 0 || strcmp(currentAdminPassword, "") == 0) {
308 printf("Error: Current admin credentials cannot be empty.\n");
309 return false;
310 }
311
312 if (!checkUsername(currentAdminUsername) || !checkUsername(username)) {
313 printf("Error: Invalid username. Username can only contain lowercase letters and digits.\n");
314 return false;
315 }
316
317 if (!validateAdmin(currentAdminUsername, currentAdminPassword)) {
318 printf("Error: Invalid current admin credentials.\n");
319 return false;
320 }
321
322 if (!adminExists(username)) {
323 printf("Error: Admin does not exist.\n");
324 return false;
325 }
326
327 if (strcmp(username, "") == 0) {
328 printf("Error: Admin username cannot be empty.\n");
329 return false;
330 }
331
332 if (strcmp(username, currentAdminUsername) == 0) {
333 printf("Error: Cannot delete current admin.\n");
334 return false;
335 }
336
337 Admin* temp = adminHead;
338 Admin* prev = NULL;
339
340 while (temp != NULL) {
341 if (strcmp(temp->username, username) == 0) {
342 if (prev == NULL) {
343 adminHead = temp->next;
344 } else {
345 prev->next = temp->next;
346 }
347 free(temp);
348 saveAdminCredentials();
349 return true;
350 }
351 prev = temp;
352 temp = temp->next;
353 }
354 return false;
355}
356
380bool changeAdminPassword(const char* username, const char* oldPassword, const char* newPassword) {
381 if (strcmp(username, "") == 0 || strcmp(oldPassword, "") == 0) {
382 printf("Error: Username or old password cannot be empty.\n");
383 return false;
384 }
385
386 if (!checkUsername(username)) {
387 printf("Error: Invalid username. Username can only contain lowercase letters and digits.\n");
388 return false;
389 }
390
391 if (!validateAdmin(username, oldPassword)) {
392 printf("Error: Invalid password.\n");
393 return false;
394 }
395
396 if (strcmp(newPassword, "") == 0) {
397 printf("Error: New password cannot be empty.\n");
398 return false;
399 }
400
401 Admin* temp = adminHead;
402 while (temp != NULL) {
403 if (strcmp(username, temp->username) == 0 && strcmp(oldPassword, temp->password) == 0) {
404 strncpy(temp->password, newPassword, sizeof(temp->password) - 1);
405 temp->password[sizeof(temp->password) - 1] = '\0';
406 saveAdminCredentials();
407 return true;
408 }
409 temp = temp->next;
410 }
411 return false;
412}
413
422void displayAdmin(void) {
423 Admin* temp = adminHead;
424 printf("\nRegistered Admins:\n");
425 while (temp != NULL) {
426 printf("\tUsername: %s\n", temp->username);
427 temp = temp->next;
428 if (temp != NULL) {
429 printf("\t----------------------------------\n");
430 }
431 }
432}
433
441void freeAdmin(void) {
442 Admin* current = adminHead;
443 while (current != NULL) {
444 Admin* temp = current;
445 current = current->next;
446 free(temp);
447 }
448 adminHead = NULL;
449}
Admin * adminHead
Admin head pointer.
Admin manager header file.
Misc header file.
Admin structure.
char username[MAX_USERNAME_LENGTH]
struct Admin * next
char password[MAX_PASSWORD_LENGTH]