This commit is contained in:
Le Deng
2017-03-09 17:01:47 -05:00
parent e50a47f180
commit d11ff78442
6 changed files with 145 additions and 43 deletions

View File

@@ -16,6 +16,16 @@
<component name="ExecutionTargetManager" SELECTED_TARGET="default_target" />
<component name="FileEditorManager">
<leaf SIDE_TABS_SIZE_LIMIT_KEY="300">
<file leaf-file-name="UserResource.java" pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/src/main/java/com/bookstore/resource/UserResource.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="170">
<caret line="207" column="85" lean-forward="true" selection-start-line="207" selection-start-column="85" selection-end-line="207" selection-end-column="85" />
<folding />
</state>
</provider>
</entry>
</file>
<file leaf-file-name="PaymentResource.java" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/src/main/java/com/bookstore/resource/PaymentResource.java">
<provider selected="true" editor-type-id="text-editor">
@@ -40,11 +50,11 @@
</provider>
</entry>
</file>
<file leaf-file-name="UserShipping.java" pinned="false" current-in-tab="true">
<file leaf-file-name="UserShipping.java" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/src/main/java/com/bookstore/domain/UserShipping.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="345">
<caret line="28" column="33" lean-forward="true" selection-start-line="28" selection-start-column="33" selection-end-line="28" selection-end-column="33" />
<caret line="26" column="0" lean-forward="true" selection-start-line="26" selection-start-column="0" selection-end-line="26" selection-end-column="0" />
<folding />
</state>
</provider>
@@ -110,6 +120,11 @@
</list>
</option>
</component>
<component name="FindInProjectRecents">
<findStrings>
<find>password</find>
</findStrings>
</component>
<component name="GradleLocalSettings">
<option name="modificationStamps">
<map>
@@ -153,13 +168,13 @@
<option value="$PROJECT_DIR$/src/main/java/com/bookstore/service/impl/BookServiceImpl.java" />
<option value="$PROJECT_DIR$/src/main/java/com/bookstore/service/BookService.java" />
<option value="$PROJECT_DIR$/src/main/java/com/bookstore/resource/BookResource.java" />
<option value="$PROJECT_DIR$/src/main/java/com/bookstore/resource/UserResource.java" />
<option value="$PROJECT_DIR$/src/main/java/com/bookstore/domain/ShoppingCart.java" />
<option value="$PROJECT_DIR$/src/main/java/com/bookstore/domain/UserPayment.java" />
<option value="$PROJECT_DIR$/src/main/java/com/bookstore/domain/UserBilling.java" />
<option value="$PROJECT_DIR$/src/main/java/com/bookstore/resource/PaymentResource.java" />
<option value="$PROJECT_DIR$/src/main/java/com/bookstore/resource/ShippingResource.java" />
<option value="$PROJECT_DIR$/src/main/java/com/bookstore/domain/UserShipping.java" />
<option value="$PROJECT_DIR$/src/main/java/com/bookstore/resource/UserResource.java" />
</list>
</option>
</component>
@@ -177,10 +192,10 @@
</option>
</component>
<component name="ProjectFrameBounds">
<option name="x" value="71" />
<option name="y" value="38" />
<option name="width" value="1663" />
<option name="height" value="961" />
<option name="x" value="82" />
<option name="y" value="23" />
<option name="width" value="1280" />
<option name="height" value="797" />
</component>
<component name="ProjectView">
<navigator currentView="ProjectPane" proportions="" version="1">
@@ -957,15 +972,15 @@
<workItem from="1486538831944" duration="450000" />
<workItem from="1488378219247" duration="650000" />
<workItem from="1488461561430" duration="18419000" />
<workItem from="1488731165185" duration="21199000" />
<workItem from="1488731165185" duration="26490000" />
</task>
<servers />
</component>
<component name="TimeTrackingManager">
<option name="totallyTimeSpent" value="83211000" />
<option name="totallyTimeSpent" value="88502000" />
</component>
<component name="ToolWindowManager">
<frame x="71" y="38" width="1663" height="961" extended-state="0" />
<frame x="82" y="23" width="1280" height="797" extended-state="0" />
<editor active="true" />
<layout>
<window_info id="Palette" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="3" side_tool="false" content_ui="tabs" />
@@ -985,11 +1000,11 @@
<window_info id="Palette&#9;" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="6" side_tool="false" content_ui="tabs" />
<window_info id="Image Layers" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="5" side_tool="false" content_ui="tabs" />
<window_info id="Capture Analysis" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="7" side_tool="false" content_ui="tabs" />
<window_info id="Run" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.32681242" sideWeight="0.5" order="2" side_tool="false" content_ui="tabs" />
<window_info id="Run" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" show_stripe_button="true" weight="0.32624114" sideWeight="0.5" order="2" side_tool="false" content_ui="tabs" />
<window_info id="Version Control" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="8" side_tool="false" content_ui="tabs" />
<window_info id="Spring" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.32907802" sideWeight="0.5" order="9" side_tool="false" content_ui="tabs" />
<window_info id="Terminal" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="10" side_tool="false" content_ui="tabs" />
<window_info id="Project" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" show_stripe_button="true" weight="0.24244294" sideWeight="0.5" order="0" side_tool="false" content_ui="combo" />
<window_info id="Project" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" show_stripe_button="true" weight="0.31744748" sideWeight="0.5" order="0" side_tool="false" content_ui="combo" />
<window_info id="Bean Validation" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="8" side_tool="false" content_ui="tabs" />
<window_info id="Theme Preview" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="9" side_tool="false" content_ui="tabs" />
<window_info id="Favorites" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="6" side_tool="true" content_ui="tabs" />
@@ -1009,7 +1024,7 @@
</component>
<component name="XDebuggerManager">
<breakpoint-manager>
<option name="time" value="7" />
<option name="time" value="8" />
</breakpoint-manager>
<watches-manager />
</component>
@@ -1270,14 +1285,6 @@
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/main/java/com/bookstore/resource/UserResource.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="368">
<caret line="99" column="0" lean-forward="false" selection-start-line="99" selection-start-column="0" selection-end-line="100" selection-end-column="0" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/main/java/com/bookstore/service/impl/UserSecurityService.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="270">
@@ -1365,7 +1372,15 @@
<entry file="file://$PROJECT_DIR$/src/main/java/com/bookstore/domain/UserShipping.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="345">
<caret line="28" column="33" lean-forward="true" selection-start-line="28" selection-start-column="33" selection-end-line="28" selection-end-column="33" />
<caret line="26" column="0" lean-forward="true" selection-start-line="26" selection-start-column="0" selection-end-line="26" selection-end-column="0" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/main/java/com/bookstore/resource/UserResource.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="170">
<caret line="207" column="85" lean-forward="true" selection-start-line="207" selection-start-column="85" selection-end-line="207" selection-end-column="85" />
<folding />
</state>
</provider>

View File

@@ -1,5 +1,6 @@
package com.bookstore.resource;
import com.bookstore.config.SecurityConfig;
import com.bookstore.config.SecurityUtility;
import com.bookstore.domain.User;
import com.bookstore.domain.security.PasswordResetToken;
@@ -17,6 +18,7 @@ import org.springframework.security.authentication.UsernamePasswordAuthenticatio
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@@ -46,23 +48,18 @@ public class UserResource {
@RequestMapping(value = "/newUser", method = RequestMethod.POST)
public ResponseEntity newUser(HttpServletRequest request,
@RequestBody HashMap<String, String> mapper,
Model model
@RequestBody HashMap<String, String> mapper
) throws Exception {
String username = mapper.get("username");
String userEmail = mapper.get("email");
// check username exists
if (userService.findByUsername(username) != null) {
model.addAttribute("usernameExists", true);
return new ResponseEntity("usernameExists", HttpStatus.BAD_REQUEST);
}
// check email address exists
if (userService.findByEmail(userEmail) != null) {
model.addAttribute("emailExists", true);
return new ResponseEntity("emailExists", HttpStatus.BAD_REQUEST);
}
@@ -95,27 +92,23 @@ public class UserResource {
mailSender.send(email);
model.addAttribute("emailSent", "true");
return new ResponseEntity("User Added Successfully!", HttpStatus.OK);
}
@RequestMapping("/addNewUser")
public ResponseEntity addNewUser(
Locale locale, Model model,
Locale locale,
@RequestParam("token") String token) {
PasswordResetToken passToken = userService.getPasswordResetToken(token);
if (passToken == null) {
String message = "Invalid Token.";
model.addAttribute("message", message);
return new ResponseEntity("Can't Add User!", HttpStatus.BAD_REQUEST);
}
Calendar cal = Calendar.getInstance();
if ((passToken.getExpiryDate().getTime() - cal.getTime().getTime()) <= 0) {
model.addAttribute("message", "Token has expired.");
return new ResponseEntity("Can't Add User!", HttpStatus.BAD_REQUEST);
}
@@ -130,21 +123,18 @@ public class UserResource {
SecurityContextHolder.getContext().setAuthentication(authentication);
model.addAttribute("user", user);
return new ResponseEntity("User Added Successfully!", HttpStatus.OK);
}
@RequestMapping("/forgetPassword")
public ResponseEntity forgetPassword(@RequestBody String email,
HttpServletRequest request,
Model model) {
model.addAttribute("classActiveForgetPassword", "true");
HttpServletRequest request
) {
User user = userService.findByEmail(email);
if (user == null) {
model.addAttribute("emailNotExists", true);
return new ResponseEntity("Email not found!", HttpStatus.BAD_REQUEST);
}
@@ -169,14 +159,74 @@ public class UserResource {
mailSender.send(newEmail);
model.addAttribute("forgetPasswordEmailSent", true);
return new ResponseEntity("Email sent!", HttpStatus.OK);
}
@RequestMapping(value = "/updateUserInfo", method = RequestMethod.POST)
public ResponseEntity profileInfo(
@RequestBody HashMap<String, Object> mapper
) throws Exception {
String email = (String) mapper.get("email");
String username = (String) mapper.get("username");
String firstName = (String) mapper.get("firstName");
String lastName = (String) mapper.get("lastName");
int id = (Integer) mapper.get("id");
String newPassword = (String) mapper.get("newPassword");
String currentPassword = (String) mapper.get("currentPassword");
User currentUser = userService.findById(Long.valueOf(id));
//
if (currentUser == null) {
throw new Exception("User not found");
}
// check email address exists
if (userService.findByEmail(email) != null) {
if (userService.findByEmail(email).getId() != currentUser.getId()) {
return new ResponseEntity("Email not found!", HttpStatus.BAD_REQUEST);
}
}
// check username exists
if (userService.findByUsername(username) != null) {
if (userService.findByUsername(username).getId() != currentUser.getId()) {
return new ResponseEntity("Username not found!", HttpStatus.BAD_REQUEST);
}
}
SecurityConfig securityConfig = new SecurityConfig();
// update password
if (newPassword != null && !newPassword.isEmpty() && !newPassword.equals("")) {
BCryptPasswordEncoder passwordEncoder = SecurityUtility.passwordEncoder();
String dbPassword = currentUser.getPassword();
System.out.println(currentPassword);
System.out.println(dbPassword);
System.out.println(passwordEncoder.matches(currentPassword, dbPassword));
if (currentPassword.equals(dbPassword)) {
currentUser.setPassword(passwordEncoder.encode(newPassword));
} else {
return new ResponseEntity("Incorrect current password!", HttpStatus.OK);
}
}
currentUser.setFirstName(firstName);
currentUser.setLastName(lastName);
currentUser.setUsername(username);
currentUser.setEmail(email);
userService.save(currentUser);
return new ResponseEntity("Update Success!", HttpStatus.OK);
}
@RequestMapping("/getCurrentUser")
public User getCurrentUser(Principal principal){
public User getCurrentUser(Principal principal) {
User user = userService.findByUsername(principal.getName());
return user;

View File

@@ -24,7 +24,7 @@
<div class="alert alert-success" *ngIf="updateSuccess">
<strong>Update Success!</strong>
</div>
<form (ngSubmit)="updateUserInfo" method="post">
<form (ngSubmit)="onUpdateUserInfo()" >
<input type="hidden" name="id" [(ngModel)]="user.id" />
<div class="bg-info" *ngIf="updateUserInfo">User info updated.</div>
<div class="form-group">
@@ -55,7 +55,7 @@
</div>
<div class="form-group">
<label for="txtNewPassword">Password</label>&nbsp;<span id="checkPasswordMatch" style="color:red;"></span>
<input type="password" class="form-control" id="txtNewPassword" name="newPassword" />
<input type="password" class="form-control" id="txtNewPassword" name="newPassword" [(ngModel)]="newPassword" />
</div>
<div class="form-group">
<label for="txtConfirmPassword">Confirm Password</label>

View File

@@ -33,6 +33,9 @@ export class MyProfileComponent implements OnInit {
private recoverEmail:string;
private user: User = new User();
private updateSuccess:boolean;
private newPassword:string;
private incorrectPassword:boolean;
private userPayment: UserPayment = new UserPayment();
private userBilling: UserBilling = new UserBilling();
private userPaymentList: UserPayment[] = [];
@@ -119,6 +122,20 @@ export class MyProfileComponent implements OnInit {
);
}
onUpdateUserInfo() {
this.userService.updateUserInfo(this.user, this.newPassword).subscribe(
res => {
console.log(res.text());
this.updateSuccess = true;
},
error => {
console.log(error.text());
let errorMessage=error.text();
if (errorMessage==="Incorrect current password!") this.incorrectPassword=true;
}
);
}
onNewPayment () {
this.paymentService.newPayment(this.userPayment).subscribe(
res => {

View File

@@ -1,6 +1,7 @@
import { Injectable } from '@angular/core';
import {AppConst} from '../constants/app-const';
import {Http, Headers} from '@angular/http';
import {User} from '../models/User';
@Injectable()
export class UserService {
@@ -18,6 +19,25 @@ export class UserService {
'Content-Type': 'application/json',
'x-auth-token' : localStorage.getItem("xAuthToken")
});
return this.http.post(url, userInfo, {headers : tokenHeader});
}
updateUserInfo(user:User, newPassword:string) {
let url = this.serverPath+"/user/updateUserInfo";
let userInfo = {
"id" : user.id,
"firstName" : user.firstName,
"lastName" : user.lastName,
"username" : user.username,
"currentPassword" : user.password,
"email" : user.email,
"newPassword" : newPassword
};
let tokenHeader = new Headers ({
'Content-Type': 'application/json',
'x-auth-token' : localStorage.getItem("xAuthToken")
});
return this.http.post(url, JSON.stringify(userInfo), {headers : tokenHeader});
}