From d11ff78442ab1e1fc87dc892bb4c696b2067ae21 Mon Sep 17 00:00:00 2001 From: Le Deng Date: Thu, 9 Mar 2017 17:01:47 -0500 Subject: [PATCH] latest --- bookstore-api/.idea/workspace.xml | 59 +++++++----- .../com/bookstore/resource/UserResource.java | 88 ++++++++++++++---- .../com/bookstore/resource/UserResource.class | Bin 8647 -> 9890 bytes .../my-profile/my-profile.component.html | 4 +- .../my-profile/my-profile.component.ts | 17 ++++ store-front/src/app/services/user.service.ts | 20 ++++ 6 files changed, 145 insertions(+), 43 deletions(-) diff --git a/bookstore-api/.idea/workspace.xml b/bookstore-api/.idea/workspace.xml index ea61eed..bf9b572 100644 --- a/bookstore-api/.idea/workspace.xml +++ b/bookstore-api/.idea/workspace.xml @@ -16,6 +16,16 @@ + + + + + + + + + + @@ -40,11 +50,11 @@ - + - + @@ -110,6 +120,11 @@ + + + password + + @@ -177,10 +192,10 @@ - @@ -957,15 +972,15 @@ - + - - + @@ -985,11 +1000,11 @@ - + - + @@ -1009,7 +1024,7 @@ - @@ -1270,14 +1285,6 @@ - - - - - - - - @@ -1365,7 +1372,15 @@ - + + + + + + + + + diff --git a/bookstore-api/src/main/java/com/bookstore/resource/UserResource.java b/bookstore-api/src/main/java/com/bookstore/resource/UserResource.java index 9c4878e..f6ca3f3 100644 --- a/bookstore-api/src/main/java/com/bookstore/resource/UserResource.java +++ b/bookstore-api/src/main/java/com/bookstore/resource/UserResource.java @@ -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 mapper, - Model model + @RequestBody HashMap 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 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; diff --git a/bookstore-api/target/classes/com/bookstore/resource/UserResource.class b/bookstore-api/target/classes/com/bookstore/resource/UserResource.class index 327acc1fc91991c16323bc070ada7537f7ee340a..ca70f9570f89ff5ae5d3adbd208bbb7b53378753 100644 GIT binary patch literal 9890 zcmbVS349#YdH=rEYG-zLY}xB`!C=V;$d>Ij4mS462i9RMTef7$vat=A)$T}IE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I4tj#5kC&y^qGHxsUR zF-OBL{*>ZgT09K*gZL0fE_~R@sDZTX=4=nsDUOfeqwK@@8175r<8<@8*$A(${s}&T z2Y5LuU-t1r25qO5ILdo5(}kcX;5m!oRxs!-vnm7B^JSsWO6#`=vx8ox+&NvCtE5}S zKJ0t&Q4Ho1Yu;5$lKpFx`+TMNE-VOYJZ_;L@EEEVm=9`TTVUQUl+C(+$UJogjHFAzN?4syiB1F@GQufnBYCLHaf230jV<%olPaimyv^VlPJ5D$@; z$rQ!I_$1f1V!4t@k~)b}&PqiZkF3Na_!M=r55@R2K0}=-inHLlh;j}4FJiKWM;%JD z97-ik>0=I$If_Tc-~s;M6X)@QgVb4y%pRVl$UI+eE$`37q#~AC%bJuTmi24Jb@i+E zGOc+*uc|*}w18B36j_C816BR%SXt7gMA4@{Yd4gVvTI5SXUSA8;pj>&(YbtLK&MUB z5{(=2z{x=$%~KTPPAyq^U6A#vnq19!J@R@&Abk|i0K-`QV-*` zoIl>5yZxD~m_LlqGtZg3{N>(7;sHysL5lf%k@H3^FH;rQ;J@Yl-6U;mS1$fTAV+@`EIUD6`U{q4;C}mXaE2J diff --git a/store-front/src/app/components/my-profile/my-profile.component.html b/store-front/src/app/components/my-profile/my-profile.component.html index a270abc..753ec5c 100644 --- a/store-front/src/app/components/my-profile/my-profile.component.html +++ b/store-front/src/app/components/my-profile/my-profile.component.html @@ -24,7 +24,7 @@
Update Success!
-
+
User info updated.
@@ -55,7 +55,7 @@
  - +
diff --git a/store-front/src/app/components/my-profile/my-profile.component.ts b/store-front/src/app/components/my-profile/my-profile.component.ts index cc15112..c11208c 100644 --- a/store-front/src/app/components/my-profile/my-profile.component.ts +++ b/store-front/src/app/components/my-profile/my-profile.component.ts @@ -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 => { diff --git a/store-front/src/app/services/user.service.ts b/store-front/src/app/services/user.service.ts index 4d01174..609059e 100644 --- a/store-front/src/app/services/user.service.ts +++ b/store-front/src/app/services/user.service.ts @@ -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}); }