Skip to content

Commit

Permalink
Development (#25)
Browse files Browse the repository at this point in the history
* hot fix UI on user modification, add reff example from schema component, enhance path mock

* enhance crud and add security configuration and enhance ui fixing some ui

* enhance delete and validation

* enhance delete and validation

* update js

* update js

* enhance pattern

* fixing package log
  • Loading branch information
dekaulitz authored Jul 10, 2020
1 parent 381e089 commit 248646e
Show file tree
Hide file tree
Showing 67 changed files with 345 additions and 223 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.github.dekaulitz.mockyup;

import com.github.dekaulitz.mockyup.db.entities.MockCreatorEntities;
import com.github.dekaulitz.mockyup.db.entities.MockEntities;
import com.github.dekaulitz.mockyup.db.entities.UserEntities;
import com.github.dekaulitz.mockyup.db.entities.UserMocksEntities;
import com.github.dekaulitz.mockyup.db.repositories.MockRepository;
import com.github.dekaulitz.mockyup.db.repositories.UserRepository;
import com.github.dekaulitz.mockyup.infrastructure.db.entities.MockCreatorEntities;
import com.github.dekaulitz.mockyup.infrastructure.db.entities.MockEntities;
import com.github.dekaulitz.mockyup.infrastructure.db.entities.UserEntities;
import com.github.dekaulitz.mockyup.infrastructure.db.entities.UserMocksEntities;
import com.github.dekaulitz.mockyup.infrastructure.db.repositories.MockRepository;
import com.github.dekaulitz.mockyup.infrastructure.db.repositories.UserRepository;
import com.github.dekaulitz.mockyup.utils.Hash;
import com.github.dekaulitz.mockyup.utils.Role;
import org.slf4j.Logger;
Expand Down Expand Up @@ -48,6 +48,7 @@ public void run(String... args) throws Exception {
UserEntities rootUser = new UserEntities();
rootUser.setUsername("root");
rootUser.setPassword(Hash.hashing("root"));
rootUser.setUpdatedDate(new Date());
rootUser.setAccessList(Arrays.asList(Role.MOCKS_READ_WRITE.name(), Role.USERS_READ_WRITE.name()));
this.userRepository.save(rootUser);
//get all mocks that has not users on the mock
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.github.dekaulitz.mockyup.controllers;
package com.github.dekaulitz.mockyup.base.controller;

import com.github.dekaulitz.mockyup.infrastructure.errors.vmodels.ResponseVmodel;
import com.github.dekaulitz.mockyup.utils.ConstantsRepository;
import com.github.dekaulitz.mockyup.utils.ResponseCode;
import com.github.dekaulitz.mockyup.vmodels.ResponseVmodel;
import lombok.extern.log4j.Log4j2;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.github.dekaulitz.mockyup.controllers;
package com.github.dekaulitz.mockyup.base.controller;

import com.github.dekaulitz.mockyup.configuration.security.AuthenticationProfileModel;
import com.github.dekaulitz.mockyup.errorhandlers.DuplicateDataEntry;
import com.github.dekaulitz.mockyup.errorhandlers.InvalidMockException;
import com.github.dekaulitz.mockyup.errorhandlers.NotFoundException;
import com.github.dekaulitz.mockyup.errorhandlers.UnathorizedAccess;
import com.github.dekaulitz.mockyup.infrastructure.configuration.security.AuthenticationProfileModel;
import com.github.dekaulitz.mockyup.infrastructure.errors.handlers.DuplicateDataEntry;
import com.github.dekaulitz.mockyup.infrastructure.errors.handlers.InvalidMockException;
import com.github.dekaulitz.mockyup.infrastructure.errors.handlers.NotFoundException;
import com.github.dekaulitz.mockyup.infrastructure.errors.handlers.UnathorizedAccess;
import com.github.dekaulitz.mockyup.infrastructure.errors.vmodels.ErrorVmodel;
import com.github.dekaulitz.mockyup.infrastructure.errors.vmodels.ResponseVmodel;
import com.github.dekaulitz.mockyup.utils.ConstantsRepository;
import com.github.dekaulitz.mockyup.utils.MockHelper;
import com.github.dekaulitz.mockyup.utils.ResponseCode;
import com.github.dekaulitz.mockyup.vmodels.ErrorVmodel;
import com.github.dekaulitz.mockyup.vmodels.ResponseVmodel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.github.dekaulitz.mockyup.controllers;

import com.github.dekaulitz.mockyup.models.UserModel;
import com.github.dekaulitz.mockyup.base.controller.BaseController;
import com.github.dekaulitz.mockyup.domain.auth.base.AuthInterface;
import com.github.dekaulitz.mockyup.domain.auth.vmodels.DoAuthVmodel;
import com.github.dekaulitz.mockyup.utils.JwtManager;
import com.github.dekaulitz.mockyup.vmodels.UserLoginVmodel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
Expand All @@ -15,19 +16,19 @@
import javax.validation.Valid;

@RestController
public class UserAuthController extends BaseController {
public class AuthController extends BaseController {
@Autowired
private final UserModel userModel;
private final AuthInterface authInterfaceModel;

public UserAuthController(UserModel userModel) {
this.userModel = userModel;
public AuthController(AuthInterface authInterfaceModel) {
this.authInterfaceModel = authInterfaceModel;
}


@PostMapping(value = "/mocks/login", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Object> login(@Valid @RequestBody UserLoginVmodel vmodel, HttpServletRequest request) {
public ResponseEntity<Object> login(@Valid @RequestBody DoAuthVmodel vmodel, HttpServletRequest request) {
try {
return ResponseEntity.ok(this.userModel.doLogin(vmodel));
return ResponseEntity.ok(this.authInterfaceModel.generateAccessToken(vmodel.getUsername(), vmodel.getPassword()));
} catch (Exception unathorizedAccess) {
return this.handlingErrorResponse(unathorizedAccess, request);
}
Expand All @@ -38,7 +39,7 @@ public ResponseEntity<Object> logOut(HttpServletRequest request) {
try {
String headerAuth = request.getHeader("Authorization");
String authorization = JwtManager.getAuthorizationHeader(headerAuth);
return ResponseEntity.ok(this.userModel.refreshToken(authorization));
return ResponseEntity.ok(this.authInterfaceModel.refreshingToken(authorization));
} catch (Exception e) {
return this.handlingErrorResponse(e, request);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.github.dekaulitz.mockyup.controllers;

import com.github.dekaulitz.mockyup.base.controller.BaseController;
import com.github.dekaulitz.mockyup.infrastructure.errors.vmodels.ResponseVmodel;
import com.github.dekaulitz.mockyup.utils.ResponseCode;
import com.github.dekaulitz.mockyup.vmodels.ResponseVmodel;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package com.github.dekaulitz.mockyup.controllers;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.dekaulitz.mockyup.db.entities.MockEntities;
import com.github.dekaulitz.mockyup.db.repositories.paging.MockEntitiesPage;
import com.github.dekaulitz.mockyup.errorhandlers.UnathorizedAccess;
import com.github.dekaulitz.mockyup.models.MockModel;
import com.github.dekaulitz.mockyup.base.controller.BaseController;
import com.github.dekaulitz.mockyup.domain.mocks.models.MockModel;
import com.github.dekaulitz.mockyup.domain.mocks.vmodels.DtoMockupDetailVmodel;
import com.github.dekaulitz.mockyup.domain.mocks.vmodels.MockVmodel;
import com.github.dekaulitz.mockyup.domain.users.vmodels.AddUserAccessVmodel;
import com.github.dekaulitz.mockyup.infrastructure.db.entities.MockEntities;
import com.github.dekaulitz.mockyup.infrastructure.db.repositories.paging.MockEntitiesPage;
import com.github.dekaulitz.mockyup.infrastructure.errors.handlers.UnathorizedAccess;
import com.github.dekaulitz.mockyup.utils.MockHelper;
import com.github.dekaulitz.mockyup.utils.ResponseCode;
import com.github.dekaulitz.mockyup.vmodels.AddUserAccessVmodel;
import com.github.dekaulitz.mockyup.vmodels.DtoMockupDetailVmodel;
import com.github.dekaulitz.mockyup.vmodels.MockVmodel;
import io.swagger.util.Json;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Paths;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.dekaulitz.mockyup.controllers;

import com.github.dekaulitz.mockyup.base.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.github.dekaulitz.mockyup.controllers;

import com.github.dekaulitz.mockyup.db.entities.UserEntities;
import com.github.dekaulitz.mockyup.db.repositories.paging.UserEntitiesPage;
import com.github.dekaulitz.mockyup.models.UserModel;
import com.github.dekaulitz.mockyup.vmodels.RegistrationResponseVmodel;
import com.github.dekaulitz.mockyup.vmodels.RegistrationVmodel;
import com.github.dekaulitz.mockyup.vmodels.UpdateUserVmodel;
import com.github.dekaulitz.mockyup.base.controller.BaseController;
import com.github.dekaulitz.mockyup.domain.users.models.UserModel;
import com.github.dekaulitz.mockyup.domain.users.vmodels.RegistrationResponseVmodel;
import com.github.dekaulitz.mockyup.domain.users.vmodels.RegistrationVmodel;
import com.github.dekaulitz.mockyup.domain.users.vmodels.UpdateUserVmodel;
import com.github.dekaulitz.mockyup.infrastructure.db.entities.UserEntities;
import com.github.dekaulitz.mockyup.infrastructure.db.repositories.paging.UserEntitiesPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.github.dekaulitz.mockyup.domain.auth.base;

import com.github.dekaulitz.mockyup.domain.auth.vmodels.DtoAuthProfileVmodel;
import com.github.dekaulitz.mockyup.infrastructure.errors.handlers.UnathorizedAccess;

import java.io.UnsupportedEncodingException;

public interface AuthInterface {
DtoAuthProfileVmodel generateAccessToken(String username, String password) throws UnathorizedAccess, UnsupportedEncodingException;

DtoAuthProfileVmodel refreshingToken(String token) throws UnathorizedAccess, UnsupportedEncodingException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.github.dekaulitz.mockyup.domain.auth.models;

import com.github.dekaulitz.mockyup.domain.auth.base.AuthInterface;
import com.github.dekaulitz.mockyup.domain.auth.vmodels.DtoAuthProfileVmodel;
import com.github.dekaulitz.mockyup.infrastructure.db.entities.UserEntities;
import com.github.dekaulitz.mockyup.infrastructure.db.repositories.UserRepository;
import com.github.dekaulitz.mockyup.infrastructure.errors.handlers.UnathorizedAccess;
import com.github.dekaulitz.mockyup.utils.Hash;
import com.github.dekaulitz.mockyup.utils.JwtManager;
import com.github.dekaulitz.mockyup.utils.ResponseCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.util.Optional;

@Service
public class AuthModel implements AuthInterface {
@Autowired
private final UserRepository userRepository;

public AuthModel(UserRepository userRepository) {
this.userRepository = userRepository;
}

@Override
public DtoAuthProfileVmodel generateAccessToken(String username, String password) throws UnsupportedEncodingException {
UserEntities userExist = this.userRepository.findFirstByUsername(username);
if (userExist == null) {
throw new UnathorizedAccess(ResponseCode.INVALID_USERNAME_OR_PASSWORD);
}
boolean isAuthenticated = Hash.verifyHash(password, userExist.getPassword());
if (!isAuthenticated) throw new UnathorizedAccess(ResponseCode.INVALID_USERNAME_OR_PASSWORD);
return this.renderingAccessToken(userExist);
}

@Override
public DtoAuthProfileVmodel refreshingToken(String token) throws UnsupportedEncodingException {
Optional<String> userId = JwtManager.getUserIdFromToken(token);
if (!userId.isPresent()) throw new UnathorizedAccess(ResponseCode.TOKEN_INVALID);
Optional<UserEntities> userEntities = this.userRepository.findById(userId.get());
if (!userEntities.isPresent())
throw new UnathorizedAccess(ResponseCode.TOKEN_INVALID);
return this.renderingAccessToken(userEntities.get());
}

private DtoAuthProfileVmodel renderingAccessToken(UserEntities userEntities) throws UnsupportedEncodingException {
DtoAuthProfileVmodel auth = new DtoAuthProfileVmodel();
auth.setId(userEntities.getId());
auth.setAccessMenus(userEntities.getAccessList());
auth.setUsername(userEntities.getUsername());
auth.setToken(JwtManager.generateToken(userEntities.getId(), userEntities.getUsername(), userEntities.getAccessList()));
return auth;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.dekaulitz.mockyup.vmodels;
package com.github.dekaulitz.mockyup.domain.auth.vmodels;

import lombok.*;

Expand All @@ -9,7 +9,7 @@
@Builder
@Getter
@Setter
public class UserLoginVmodel {
public class DoAuthVmodel {
@NotEmpty(message = "Please provide a username")
private String username;
@NotEmpty(message = "Please provide a password")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.github.dekaulitz.mockyup.domain.auth.vmodels;

import lombok.*;

import java.util.List;

@Builder
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter

public class DtoAuthProfileVmodel {
private String id;
private String username;
private String token;
private List<String> accessMenus;
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package com.github.dekaulitz.mockyup.models;
package com.github.dekaulitz.mockyup.domain.mocks.base;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.dekaulitz.mockyup.configuration.security.AuthenticationProfileModel;
import com.github.dekaulitz.mockyup.db.entities.MockEntities;
import com.github.dekaulitz.mockyup.db.entities.UserMocksEntities;
import com.github.dekaulitz.mockyup.errorhandlers.InvalidMockException;
import com.github.dekaulitz.mockyup.errorhandlers.NotFoundException;
import com.github.dekaulitz.mockyup.models.base.BaseMock;
import com.github.dekaulitz.mockyup.domain.mocks.vmodels.MockVmodel;
import com.github.dekaulitz.mockyup.infrastructure.configuration.security.AuthenticationProfileModel;
import com.github.dekaulitz.mockyup.infrastructure.db.entities.MockEntities;
import com.github.dekaulitz.mockyup.infrastructure.db.entities.UserMocksEntities;
import com.github.dekaulitz.mockyup.infrastructure.errors.handlers.InvalidMockException;
import com.github.dekaulitz.mockyup.infrastructure.errors.handlers.NotFoundException;
import com.github.dekaulitz.mockyup.utils.JsonMapper;
import com.github.dekaulitz.mockyup.utils.MockHelper;
import com.github.dekaulitz.mockyup.utils.ResponseCode;
import com.github.dekaulitz.mockyup.utils.Role;
import com.github.dekaulitz.mockyup.vmodels.MockVmodel;
import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.oas.models.*;
import io.swagger.v3.parser.core.models.SwaggerParseResult;
Expand All @@ -27,12 +26,11 @@
import java.util.List;
import java.util.Map;

abstract class BaseMockModel<M, M1> implements BaseMock<M, M1> {
public class BaseMockModel {

protected Logger log = LoggerFactory.getLogger(this.getClass());

public void setSaveMockEntity(MockVmodel body, MockEntities mockEntities, AuthenticationProfileModel authenticationProfileModel) throws InvalidMockException {
SwaggerParseResult result = null;
try {
mockEntities.setSwagger(JsonMapper.mapper().writeValueAsString(body.getSpec()));
mockEntities.setTitle(body.getTitle());
Expand Down Expand Up @@ -70,8 +68,7 @@ private void parsingSpecToOpenApi(MockVmodel body, MockEntities mockEntities) th
mockEntities.setSpec(JsonMapper.mapper().writeValueAsString(openAPI));
}

public void setUpdateMockEntity(MockVmodel body, MockEntities mockEntities, AuthenticationProfileModel authenticationProfileModel) throws InvalidMockException {
SwaggerParseResult result = null;
public void setUpdateMockEntity(MockVmodel body, MockEntities mockEntities) throws InvalidMockException {
try {
mockEntities.setSwagger(JsonMapper.mapper().writeValueAsString(body.getSpec()));
parsingSpecToOpenApi(body, mockEntities);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.github.dekaulitz.mockyup.domain.mocks.base;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.dekaulitz.mockyup.domain.mocks.vmodels.DtoMockLookupVmodel;
import com.github.dekaulitz.mockyup.domain.mocks.vmodels.DtoMockupDetailVmodel;
import com.github.dekaulitz.mockyup.domain.mocks.vmodels.DtoMockupHistoryVmodel;
import com.github.dekaulitz.mockyup.domain.mocks.vmodels.MockVmodel;
import com.github.dekaulitz.mockyup.domain.users.vmodels.AddUserAccessVmodel;
import com.github.dekaulitz.mockyup.infrastructure.configuration.security.AuthenticationProfileModel;
import com.github.dekaulitz.mockyup.infrastructure.db.entities.MockEntities;
import com.github.dekaulitz.mockyup.infrastructure.db.entities.MockHistoryEntities;
import com.github.dekaulitz.mockyup.infrastructure.db.repositories.paging.MockEntitiesPage;
import com.github.dekaulitz.mockyup.infrastructure.errors.handlers.InvalidMockException;
import com.github.dekaulitz.mockyup.infrastructure.errors.handlers.NotFoundException;
import com.github.dekaulitz.mockyup.utils.MockHelper;
import org.springframework.data.domain.Pageable;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.List;

public interface MockInterface {
List<MockEntities> all();

MockEntities getById(String id, AuthenticationProfileModel authenticationProfileModel) throws NotFoundException;

MockEntities save(MockVmodel view, AuthenticationProfileModel authenticationProfileModel) throws InvalidMockException;

MockEntities updateByID(String id, MockVmodel view, AuthenticationProfileModel authenticationProfileModel) throws NotFoundException, InvalidMockException;

void deleteById(String id, AuthenticationProfileModel authenticationProfileModel) throws NotFoundException;

MockEntitiesPage paging(Pageable pageable, String q, AuthenticationProfileModel authenticationProfileModel);

List<MockHistoryEntities> getMockHistories(String id);

MockEntities getUserMocks(String id);

List<DtoMockLookupVmodel> getUsersListOfMocks(String mockId);

List<DtoMockupDetailVmodel> getDetailMockUpIdByUserAccess(String id, AuthenticationProfileModel authenticationProfileModel);

MockHelper getMockMocking(HttpServletRequest request, String path, String id, String body) throws NotFoundException, JsonProcessingException, UnsupportedEncodingException, InvalidMockException;

Object addUserAccessOnMock(String id, AddUserAccessVmodel vmodel, AuthenticationProfileModel authenticationProfileModel) throws NotFoundException;

Object removeAccessUserOnMock(String id, String userId, AuthenticationProfileModel authenticationProfileModel) throws NotFoundException;

DtoMockupHistoryVmodel geMockHistoryId(String id, String historyId, AuthenticationProfileModel authenticationProfileModel) throws NotFoundException;
}
Loading

0 comments on commit 248646e

Please sign in to comment.