timekeeper

My first (abandoned unfinished) web application for time tracking
git clone git://jacobedwards.org/timekeeper
Log | Files | Refs | README

account.c (2829B)


      1 #define const
      2 
      3 #include <string.h>
      4 
      5 #include "common.h"
      6 #include "pages.h"
      7 
      8 #define DeleteAccountStr "account"
      9 #define DeleteAuthTokensStr "logins"
     10 
     11 enum kcgi_err
     12 pageaccount(struct pagedata *pd)
     13 {
     14 	static char *css[] = { "css/main.css", NULL };
     15 	static struct pagetemplate template = {
     16 		"Account",
     17 		.css = css
     18 	};
     19 	char *k;
     20 
     21 	enum kcgi_err status;
     22 
     23 	if (!pd->user)
     24 		return tk_prompt_login(pd);
     25 
     26 	if (pd->req.fieldmap[KeyDelete]) {
     27 		k = pd->req.fieldmap[KeyDelete]->parsed.s;
     28 		if (strcmp(k, DeleteAccountStr) == 0) {
     29 			if (deleteuser(pd, pd->user->hash) == 0)
     30 				return redirect(pd, pd->pages[PageIndex], "Account deleted");
     31 			else
     32 				return errorpage(pd, KHTTP_500);
     33 		} else if (strcmp(k, DeleteAuthTokensStr) == 0) {
     34 			if (revoketokens(pd, pd->user->hash))
     35 				return errorpage(pd, KHTTP_500);
     36 			else
     37 				return redirect(pd, pd->pages[PageIndex], "Session revoked");
     38 		}
     39 	}
     40 
     41 	if ((status = tk_startpage(pd, &template, KHTTP_200)) != KCGI_OK ||
     42 	    (status = htmlwithin(pd, KELEM_H1, "Account")) != KCGI_OK ||
     43 	    (status = htmlwithin(pd, KELEM_H2, "Manage Sessions")) != KCGI_OK ||
     44 	/* Session Management */
     45 	    (status = khtml_elem(&pd->html, KELEM_FORM)) != KCGI_OK ||
     46 	    (status = khtml_elem(&pd->html, KELEM_LABEL)) != KCGI_OK ||
     47 	    (status = khtml_puts(&pd->html, "Revoke session keys: ")) != KCGI_OK ||
     48 	    (status = khtml_attr(&pd->html, KELEM_INPUT,
     49 		KATTR_TYPE, "hidden",
     50 		KATTR_NAME, pd->keys[KeyDelete].name,
     51 		KATTR_VALUE, DeleteAuthTokensStr,
     52 		KATTR__MAX)) != KCGI_OK ||
     53 	    (status = khtml_closeelem(&pd->html, 1)) != KCGI_OK ||
     54 	    (status = khtml_putc(&pd->html, ' ')) != KCGI_OK ||
     55 	    (status = khtml_attr(&pd->html, KELEM_INPUT,
     56 		KATTR_TYPE, "submit",
     57 		KATTR_VALUE, "Revoke",
     58 		KATTR__MAX)) != KCGI_OK ||
     59 	    (status = khtml_closeelem(&pd->html, 1)) != KCGI_OK ||
     60 	/* Account Deletion */
     61 	    (status = htmlwithin(pd, KELEM_H2, "Delete Account")) != KCGI_OK ||
     62 	    (status = htmlwithin(pd, KELEM_P, "Deleting an account is irreversible:"
     63 		" no account data can be restored after deletion.")) != KCGI_OK ||
     64 	    (status = khtml_elem(&pd->html, KELEM_FORM)) != KCGI_OK ||
     65 	    (status = khtml_elem(&pd->html, KELEM_LABEL)) != KCGI_OK ||
     66 	    (status = khtml_puts(&pd->html, "Confirm permanent deletion:")) != KCGI_OK ||
     67 	    (status = khtml_attr(&pd->html, KELEM_INPUT,
     68 		KATTR_TYPE, "checkbox",
     69 		KATTR_NAME, pd->keys[KeyDelete].name,
     70 		KATTR_VALUE, DeleteAccountStr,
     71 		KATTR_REQUIRED, "true",
     72 		KATTR__MAX)) != KCGI_OK ||
     73 	    (status = khtml_closeelem(&pd->html, 1)) != KCGI_OK ||
     74 	    (status = khtml_putc(&pd->html, ' ')) != KCGI_OK ||
     75 	    (status = khtml_attr(&pd->html, KELEM_INPUT,
     76 		KATTR_TYPE, "submit",
     77 		KATTR_VALUE, "Delete",
     78 		KATTR__MAX)) != KCGI_OK ||
     79 	    (status = khtml_closeelem(&pd->html, 1)) != KCGI_OK)
     80 		return status;
     81 
     82 	return endpage(pd, &template);
     83 }