1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/main/kotlin/de/uapcore/lightpit/servlet/UsersServlet.kt Fri Apr 02 11:59:14 2021 +0200 1.3 @@ -0,0 +1,112 @@ 1.4 +/* 1.5 + * Copyright 2021 Mike Becker. All rights reserved. 1.6 + * 1.7 + * Redistribution and use in source and binary forms, with or without 1.8 + * modification, are permitted provided that the following conditions are met: 1.9 + * 1.10 + * 1. Redistributions of source code must retain the above copyright 1.11 + * notice, this list of conditions and the following disclaimer. 1.12 + * 1.13 + * 2. Redistributions in binary form must reproduce the above copyright 1.14 + * notice, this list of conditions and the following disclaimer in the 1.15 + * documentation and/or other materials provided with the distribution. 1.16 + * 1.17 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 1.18 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1.19 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 1.20 + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 1.21 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1.22 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 1.23 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 1.24 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 1.25 + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.26 + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.27 + */ 1.28 + 1.29 +package de.uapcore.lightpit.servlet 1.30 + 1.31 +import de.uapcore.lightpit.AbstractServlet 1.32 +import de.uapcore.lightpit.HttpRequest 1.33 +import de.uapcore.lightpit.LoggingTrait 1.34 +import de.uapcore.lightpit.dao.DataAccessObject 1.35 +import de.uapcore.lightpit.entities.User 1.36 +import de.uapcore.lightpit.logger 1.37 +import de.uapcore.lightpit.viewmodel.UserEditView 1.38 +import de.uapcore.lightpit.viewmodel.UsersView 1.39 +import javax.servlet.annotation.WebServlet 1.40 + 1.41 +@WebServlet(urlPatterns = ["/users/*"]) 1.42 +class UsersServlet : AbstractServlet(), LoggingTrait { 1.43 + 1.44 + init { 1.45 + get("/", this::index) 1.46 + get("/-/create", this::create) 1.47 + get("/%userid/edit", this::edit) 1.48 + post("/-/commit", this::commit) 1.49 + } 1.50 + 1.51 + private val list = "users" 1.52 + private val form = "user-form" 1.53 + 1.54 + fun index(http: HttpRequest, dao: DataAccessObject) { 1.55 + with(http) { 1.56 + view = UsersView(dao.listUsers()) 1.57 + render(list) 1.58 + } 1.59 + } 1.60 + 1.61 + fun create(http: HttpRequest, dao: DataAccessObject) { 1.62 + with(http) { 1.63 + view = UserEditView(User(-1)) 1.64 + render(form) 1.65 + } 1.66 + } 1.67 + 1.68 + fun edit(http: HttpRequest, dao: DataAccessObject) { 1.69 + val id = http.pathParams["userid"]?.toIntOrNull() 1.70 + if (id == null) { 1.71 + http.response.sendError(404) 1.72 + } else { 1.73 + val user = dao.findUser(id) 1.74 + if (user == null) { 1.75 + http.response.sendError(404) 1.76 + } else { 1.77 + with(http) { 1.78 + view = UserEditView(user) 1.79 + render(form) 1.80 + } 1.81 + } 1.82 + } 1.83 + } 1.84 + 1.85 + fun commit(http: HttpRequest, dao: DataAccessObject) { 1.86 + val id = http.param("userid")?.toIntOrNull() 1.87 + if (id == null) { 1.88 + http.response.sendError(400) 1.89 + return 1.90 + } 1.91 + 1.92 + val user = User(id) 1.93 + with(user) { 1.94 + username = http.param("username") ?: "" 1.95 + givenname = http.param("givenname") 1.96 + lastname = http.param("lastname") 1.97 + mail = http.param("mail") 1.98 + } 1.99 + 1.100 + if (dao.findUserByName(user.username) != null) { 1.101 + with(http) { 1.102 + view = UserEditView(user).apply { errorText = "validation.username.unique" } 1.103 + } 1.104 + } 1.105 + 1.106 + if (user.id > 0) { 1.107 + logger().info("Update user ${user.username} with id ${user.id}.") 1.108 + dao.updateUser(user) 1.109 + } else { 1.110 + logger().info("Insert user ${user.username}.") 1.111 + dao.insertUser(user) 1.112 + } 1.113 + http.renderCommit("users/") 1.114 + } 1.115 +} 1.116 \ No newline at end of file