edit_profile.lua 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. local respond_to = require("lapis.application").respond_to
  2. local csrf_tool = require("utils.csrf_tool")
  3. local capture_errors = require("lapis.application").capture_errors
  4. local object = require("controllers.userController")
  5. return respond_to({
  6. GET = function(self)
  7. if self.session.user then
  8. self.head_title = "Editar perfil"
  9. -- Genera un token contra ataques CSRF con tiempo de expiración.
  10. self.csrf_token = csrf_tool.generate_token(self)
  11. -- Obtiene datos que únicamente un usuario puede modificar.
  12. self.profile = object:userProfile(self.session.user.username)
  13. if self.session.message then
  14. self.message = self.session.message
  15. self.session.message = nil
  16. end
  17. -- Formatos de imagen permitidos en el formulario.
  18. local image_formats = require("utils.website_config").image_formats
  19. self.max_image_size = require("utils.website_config").max_image_size
  20. self.valid_image_formats = ""
  21. for index,format in ipairs(image_formats) do
  22. self.valid_image_formats = self.valid_image_formats.."image/"..format..","
  23. end
  24. -- Elimina la "," del final.
  25. self.valid_image_formats = string.sub(self.valid_image_formats,1,-2)
  26. return { render = "pages.edit_profile" }
  27. else
  28. return { redirect_to = self:url_for("index") }
  29. end
  30. end,
  31. POST = capture_errors({
  32. -- Captura y muestra fallos (excepciones) dentro de una plantilla.
  33. on_error = require('utils.errors'),
  34. function(self)
  35. if self.session.user then
  36. -- Autentifica y valida el tiempo de expiración del token.
  37. csrf_tool.validation_token(self)
  38. -- Selecciona los campos de un formulario que solo son necesarios y
  39. -- elimina espacios sobrantes de inicio y fin.
  40. local inputs_form = { "name", "username", "password", "password_confirm", "description" }
  41. local trim = require("lapis.util").trim
  42. local data = {}
  43. for index,input in ipairs(inputs_form) do
  44. data[input] = trim(self.params[input])
  45. end
  46. -- Valida los campos de un formulario.
  47. local validate = require("utils.validate")
  48. if self.params.avatar then
  49. if self.params.avatar.content ~= "" then
  50. data.avatar = self.params.avatar
  51. validate.assert_valid(data, {{ "avatar", is_file = true, is_image = true }})
  52. end
  53. end
  54. if self.params.background then
  55. if self.params.background.content ~= "" then
  56. data.background = self.params.background
  57. validate.assert_valid(data, {{ "background", is_file = true, is_image = true }})
  58. end
  59. end
  60. validate.assert_valid(data, {
  61. { "description", optional = true, min_length = 1, max_length = 255, type = "string" },
  62. { "name", optional = true, min_length = 5, max_length = 255, type = "string" },
  63. { "username", optional = true, min_length = 1, max_length = 255, not_spaces = true, is_username = true, type = "string" },
  64. { "password", optional = true, min_length = 6, max_length = 255, not_spaces = true, type = "string" },
  65. { "password_confirm", equals = data.password }
  66. })
  67. -- Edita los datos de un perfil de usuario.
  68. self.session.message = object:userEdit(self.session.user, data)
  69. return { redirect_to = self:url_for("edit_profile") }
  70. else
  71. return { redirect_to = self:url_for("index") }
  72. end
  73. end
  74. })
  75. })