Compare commits

..

No commits in common. "staging" and "development" have entirely different histories.

68 changed files with 23667 additions and 1469 deletions

1
.gitignore vendored
View File

@ -29,7 +29,6 @@ build/
### VS Code ###
.vscode/
.cache/
node_modules
bundle.js

View File

@ -1,51 +0,0 @@
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**
!**/src/test/**
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
### VS Code ###
.vscode/
.cache/
node_modules
bundle.js
bundle.min.js
adminBundle.js
adminBundle.min.js
worker.js
chatWorker.js
chatWorker.min.js
node
src/main/javascript/node/
dist
out
yarn-error.log
gen-password.txt
**/static/**
License.md

View File

@ -1,5 +0,0 @@
{
"tabWidth": 2,
"useTabs": false,
"printWidth": 80
}

View File

@ -34,7 +34,6 @@
"grunt-banner": "^0.6.0",
"grunt-browserify": "^5.3.0",
"grunt-terser": "^1.0.0",
"prettier": "2.0.5",
"tsify": "^4.0.1",
"typescript": "^3.7.4",
"uglify-js": "^3.7.5"

18
pom.xml
View File

@ -4,7 +4,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.16.RELEASE</version>
<version>2.1.8.RELEASE</version>
<relativePath />
</parent>
<groupId>org.ros</groupId>
@ -103,11 +103,6 @@
<artifactId>flyway-core</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.spencerwi</groupId>
<artifactId>Either.java</artifactId>
<version>2.2.0</version>
</dependency>
</dependencies>
<build>
<plugins>
@ -223,17 +218,6 @@
</excludeProperties>
</configuration>
</plugin>
<plugin>
<groupId>net.revelc.code.formatter</groupId>
<artifactId>formatter-maven-plugin</artifactId>
<version>2.12.0</version>
<configuration>
<configFile>${project.basedir}/eclipse-formatter.xml</configFile>
<compilerSource>11</compilerSource>
<compilerCompliance>11</compilerCompliance>
<compilerTargetPlatform>11</compilerTargetPlatform>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -2,7 +2,6 @@ import moment from "moment";
import { capitalize } from "../../../common/util/Util";
import { AdminUserDTO } from "../../../common/dto/AdminUserDTO";
import log from "loglevel";
import { createApiHeaders } from "../../../common/ajax/util";
export async function viewUsers(authToken: string) {
// const users = await getOtherUsers(authToken);
@ -47,24 +46,13 @@ export async function viewUsers(authToken: string) {
extend: "selectedSingle",
text: "Delete User",
action: (e, dt, button, config) => {
const selectedRow = dt.row({ selected: true });
const username = (selectedRow.data() as AdminUserDTO).userName;
fetch(`/api/admin/delete/users/${username}`, {
headers: createApiHeaders(authToken),
method: "DELETE",
}).then((resp) => {
if (resp.status === 200) {
selectedRow.remove();
log.info("User deleted successfully");
dt.draw();
}
});
log.error("Not implemented yet");
},
},
{
text: "New User",
action: () => {
window.location.assign("/admin/users?mode=new");
log.error("Not implemented yet");
},
},
],

View File

@ -1,34 +1,34 @@
{
"properties" : [
{
"name" : "chatto.token.timeout-duration",
"type" : "java.lang.String",
"description" : "The duration for auth token validity. Token expires after this period of inactivity"
},
{
"name" : "chatto.frontend.log-level",
"type" : "java.lang.String",
"description" : "The log level for the frontend JS application"
},
{
"name" : "chatto.frontend.chat-page-size",
"type" : "java.lang.Integer",
"description" : "The pagination size for the chat area"
},
{
"name" : "chat-worker-bundle",
"type" : "java.lang.String",
"description" : "Name of the chat worker js bundle"
},
{
"name" : "chat-bundle",
"type" : "java.lang.String",
"description" : "Name of the chatjs bundle"
},
{
"name" : "admin-bundle",
"type" : "java.lang.String",
"description" : "Name of the admin js bundle"
}
]
"properties": [
{
"name": "chatto.token.timeout-duration",
"type": "java.lang.String",
"description": "The duration for auth token validity. Token expires after this period of inactivity"
},
{
"name": "chatto.frontend.log-level",
"type": "java.lang.String",
"description": "The log level for the frontend JS application"
},
{
"name": "chatto.frontend.chat-page-size",
"type": "java.lang.Integer",
"description": "The pagination size for the chat area"
},
{
"name": "chat-worker-bundle",
"type": "java.lang.String",
"description": "Name of the chat worker js bundle"
},
{
"name": "chat-bundle",
"type": "java.lang.String",
"description": "Name of the chatjs bundle"
},
{
"name": "admin-bundle",
"type": "java.lang.String",
"description": "Name of the admin js bundle"
}
]
}

View File

@ -2,9 +2,6 @@ package db.migration;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFilePermission;
import java.security.SecureRandom;
import java.sql.PreparedStatement;
@ -15,75 +12,50 @@ import org.springframework.security.crypto.password.PasswordEncoder;
public class V3__add_default_admin extends BaseJavaMigration {
private final PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
private final PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
private final SecureRandom random = new SecureRandom();
private final SecureRandom random = new SecureRandom();
/** different dictionaries used */
private final String ALPHA_CAPS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private final String ALPHA = "abcdefghijklmnopqrstuvwxyz";
private final String NUMERIC = "0123456789";
private final String SPECIAL_CHARS = "!@#$%^&*_=+-/";
/** different dictionaries used */
private final String ALPHA_CAPS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private final String ALPHA = "abcdefghijklmnopqrstuvwxyz";
private final String NUMERIC = "0123456789";
private final String SPECIAL_CHARS = "!@#$%^&*_=+-/";
/**
* Method will generate random string based on the parameters
*
* @param len
* the length of the random string
* @param dic
* the dictionary used to generate the password
* @return the random password
*/
public String generatePassword(int len, String dic) {
String result = "";
for (int i = 0; i < len; i++) {
int index = random.nextInt(dic.length());
result += dic.charAt(index);
}
return result;
}
/**
* Method will generate random string based on the parameters
*
* @param len the length of the random string
* @param dic the dictionary used to generate the password
* @return the random password
*/
public String generatePassword(int len, String dic) {
String result = "";
for (int i = 0; i < len; i++) {
int index = random.nextInt(dic.length());
result += dic.charAt(index);
}
return result;
}
@Override
public void migrate(final Context context) throws Exception {
try (final PreparedStatement ps = context.getConnection()
.prepareStatement(
"insert into users (user_id, name, password) values (0,?,?)")) {
final String generatedPassword = generatePassword(60,
ALPHA_CAPS + ALPHA + SPECIAL_CHARS);
final BufferedWriter bw = new BufferedWriter(
new FileWriter("gen-password.txt"));
@Override
public void migrate(final Context context) throws Exception {
try (final PreparedStatement ps = context.getConnection()
.prepareStatement("insert into users (user_id, name, password) values (0,?,?)")) {
final String generatedPassword = generatePassword(60, ALPHA_CAPS + ALPHA + SPECIAL_CHARS);
final BufferedWriter bw = new BufferedWriter(new FileWriter("gen-password.txt"));
bw.write(generatedPassword);
bw.write("\nPlease delete this file");
bw.close();
ps.setString(1, "admin");
ps.setString(2, passwordEncoder.encode(generatedPassword));
ps.execute();
}
bw.write(generatedPassword);
bw.write("\nPlease delete this file");
bw.close();
final var perms = Files
.getPosixFilePermissions(Paths.get("gen-password.txt"));
// add owners permission
perms.add(PosixFilePermission.OWNER_READ);
perms.add(PosixFilePermission.OWNER_WRITE);
perms.remove(PosixFilePermission.OWNER_EXECUTE);
// add group permissions
perms.remove(PosixFilePermission.GROUP_READ);
perms.remove(PosixFilePermission.GROUP_WRITE);
perms.remove(PosixFilePermission.GROUP_EXECUTE);
// add others permissions
perms.remove(PosixFilePermission.OTHERS_READ);
perms.remove(PosixFilePermission.OTHERS_WRITE);
perms.remove(PosixFilePermission.OTHERS_EXECUTE);
Files.setPosixFilePermissions(Paths.get("gen-password.txt"), perms);
ps.setString(1, "admin");
ps.setString(2, passwordEncoder.encode(generatedPassword));
ps.execute();
}
try (final PreparedStatement ps = context.getConnection()
.prepareStatement(
"insert into users_roles (user_id, role_id) values (1,0)")) {
ps.execute();
}
}
try (final PreparedStatement ps = context.getConnection()
.prepareStatement("insert into users_roles (user_id, role_id) values (1,0)")) {
ps.execute();
}
}
}

View File

@ -17,8 +17,8 @@ public class ChattoApplication extends SpringBootServletInitializer {
}
@Configuration
@Profile("development")
@ComponentScan(lazyInit = true)
static class LocalConfig {
}
@Profile("development")
@ComponentScan(lazyInit = true)
static class LocalConfig {
}
}

View File

@ -12,21 +12,19 @@ import org.springframework.security.web.authentication.www.BasicAuthenticationEn
import org.springframework.stereotype.Component;
@Component
public final class RESTAuthenticationEntryPoint
extends BasicAuthenticationEntryPoint {
public final class RESTAuthenticationEntryPoint extends BasicAuthenticationEntryPoint {
@Override
public void commence(HttpServletRequest request,
HttpServletResponse response, AuthenticationException authEx)
throws IOException, ServletException {
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
PrintWriter writer = response.getWriter();
writer.println("HTTP ApplicationStatus 401 - " + authEx.getMessage());
}
@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authEx)
throws IOException, ServletException {
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
PrintWriter writer = response.getWriter();
writer.println("HTTP ApplicationStatus 401 - " + authEx.getMessage());
}
@Override
public void afterPropertiesSet() throws Exception {
setRealmName("Chatto");
super.afterPropertiesSet();
}
@Override
public void afterPropertiesSet() throws Exception {
setRealmName("Chatto");
super.afterPropertiesSet();
}
}

View File

@ -6,8 +6,7 @@ import org.springframework.boot.web.servlet.support.SpringBootServletInitializer
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(
SpringApplicationBuilder application) {
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(ChattoApplication.class);
}

View File

@ -5,14 +5,16 @@ import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
// @EnableWebMvc
//@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/api/**").allowedOrigins("*")
.allowedMethods("POST", "GET", "OPTIONS").allowedHeaders("*")
.allowCredentials(false).maxAge(3600);
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/api/**")
.allowedOrigins("*")
.allowedMethods("POST","GET","OPTIONS")
.allowedHeaders("*")
.allowCredentials(false).maxAge(3600);
}
}
}

View File

@ -20,17 +20,14 @@ import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
import lombok.RequiredArgsConstructor;
@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
private final MyUserDetailsService myUserDetailsService;
private MyUserDetailsService myUserDetailsService;
@Autowired
private final PasswordEncoder passwordEncoder;
private PasswordEncoder passwordEncoder;
@Override
@Bean
@ -40,7 +37,7 @@ public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Bean
public AuthenticationProvider authenticationProvider() {
final DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setUserDetailsService(myUserDetailsService);
provider.setPasswordEncoder(passwordEncoder);
return provider;
@ -53,76 +50,80 @@ public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Configuration
@Order(1)
@RequiredArgsConstructor
public static class ApiWebSecurity extends WebSecurityConfigurerAdapter {
@Autowired
private final RESTAuthenticationEntryPoint authenticationEntryPoint;
private RESTAuthenticationEntryPoint authenticationEntryPoint;
@Autowired
private CustomBasicAuthenticationFilter customBasicAuthFilter;
@Autowired
private final CustomBasicAuthenticationFilter customBasicAuthFilter;
@Autowired
private final TokenAuthenticationFilter tokenFilter;
private TokenAuthenticationFilter tokenFilter;
@Override
protected void configure(final HttpSecurity http) throws Exception {
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable().exceptionHandling()
.and().sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
// .cors().and()
.and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
// .cors().and()
.antMatcher("/api/**").authorizeRequests()
.antMatchers("/api/chat/**")
.hasAnyRole("USER", "ADMIN", "SUPER_USER")
.antMatchers("/api/admin/**")
.hasAnyRole("ADMIN", "SUPER_USER")
.antMatchers("/api/chat/**").hasAnyRole("USER", "ADMIN", "SUPER_USER")
.antMatchers("/api/demo/**").hasRole("SUPER_USER")
.anyRequest().authenticated().and().httpBasic()
.authenticationEntryPoint(authenticationEntryPoint).and()
.addFilterBefore(tokenFilter,
BasicAuthenticationFilter.class)
// Creating token when basic authentication is successful
// and the
// same token can
// be used to authenticate for further requests
.addFilter(customBasicAuthFilter);
// .antMatchers("/perform-login").permitAll()
.anyRequest()
// .hasAnyRole("USER", "ADMIN", "SUPER_USER")
.authenticated()
.and().httpBasic().authenticationEntryPoint(authenticationEntryPoint)
;
http.addFilterBefore(tokenFilter, BasicAuthenticationFilter.class);
// Creating token when basic authentication is successful and the same token can
// be used to authenticate for further requests
// final CustomBasicAuthenticationFilter customBasicAuthFilter = new CustomBasicAuthenticationFilter(
// authenticationManagerBean());
http.addFilter(customBasicAuthFilter);
}
}
@Configuration
@Order(2)
@RequiredArgsConstructor
public static class FormWebSecurity extends WebSecurityConfigurerAdapter {
@Autowired
private final UserSessionLoggingLoginSuccessHandler loginSuccessHandler;
private UserSessionLoggingLoginSuccessHandler loginSuccessHandler;
@Autowired
private final UserSessionLoggingLogoutSuccessHandler logoutSuccessHandler;
private UserSessionLoggingLogoutSuccessHandler logoutSuccessHandler;
@Override
protected void configure(final HttpSecurity httpSecurity)
throws Exception {
httpSecurity.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.ALWAYS).and()
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.ALWAYS).and()
.authorizeRequests()
.antMatchers("/", "perform_login", "/logout**",
"/favicon.ico", "/login*", "/registration",
"/perform_registration", "/css/**", "/js/**",
"/img/**")
.permitAll().antMatchers("/user/**")
.hasAnyRole("USER", "ADMIN", "SUPER_USER")
.antMatchers("/admin/**").hasAnyRole("ADMIN", "SUPER_USER")
// .antMatchers(HttpMethod.POST, "/api/**").permitAll()
.antMatchers("/", "perform_login", "/logout**", "/favicon.ico", "/login*", "/registration",
"/perform_registration", "/css/**", "/js/**", "/img/**")
.permitAll()
// .antMatchers("/","/api**","/api/**","/login*","/registration","/perform_registration","/css/**", "/js/**", "/images/**").permitAll()
.antMatchers("/user/**").hasAnyRole("USER", "ADMIN", "SUPER_USER").antMatchers("/admin/**")
.hasAnyRole("ADMIN", "SUPER_USER")
// .and()
// .antMatcher("/api/**")
// .authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin().loginPage("/login").permitAll()
.loginProcessingUrl("/perform_login")
.formLogin().loginPage("/login").permitAll().loginProcessingUrl("/perform_login")
.successHandler(loginSuccessHandler).and().logout()
.logoutSuccessHandler(logoutSuccessHandler);
.logoutSuccessHandler(logoutSuccessHandler)
;
}
// }
}
}

View File

@ -4,10 +4,7 @@ import java.awt.image.BufferedImage;
interface CaptchaBehaviour {
public BufferedImage generateCaptcha();
public BufferedImage generateCaptcha(String captchaText);
public String getRandomChars(int size);
public String getRandomChars();
}

View File

@ -6,10 +6,9 @@ import lombok.Builder;
/*Class for providing your own captcha generator*/
@Builder
public class ManualCaptchaBehaviour implements CaptchaBehaviour {
public class ManualCaptchaBehaviour implements CaptchaBehaviour{
private final int length;
private final String style;
@Override
public BufferedImage generateCaptcha() {
// TODO Auto-generated method stub
@ -34,4 +33,5 @@ public class ManualCaptchaBehaviour implements CaptchaBehaviour {
return null;
}
}

View File

@ -10,183 +10,174 @@ import java.io.IOException;
import java.util.Random;
/**
* This class represents a simple captcha consisting of an image {@code png} and
* its text value. Comic Neue Bold Font. Capital english letters {@code ONLY}.
* This class represents a simple captcha consisting
* of an image {@code png} and its text value.
* Comic Neue Bold Font.
* Capital english letters {@code ONLY}.
*
* @since 1.3
* @author Gennadiy Golovin
*/
public final class SimpleCaptcha {
private BufferedImage imagePng;
private char[] text;
private BufferedImage imagePng;
private char[] text;
/**
* Initializes a newly created default object consisting of 8 capital
* english letters.
*/
public SimpleCaptcha() {
this.text = getRandomChars();
/**
* Initializes a newly created default object
* consisting of 8 capital english letters.
*/
public SimpleCaptcha() {
this.text = getRandomChars();
try {
generateCaptcha();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
generateCaptcha();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Initializes a newly created object, which length depends on the passed
* {@code int} parameter, which {@code MUST} be greater than 0. If the
* condition is not met, initializes a newly created default object
* consisting of 8 symbols.
*
* @param length
* the quantity of symbols, that the captcha consists of, greater
* than 0.
*/
public SimpleCaptcha(int length) {
if (length < 1) {
this.text = getRandomChars();
} else {
this.text = getRandomChars(length);
}
/**
* Initializes a newly created object, which length
* depends on the passed {@code int} parameter,
* which {@code MUST} be greater than 0.
* If the condition is not met, initializes a newly
* created default object consisting of 8 symbols.
*
* @param length the quantity of symbols, that the
* captcha consists of, greater than 0.
*/
public SimpleCaptcha(int length) {
if (length < 1) {
this.text = getRandomChars();
} else {
this.text = getRandomChars(length);
}
try {
generateCaptcha();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
generateCaptcha();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Initializes a newly created object based on the passed {@link String}
* parameter, consisting of capital english letters. If the condition is not
* met, initializes a newly created default object consisting of 8 capital
* english letters.
*
* @param text
* the text string with the value of the captcha, length greater
* than 0.
*/
public SimpleCaptcha(String text) {
if (text == null || text.equals("")) {
this.text = getRandomChars();
} else {
this.text = text.toCharArray();
}
/**
* Initializes a newly created object based on the passed
* {@link String} parameter, consisting of capital english
* letters. If the condition is not met, initializes a newly
* created default object consisting of 8 capital english letters.
*
* @param text the text string with the value of the captcha,
* length greater than 0.
*/
public SimpleCaptcha(String text) {
if (text == null || text.equals("")) {
this.text = getRandomChars();
} else {
this.text = text.toCharArray();
}
try {
generateCaptcha();
} catch (IOException e) {
this.text = getRandomChars();
try {
generateCaptcha();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
try {
generateCaptcha();
} catch (IOException e) {
this.text = getRandomChars();
try {
generateCaptcha();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
/**
* Returns the picture with captcha
*
* @return {@link BufferedImage}
*/
public BufferedImage getImagePng() {
return imagePng;
}
/**
* Returns the picture with captcha
*
* @return {@link BufferedImage}
*/
public BufferedImage getImagePng() {
return imagePng;
}
/**
* Returns the text value of the captcha
*
* @return {@link String}
*/
public String getText() {
return String.valueOf(text);
}
/**
* Returns the text value of the captcha
*
* @return {@link String}
*/
public String getText() {
return String.valueOf(text);
}
//////// //////// //////// //////// //////// //////// //////// ////////
//////// //////// //////// //////// //////// //////// //////// ////////
private char[] getRandomChars() {
return getRandomChars(8);
}
private char[] getRandomChars() {
return getRandomChars(8);
}
private char[] getRandomChars(int quantity) {
private char[] getRandomChars(int quantity) {
char[] randomString = new char[quantity];
char[] randomString = new char[quantity];
Random random = new Random();
Random random = new Random();
int capitalLetter;
int capitalLetter;
for (int i = 0; i < quantity; i++) {
capitalLetter = 65 + random.nextInt(26);
randomString[i] = (char) capitalLetter;
}
for (int i = 0; i < quantity; i++) {
capitalLetter = 65 + random.nextInt(26);
randomString[i] = (char) capitalLetter;
}
return randomString;
}
return randomString;
}
private void generateCaptcha() throws IOException {
int charsQuantity = this.text.length;
BufferedImage[] images = new BufferedImage[charsQuantity];
private void generateCaptcha() throws IOException {
int charsQuantity = this.text.length;
BufferedImage[] images = new BufferedImage[charsQuantity];
for (int i = 0; i < charsQuantity; i++) {
images[i] = ImageIO.read(SimpleCaptcha.class
.getResourceAsStream("/pictures/" + this.text[i] + ".png"));
if (i % 2 == 0) {
images[i] = rotateImage(images[i], 25);
} else {
images[i] = rotateImage(images[i], -20);
}
}
for (int i = 0; i < charsQuantity; i++) {
images[i] = ImageIO.read(SimpleCaptcha.class.getResourceAsStream("/pictures/" + this.text[i] + ".png"));
if (i % 2 == 0) {
images[i] = rotateImage(images[i], 25);
} else {
images[i] = rotateImage(images[i], -20);
}
}
int imageSize = 30;
int rotatedImageSize = (int) Math.sqrt(imageSize * imageSize * 2);
int imageSize = 30;
int rotatedImageSize = (int) Math.sqrt(imageSize * imageSize * 2);
BufferedImage captchaImg = new BufferedImage(
rotatedImageSize * (charsQuantity - 1) / 10 * 6
+ rotatedImageSize,
rotatedImageSize, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics2d = captchaImg.createGraphics();
graphics2d.setBackground(Color.WHITE);
graphics2d.clearRect(0, 0, captchaImg.getWidth(),
captchaImg.getHeight());
for (int i = 0; i < charsQuantity; i++) {
captchaImg.getGraphics().drawImage(images[i],
rotatedImageSize * i / 10 * 6, 0, null);
}
graphics2d.dispose();
this.imagePng = captchaImg;
}
BufferedImage captchaImg = new BufferedImage(rotatedImageSize * (charsQuantity - 1) / 10 * 6 + rotatedImageSize, rotatedImageSize, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics2d = captchaImg.createGraphics();
graphics2d.setBackground(Color.WHITE);
graphics2d.clearRect(0, 0, captchaImg.getWidth(), captchaImg.getHeight());
for (int i = 0; i < charsQuantity; i++) {
captchaImg.getGraphics().drawImage(images[i], rotatedImageSize * i / 10 * 6, 0, null);
}
graphics2d.dispose();
this.imagePng = captchaImg;
}
private BufferedImage rotateImage(BufferedImage buffImage, double angle) {
private BufferedImage rotateImage(BufferedImage buffImage, double angle) {
double radian = Math.toRadians(angle);
double sin = Math.abs(Math.sin(radian));
double cos = Math.abs(Math.cos(radian));
double radian = Math.toRadians(angle);
double sin = Math.abs(Math.sin(radian));
double cos = Math.abs(Math.cos(radian));
int width = buffImage.getWidth();
int height = buffImage.getHeight();
int width = buffImage.getWidth();
int height = buffImage.getHeight();
int nWidth = (int) Math
.floor((double) width * cos + (double) height * sin);
int nHeight = (int) Math
.floor((double) height * cos + (double) width * sin);
int nWidth = (int) Math.floor((double) width * cos + (double) height * sin);
int nHeight = (int) Math.floor((double) height * cos + (double) width * sin);
BufferedImage rotatedImage = new BufferedImage(nWidth, nHeight,
BufferedImage.TYPE_INT_ARGB);
BufferedImage rotatedImage = new BufferedImage(nWidth, nHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = rotatedImage.createGraphics();
Graphics2D graphics = rotatedImage.createGraphics();
graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
graphics.translate((nWidth - width) / 2, (nHeight - height) / 2);
graphics.rotate(radian, (double) (width / 2), (double) (height / 2));
graphics.drawImage(buffImage, 0, 0, null);
graphics.dispose();
graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
graphics.translate((nWidth - width) / 2, (nHeight - height) / 2);
graphics.rotate(radian, (double) (width / 2), (double) (height / 2));
graphics.drawImage(buffImage, 0, 0,null);
graphics.dispose();
return rotatedImage;
}
return rotatedImage;
}
}

View File

@ -9,30 +9,30 @@ public class SimpleCaptchaBehavior implements CaptchaBehaviour {
SimpleCaptcha simpleCaptcha = new SimpleCaptcha();
return simpleCaptcha.getImagePng();
}
@Override
public BufferedImage generateCaptcha(String captchaText) {
SimpleCaptcha simpleCaptcha = new SimpleCaptcha(captchaText);
return simpleCaptcha.getImagePng();
}
public String getRandomChars() {
return getRandomChars(8);
}
public String getRandomChars(int quantity) {
return getRandomChars(8);
}
public String getRandomChars(int quantity)
{
char[] randomString = new char[quantity];
Random random = new Random();
Random random = new Random();
int capitalLetter;
int capitalLetter;
for (int i = 0; i < quantity; i++) {
capitalLetter = 65 + random.nextInt(26);
randomString[i] = (char) capitalLetter;
}
for (int i = 0; i < quantity; i++) {
capitalLetter = 65 + random.nextInt(26);
randomString[i] = (char) capitalLetter;
}
return new String(randomString);
return new String(randomString);
}
}

View File

@ -7,35 +7,34 @@ import lombok.Builder;
@Builder
public class WebCaptcha {
private final CaptchaBehaviour captchaBehaviour;
public BufferedImage generateCaptcha() {
return captchaBehaviour.generateCaptcha();
}
}
public BufferedImage generateCaptcha(String captchaText) {
return captchaBehaviour.generateCaptcha(captchaText);
}
}
public String getRandomChars() {
return captchaBehaviour.getRandomChars();
}
return captchaBehaviour.getRandomChars();
}
public String getRandomChars(int quantity) {
return captchaBehaviour.getRandomChars(quantity);
}
public String getRandomChars(int quantity) {
return captchaBehaviour.getRandomChars(quantity);
}
}
// WebCaptcha webCaptcha = WebCaptcha.builder().captchaBehaviour(new
// SimpleCaptchaBehavior()).build();
// webCaptcha.generateCaptcha();
// WebCaptcha webCaptcha = WebCaptcha.builder().captchaBehaviour(new SimpleCaptchaBehavior()).build();
// webCaptcha.generateCaptcha();
//
// // @formatter:off
// webCaptcha = WebCaptcha.builder()
// .captchaBehaviour(
// ManualCaptchaBehaviour.builder()
// .length(8)
// .style("black")
// .build()
// ).build();
//
// // @formatter:on
// // @formatter:off
// webCaptcha = WebCaptcha.builder()
// .captchaBehaviour(
// ManualCaptchaBehaviour.builder()
// .length(8)
// .style("black")
// .build()
// ).build();
//
// // @formatter:on

View File

@ -10,13 +10,13 @@ import lombok.Getter;
@PropertySource(value = "classpath:git.properties")
@Getter
public class BuildInfo {
private final String buildVersion;
private final String branchName;
private final String buildVersion;
private final String branchName;
public BuildInfo(@Value("${git.build.version") String buildVersion,
@Value("${git.branch") String branchName) {
this.buildVersion = buildVersion;
this.branchName = branchName;
}
public BuildInfo(@Value("${git.build.version") String buildVersion,
@Value("${git.branch") String branchName) {
this.buildVersion = buildVersion;
this.branchName = branchName;
}
}

View File

@ -6,14 +6,11 @@ import org.ehcache.event.CacheEventListener;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class CustomCacheEventLogger
implements CacheEventListener<Object, Object> {
public class CustomCacheEventLogger implements CacheEventListener<Object, Object> {
@Override
public void onEvent(
CacheEvent<? extends Object, ? extends Object> cacheEvent) {
log.debug("custom Caching event {} key = {} old {} new {} ",
cacheEvent.getType(), cacheEvent.getKey(),
public void onEvent(CacheEvent<? extends Object, ? extends Object> cacheEvent) {
log.debug("custom Caching event {} key = {} old {} new {} ", cacheEvent.getType(), cacheEvent.getKey(),
cacheEvent.getOldValue(), cacheEvent.getNewValue());
}
}

View File

@ -1,5 +1,6 @@
package org.ros.chatto.config;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Configuration;

View File

@ -11,7 +11,6 @@ import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.server.ResponseStatusException;
import lombok.RequiredArgsConstructor;
@ -38,13 +37,7 @@ public class AdminController {
}
@GetMapping("/users")
public String usersPage(@RequestParam Optional<String> mode) {
if (mode.isPresent()) {
switch (mode.get()) {
case "new":
return "admin/new-user";
}
}
public String usersPage() {
return "admin/users";
}

View File

@ -2,29 +2,25 @@ package org.ros.chatto.controller;
import java.security.Principal;
import java.time.Instant;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import javax.validation.Valid;
import com.spencerwi.either.Result;
import org.ros.chatto.dto.AdminUserDTO;
import org.ros.chatto.dto.ChatMessageDTO;
import org.ros.chatto.dto.ReencryptionDTO;
import org.ros.chatto.service.AdminService;
import org.ros.chatto.service.ChatService;
import org.ros.chatto.service.UserDTOSpec;
import org.ros.chatto.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import lombok.RequiredArgsConstructor;
@ -65,33 +61,13 @@ public class AdminRESTController {
return chatMessageDTOs;
}
@GetMapping("/get/users")
public List<AdminUserDTO> getUsers(Principal principal,
@RequestParam Optional<String> select) {
if (select.isPresent()) {
switch (select.get()) {
case "all":
return adminService.getUsers("", UserDTOSpec.ALL_USERS);
case "regular":
return adminService.getUsers("", UserDTOSpec.REGULAR_USERS);
}
}
return adminService.getUsers(principal.getName(),
UserDTOSpec.OTHER_USERS);
@GetMapping("/regular-users")
public List<String> getAllRegularUsers() {
return userService.getAllRegularUsers();
}
@DeleteMapping("/delete/users/{userName}")
public ResponseEntity<?> deleteUser(@PathVariable String userName) {
Result<Void> res = adminService.deleteUser(userName);
if (res.isOk()) {
return ResponseEntity.ok()
.body("Deleted User with name - " + userName);
} else {
return ResponseEntity.badRequest().body(
"An error occured while trying to delete user with name - "
+ userName);
}
@GetMapping("/get/users")
public List<AdminUserDTO> getAllOtherUsers(Principal principal) {
return adminService.getOtherUsers(principal.getName());
}
}

View File

@ -89,8 +89,8 @@ public class ChatMessageController {
@PathVariable final String userName,
@PathVariable final Instant lastMessageTime,
final Principal principal) {
final List<ChatMessageDTO> chatMessageDTOs = chatService
.getNewMessages(principal.getName(), userName, lastMessageTime);
final List<ChatMessageDTO> chatMessageDTOs = chatService.getNewMessages(
principal.getName(), userName, lastMessageTime);
return chatMessageDTOs;
}

View File

@ -1,14 +0,0 @@
package org.ros.chatto.controller;
import org.springframework.security.web.csrf.CsrfToken;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CsrfController {
@RequestMapping("/csrf")
public CsrfToken csrf(CsrfToken token) {
return token;
}
}

View File

@ -45,7 +45,7 @@ public class Home {
|| r.getAuthority().equals("ROLE_SUPER_USER"));
log.trace("Is admin? " + isAdmin);
// model.addAttribute("activeUsers",
// userService.getOtherActiveUsers(principal.getName()));
// userService.getOtherActiveUsers(principal.getName()));
return "chat";
}
}

View File

@ -14,16 +14,16 @@ import lombok.RequiredArgsConstructor;
@RequestMapping("/api/stats")
@RequiredArgsConstructor
public class StatisticsController {
private final StatisticsService statisticsService;
private final StatisticsService statisticsService;
@GetMapping
public StatsDTO rootStats() {
return StatsDTO.builder()
.totalMessages(statisticsService.totalMessage())
.totalOnlineUsers(statisticsService.totalUsersOnline())
.numMessagesToday(
statisticsService.messagesOnDay(Instant.now()))
.totalUsers(statisticsService.totalUsers()).build();
}
@GetMapping
public StatsDTO rootStats() {
return StatsDTO.builder()
.totalMessages(statisticsService.totalMessage())
.totalOnlineUsers(statisticsService.totalUsersOnline())
.numMessagesToday(
statisticsService.messagesOnDay(Instant.now()))
.totalUsers(statisticsService.totalUsers()).build();
}
}

View File

@ -6,9 +6,9 @@ import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping
public String viewUserProfile() {
return "user/home";
}
@RequestMapping
public String viewUserProfile() {
return "user/home";
}
}

View File

@ -1,5 +1,6 @@
package org.ros.chatto.dto;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
@ -11,10 +12,10 @@ import lombok.Data;
public class ChatMessageDTO {
@NotBlank(message = "Username should not be blank")
@Pattern(regexp = "^[A-Za-z0-9]+$", message = "Username must be alphanumeric")
@Size(max = 15)
@Size(max=15)
private String toUser;
@Pattern(regexp = "^[A-Za-z0-9]+$", message = "Username must be alphanumeric")
@Size(max = 15)
@Size(max=15)
private String fromUser;
private MessageCipherDTO messageCipher;
private Instant messageTime;

View File

@ -12,9 +12,7 @@ import lombok.Data;
@Data
public class MessageCipherDTO {
@Pattern(regexp = "^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$") // regex
// for
// base64
@Pattern(regexp = "^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$") // regex for base64
@NotBlank
private String iv;
@Max(1)

View File

@ -16,13 +16,11 @@ public class UserRegistrationDTO {
@Transient
@Size(min = 4, max = 75, message = "Password must be between 4 and 75 characters")
@NotBlank(message = "Password should not be blank")
// @Pattern(regexp =
// "^.*(?=.{6,})(?=.*d)(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*? ]).*$", message
// = "Invalid password format")
// @Pattern(regexp = "^.*(?=.{6,})(?=.*d)(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*? ]).*$", message = "Invalid password format")
private String password;
private Long captchaID;
private String captchaText;
private String captchaInput;
}

View File

@ -7,14 +7,15 @@ import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@NoArgsConstructor
@AllArgsConstructor
public class ErrorModel {
public class ErrorModel{
@JsonProperty("field_name")
private String fieldName;
private String fieldName;
@JsonProperty("rejected_value")
private Object rejectedValue;
private Object rejectedValue;
@JsonProperty("error_message")
private String messageError;
private String messageError;
}

View File

@ -15,6 +15,6 @@ import lombok.NoArgsConstructor;
@AllArgsConstructor
public class ErrorResponse {
@JsonProperty("errors")
private List<ErrorModel> errorMessage;
private List<ErrorModel> errorMessage;
}

View File

@ -4,6 +4,5 @@ import org.springframework.cache.annotation.CacheEvict;
public class TokenCacheUtil {
@CacheEvict(value = "userTokenCache", key = "#cacheKey")
public static void evictSingleTokenValue(String cacheKey) {
}
public static void evictSingleTokenValue(String cacheKey) {}
}

View File

@ -26,8 +26,7 @@ public class UserLoggingSessionListener implements HttpSessionBindingListener {
@Override
public void valueBound(HttpSessionBindingEvent event) {
UserLoggingSessionListener user = (UserLoggingSessionListener) event
.getValue();
UserLoggingSessionListener user = (UserLoggingSessionListener) event.getValue();
log.debug("Incrementing session count for user {}", user.getUsername());
@ -39,8 +38,7 @@ public class UserLoggingSessionListener implements HttpSessionBindingListener {
@Override
public void valueUnbound(HttpSessionBindingEvent event) {
UserLoggingSessionListener user = (UserLoggingSessionListener) event
.getValue();
UserLoggingSessionListener user = (UserLoggingSessionListener) event.getValue();
log.debug("Decrementing session count for user {}", user.getUsername());

View File

@ -11,19 +11,17 @@ import org.springframework.security.web.authentication.AuthenticationSuccessHand
import org.springframework.stereotype.Component;
@Component("myAuthenticationSuccessHandler")
public class UserSessionLoggingLoginSuccessHandler
implements AuthenticationSuccessHandler {
@Override
public void onAuthenticationSuccess(HttpServletRequest request,
HttpServletResponse response, Authentication authentication)
throws IOException {
HttpSession session = request.getSession(false);
if (session != null) {
UserLoggingSessionListener user = new UserLoggingSessionListener(
authentication.getName());
session.setAttribute("user", user);
}
response.sendRedirect("/chat");
}
public class UserSessionLoggingLoginSuccessHandler implements AuthenticationSuccessHandler {
@Override
public void onAuthenticationSuccess(HttpServletRequest request,
HttpServletResponse response, Authentication authentication)
throws IOException {
HttpSession session = request.getSession(false);
if (session != null) {
UserLoggingSessionListener user = new UserLoggingSessionListener(authentication.getName());
session.setAttribute("user", user);
}
response.sendRedirect("/chat");
}
}

View File

@ -12,16 +12,15 @@ import org.springframework.security.web.authentication.logout.LogoutSuccessHandl
import org.springframework.stereotype.Component;
@Component("myLogoutSuccessHandler")
public class UserSessionLoggingLogoutSuccessHandler
implements LogoutSuccessHandler {
@Override
public void onLogoutSuccess(HttpServletRequest request,
HttpServletResponse response, Authentication authentication)
throws IOException, ServletException {
HttpSession session = request.getSession();
if (session != null) {
session.removeAttribute("user");
}
response.sendRedirect("/login?logout");
}
public class UserSessionLoggingLogoutSuccessHandler implements LogoutSuccessHandler{
@Override
public void onLogoutSuccess(HttpServletRequest request,
HttpServletResponse response, Authentication authentication)
throws IOException, ServletException {
HttpSession session = request.getSession();
if (session != null){
session.removeAttribute("user");
}
response.sendRedirect("/login?logout");
}
}

View File

@ -24,18 +24,18 @@ public class ChatMessage {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "m_id")
private Long messageID;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "from_user")
private ChatUser fromUser;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "to_user")
private ChatUser toUser;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "message")
private MessageCipher messageCipher;
private Instant messageTime;
private Instant messageTime;
}

View File

@ -19,7 +19,7 @@ this is what the json will look like*/
@Entity
@Table(name = "message_ciphers")
@EntityListeners(AuditingEntityListener.class)
// @JsonIgnoreProperties(value = { "id"}, allowGetters = false)
//@JsonIgnoreProperties(value = { "id"}, allowGetters = false)
public class MessageCipher {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)

View File

@ -16,6 +16,7 @@ import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
@Entity
@Table(name = "roles")
@Data
@ -23,14 +24,14 @@ public class Role {
@Id
@Column(name = "role_id")
private int roleID;
@Column(name = "role_name")
private String name;
private String name;
private String description;
@OneToMany(mappedBy = "role", cascade = { CascadeType.PERSIST,
CascadeType.MERGE, CascadeType.DETACH, CascadeType.REFRESH })
@OneToMany(mappedBy = "role", cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.DETACH,
CascadeType.REFRESH })
@JsonBackReference
@ToString.Exclude
@EqualsAndHashCode.Exclude

View File

@ -20,13 +20,13 @@ public class UserSession {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private ChatUser user;
private boolean online;
private int numSessions;
private Instant timeStamp;
}

View File

@ -1,25 +0,0 @@
package org.ros.chatto.repository;
import java.util.List;
import org.ros.chatto.dto.AdminUserDTO;
import org.ros.chatto.model.ChatUser;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
@Repository
public interface AdminUserRepository extends JpaRepository<ChatUser, Long> {
@Query("select new org.ros.chatto.dto.AdminUserDTO(u.userID, u.userName, ur.role.name, u.joinDate )"
+ " from ChatUser u join u.userRoles ur where u.userName != ?1")
public List<AdminUserDTO> getOtherUsers(String principal);
@Query("select new org.ros.chatto.dto.AdminUserDTO(u.userID, u.userName, ur.role.name, u.joinDate )"
+ " from ChatUser u join u.userRoles ur ")
public List<AdminUserDTO> getAllUsers();
@Query("select new org.ros.chatto.dto.AdminUserDTO(u.userID, u.userName, ur.role.name, u.joinDate )"
+ " from ChatUser u join u.userRoles ur where ur.role.roleID = 2")
public List<AdminUserDTO> getRegularUsers();
}

View File

@ -1,10 +1,9 @@
// package org.ros.chatto.repository;
//package org.ros.chatto.repository;
//
// import org.springframework.data.jpa.repository.JpaRepository;
// import org.springframework.stereotype.Repository;
//import org.springframework.data.jpa.repository.JpaRepository;
//import org.springframework.stereotype.Repository;
//
// @Repository
// public interface DBInitializerRepostory extends JpaRepository<Integer,
// Integer>{
//
// }
//@Repository
//public interface DBInitializerRepostory extends JpaRepository<Integer, Integer>{
//
//}

View File

@ -5,7 +5,6 @@ import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface MessageCipherRepository
extends JpaRepository<MessageCipher, Long> {
public interface MessageCipherRepository extends JpaRepository<MessageCipher, Long>{
}

View File

@ -6,7 +6,7 @@ import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
@Repository
public interface RoleRepository extends JpaRepository<Role, Long> {
public interface RoleRepository extends JpaRepository<Role, Long>{
@Query("select r from Role r where r.name = ?1")
public Role findByName(String roleName);
}

View File

@ -19,4 +19,8 @@ public interface UserRepository extends JpaRepository<ChatUser, Long> {
@Query("select count(u) from ChatUser u")
public Long totalUsers();
@Query("select new org.ros.chatto.dto.AdminUserDTO(u.userID, u.userName, ur.role.name, u.joinDate )"
+ " from ChatUser u join u.userRoles ur ")
public List<AdminUserDTO> getOtherUsers(String principal);
}

View File

@ -4,8 +4,8 @@ import java.util.List;
public interface UserRepositoryCustom {
// @Query("select s from Article s where s.author like ?1 and s.title = ?2")
// List<Article> findByAuthorAndTitle(String author, String title);
// @Query("select u from ChatUser u")
// @Query("select s from Article s where s.author like ?1 and s.title = ?2")
// List<Article> findByAuthorAndTitle(String author, String title);
// @Query("select u from ChatUser u")
public List<String> getAllUserNames(String s);
}

View File

@ -14,34 +14,30 @@ import org.ros.chatto.model.ChatUser;
import org.ros.chatto.repository.UserRepositoryCustom;
@Service
class UserRepositoryCustomImpl implements UserRepositoryCustom {
class UserRepositoryCustomImpl implements UserRepositoryCustom{
@PersistenceContext
private EntityManager entityManager;
@Override
public List<String> getAllUserNames(String userName) {
List<String> userNamesList = null;
// Session session = null;
// Session session = null;
try {
CriteriaBuilder criteriaBuilder = entityManager
.getCriteriaBuilder();
CriteriaQuery<String> criteriaQuery = criteriaBuilder
.createQuery(String.class);
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<String> criteriaQuery = criteriaBuilder.createQuery(String.class);
Root<ChatUser> root = criteriaQuery.from(ChatUser.class);
criteriaQuery.select(root.get("userName"));
criteriaQuery.where(
criteriaBuilder.notEqual(root.get("userName"), userName));
userNamesList = entityManager.createQuery(criteriaQuery)
.getResultList();
// for(String un: userNamesList)
// {
// System.out.println(un);
// }
criteriaQuery.where(criteriaBuilder.notEqual(root.get("userName"), userName));
userNamesList = entityManager.createQuery(criteriaQuery).getResultList();
// for(String un: userNamesList)
// {
// System.out.println(un);
// }
} catch (Exception e) {
e.printStackTrace();
}
return userNamesList;
}
}

View File

@ -16,29 +16,20 @@ import org.springframework.security.web.authentication.AuthenticationSuccessHand
import org.springframework.stereotype.Component;
@Component
public class AuthenticationSuccessHandlerImpl
implements AuthenticationSuccessHandler {
private static final SimpleGrantedAuthority SUPER_USER_AUTHORITY = new SimpleGrantedAuthority(
"ROLE_SUPER_USER");
private static final SimpleGrantedAuthority ADMIN_AUTHORITY = new SimpleGrantedAuthority(
"ROLE_ADMIN");
private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
public class AuthenticationSuccessHandlerImpl implements AuthenticationSuccessHandler {
private static final SimpleGrantedAuthority SUPER_USER_AUTHORITY = new SimpleGrantedAuthority("ROLE_SUPER_USER");
private static final SimpleGrantedAuthority ADMIN_AUTHORITY = new SimpleGrantedAuthority("ROLE_ADMIN");
private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
@Override
public void onAuthenticationSuccess(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse,
Authentication authentication)
throws IOException, ServletException {
Collection<? extends GrantedAuthority> authorities = authentication
.getAuthorities();
if (authorities.contains(ADMIN_AUTHORITY)
|| authorities.contains(SUPER_USER_AUTHORITY)) {
redirectStrategy.sendRedirect(httpServletRequest,
httpServletResponse, "/admin");
} else {
redirectStrategy.sendRedirect(httpServletRequest,
httpServletResponse, "/user");
}
public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
Authentication authentication) throws IOException, ServletException {
Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
if (authorities.contains(ADMIN_AUTHORITY) || authorities.contains(SUPER_USER_AUTHORITY)) {
redirectStrategy.sendRedirect(httpServletRequest, httpServletResponse, "/admin");
} else {
redirectStrategy.sendRedirect(httpServletRequest, httpServletResponse, "/user");
}
}
}

View File

@ -1,46 +1,20 @@
package org.ros.chatto.service;
import java.security.Principal;
import java.util.List;
import java.util.Optional;
import com.spencerwi.either.Result;
import org.ros.chatto.dto.AdminUserDTO;
import org.ros.chatto.model.ChatUser;
import org.ros.chatto.repository.AdminUserRepository;
import org.ros.chatto.repository.UserRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@Service
@RequiredArgsConstructor
@Slf4j
@Transactional
public class AdminService {
private final UserRepository userRepository;
private final AdminUserRepository adminUserRepository;
public List<AdminUserDTO> getUsers(String principal, UserDTOSpec spec) {
switch (spec) {
case OTHER_USERS:
return adminUserRepository.getOtherUsers(principal);
case ALL_USERS:
return adminUserRepository.getAllUsers();
case REGULAR_USERS:
return adminUserRepository.getRegularUsers();
default:
throw new UnsupportedOperationException();
}
}
public Result<Void> deleteUser(String userName) {
Optional<ChatUser> user = userRepository.findByUserName(userName);
return Result.attempt(() -> user.get()).map((u) -> {
userRepository.delete(u);
return (Void) null;
});
public List<AdminUserDTO> getOtherUsers(String principal) {
return userRepository.getOtherUsers(principal);
}
}

View File

@ -9,17 +9,18 @@ import org.springframework.stereotype.Service;
@Service
public class CaptchaService {
private final WebCaptcha webCaptcha;
public CaptchaService() {
webCaptcha = WebCaptcha.builder()
.captchaBehaviour(new SimpleCaptchaBehavior()).build();
webCaptcha = WebCaptcha.builder().captchaBehaviour(new SimpleCaptchaBehavior()).build();
}
public BufferedImage createCaptchaImage(final String captchaText) {
public BufferedImage createCaptchaImage(final String captchaText)
{
return webCaptcha.generateCaptcha(captchaText);
}
public String getRandomText() {
public String getRandomText()
{
return webCaptcha.getRandomChars();
}
}

View File

@ -46,15 +46,13 @@ public class DBInitializerService {
});
}
private void resetAllUserSessions(final Connection connection)
throws SQLException {
private void resetAllUserSessions(final Connection connection) throws SQLException {
final PreparedStatement preparedStatement = connection
.prepareStatement(dbInitializerConfig.getResetSessionsQuery());
preparedStatement.executeUpdate();
}
private void clearAllTokens(final Connection connection)
throws SQLException {
private void clearAllTokens(final Connection connection) throws SQLException {
final PreparedStatement preparedStatement = connection
.prepareStatement(dbInitializerConfig.getClearTokensQuery());
preparedStatement.executeUpdate();

View File

@ -1,5 +0,0 @@
package org.ros.chatto.service;
public enum UserDTOSpec {
ALL_USERS, OTHER_USERS, REGULAR_USERS
}

View File

@ -28,5 +28,4 @@ public interface UserService {
public void incrementUserSession(String userName);
public UserSession decrementUserSession(String userName);
}

View File

@ -194,14 +194,4 @@ public class UserServiceImpl implements UserService {
return user.getUserRoles().stream().map(ur -> ur.getRole())
.collect(Collectors.toSet());
}
public void deleteUser(String userName) {
//
Optional<ChatUser> user = userRepository.findByUserName(userName);
user.ifPresentOrElse(u -> {
userRepository.delete(u);
}, () -> {
log.error("User with name {} not found for deletion", userName);
});
}
}

File diff suppressed because one or more lines are too long

View File

@ -101,24 +101,3 @@ textarea {
width: 90%;
} */
}
html {
--scrollbarBG: hsla(0, 0%, 100%, 0.1);
--thumbBG: #90a4ae;
}
*::-webkit-scrollbar {
width: 11px;
}
* {
scrollbar-width: thin;
scrollbar-color: var(--thumbBG) var(--scrollbarBG);
}
*::-webkit-scrollbar-track {
background: var(--scrollbarBG);
}
*::-webkit-scrollbar-thumb {
background-color: var(--thumbBG);
border-radius: 6px;
border: 3px solid var(--scrollbarBG);
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Webpack App</title>
</head>
<body>
<script type="text/javascript" src="/bundle.js"></script></body>
</html>

View File

@ -0,0 +1,110 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<div th:replace="fragments/head :: headFragment">
<title id="pageTitle">Admin Home</title>
</div>
<script src="https://code.jquery.com/jquery-2.1.4.min.js" th:if="false"></script>
<script src="http://blackpeppersoftware.github.io/thymeleaf-fragment.js/thymeleaf-fragment.js" data-template-prefix="../" defer="defer" th:if="false"></script>
<script th:src="@{js/admin.js}" src="../../static/js/admin.js" defer="defer"></script>
<link th:href="@{/css/master.css}" href="../../static/css/master.css" rel="stylesheet" th:if="false">
<link th:href="@{/css/colors.css}" href="../../static/css/colors.css" rel="stylesheet" th:if="false">
</head>
<!-- TODO
Make user admin / remove user from admin
Change E2E passphrase
Delete Messages
-->
<body>
<div th:include="fragments/navbar :: navbarFragment"></div>
<header>
<div class="container bg-primary">
<div class="row">
<div class="col-sm py-5">
<h1 class="display-4 text-center">Admin Page</h1>
<p class="alert-danger px-2">Warning: these settings can be dangerous..</p>
</div>
</div>
<div class="row">
<div class="col-sm col-md-4">
<h4>Make User an Admin</h4>
<form id="addUserToAdminForm">
<div class="form-group">
<label for="addUserToDropDown">Select User:</label>
<select class="form-control custom-select" size="4" id="addUserToAdminDropDown">
<option th:each="userName : ${userNames}"
th:value="${userName}"
th:text="#{${userName}}">
Wireframe
</option>
</select>
</div>
<div class="form-group">
<button class="btn btn-danger form-control">Make admin</button>
</div>
</form>
</div>
<div class="col-sm col-md-4">
<h4>Change passphrases</h4>
<form id="changePassphraseForm">
<div class="form-group">
<label for="changePassphraseDropDown">Select User:</label>
<select class="form-control" id="changePassphraseDropDown">
<option th:each="userName : ${userNames}"
th:value="${userName}"
th:text="#{${userName}}">
Wireframe
</option>
</select>
</div>
<div class="form-group">
<label for="passphraseOld">Passphrase Old</label>
<input type="password" id="passphraseOld" class="form-control">
</div>
<div class="form-group">
<label for="passphraseNew">Passphrase New</label>
<input type="password" id="passphraseNew" class="form-control">
</div>
<div class="form-group">
<button class="btn btn-danger form-control">Change Passphrase</button>
</div>
</form>
</div>
</div>
<div class="row">
<!-- <div class="col-sm"></div> -->
<div class="col-sm d-lg-block">
<div class="d-flex justify-content-center">
<div class="py-5">
<h4 class="p-2 text-center">Logout</h4>
<form action="#" th:action="@{/logout}" method="POST">
<!-- <input type="submit" value="logout"> -->
<!-- <input type="hidden" th:name="${_csrf.parameterName}"
th:value="${_csrf.token}" /> -->
<div class="form-group">
<button class="btn btn-secondary form-control">Logout</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</header>
</body>
</html>

View File

@ -1,86 +0,0 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<div th:replace="fragments/head :: headFragment">
<title id="pageTitle" th:text="${userName}">New User</title>
</div>
<script src="https://code.jquery.com/jquery-2.1.4.min.js" th:if="false"></script>
<script src="http://blackpeppersoftware.github.io/thymeleaf-fragment.js/thymeleaf-fragment.js"
data-template-prefix="../" defer="defer" th:if="false"></script>
<th:block th:include="fragments/admin :: headFragment"></th:block>
</head>
<!-- TODO
Make user admin / remove user from admin
Change E2E passphrase
Delete Messages
-->
<!-- <div th:include="fragments/admin :: admin-sidebar"></div> -->
<body id="page-top">
<!-- Page Wrapper -->
<div id="wrapper">
<div th:include="fragments/admin :: sidebar-fragment"></div>
<!-- Content Wrapper -->
<div id="content-wrapper" class="d-flex flex-column" style="background-color: #333;">
<!-- Main Content -->
<div id="content">
<div th:include="fragments/admin :: topbar-fragment"></div>
<!-- Begin Page Content -->
<div class="container-fluid">
<!-- Page Heading -->
<div class="d-sm-flex align-items-center justify-content-between mb-4">
<h1 class="h3 mb-0 text-light">Dashboard</h1>
</div>
<!-- Content Row -->
<div class="row">
<div class="col-lg-4 mb-4 offset-lg-4">
<!-- Approach -->
<div class="card bg-dark border border-dark text-white shadow mb-4">
<div class="card-header bg-secondary border border-secondary py-3">
<h6 class="m-0 font-weight-bold text-white">New User</h6>
</div>
<div class="card-body">
page under construction
</div>
</div>
</div>
</div>
</div>
<!-- /.container-fluid -->
</div>
<!-- End of Main Content -->
<div th:include="fragments/admin :: footer"></div>
</div>
<!-- End of Content Wrapper -->
</div>
<!-- End of Page Wrapper -->
<th:block th:include="fragments/admin :: modal"></th:block>
<div th:if="false">
<th:block th:include="admin :: modal"></th:block>
</div>
</body>
</html>

View File

@ -0,0 +1,18 @@
<html>
<head xmlns:th="http://www.thymeleaf.org">
<title></title>
<!--
<link th:href="@{/css/things.css}" rel="stylesheet"></link>
JS
<script th:src="@{/js/things.js}" type="text/javascript"></script> -->
<script th:src="@{js/scljs.js}" type="text/javascript"></script>
<script th:src="@{js/my_Crypto.js}" type="text/javascript"></script>
</head>
<body>
<div id="crypt"></div>
</body>
</html>

View File

@ -51,7 +51,7 @@
<link href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" rel="stylesheet">
<link href="http://fonts.googleapis.com/css?family=Roboto:400,700,300" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/startbootstrap-sb-admin-2/4.0.7/css/sb-admin-2.min.css" integrity="sha512-FXgL8f6gtCYx8PjODtilf5GCHlgTDdIVZKRcUT/smwfum7hr4M1ytewqTtNd9LK4/CzbW4czU6Tr3f3Xey6lRg==" crossorigin="anonymous" />
<link th:href="@{/css/sb-admin-2.css}" href="../../static/css/sb-admin-2.css" rel="stylesheet">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

View File

@ -6,13 +6,10 @@
<meta charset="UTF-8">
<title id="pageTitle">Navbar Fragment</title>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.15.0/esm/popper.js" type="module"
th:if="false"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.css" th:if="false"
rel="stylesheet" type="text/css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.15.0/esm/popper.js" type="module" th:if="false"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.css" th:if="false" rel="stylesheet" type="text/css">
<script src="https://code.jquery.com/jquery-2.1.4.min.js" th:if="false"></script>
<script src="http://blackpeppersoftware.github.io/thymeleaf-fragment.js/thymeleaf-fragment.js" defer="defer"
th:if="false"></script>
<script src="http://blackpeppersoftware.github.io/thymeleaf-fragment.js/thymeleaf-fragment.js" defer="defer" th:if="false"></script>
</head>
<body>
@ -21,6 +18,47 @@
<nav class="navbar navbar-expand-sm bg-dark navbar-dark fixed-top">
<div class="container">
<a href="home.html" th:href="@{/}" class="navbar-brand">Chatto</a>
<!-- <div class="navbar-header">
<button class="navbar-toggler" data-toggle="collapse" data-target="#navbarCollapse">
<i class="fas fa-chevron-down"></i>
</button>
</div>
<div class="collapse navbar-collapse" id="navbarCollapse">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a href="home.html" th:href="@{/}" class="nav-link">Home</a>
</li>
<li class="nav-item">
<a href="user/home.html" sec:authorize="isFullyAuthenticated()" th:href="@{/user}" class="nav-link">User Area</a>
</li>
<li class="nav-item">
<a th:href="chat" href="chat.html" class="nav-link">Chat</a>
</li>
<li class="nav-item">
<a th:href="login" sec:authorize="!isFullyAuthenticated()" href="login.html" class="nav-link">Login</a>
</li>
<li class="nav-item">
<a th:href="registration" sec:authorize="!isFullyAuthenticated()" href="registration.html" class="nav-link">Register</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">About</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">Contact</a>
</li>
<li class="nav-item">
<a href="#" sec:authorize="isFullyAuthenticated()" th:href="@{/admin}" class="nav-link">
Admin Area
</a>
</li>
<li class="nav-item">
<a href="#" sec:authorize="isFullyAuthenticated()" th:text="${#authentication.name}" class="nav-link text-white font-weight-bold">
nova
</a>
</li>
</ul>
</div> -->
<th:block th:include="fragments/navbar :: navbarContent"></th:block>
</div>
</nav>
@ -28,46 +66,42 @@
</th:block>
<th:block th:fragment="navbarContent">
<div class="navbar-header">
<button class="navbar-toggler" data-toggle="collapse" data-target="#navbarCollapse">
<i class="fas fa-chevron-down"></i>
</button>
</div>
<div class="collapse navbar-collapse" id="navbarCollapse">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a href="home.html" th:href="@{/}" class="nav-link">Home</a>
</li>
<li class="nav-item">
<a href="user/home.html" sec:authorize="isFullyAuthenticated()" th:href="@{/user}"
class="nav-link">User Area</a>
</li>
<li class="nav-item">
<a th:href="@{/chat}" href="chat.html" class="nav-link">Chat</a>
</li>
<li class="nav-item">
<a th:href="login" sec:authorize="!isFullyAuthenticated()" href="login.html"
class="nav-link">Login</a>
</li>
<li class="nav-item">
<a th:href="registration" sec:authorize="!isFullyAuthenticated()" href="registration.html"
class="nav-link">Register</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">About</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">Contact</a>
</li>
<li class="nav-item">
<a href="#" sec:authorize="hasRole('ROLE_ADMIN') || hasRole('ROLE_SUPER_USER')" th:href="@{/admin}"
class="nav-link">
Admin Area
</a>
</li>
<li class="nav-item">
<a href="#" sec:authorize="isFullyAuthenticated()" th:text="${#authentication.name}" class="nav-link text-white font-weight-bold">
<div class="navbar-header">
<button class="navbar-toggler" data-toggle="collapse" data-target="#navbarCollapse">
<i class="fas fa-chevron-down"></i>
</button>
</div>
<div class="collapse navbar-collapse" id="navbarCollapse">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a href="home.html" th:href="@{/}" class="nav-link">Home</a>
</li>
<li class="nav-item">
<a href="user/home.html" sec:authorize="isFullyAuthenticated()" th:href="@{/user}" class="nav-link">User Area</a>
</li>
<li class="nav-item">
<a th:href="@{/chat}" href="chat.html" class="nav-link">Chat</a>
</li>
<li class="nav-item">
<a th:href="login" sec:authorize="!isFullyAuthenticated()" href="login.html" class="nav-link">Login</a>
</li>
<li class="nav-item">
<a th:href="registration" sec:authorize="!isFullyAuthenticated()" href="registration.html" class="nav-link">Register</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">About</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">Contact</a>
</li>
<li class="nav-item">
<a href="#" sec:authorize="hasRole('ROLE_ADMIN') || hasRole('ROLE_SUPER_USER')" th:href="@{/admin}" class="nav-link">
Admin Area
</a>
</li>
<li class="nav-item">
<a href="#" sec:authorize="isFullyAuthenticated()" th:text="${#authentication.name}" class="nav-link text-white font-weight-bold">
nova
</a>
</li>

View File

@ -15,68 +15,65 @@
// @RunWith(SpringRunner.class)
// @SpringBootTest
// public class ChattoApplicationTests {
// // @Autowired
// // ChatMessageRepository chatMessageRepository;
// // @Autowired
// // ChatMessageRepository chatMessageRepository;
// //
// // @Mock
// // ChatMessageRepository mockChatMessageRepository;
// //
// @Autowired
// private UserRepository userRepository;
// @Autowired
// private UserRoleRepository userRoleRepository;
// private final Logger logger = LoggerFactory.getLogger(ChattoApplicationTests.class);
// //
// // @Mock
// // ChatMessageRepository mockChatMessageRepository;
// @Test
// public void contextLoads() {
// }
// //
// @Autowired
// private UserRepository userRepository;
// @Autowired
// private UserRoleRepository userRoleRepository;
// private final Logger logger =
// LoggerFactory.getLogger(ChattoApplicationTests.class);
// //
// @Test
// public void contextLoads() {
// }
// //
// // @Test
// // public void testMessageRepo() {
// // chatMessageRepository.findAll().toString();
// // }
// @Test
// public void testRoleRepo() {
// List<String> list = userRoleRepository.getAllRegularUser();
// logger.info("List = {} ", list);
// }
// @Test
// public void findAllOtherUsers() {
// logger.info("Usernames =
// {}",userRepository.findAllOtherUserNames("hmm").toString());
// }
// // @Test
// // public void testSave() {
// // ChatUser fromUser = new ChatUser();
// // fromUser = userRepository.findByUserName("hmm");
// // ChatUser toUser = new ChatUser();
// // toUser = userRepository.findByUserName("user2");
// // ChatMessage chatMessage = new ChatMessage();
// // chatMessage.setMessage("Hello!");
// // chatMessage.setFromUser(fromUser);
// // chatMessage.setToUser(toUser);
// //
// // chatMessageRepository.save(chatMessage);
// // }
// /*
// * @Test public void testSave() { ChatUser fromUser = new ChatUser(); fromUser
// =
// * userRepository.findByUserName("hmm"); ChatUser toUser = new ChatUser();
// * toUser = userRepository.findByUserName("user2"); ChatMessage chatMessage =
// * new ChatMessage(); chatMessage.setMessage("Hello!");
// * chatMessage.setFromUser(fromUser); chatMessage.setToUser(toUser);
// *
// * // chatMessageRepository.save(chatMessage);
// * when(mockChatMessageRepository.save(any(ChatMessage.class))).thenReturn(
// * chatMessage); verify(mockChatMessageRepository,
// * times(1)).save(Mockito.any(ChatMessage.class)); }
// */
// // @Test
// // public void testMessageRepo() {
// // chatMessageRepository.findAll().toString();
// // }
// @Test
// public void testRoleRepo() {
// List<String> list = userRoleRepository.getAllRegularUser();
// logger.info("List = {} ", list);
// }
// @Test
// public void findAllOtherUsers() {
// logger.info("Usernames = {}",userRepository.findAllOtherUserNames("hmm").toString());
// }
// // @Test
// // public void testSave() {
// // ChatUser fromUser = new ChatUser();
// // fromUser = userRepository.findByUserName("hmm");
// // ChatUser toUser = new ChatUser();
// // toUser = userRepository.findByUserName("user2");
// // ChatMessage chatMessage = new ChatMessage();
// // chatMessage.setMessage("Hello!");
// // chatMessage.setFromUser(fromUser);
// // chatMessage.setToUser(toUser);
// //
// // chatMessageRepository.save(chatMessage);
// // }
// /*
// * @Test public void testSave() { ChatUser fromUser = new ChatUser(); fromUser =
// * userRepository.findByUserName("hmm"); ChatUser toUser = new ChatUser();
// * toUser = userRepository.findByUserName("user2"); ChatMessage chatMessage =
// * new ChatMessage(); chatMessage.setMessage("Hello!");
// * chatMessage.setFromUser(fromUser); chatMessage.setToUser(toUser);
// *
// * // chatMessageRepository.save(chatMessage);
// * when(mockChatMessageRepository.save(any(ChatMessage.class))).thenReturn(
// * chatMessage); verify(mockChatMessageRepository,
// * times(1)).save(Mockito.any(ChatMessage.class)); }
// */
// }

View File

@ -32,62 +32,61 @@
// @RunWith(MockitoJUnitRunner.class)
// @Slf4j
// public class UnitTest {
// @InjectMocks
// @InjectMocks
// // private RoleService roleService;
// // private UserTokenService userTokenService;
// // private RoleService roleService;
// // private UserTokenService userTokenService;
// @Mock
// private UserRoleRepository userRoleRepository;
// @Mock
// private UserRoleRepository userRoleRepository;
// @Mock
// private PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
// @Mock
// private PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
// @Mock
// private RoleRepository roleRepository;
// @Mock
// private RoleRepository roleRepository;
// @Mock
// private UserRepositoryCustom userRepositoryCustom;
// @Mock
// private UserRepositoryCustom userRepositoryCustom;
// @Mock
// private UserSessionRepository userSessionRepository;
// @Mock
// private UserSessionRepository userSessionRepository;
// @Mock
// private UserRepository userRepository;
// @Mock
// private UserRepository userRepository;
// private UserService userService = new UserServiceImpl(userRepository,
// userRoleRepository, passwordEncoder, roleRepository,
// userSessionRepository);
// // private ChatUser chatUser;
// private UserService userService = new UserServiceImpl(userRepository, userRoleRepository, passwordEncoder, roleRepository,
// userSessionRepository);
// // private ChatUser chatUser;
// @Before
// public void setupMock() {
// // userRepository = mock(UserRepository.class);
// // chatUser = mock(ChatUser.class);
// }
// @Before
// public void setupMock() {
// // userRepository = mock(UserRepository.class);
// // chatUser = mock(ChatUser.class);
// }
// @Test
// public void userRegistrationTest() {
// UserRegistrationDTO userRegistrationDTO = new UserRegistrationDTO();
// userRegistrationDTO.setUserName("mickey");
// userRegistrationDTO.setPassword("mouse");
// @Test
// public void userRegistrationTest() {
// UserRegistrationDTO userRegistrationDTO = new UserRegistrationDTO();
// userRegistrationDTO.setUserName("mickey");
// userRegistrationDTO.setPassword("mouse");
// ChatUser chatUser = new ChatUser();
// chatUser.setUserName("mickey");
// // chatUser.setPassword("mouse");
// ChatUser chatUser = new ChatUser();
// chatUser.setUserName("mickey");
// // chatUser.setPassword("mouse");
// Role role = new Role();
// role.setRoleID(2);
// role.setName("USER");
// when(roleRepository.findByName("USER")).thenReturn(role);
// when(userRepository.save(chatUser)).thenReturn(chatUser);
// Role role = new Role();
// role.setRoleID(2);
// role.setName("USER");
// when(roleRepository.findByName("USER")).thenReturn(role);
// when(userRepository.save(chatUser)).thenReturn(chatUser);
// UserRole userRole = userService.registerUser(userRegistrationDTO);
// assertArrayEquals(new Object[] { 2, "USER","mickey" },
// new Object[] { userRole.getRole().getRoleID(),
// userRole.getRole().getName(), userRole.getUser().getUserName() });
// verify(userRepository, times(1)).save(chatUser);
// verify(userRoleRepository,times(1)).save(userRole);
// }
// UserRole userRole = userService.registerUser(userRegistrationDTO);
// assertArrayEquals(new Object[] { 2, "USER","mickey" },
// new Object[] { userRole.getRole().getRoleID(),
// userRole.getRole().getName(), userRole.getUser().getUserName() });
// verify(userRepository, times(1)).save(chatUser);
// verify(userRoleRepository,times(1)).save(userRole);
// }
// }

1011
yarn.lock

File diff suppressed because it is too large Load Diff