-
Notifications
You must be signed in to change notification settings - Fork 64
Sprint2 sign in #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: Sprint2Starter
Are you sure you want to change the base?
Sprint2 sign in #13
Changes from 37 commits
4ff3530
656e63d
4104a9b
6543709
0fbae3b
51bb8c9
ba4db6c
6592a95
e5034e6
8c54573
574097f
de50cc6
7be099d
5bb9e8d
090ceca
deb0e6c
1f4a16c
6e01ed0
14eb849
8c0c4ec
6cccec9
17f6aae
10a3d1b
e3e08ef
58d8522
684e30b
8aaa04a
e8710bd
e51c663
88f56e0
30c4115
f3ba2f7
47f1ec5
7cf7dd5
51ce713
a58bcb8
8553e77
466de2f
f8a1bea
7c6b0c2
dbeeeeb
babf1cf
056f216
8ea2876
eaadfdc
168d0bd
be672ee
2825e5d
1f8aae9
cf94ca2
3322013
746fdd8
a70343f
077b1bd
68488ea
8753963
5764c0c
56a3d31
7b41b06
9bc6eba
9de52e0
72e856b
0e1cb9c
14dfe35
16e1198
34c527d
1310e9e
c822388
ac08405
4f952d3
236c05d
4f20f8d
0b6168f
24b4c2e
dbb417c
223ddf5
126556b
e6c4575
455d6ad
c250a87
366ce39
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| document.addEventListener("DOMContentLoaded", function(event) { | ||
| const employeeIdEditElement = getEmployeeId(); | ||
| employeeIdEditElement.focus(); | ||
| employeeIdEditElement.select(); | ||
| }); | ||
|
|
||
| //Get elements for the Employee Id and Password | ||
| function getEmployeeId() { | ||
| return document.getElementById("employeeId"); | ||
| } | ||
|
|
||
| function getPassword() { | ||
| return document.getElementById("password"); | ||
| } | ||
|
|
||
| //Validates the Id is not blank and is a number; Validates that Password is not null | ||
| function validateForm() { | ||
| const employeeIdEditElement = getEmployeeId(); | ||
|
Comment on lines
+1
to
+18
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. change |
||
| if(isNaN(Number(employeeIdEditElement.value)) || (Number(employeeIdEditElement.value) <= 0)) | ||
| { | ||
| displayError("Employee Id must be a positive numerical value."); | ||
|
|
||
| employeeIdEditElement.focus(); | ||
| employeeIdEditElement.select(); | ||
| return false; | ||
| } | ||
|
|
||
| const passwordEditElement = getPassword(); | ||
| if ((passwordEditElement.value == null) || (passwordEditElement.value.trim() === "")) | ||
| { | ||
| displayError("Password must be valid and cannot be blank."); | ||
|
|
||
| passwordEditElement.focus(); | ||
|
Comment on lines
+19
to
+33
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here |
||
| passwordEditElement.select(); | ||
| return false; | ||
|
|
||
| } | ||
| return true; | ||
| } | ||
|
Comment on lines
+34
to
+39
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package edu.uark.registerapp.commands.activeUsers; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| import org.apache.commons.lang3.StringUtils; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.pringframework.transaction.annotation.Transactional; | ||
|
|
||
|
|
||
| import edu.uark.registerapp.commands.VoidCommandInterface; | ||
| import edu.uark.registerapp.nodels.entities.ActiveUserEntity; | ||
| import edu.uark.registerapp.models.repositories.ActiveUserRepository; | ||
|
|
||
| @Service | ||
| public class ActiveUserDeleteCommand implements VoidCommandInterface{ | ||
| @Override | ||
| @Transactional | ||
| public void execute(){ | ||
| final Optional<ActiveUserEntity> activeUserEntity= | ||
| this.activeUserRepository.findBySessionKey(this.sessionKey); | ||
|
|
||
| if(!activeUserEntity.isPresent()){ | ||
| this.activeUserRepository.delete(activeUserEntity.get()); | ||
| } | ||
| } | ||
|
|
||
| private String sessionKey; | ||
|
|
||
| public String getSessionKey(){ | ||
| return this.sessionKey; | ||
| } | ||
|
|
||
| public ActiveUserDeleteCommand setSessionKey(final String sessionKey){ | ||
| this.sessionKey = sessionKey; | ||
| return this; | ||
| } | ||
|
|
||
| @Autowired | ||
| private ActiveUserRepository activeUserRepository; | ||
| } | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package edu.uark.registerapp.commands.employees; | ||
|
|
||
| import org.apache.commons.lang3.StringUtils; | ||
| import edu.uark.registerapp.models.repositories.EmployeeRepository; | ||
| import edu.uark.registerapp.commands.exceptions.NotFoundException; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| @Service | ||
| public class ActiveEmployeeExistsQuery implements EmployeeRepository{ | ||
| @Override | ||
| public void ActiveEmployeeExistsQuery(){ | ||
| if(!this.employeeRepository.existsByIsActive(true)){ | ||
| throw new NotFoundException("Employee"); | ||
| } | ||
| } | ||
|
|
||
| @Autowire | ||
| private EmployeeRepository employeeRepository; | ||
| } | ||
|
Comment on lines
+1
to
+20
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| package edu.uark.registerapp.commands.employees; | ||
|
|
||
| import org.apache.commons.lang3.StringUtils; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import edu.uark.registerapp.commands.ResultCommandInterface; | ||
| import edu.uark.registerapp.commands.employees.helpers.EmployeeHelper; | ||
| import edu.uark.registerapp.commands.exceptions.UnauthorizedException; | ||
| import edu.uark.registerapp.commands.exceptions.UnprocessableEntityException; | ||
| import edu.uark.registerapp.models.api.Employee; | ||
| import edu.uark.registerapp.models.api.EmployeeSignIn; | ||
| import edu.uark.registerapp.models.entities.ActiveUserEntity; | ||
| import edu.uark.registerapp.models.entities.EmployeeEntity; | ||
| import edu.uark.registerapp.models.repositories.ActiveUesrRepository; | ||
| import edu.uark.registerapp.models.repositories.EmployeeRepository; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Optional; | ||
| import javax.transaction.Transactional; | ||
|
|
||
| @Service | ||
| public class EmployeeSignInCommand implements ResultCommandInterface<Employee>{ | ||
| @Override | ||
| public Employee execute(){ | ||
| this.validateProperties(); | ||
| return new Employee(this.SignInEmployee()); | ||
| } | ||
|
|
||
| private void validateProperties(){ | ||
| if(StringUtils.isBlank(this.employeeSignIn.getEmployeeId()){ | ||
| throw new UnprocessableEntityException("Employee ID"); | ||
| } | ||
| try{ | ||
| Integer.parseInt(this.employeeSignIn.getEmployeeId()); | ||
| } catch(final NumberFormatException e){ | ||
| throw new UnprocessableEntityException("Employee ID"); | ||
| } if(StringUtils.isBlank(this.employeeSignin.getPassword()){ | ||
| throw new UnprocessableEntityException("Password"); | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here |
||
| } | ||
| } | ||
|
|
||
| @Transactional | ||
| private EmployeeEntity SignInEmployee(){ | ||
| final Optional<EmployeeEntity> employeeEntity = | ||
| this.employeeRepository.findByEmployeeId( | ||
| Integer.parseInt(this.employeeSignIn.getEmployeeId())); | ||
|
|
||
| if(!employeeEntity.isPresent() || !Arrays.equals(employeeEntity.get().getPassword(), | ||
| EmployeeHelper.hashPassword(this.employeeSignIn.getPassword())){ | ||
| throw new UnauthorizedException(); | ||
| } | ||
|
|
||
| final Optional<ActiveUserEntity> activeUserEntity = | ||
| this.activeUserRepository.findByEmployeeId(employeeEntity.get().getId()); | ||
|
|
||
| if(!activeUserRepository.isPresent()){ | ||
| this.activeUserRepository.save((new ActiveUserEntity()).setSessionKey(this.sessionId) | ||
| .setEmployeeId(employeeEntity.get().getId()).setClassification( | ||
| employeeEntity.get().getClassification()) | ||
| .setName(employeeEntity.get().getFirstName().concat(" "). | ||
| .concat(employeeEntity.get().getLastName()))); | ||
| } else{ | ||
| this.activeUserRepository.save(activeUserEntity.get().setSessionKey(this.sessionId)); | ||
| } | ||
|
|
||
| return employeeEntity.get(); | ||
| } | ||
|
|
||
| private EmployeeSignIn employeeSignIn; | ||
|
|
||
| public EmployeeSignIn getEmployeeSignIn(){ | ||
| return this.employeeSignIn; | ||
| } | ||
|
|
||
| public EmployeeSignInCommand setEmployeeSignIn(final EmployeeSignIn employeeSignIn){ | ||
| this.employeeSignIn = employeeSignIn; | ||
|
|
||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here |
||
| return this; | ||
| } | ||
|
|
||
| private String sessionId; | ||
|
|
||
| public String getSessionId(){ | ||
| return this.sessionId; | ||
| } | ||
|
|
||
| public EmployeeSignInCommand setSessionId(final String sessionId){ | ||
| this.sessionId = sessionId; | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| @Autowired | ||
| private EmployeeRepository employeeRepository; | ||
|
|
||
| @Autowired | ||
| private ActiveUserRepository activeUserRepository; | ||
| } | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package edu.uark.registerapp.controllers; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.Optional; | ||
|
|
||
| import javax.servlet.http.HttpServletRequest; | ||
|
|
||
| import org.springframework.stereotype.Controller; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RequestMethod; | ||
| import org.springframework.web.bind.annotation.RequestParam; | ||
| import org.springframework.web.servlet.ModelAndView; | ||
|
|
||
| import edu.uark.registerapp.controllers.enums.ViewModelNames; | ||
| import edu.uark.registerapp.controllers.enums.ViewNames; | ||
| import edu.uark.registerapp.models.entities.ActiveUserEntity; | ||
|
|
||
| @Controller | ||
| @RequestMapping(value = "/mainMenu") | ||
| public class MainMenuRouteController extends BaseRouteController { | ||
| @RequestMapping(method = RequestMethod.GET) | ||
| public ModelAndView start( | ||
| @RequestParam final Map<String, String> queryParameters, | ||
| final HttpServletRequest request | ||
| ) { | ||
|
|
||
| final Optional<ActiveUserEntity> activeUserEntity = | ||
| this.getCurrentUser(request); | ||
| if (!activeUserEntity.isPresent()) { | ||
| return this.buildInvalidSessionResponse(); | ||
| } | ||
|
|
||
| ModelAndView modelAndView = | ||
| this.setErrorMessageFromQueryString( | ||
| new ModelAndView(ViewNames.MAIN_MENU.getViewName()), | ||
| queryParameters); | ||
| //start | ||
| // TODO: Examine the ActiveUser classification if you want this information | ||
|
|
||
| modelAndView.addObject( | ||
| ViewModelNames.IS_ELEVATED_USER.getValue(), | ||
|
Comment on lines
+1
to
+40
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here |
||
| true); | ||
|
|
||
| return modelAndView; | ||
| } | ||
| } | ||
|
Comment on lines
+42
to
+46
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package edu.uark.registerapp.controllers; | ||
|
|
||
| import javax.servlet.http.HttpServletRequest; | ||
|
|
||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RequestMethod; | ||
| import org.springframework.web.bind.annotation.ResponseBody; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| import edu.uark.registerapp.commands.activeUsers.ActiveUserDeleteCommand; | ||
| import edu.uark.registerapp.controllers.enums.ViewNames; | ||
| import edu.uark.registerapp.models.api.ApiResponse; | ||
|
|
||
| @RestController | ||
| @RequestMapping(value = "/api") | ||
| public class SignInRestController extends BaseRestController { | ||
| @RequestMapping(value="/signOut", method = RequestMethod.DELETE) | ||
| public @ResponseBody ApiResponse removeActiveUser( | ||
| final HttpServletRequest request | ||
| ) { | ||
|
|
||
| this.activeUserDeleteCommand | ||
| .setSessionKey(request.getSession().getId()) | ||
| .execute(); | ||
|
|
||
| return (new ApiResponse()) | ||
| .setRedirectUrl(ViewNames.SIGN_IN.getRoute()); | ||
| } | ||
|
|
||
| // Properties | ||
| @Autowired | ||
| private ActiveUserDeleteCommand activeUserDeleteCommand; | ||
| } | ||
|
Comment on lines
+1
to
+34
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| package edu.uark.registerapp.controllers; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| import javax.servlet.http.HttpServletRequest; | ||
|
|
||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.http.MediaType; | ||
| import org.springframework.stereotype.Controller; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RequestMethod; | ||
| import org.springframework.web.bind.annotation.RequestParam; | ||
| import org.springframework.web.servlet.ModelAndView; | ||
|
|
||
| import edu.uark.registerapp.commands.employees.ActiveEmployeeExistsQuery; | ||
| import edu.uark.registerapp.commands.employees.EmployeeSignInCommand; | ||
| import edu.uark.registerapp.commands.exceptions.NotFoundException; | ||
| import edu.uark.registerapp.controllers.enums.QueryParameterNames; | ||
| import edu.uark.registerapp.controllers.enums.ViewModelNames; | ||
| import edu.uark.registerapp.controllers.enums.ViewNames; | ||
| import edu.uark.registerapp.models.api.EmployeeSignIn; | ||
|
|
||
| @Controller | ||
| @RequestMapping(value = "/") | ||
| public class SignInRouteController extends BaseRouteController { | ||
| @RequestMapping(method = RequestMethod.GET) | ||
| public ModelAndView showSignIn( | ||
| @RequestParam final Map<String, String> queryParameters | ||
| ) { | ||
|
|
||
| try { | ||
| this.activeEmployeeExistsQuery.execute(); | ||
|
Comment on lines
+1
to
+32
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here |
||
| } catch (NotFoundException e) { | ||
| return new ModelAndView( | ||
| REDIRECT_PREPEND.concat( | ||
| ViewNames.EMPLOYEE_DETAIL.getRoute())); | ||
| } | ||
|
|
||
| ModelAndView modelAndView = | ||
| this.setErrorMessageFromQueryString( | ||
| new ModelAndView(ViewNames.SIGN_IN.getViewName()), | ||
| queryParameters); | ||
|
|
||
| if (queryParameters.containsKey(QueryParameterNames.EMPLOYEE_ID.getValue())) { | ||
| modelAndView.addObject( | ||
| ViewModelNames.EMPLOYEE_ID.getValue(), | ||
| queryParameters.get(QueryParameterNames.EMPLOYEE_ID.getValue())); | ||
| } | ||
|
|
||
| return modelAndView; | ||
| } | ||
|
|
||
| @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) | ||
| public ModelAndView performSignIn( | ||
| EmployeeSignIn employeeSignIn, | ||
| HttpServletRequest request | ||
| ) { | ||
|
|
||
| try { | ||
| this.employeeSignInCommand | ||
| .setSessionId(request.getSession().getId()) | ||
| .setEmployeeSignIn(employeeSignIn) | ||
| .execute(); | ||
| } catch (Exception e) { | ||
| ModelAndView modelAndView = | ||
| new ModelAndView(ViewNames.SIGN_IN.getViewName()); | ||
|
|
||
| modelAndView.addObject( | ||
| ViewModelNames.ERROR_MESSAGE.getValue(), | ||
| e.getMessage()); | ||
| modelAndView.addObject( | ||
| ViewModelNames.EMPLOYEE_ID.getValue(), | ||
| employeeSignIn.getEmployeeId()); | ||
|
|
||
|
Comment on lines
+33
to
+74
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here |
||
| return modelAndView; | ||
| } | ||
|
|
||
| return new ModelAndView( | ||
| REDIRECT_PREPEND.concat( | ||
| ViewNames.MAIN_MENU.getRoute())); | ||
| } | ||
|
|
||
| // Properties | ||
| @Autowired | ||
| private EmployeeSignInCommand employeeSignInCommand; | ||
|
|
||
| @Autowired | ||
| private ActiveEmployeeExistsQuery activeEmployeeExistsQuery; | ||
| } | ||
|
Comment on lines
+75
to
+89
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
review