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

WIP mvc

parent d581ab85
No related branches found
No related tags found
No related merge requests found
Showing
with 164 additions and 63 deletions
package com.museum.projection.controller;
import com.museum.projection.security.ApplicationUserPermission;
import com.museum.projection.service.MainService;
import com.museum.projection.service.ResponseService;
import org.springframework.security.core.Authentication;
......@@ -20,15 +21,20 @@ public class PageController {
public MainService mainService;
@GetMapping
public String Index(Authentication authentication, Principal principal, Model model) {
public String Index(Authentication authentication, Model model) {
if (authentication != null) {
var details = authentication.getDetails();
mainService.populateForAdmin(principal, model);
mainService.populateForAdmin(authentication, model);
authentication.getAuthorities().contains(ApplicationUserPermission.USER_READ);
return "main";
}
return "index";
}
@GetMapping
public String Test(){
return "asd";
}
@GetMapping("/signin")
public String Login() {
return "login";
......
......@@ -2,9 +2,12 @@ package com.museum.projection.dao;
import com.museum.projection.security.ApplicationUser;
import java.util.List;
import java.util.Optional;
public interface ApplicationUserDao {
Optional<ApplicationUser> selectApplicationUserByUsername(String username);
List<ApplicationUser> getApplicationUsers();
}
......@@ -30,7 +30,8 @@ public class FakeApplicationUserDaoService implements ApplicationUserDao {
.findFirst();
}
private List<ApplicationUser> getApplicationUsers() {
@Override
public List<ApplicationUser> getApplicationUsers() {
List<ApplicationUser> applicationUsers = Lists.newArrayList(
new ApplicationUser(
STUDENT.getGrantedAuthorities(),
......@@ -51,7 +52,7 @@ public class FakeApplicationUserDaoService implements ApplicationUserDao {
true
),
new ApplicationUser(
ADMINTRAINEE.getGrantedAuthorities(),
PRESENTATOR.getGrantedAuthorities(),
passwordEncoder.encode("tom"),
"tom",
true,
......
......@@ -37,7 +37,8 @@ public class PostgresApplicationUserDaoService implements ApplicationUserDao {
.findFirst();
}
private List<ApplicationUser> getApplicationUsers() {
@Override
public 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()),
account.getPassword(),
......@@ -49,7 +50,7 @@ public class PostgresApplicationUserDaoService implements ApplicationUserDao {
return applicationUsers;
}
private List<Account> getAccounts() {
public List<Account> getAccounts() {
final String sql = "SELECT id, username, password, roles FROM account";
List<Account> accounts = jdbcTemplate.query(sql, (resultSet, i) -> {
return new Account(UUID.fromString(resultSet.getString("id")),
......
package com.museum.projection.dto;
public class User {
}
package com.museum.projection.security;
public enum ApplicationUserPermission {
STUDENT_READ("student:read"),
STUDENT_WRITE("student:write"),
USER_READ("user:read"),
USER_WRITE("user:write"),
COURSE_READ("course:read"),
COURSE_WRITE("course:write");
......
......@@ -11,8 +11,8 @@ import static com.museum.projection.security.ApplicationUserPermission.*;
public enum ApplicationUserRole {
STUDENT(Sets.newHashSet()),
ADMIN(Sets.newHashSet(COURSE_READ, COURSE_WRITE, STUDENT_READ, STUDENT_WRITE)),
ADMINTRAINEE(Sets.newHashSet(COURSE_READ, STUDENT_READ));
ADMIN(Sets.newHashSet(COURSE_READ, COURSE_WRITE, USER_READ, USER_WRITE)),
PRESENTATOR(Sets.newHashSet(COURSE_READ, USER_READ));
private final Set<ApplicationUserPermission> permissions;
......
package com.museum.projection.service;
import com.museum.projection.dao.ApplicationUserDao;
import com.museum.projection.security.ApplicationUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import java.util.List;
public class UserService {
private final ApplicationUserDao applicationUserDao;
@Autowired
public UserService(@Qualifier("postgres") ApplicationUserDao applicationUserDao) {
this.applicationUserDao = applicationUserDao;
}
public List<ApplicationUser> GetUsers(){
return applicationUserDao.getApplicationUsers();
}
}
......@@ -9,5 +9,5 @@ CREATE TABLE account (
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','STUDENT');
INSERT INTO account (id,username,password,roles) VALUES (uuid_generate_v4(),'user2','$2a$10$2DAipSvgd75ir6BZ3a7NiOHRwi5sEWr9AP5yDO65034aZjZnc2f8e','ADMIN,STUDENT');
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');
.user {
background-color: black;
}
a{
text-decoration: none;
color: white;
}
\ No newline at end of file
......@@ -14,50 +14,50 @@ var serverUrl = 'http://localhost:8080';
var apiVlcPrefix = '/api/vlc';
function getCookie(name) {
var value = "; " + document.cookie;
var parts = value.split("; " + name + "=");
if (parts.length == 2) return parts.pop().split(";").shift();
var value = "; " + document.cookie;
var parts = value.split("; " + name + "=");
if (parts.length == 2) return parts.pop().split(";").shift();
}
const sendHttpRequest = (method, url, data) => {
alertMessageBox.innerHTML = '';
const promise = new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open(method,url)
xhr.setRequestHeader('Authorization',getCookie("Authorization"));
console.log("data "+ data)
if(data){
xhr.setRequestHeader('Content-Type', 'application/json');
}
xhr.onload = () => {
if(xhr.status > 400){
if(xhr.status == 403){
handeUnauthorizedRequest();
}
reject(xhr.response)
} else {
resolve(xhr.response)
const xhr = new XMLHttpRequest();
xhr.open(method, url)
xhr.setRequestHeader('Authorization', getCookie("Authorization"));
console.log("data " + data)
if (data) {
xhr.setRequestHeader('Content-Type', 'application/json');
}
};
xhr.onerror = () => {
reject('Something went wrong check the backend log')
};
xhr.onload = () => {
if (xhr.status > 400) {
if (xhr.status == 403) {
handeUnauthorizedRequest();
}
reject(xhr.response)
} else {
resolve(xhr.response)
}
};
xhr.onerror = () => {
reject('Something went wrong check the backend log')
};
xhr.send(JSON.stringify(data));
xhr.send(JSON.stringify(data));
});
return promise;
}
function controlAdd () {
function controlAdd() {
addSpinner(this);
sendHttpRequest('POST', serverUrl + apiVlcPrefix+'/add', {
video: '1.mp4'
}).then(responseData =>{
sendHttpRequest('POST', serverUrl + apiVlcPrefix + '/add', {
video: '1.mp4'
}).then(responseData => {
handeResponseAlert(responseData);
removeSpinner(this);
}).catch(err => {
......@@ -65,9 +65,9 @@ function controlAdd () {
})
}
function controlPlay () {
function controlPlay() {
addSpinner(this);
sendHttpRequest('POST', serverUrl + apiVlcPrefix+'/play').then(responseData =>{
sendHttpRequest('POST', serverUrl + apiVlcPrefix + '/play').then(responseData => {
handeResponseAlert(responseData);
removeSpinner(this);
}).catch(err => {
......@@ -77,7 +77,7 @@ function controlPlay () {
function controlPause() {
addSpinner(this);
sendHttpRequest('POST', serverUrl + apiVlcPrefix+'/pause').then(responseData =>{
sendHttpRequest('POST', serverUrl + apiVlcPrefix + '/pause').then(responseData => {
handeResponseAlert(responseData);
removeSpinner(this);
}).catch(err => {
......@@ -85,14 +85,14 @@ function controlPause() {
})
}
function controlShutdown(btn,client) {
function controlShutdown(btn, client) {
addSpinner(btn);
console.log("shutdown",btn)
sendHttpRequest('POST', serverUrl + apiVlcPrefix+'/shutdown/?clientId='+client)
.then(responseData =>{
handeResponseAlert(responseData);
removeSpinner(btn);
}).catch(err => {
console.log("shutdown", btn)
sendHttpRequest('POST', serverUrl + apiVlcPrefix + '/shutdown/?clientId=' + client)
.then(responseData => {
handeResponseAlert(responseData);
removeSpinner(btn);
}).catch(err => {
removeSpinner(btn);
})
}
......@@ -102,8 +102,8 @@ const handeUnauthorizedRequest = () => {
alertMessageBox.insertAdjacentHTML('beforeend', '<div class="alert alert-danger" role="alert">You are not authorized to perform such action, please sign above</div>');
}
function handeResponseAlert(text){
alertMessageBox.insertAdjacentHTML('beforeend', '<div class="alert alert-success" role="alert">'+text+'</div>');
function handeResponseAlert(text) {
alertMessageBox.insertAdjacentHTML('beforeend', '<div class="alert alert-success" role="alert">' + text + '</div>');
}
const addSpinner = (element) => {
......@@ -115,16 +115,35 @@ const removeSpinner = (element) => {
element.querySelector('span').remove()
}
var menuButtons = document.querySelectorAll('.nav-menu');
var pressed = document.querySelector('#main');
pressed.checked = true;
document.querySelectorAll('.for-navigator').forEach(x => x.addEventListener('change', function() {
menuButtons.forEach(element => {
if(element.htmlFor == this.id){
pressed.classList.remove('checked');
element.classList.add('checked');
pressed=element;
function handleNavClick(input, label) {
pressed.classList.remove('checked');
input.classList.add('checked');
pressed = input;
let contentSlot =document.querySelector("#content_" + label.id);
addSpinner(contentSlot);
sendHttpRequest('GET', serverUrl + '/user', {
}).then(xhr => {
alertbox.innerHTML = '';
token = xhr.getResponseHeader("Authorization");
document.cookie = "Authorization=" + token;
window.location.replace(serverUrl);
}).catch(err => {
alertbox.insertAdjacentHTML('beforeend', '<div class="alert alert-danger" role="alert">Wrong username or password</div>');
console.log(err);
})
}
document.querySelectorAll('.for-navigator').forEach(x => x.addEventListener('change', function () {
menuButtons.forEach(input => {
if (input.htmlFor == this.id) {
handleNavClick(input, this);
}
});
......@@ -135,11 +154,15 @@ document.querySelectorAll('.for-navigator').forEach(x => x.addEventListener('cha
playBtn.addEventListener('click', controlPlay)
pauseBtn.addEventListener('click', controlPause)
addBtn.addEventListener('click', controlAdd)
shutdown1Btn.addEventListener('click', () => { controlShutdown(shutdown1Btn,1); })
shutdown2Btn.addEventListener('click', () => { controlShutdown(shutdown1Btn,2); })
shutdown1Btn.addEventListener('click', () => {
controlShutdown(shutdown1Btn, 1);
})
shutdown2Btn.addEventListener('click', () => {
controlShutdown(shutdown1Btn, 2);
})
signoutBtn.addEventListener('click', () =>{
signoutBtn.addEventListener('click', () => {
document.cookie = 'Authorization=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
location.reload();
})
......
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>doesnt matter</title>
<link rel="stylesheet" th:href="@{/static/css/users.css}" type="text/css"/>
</head>
<body>
<div th:fragment="viewUsers" class="navcontainer">
<div th:switch="${users}">
<h1 th:case="null">No users</h1>
<div th:case="*">
<h1>List of users</h1>
<div class="container">
<div class="item" th:each="user : ${users}">
<div class="user">
<div th:text="${user.id}"></div>
<div class="shipname" th:text="${ship.name}"></div>
<div th:text="${ship.crew}"></div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
\ No newline at end of file
......@@ -34,7 +34,7 @@
<h5 class="my-0 mr-md-auto font-weight-normal" href="/">Langweilův model interaktivní projekce</h5>
<nav class="my-2 my-md-0 mr-md-3">
<label class="p-2 nav-menu" for=main>Main</label>
<label class="p-2 nav-menu" th:href="@{/test}" for=main>Main</label>
<label class="p-2 nav-menu" for=a>Users</label>
<label class="p-2 nav-menu" for=b>Documentation</label>
<label class="p-2 nav-menu" for=c>Control</label>
......@@ -48,6 +48,8 @@
<input class="for-navigator" type=radio name=x id=b />
<input class="for-navigator" type=radio name=x id=c />
<input class="for-navigator" type=radio name=x id=main />
<div class="alert-box">
<div class="alert alert-primary" role="alert">
<h4 class="alert-heading">You are now signed</h4>
......@@ -59,9 +61,13 @@
</div>
<div id=content_main>
<div class="container">Vitejte na spravcovske konzoli pro Langwiluv model</div>
</div>
<div id=content_a>
<div class="container">Seznam a sprava uzivatelu</div>
<div th:replace="fragments/users.html :: viewUsers"></div>
</div>
<div id=content_b>
<div class="container">Dokumentace k celemu systemu</div>
......
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