From 06c15c2b3cc5e89bde7755a0acdb6df177f6f39c Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Sun, 24 Nov 2013 21:54:01 +0000 Subject: [PATCH] basic user update functionality --- Makefile.am | 1 + assets/html/_menu.html | 1 + assets/html/_myaccount.html | 27 ++++++++ assets/html/layout.html | 21 ++++++ assets/js/application.js | 12 ++-- assets/js/init.js | 13 ++++ assets/js/menu.js | 28 ++++++-- assets/style/common.css | 11 +++ assets/style/taskrambler.css | 10 +-- include/application/application.h | 1 + src/application/Makefile.am | 7 +- .../controller/_get_credential_from_args.c | 63 +++++++++++++++++ .../controller/_get_user_from_args.c | 54 +++++++++++++++ .../controller/_process_user_create_args.c | 42 +++--------- .../controller/_update_user_from_args.c | 60 +++++++++++++++++ src/application/controller/user/update.c | 56 ++++++++++++++++ src/application/update_user.c | 67 +++++++++++++++++++ src/http/worker/process.c | 4 +- src/router/route.c | 1 + 19 files changed, 429 insertions(+), 50 deletions(-) create mode 100644 assets/html/_myaccount.html create mode 100644 src/application/controller/_get_credential_from_args.c create mode 100644 src/application/controller/_get_user_from_args.c create mode 100644 src/application/controller/_update_user_from_args.c create mode 100644 src/application/controller/user/update.c create mode 100644 src/application/update_user.c diff --git a/Makefile.am b/Makefile.am index 228e58c..88513cb 100644 --- a/Makefile.am +++ b/Makefile.am @@ -24,6 +24,7 @@ nobase_trdata_DATA = assets/html/example.html \ assets/html/layout.html \ assets/html/_documentation.html \ assets/html/_download.html \ + assets/html/_myaccount.html \ assets/image/waldschrat.jpg \ assets/image/fav128.png \ assets/image/fav16.png \ diff --git a/assets/html/_menu.html b/assets/html/_menu.html index 833209a..9580214 100644 --- a/assets/html/_menu.html +++ b/assets/html/_menu.html @@ -5,6 +5,7 @@
  • download
  • signup
  • login
  • +
  • my account
  • logout
  • diff --git a/assets/html/_myaccount.html b/assets/html/_myaccount.html new file mode 100644 index 0000000..b5f35ab --- /dev/null +++ b/assets/html/_myaccount.html @@ -0,0 +1,27 @@ +

    My account

    +
    +
    +
    + +
    +
    +
    + +
    +
    + +
    +
    +
    +
    +
    +
    +
    + +
    + + + diff --git a/assets/html/layout.html b/assets/html/layout.html index 1ba2196..c54ee46 100644 --- a/assets/html/layout.html +++ b/assets/html/layout.html @@ -63,6 +63,27 @@
    +
    + + + + + + + + + + + + + + + + +
    +
    +
    +
    diff --git a/assets/js/application.js b/assets/js/application.js index a7a16d8..12a895f 100644 --- a/assets/js/application.js +++ b/assets/js/application.js @@ -25,20 +25,24 @@ Application.prototype.logout = function() { } Application.prototype.login = function(ev) { - this._sendForm("#login", "/authenticate/", ev); + this._sendForm("#login", "/authenticate/", "POST", ev); } Application.prototype.signup = function(ev) { - this._sendForm("#signup", "/signup/", ev); + this._sendForm("#signup", "/signup/", "POST", ev); +} + +Application.prototype.userupdate = function(ev) { + this._sendForm("#myaccount", "/user/", "PUT", ev); } -Application.prototype._sendForm = function(id, url, ev) { +Application.prototype._sendForm = function(id, url, method, ev) { ev.preventDefault(); $.ajax({ dataType : "json", url : url, - type : "POST", + type : method, data : $(id + " form").serialize(), success : $.proxy(this._formSuccess, this, id), complete : $.proxy(this.session.update, this.session) diff --git a/assets/js/init.js b/assets/js/init.js index f63fe56..b1ce3ab 100644 --- a/assets/js/init.js +++ b/assets/js/init.js @@ -55,6 +55,19 @@ $(document).ready(function() { $("#signup form").submit($.proxy(application.signup, application)); }); + $("#myaccount").load("/_myaccount.html", function (){ + $(function() { + $("#myaccount-container").draggable(); + }); + + $("#myaccount-close").click(function (e) { + $("#myaccount-container").addClass("hide"); + }); + + $("#myaccount form").submit( + $.proxy(application.userupdate, application)); + }); + $("#footer").load("/_footer.html", function (){ $.getJSON( "/loc/", diff --git a/assets/js/menu.js b/assets/js/menu.js index 7336785..c1fc38c 100644 --- a/assets/js/menu.js +++ b/assets/js/menu.js @@ -1,10 +1,11 @@ -function Menu(menuSelector, application, user) +function Menu(menuSelector, application) { - this.application = application; + this.application = application; - this.signupSelector = menuSelector + " ul li.signup"; - this.loginSelector = menuSelector + " ul li.login"; - this.logoutSelector = menuSelector + " ul li.logout"; + this.signupSelector = menuSelector + " ul li.signup"; + this.loginSelector = menuSelector + " ul li.login"; + this.logoutSelector = menuSelector + " ul li.logout"; + this.myaccountSelector = menuSelector + " ul li.my_account"; this.menuElement = $(menuSelector); } @@ -14,14 +15,20 @@ Menu.prototype.init = function() { } Menu.prototype.update = function() { + $("#myaccount div.firstname input").val(this.application.user.firstname); + $("#myaccount div.surname input").val(this.application.user.surname); + $("#myaccount div.email input").val(this.application.user.email); + if (this.application.user.isEmpty()) { $(this.signupSelector).removeClass("hide"); $(this.loginSelector).removeClass("hide"); $(this.logoutSelector).addClass("hide"); + $(this.myaccountSelector).addClass("hide"); } else { $(this.signupSelector).addClass("hide"); $(this.loginSelector).addClass("hide"); $(this.logoutSelector).removeClass("hide"); + $(this.myaccountSelector).removeClass("hide"); } } @@ -51,6 +58,17 @@ Menu.prototype._menuActions = function() { $(this.logoutSelector) .click($.proxy(this.application.logout, this.application)); + + $(this.myaccountSelector).click(function(ev) { + if ($("#myaccount-container").hasClass("hide")) { + $("#myaccount-container").css("top", ev.pageY + 20); + $("#myaccount-container").css("left", ev.pageX - 100); + $("#myaccount-container").removeClass("hide"); + } else { + $("#myaccount-container").addClass("hide"); + } + }); + } // vim: set ts=4 sw=4: diff --git a/assets/style/common.css b/assets/style/common.css index 42663a9..15c4e90 100644 --- a/assets/style/common.css +++ b/assets/style/common.css @@ -12,6 +12,17 @@ div#randval { z-index: 20; } +div#myaccount-container { + -moz-box-shadow: 0 4px 8px rgba(0,0,0,0.5); + -webkit-box-shadow: 0 4px 8px rgba(0,0,0,0.5); + box-shadow: 0 4px 8px rgba(0,0,0,0.5); + padding: 0px; + position: fixed; + background-image: url(/image/rambler-bg.jpg); + border-radius: 5px; + z-index: 20; +} + div#signup-container { -moz-box-shadow: 0 4px 8px rgba(0,0,0,0.5); -webkit-box-shadow: 0 4px 8px rgba(0,0,0,0.5); diff --git a/assets/style/taskrambler.css b/assets/style/taskrambler.css index 834af72..d324dc6 100644 --- a/assets/style/taskrambler.css +++ b/assets/style/taskrambler.css @@ -261,7 +261,7 @@ div.border .br { vertical-align: center; } -#login label, #signup label { +#login label, #signup label, #myaccount label { font-family: old_newspaper; font-size: 15px; font-weight: bold; @@ -269,19 +269,19 @@ div.border .br { display: table-cell; } -#login input, #signup input { +#login input, #signup input, #myaccount input { display: table-cell; } -#login div, #signup div { +#login div, #signup div, #myaccount div { display: table-row; } -#login form, #signup form { +#login form, #signup form, #myaccount form { display: table; } -#login form hr, #signup form hr { +#login form hr, #signup form hr, #myaccount form hr { display: table-cell; } diff --git a/include/application/application.h b/include/application/application.h index 1520cc1..3a80c99 100644 --- a/include/application/application.h +++ b/include/application/application.h @@ -66,6 +66,7 @@ CLASS(Application) { int applicationLogin(Application, Credential, Session); void applicationLogout(Application, Session); Uuid applicationCreateUser(Application, Credential, User); +Uuid applicationUpdateUser(Application, User); User applicationGetUser(Application, Uuid); int applicationUpdatePassword(Application, Credential, User); diff --git a/src/application/Makefile.am b/src/application/Makefile.am index f0417b4..c95edc2 100644 --- a/src/application/Makefile.am +++ b/src/application/Makefile.am @@ -6,6 +6,7 @@ APPLICATION = application.c \ logout.c \ get_user.c \ create_user.c \ + update_user.c \ update_password.c \ session_start.c \ session_stop.c \ @@ -21,12 +22,16 @@ CONTROLLER = controller/authenticate/create.c \ controller/randval/read.c \ controller/sessinfo/read.c \ controller/user/create.c \ + controller/user/update.c \ controller/user/read.c \ controller/signup/create.c \ controller/version/read.c \ controller/loc/read.c \ controller/_validate_password_repeat.c \ - controller/_process_user_create_args.c + controller/_process_user_create_args.c \ + controller/_get_user_from_args.c \ + controller/_update_user_from_args.c \ + controller/_get_credential_from_args.c AM_CFLAGS += -I../../include/ diff --git a/src/application/controller/_get_credential_from_args.c b/src/application/controller/_get_credential_from_args.c new file mode 100644 index 0000000..fb4831d --- /dev/null +++ b/src/application/controller/_get_credential_from_args.c @@ -0,0 +1,63 @@ +/** + * \file + * + * \author Georg Hopp + * + * \copyright + * Copyright © 2013 Georg Hopp + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#define _GNU_SOURCE + +#include "hash.h" +#include "auth/credential.h" + +#include "utils/memory.h" +#include "commons.h" + +int _controllerValidatePasswordRepeat(char *, size_t, char *, size_t); + +Credential +_controllerGetCredentialFromArgs(Hash args) +{ + HashValue email = hashGet(args, CSTRA("email")); + HashValue password = hashGet(args, CSTRA("password")); + HashValue pwrepeat = hashGet(args, CSTRA("pwrepeat")); + + if ( + NULL == email || + NULL == password || + NULL == pwrepeat) + { + return FALSE; + } + + if (! _controllerValidatePasswordRepeat( + password->value, + password->nvalue, + pwrepeat->value, + pwrepeat->nvalue)) + { + return FALSE; + } + + return new(Credential, + CRED_PASSWORD, + (char *)(email->value), email->nvalue, + (char *)(password->value), password->nvalue); +} + +// vim: set ts=4 sw=4: diff --git a/src/application/controller/_get_user_from_args.c b/src/application/controller/_get_user_from_args.c new file mode 100644 index 0000000..16cd562 --- /dev/null +++ b/src/application/controller/_get_user_from_args.c @@ -0,0 +1,54 @@ +/** + * \file + * + * \author Georg Hopp + * + * \copyright + * Copyright © 2013 Georg Hopp + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#define _GNU_SOURCE + +#include "hash.h" +#include "user.h" + +#include "utils/memory.h" +#include "commons.h" + + +User +_controllerGetUserFromArgs(Hash args) +{ + HashValue email = hashGet(args, CSTRA("email")); + HashValue firstname = hashGet(args, CSTRA("firstname")); + HashValue surname = hashGet(args, CSTRA("surname")); + + if ( + NULL == email || + NULL == firstname || + NULL == surname) + { + return FALSE; + } + + return new(User, + (char *)(email->value), email->nvalue, + (char *)(email->value), email->nvalue, + (char *)(firstname->value), firstname->nvalue, + (char *)(surname->value), surname->nvalue); +} + +// vim: set ts=4 sw=4: diff --git a/src/application/controller/_process_user_create_args.c b/src/application/controller/_process_user_create_args.c index f662207..ee9448e 100644 --- a/src/application/controller/_process_user_create_args.c +++ b/src/application/controller/_process_user_create_args.c @@ -29,48 +29,22 @@ #include "utils/memory.h" #include "commons.h" -int _controllerValidatePasswordRepeat(char *, size_t, char *, size_t); - +User _controllerGetUserFromArgs(Hash args); +Credential _controllerGetCredentialFromArgs(Hash args); int _controllerProcessUserCreateArgs(Hash args, User * user, Credential * cred) { - HashValue email = hashGet(args, CSTRA("email")); - HashValue password = hashGet(args, CSTRA("password")); - HashValue pwrepeat = hashGet(args, CSTRA("pwrepeat")); - HashValue firstname = hashGet(args, CSTRA("firstname")); - HashValue surname = hashGet(args, CSTRA("surname")); - - if ( - NULL == email || - NULL == password || - NULL == pwrepeat || - NULL == firstname || - NULL == surname) - { - return FALSE; - } + *user = _controllerGetUserFromArgs(args); + *cred = _controllerGetCredentialFromArgs(args); + + if (NULL == *user || NULL == *cred) { + delete(*user); + delete(*cred); - if (! _controllerValidatePasswordRepeat( - password->value, - password->nvalue, - pwrepeat->value, - pwrepeat->nvalue)) - { return FALSE; } - *cred = new(Credential, - CRED_PASSWORD, - (char *)(email->value), email->nvalue, - (char *)(password->value), password->nvalue); - - *user = new(User, - (char *)(email->value), email->nvalue, - (char *)(email->value), email->nvalue, - (char *)(firstname->value), firstname->nvalue, - (char *)(surname->value), surname->nvalue); - return TRUE; } diff --git a/src/application/controller/_update_user_from_args.c b/src/application/controller/_update_user_from_args.c new file mode 100644 index 0000000..7fd6973 --- /dev/null +++ b/src/application/controller/_update_user_from_args.c @@ -0,0 +1,60 @@ +/** + * \file + * + * \author Georg Hopp + * + * \copyright + * Copyright © 2013 Georg Hopp + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#define _GNU_SOURCE + +#include "hash.h" +#include "user.h" + +#include "utils/memory.h" +#include "commons.h" + + +int +_controllerUpdateUserFromArgs(Hash args, User * user) +{ + HashValue email = hashGet(args, CSTRA("email")); + HashValue firstname = hashGet(args, CSTRA("firstname")); + HashValue surname = hashGet(args, CSTRA("surname")); + User new_user; + + if ( + NULL == email || + NULL == firstname || + NULL == surname) + { + return FALSE; + } + + new_user = new(User, + (char *)((*user)->username), *(*user)->nusername, + (char *)(email->value), email->nvalue, + (char *)(firstname->value), firstname->nvalue, + (char *)(surname->value), surname->nvalue); + + delete(*user); + *user = new_user; + + return TRUE; +} + +// vim: set ts=4 sw=4: diff --git a/src/application/controller/user/update.c b/src/application/controller/user/update.c new file mode 100644 index 0000000..2169252 --- /dev/null +++ b/src/application/controller/user/update.c @@ -0,0 +1,56 @@ +/** + * \file + * + * \author Georg Hopp + * + * \copyright + * Copyright © 2013 Georg Hopp + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#define _GNU_SOURCE + +#include "application/application.h" +#include "session.h" +#include "hash.h" +#include "user.h" + +#include "utils/memory.h" +#include "commons.h" + +char * controllerCurrentuserRead(Application, Session, Hash); +int _controllerUpdateUserFromArgs(Hash, User *); + +char * +controllerUserUpdate( + Application application, + Session session, + Hash args) +{ + if (! _controllerUpdateUserFromArgs(args, &(session->user))) { + return NULL; + } + + if (0 == uuidCompare( + uuidZero, + applicationUpdateUser(application, session->user))) + { + return NULL; + } + + return controllerCurrentuserRead(application, session, NULL); +} + +// vim: set ts=4 sw=4: diff --git a/src/application/update_user.c b/src/application/update_user.c new file mode 100644 index 0000000..d6f99d6 --- /dev/null +++ b/src/application/update_user.c @@ -0,0 +1,67 @@ +/** + * \file + * + * \author Georg Hopp + * + * \copyright + * Copyright © 2013 Georg Hopp + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#define _GNU_SOURCE + +#include +#include +#include + +#include "class.h" +#include "auth.h" +#include "user.h" +#include "uuid.h" +#include "storage/storage.h" +#include "application/application.h" + +#include "interface/serializable.h" +#include "interface/indexable.h" + +#include "utils/memory.h" +#include "commons.h" + +Uuid +applicationUpdateUser( + Application this, + User user) +{ + char * user_serialized; + size_t nuser_serialized; + Uuid index; + + index = indexUuid(user, this->user_namespace); + serialize(user, (unsigned char **)&user_serialized, &nuser_serialized); + + if (SPR_OK != storageUpdate( + this->users, + (char *)(index->uuid).value, + sizeof((index->uuid).value), + user_serialized, + nuser_serialized)) + { + return uuidZero; + } + + return index; +} + +// vim: set ts=4 sw=4: diff --git a/src/http/worker/process.c b/src/http/worker/process.c index c7093b1..c2c5a1f 100644 --- a/src/http/worker/process.c +++ b/src/http/worker/process.c @@ -77,7 +77,9 @@ httpWorkerProcess(HttpWorker this, Stream st) subjectNotify(this); if (NULL == this->current_response) { - if (0 == strcmp("POST", this->current_request->method)) { + if (0 == strcmp("POST", this->current_request->method) || + 0 == strcmp("PUT", this->current_request->method)) + { /* * we can't do post requests on our own... */ diff --git a/src/router/route.c b/src/router/route.c index ab207c9..0abe276 100644 --- a/src/router/route.c +++ b/src/router/route.c @@ -112,6 +112,7 @@ routerRoute( break; case HTTP_PUT: + args = request->post; strcpy(&(functionName[this->nprefix + ncommand]), "Update"); break;