auth_controller.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (C) 2007 Martin Willi
  3. * HSR Hochschule fuer Technik Rapperswil
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. * for more details.
  14. */
  15. #include "auth_controller.h"
  16. #include "../manager.h"
  17. #include <library.h>
  18. typedef struct private_auth_controller_t private_auth_controller_t;
  19. /**
  20. * private data of the task manager
  21. */
  22. struct private_auth_controller_t {
  23. /**
  24. * public functions
  25. */
  26. auth_controller_t public;
  27. /**
  28. * manager instance
  29. */
  30. manager_t *manager;
  31. };
  32. static void login(private_auth_controller_t *this, fast_request_t *request)
  33. {
  34. request->set(request, "action", "check");
  35. request->set(request, "title", "Login");
  36. request->render(request, "templates/auth/login.cs");
  37. }
  38. static void check(private_auth_controller_t *this, fast_request_t *request)
  39. {
  40. char *username, *password;
  41. username = request->get_query_data(request, "username");
  42. password = request->get_query_data(request, "password");
  43. if (username && password &&
  44. this->manager->login(this->manager, username, password))
  45. {
  46. request->redirect(request, "ikesa/list");
  47. }
  48. else
  49. {
  50. request->redirect(request, "auth/login");
  51. }
  52. }
  53. static void logout(private_auth_controller_t *this, fast_request_t *request)
  54. {
  55. this->manager->logout(this->manager);
  56. request->redirect(request, "auth/login");
  57. }
  58. METHOD(fast_controller_t, get_name, char*,
  59. private_auth_controller_t *this)
  60. {
  61. return "auth";
  62. }
  63. METHOD(fast_controller_t, handle, void,
  64. private_auth_controller_t *this, fast_request_t *request, char *action,
  65. char *p2, char *p3, char *p4, char *p5)
  66. {
  67. if (action)
  68. {
  69. if (streq(action, "login"))
  70. {
  71. return login(this, request);
  72. }
  73. else if (streq(action, "check"))
  74. {
  75. return check(this, request);
  76. }
  77. else if (streq(action, "logout"))
  78. {
  79. return logout(this, request);
  80. }
  81. }
  82. request->redirect(request, "auth/login");
  83. }
  84. METHOD(fast_controller_t, destroy, void,
  85. private_auth_controller_t *this)
  86. {
  87. free(this);
  88. }
  89. /*
  90. * see header file
  91. */
  92. fast_controller_t *auth_controller_create(fast_context_t *context, void *param)
  93. {
  94. private_auth_controller_t *this;
  95. INIT(this,
  96. .public = {
  97. .controller = {
  98. .get_name = _get_name,
  99. .handle = _handle,
  100. .destroy = _destroy,
  101. },
  102. },
  103. .manager = (manager_t*)context,
  104. );
  105. return &this->public.controller;
  106. }