Skip to content
Snippets Groups Projects
Commit 04e0a1d1 authored by Ondřej Trojan's avatar Ondřej Trojan
Browse files

users integration

parent 92db0354
Branches layout
No related tags found
No related merge requests found
package com.museum.projection.controller;
import com.museum.projection.dto.UserDto;
import com.museum.projection.service.ModelViewService;
import com.museum.projection.service.ResponseService;
import com.museum.projection.service.UserService;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
......@@ -9,6 +11,7 @@ import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.security.Principal;
import java.util.List;
@Controller
......@@ -18,12 +21,14 @@ public class PageController {
public ResponseService responseService;
@Resource()
public ModelViewService modelViewService;
@Resource()
public UserService userService;
@GetMapping
public String Index(Authentication authentication, Principal principal, Model model) {
if (authentication != null) {
modelViewService.populateHeader(principal, model);
model.addAttribute("activeTab","main");
model.addAttribute("activeTab", "main");
return "main";
}
return "anonymous";
......@@ -35,8 +40,7 @@ public class PageController {
}
@GetMapping("/logout")
public String Logout(Authentication authentication)
{
public String Logout(Authentication authentication) {
authentication.setAuthenticated(false);
return "anonymous";
}
......@@ -45,30 +49,31 @@ public class PageController {
// Navigation
@GetMapping("/control")
public String Control(Principal principal, Model model){
public String Control(Principal principal, Model model) {
modelViewService.populateHeader(principal, model);
model.addAttribute("activeTab","control");
model.addAttribute("activeTab", "control");
return "control";
}
@GetMapping("/documentation")
public String Documentation(Principal principal, Model model){
public String Documentation(Principal principal, Model model) {
modelViewService.populateHeader(principal, model);
model.addAttribute("activeTab","documentation");
model.addAttribute("activeTab", "documentation");
return "documentation";
}
@GetMapping("/main")
public String Main(Principal principal,Model model){
public String Main(Principal principal, Model model) {
modelViewService.populateHeader(principal, model);
model.addAttribute("activeTab","main");
model.addAttribute("activeTab", "main");
return "main";
}
@GetMapping("/users")
public String Users(Principal principal,Model model){
public String Users(Principal principal, Model model) {
modelViewService.populateHeader(principal, model);
model.addAttribute("activeTab","users");
model.addAttribute("activeTab", "users");
model.addAttribute("users",userService.getAllUsers());
return "users";
}
......
package com.museum.projection.dao;
import com.museum.projection.model.Account;
import com.museum.projection.security.ApplicationUser;
import java.util.List;
import java.util.Optional;
public interface ApplicationUserDao {
Optional<ApplicationUser> selectApplicationUserByUsername(String username);
List<Account> getAllAccount();
}
package com.museum.projection.dao;
import com.google.common.collect.Lists;
import com.museum.projection.model.Account;
import com.museum.projection.security.ApplicationUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
......@@ -30,6 +31,11 @@ public class FakeApplicationUserDaoService implements ApplicationUserDao {
.findFirst();
}
@Override
public List<Account> getAllAccount() {
return List.of();
}
private List<ApplicationUser> getApplicationUsers() {
List<ApplicationUser> applicationUsers = Lists.newArrayList(
new ApplicationUser(
......
......@@ -6,16 +6,12 @@ import com.museum.projection.security.ApplicationUser;
import com.museum.projection.security.ApplicationUserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Repository;
import java.util.*;
import java.util.stream.Collectors;
import static com.museum.projection.security.ApplicationUserRole.*;
@Repository("postgres")
public class PostgresApplicationUserDaoService implements ApplicationUserDao {
......@@ -37,6 +33,11 @@ public class PostgresApplicationUserDaoService implements ApplicationUserDao {
.findFirst();
}
@Override
public List<Account> getAllAccount() {
return getAccounts();
}
private List<ApplicationUser> getApplicationUsers() {
List<ApplicationUser> applicationUsers = getAccounts().stream().map(account -> new ApplicationUser(
Arrays.stream(account.getRoles().split(",")).map(role -> ApplicationUserRole.valueOf(role).getGrantedAuthorities()).findFirst().orElse(Collections.emptySet()),
......@@ -50,12 +51,13 @@ public class PostgresApplicationUserDaoService implements ApplicationUserDao {
}
private List<Account> getAccounts() {
final String sql = "SELECT id, username, password, roles FROM account";
final String sql = "SELECT id, username, password, roles, active FROM account";
List<Account> accounts = jdbcTemplate.query(sql, (resultSet, i) -> {
return new Account(UUID.fromString(resultSet.getString("id")),
resultSet.getString("username"),
resultSet.getString("password"),
resultSet.getString("roles"));
resultSet.getString("roles"),
resultSet.getString("active"));
});
return accounts;
}
......
package com.museum.projection.dto;
public class UserDto {
private String id;
private String name;
private String isEnabled;
private String roles;
public UserDto(String id, String name , String isEnabled, String roles){
this.id=id;
this.name=name;
this.isEnabled=isEnabled;
this.roles=roles;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIsEnabled() {
return isEnabled;
}
public void setIsEnabled(String isEnabled) {
this.isEnabled = isEnabled;
}
public String getRoles() {
return roles;
}
public void setRoles(String roles) {
this.roles = roles;
}
}
......@@ -18,11 +18,15 @@ public class Account {
private final String roles;
public Account(@JsonProperty("id") UUID id, @JsonProperty("username") String username, @JsonProperty("password") String password, @JsonProperty("roles") String roles) {
@NotBlank
private final String isEnabled;
public Account(@JsonProperty("id") UUID id, @JsonProperty("username") String username, @JsonProperty("password") String password, @JsonProperty("roles") String roles, @JsonProperty("active") String enabled) {
this.id = id;
this.username = username;
this.password = password;
this.roles = roles;
this.isEnabled = enabled;
}
public UUID getId() {
......@@ -41,4 +45,8 @@ public class Account {
public String getRoles() {
return roles;
}
public String getIsEnabled() {
return isEnabled;
}
}
package com.museum.projection.service;
import com.museum.projection.dao.ApplicationUserDao;
import com.museum.projection.dto.UserDto;
import com.museum.projection.security.ApplicationUser;
import com.museum.projection.security.ApplicationUserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class UserService {
private final ApplicationUserDao applicationUserDao;
@Autowired
public UserService(@Qualifier("postgres") ApplicationUserDao applicationUserDao) {
this.applicationUserDao = applicationUserDao;
}
public List<UserDto> getAllUsers() {
var test = applicationUserDao.getAllAccount();
List<UserDto> applicationUsers = applicationUserDao.getAllAccount().stream().map(account -> new UserDto(
String.valueOf(account.getId()),
account.getUsername(),
account.getIsEnabled().equals("t") ? "true" : "false",
account.getRoles()
)).collect(Collectors.toList());
return applicationUsers;
}
}
DROP TABLE IF EXISTS account;
CREATE TABLE account (
id UUID NOT NULL PRIMARY KEY,
CREATE TABLE account
(
id UUID NOT NULL PRIMARY KEY,
username VARCHAR(100) NOT NULL,
password VARCHAR(100) NOT NULL,
roles VARCHAR(100)
roles VARCHAR(100),
active BOOLEAN NOT NULL
);
CREATE EXTENSION "uuid-ossp";
INSERT INTO account (id,username,password,roles) VALUES (uuid_generate_v4(),'admin','$2a$10$2DAipSvgd75ir6BZ3a7NiOHRwi5sEWr9AP5yDO65034aZjZnc2f8e','ADMIN');
INSERT INTO account (id,username,password,roles) VALUES (uuid_generate_v4(),'user1','$2a$10$2DAipSvgd75ir6BZ3a7NiOHRwi5sEWr9AP5yDO65034aZjZnc2f8e','PRESENTATOR');
INSERT INTO account (id,username,password,roles) VALUES (uuid_generate_v4(),'user2','$2a$10$2DAipSvgd75ir6BZ3a7NiOHRwi5sEWr9AP5yDO65034aZjZnc2f8e','ADMIN,PRESENTATOR');
INSERT INTO account (id, username, password, roles, active)
VALUES (uuid_generate_v4(), 'admin', '$2a$10$2DAipSvgd75ir6BZ3a7NiOHRwi5sEWr9AP5yDO65034aZjZnc2f8e', 'ADMIN', TRUE);
INSERT INTO account (id, username, password, roles, active)
VALUES (uuid_generate_v4(), 'user1', '$2a$10$2DAipSvgd75ir6BZ3a7NiOHRwi5sEWr9AP5yDO65034aZjZnc2f8e', 'PRESENTATOR', TRUE);
INSERT INTO account (id, username, password, roles, active)
VALUES (uuid_generate_v4(), 'user2', '$2a$10$2DAipSvgd75ir6BZ3a7NiOHRwi5sEWr9AP5yDO65034aZjZnc2f8e',
'ADMIN,PRESENTATOR', TRUE);
......@@ -4,12 +4,35 @@
>
<head>
<title>Users</title>
<!-- <link rel="stylesheet" th:href="@{/css/newspaceship.css}" type="text/css"/>-->
<!-- <link rel="stylesheet" th:href="@{/css/newspaceship.css}" type="text/css"/>-->
</head>
<body>
<div layout:fragment="content">
<h1>Users</h1>
<table class="table">
<thead>
<tr>
<th scope="col">Id</th>
<th scope="col">Name</th>
<th scope="col">Roles</th>
<th scope="col">Is active</th>
</tr>
</thead>
<tbody>
<tr th:if="${users.isEmpty()}">
<td scope="row"> No users to be displayed</td>
</tr>
<tr th:each="user : ${users}">
<td scope="row"><span th:text="${user.id}"> Id </span></td>
<td><span th:text="${user.name}"> name </span></td>
<td><span th:text="${user.roles}"> Id </span></td>
<td><span th:text="${user.isEnabled}"> name </span></td>
</tr>
</tbody>
</table>
</div>
</body>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment