|
| 1 | +/* |
| 2 | + * SteVe - SteckdosenVerwaltung - https://github.com/steve-community/steve |
| 3 | + * Copyright (C) 2013-2024 SteVe Community Team |
| 4 | + * All Rights Reserved. |
| 5 | + * |
| 6 | + * This program is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | +package de.rwth.idsg.steve.repository.impl; |
| 20 | + |
| 21 | +import de.rwth.idsg.steve.repository.WebUserRepository; |
| 22 | +import jooq.steve.db.tables.records.WebUserRecord; |
| 23 | +import lombok.RequiredArgsConstructor; |
| 24 | +import lombok.extern.slf4j.Slf4j; |
| 25 | +import org.jooq.DSLContext; |
| 26 | +import org.jooq.JSON; |
| 27 | +import org.springframework.stereotype.Repository; |
| 28 | + |
| 29 | +import static jooq.steve.db.Tables.WEB_USER; |
| 30 | +import static org.jooq.impl.DSL.condition; |
| 31 | +import static org.jooq.impl.DSL.count; |
| 32 | + |
| 33 | +/** |
| 34 | + * @author Sevket Goekay <[email protected]> |
| 35 | + * @since 10.08.2024 |
| 36 | + */ |
| 37 | +@Slf4j |
| 38 | +@Repository |
| 39 | +@RequiredArgsConstructor |
| 40 | +public class WebUserRepositoryImpl implements WebUserRepository { |
| 41 | + |
| 42 | + private final DSLContext ctx; |
| 43 | + |
| 44 | + @Override |
| 45 | + public void createUser(WebUserRecord user) { |
| 46 | + ctx.insertInto(WEB_USER) |
| 47 | + .set(WEB_USER.USERNAME, user.getUsername()) |
| 48 | + .set(WEB_USER.PASSWORD, user.getPassword()) |
| 49 | + .set(WEB_USER.ENABLED, user.getEnabled()) |
| 50 | + .set(WEB_USER.AUTHORITIES, user.getAuthorities()) |
| 51 | + .execute(); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void updateUser(WebUserRecord user) { |
| 56 | + ctx.update(WEB_USER) |
| 57 | + .set(WEB_USER.PASSWORD, user.getPassword()) |
| 58 | + .set(WEB_USER.ENABLED, user.getEnabled()) |
| 59 | + .set(WEB_USER.AUTHORITIES, user.getAuthorities()) |
| 60 | + .where(WEB_USER.USERNAME.eq(user.getUsername())) |
| 61 | + .execute(); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public void deleteUser(String username) { |
| 66 | + ctx.delete(WEB_USER) |
| 67 | + .where(WEB_USER.USERNAME.eq(username)) |
| 68 | + .execute(); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public void deleteUser(int webUserPk) { |
| 73 | + ctx.delete(WEB_USER) |
| 74 | + .where(WEB_USER.WEB_USER_PK.eq(webUserPk)) |
| 75 | + .execute(); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void changeStatusOfUser(String username, boolean enabled) { |
| 80 | + ctx.update(WEB_USER) |
| 81 | + .set(WEB_USER.ENABLED, enabled) |
| 82 | + .where(WEB_USER.USERNAME.eq(username)) |
| 83 | + .execute(); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public Integer getUserCountWithAuthority(String authority) { |
| 88 | + JSON authValue = JSON.json("\"" + authority + "\""); |
| 89 | + return ctx.selectCount() |
| 90 | + .from(WEB_USER) |
| 91 | + .where(condition("json_contains({0}, {1})", WEB_USER.AUTHORITIES, authValue)) |
| 92 | + .fetchOne(count()); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public void changePassword(String username, String newPassword) { |
| 97 | + ctx.update(WEB_USER) |
| 98 | + .set(WEB_USER.PASSWORD, newPassword) |
| 99 | + .where(WEB_USER.USERNAME.eq(username)) |
| 100 | + .execute(); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public boolean userExists(String username) { |
| 105 | + return ctx.selectOne() |
| 106 | + .from(WEB_USER) |
| 107 | + .where(WEB_USER.USERNAME.eq(username)) |
| 108 | + .fetchOptional() |
| 109 | + .isPresent(); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public WebUserRecord loadUserByUsername(String username) { |
| 114 | + return ctx.selectFrom(WEB_USER) |
| 115 | + .where(WEB_USER.USERNAME.eq(username)) |
| 116 | + .fetchOne(); |
| 117 | + } |
| 118 | +} |
0 commit comments