Update User Rest Api
This commit is contained in:
parent
603c6b1ffc
commit
30c806ec0c
|
@ -15,11 +15,12 @@ import java.util.LinkedList;
|
|||
public class TransactionWrapper {
|
||||
private static Logger logger = Logger.getLogger(TransactionWrapper.class);
|
||||
|
||||
public BlockEvent.TransactionEvent sendTransaction(String functionName, String[] args){
|
||||
public BlockEvent.TransactionEvent sendTransaction(String functionName, String[] args) throws Exception {
|
||||
BasicConfigurator.configure();
|
||||
UserContext user = Util.readUserContext(Config.ORG1,"admin");
|
||||
|
||||
try{
|
||||
//try{
|
||||
|
||||
Collection<ProposalResponse> successful = new LinkedList<>();
|
||||
Collection<ProposalResponse> failed = new LinkedList<>();
|
||||
FabricClientWrapper fabricClientWrapper;
|
||||
|
@ -62,10 +63,10 @@ public class TransactionWrapper {
|
|||
|
||||
|
||||
return channelClientWrapper.sendTransaction(successful); //Send successful transaction to orderer
|
||||
|
||||
/*
|
||||
} catch (Exception e){
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
|
4
src/main/java/database/Wallet/Wallet.java
Normal file
4
src/main/java/database/Wallet/Wallet.java
Normal file
|
@ -0,0 +1,4 @@
|
|||
package database.Wallet;
|
||||
|
||||
public class Wallet {
|
||||
}
|
4
src/main/java/database/Wallet/WalletDao.java
Normal file
4
src/main/java/database/Wallet/WalletDao.java
Normal file
|
@ -0,0 +1,4 @@
|
|||
package database.Wallet;
|
||||
|
||||
public class WalletDao {
|
||||
}
|
|
@ -20,9 +20,9 @@ public class User {
|
|||
private long creation_date;
|
||||
@DatabaseField(canBeNull = false)
|
||||
private long modification_date;
|
||||
@DatabaseField
|
||||
@DatabaseField(canBeNull = false)
|
||||
private long phone;
|
||||
@DatabaseField
|
||||
@DatabaseField(canBeNull = false)
|
||||
private String association;
|
||||
@DatabaseField(canBeNull = false)
|
||||
private boolean verified;
|
||||
|
@ -150,6 +150,7 @@ public class User {
|
|||
this.approved = approved;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User{" +
|
||||
|
|
|
@ -10,6 +10,7 @@ import java.util.Map;
|
|||
|
||||
public class DatabaseUserImplementation {
|
||||
|
||||
/*
|
||||
public Map<String,String> saveUser(User user) throws Exception {
|
||||
UserDao userDao = new UserDao();
|
||||
Map<String,String> response = new HashMap<String, String>();
|
||||
|
@ -35,8 +36,10 @@ public class DatabaseUserImplementation {
|
|||
return response;
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
public User getUser(String email, String password) throws Exception{
|
||||
UserDao userDao = new UserDao();
|
||||
User user1 = userDao.getUser(email);
|
||||
|
@ -51,8 +54,9 @@ public class DatabaseUserImplementation {
|
|||
return null;
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
public Map<String,String> userLogger(User user) throws Exception {
|
||||
UserDao userDao = new UserDao();
|
||||
Map<String,String> response = new HashMap<String, String>();
|
||||
|
@ -72,8 +76,11 @@ public class DatabaseUserImplementation {
|
|||
}
|
||||
return response;
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
private String hashPassword(String plainTextPassword){
|
||||
return BCrypt.hashpw(plainTextPassword,BCrypt.gensalt());
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
|
86
src/main/java/restImplementation/UserImplementation.java
Normal file
86
src/main/java/restImplementation/UserImplementation.java
Normal file
|
@ -0,0 +1,86 @@
|
|||
package restImplementation;
|
||||
|
||||
import blockchain.query.TransactionWrapper;
|
||||
import database.user.User;
|
||||
import database.user.UserDao;
|
||||
import org.springframework.security.crypto.bcrypt.BCrypt;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class UserImplementation {
|
||||
|
||||
public Map<String,String> registerUser(User user) throws Exception {
|
||||
TransactionWrapper transactionWrapper = new TransactionWrapper();
|
||||
UserDao userDao = new UserDao();
|
||||
|
||||
//CREATE USER HASH. TEMPORARY USER HASH IS : name+email+firstname+association
|
||||
user.setUser_hash(hashPassword(user.getName()+user.getEmail()+user.getFirstname()+user.getAssociation()));
|
||||
|
||||
//REGISTER IN BLOCKCHAIN
|
||||
String[] userInfos = new String[]{user.getUser_hash(),user.getName(),user.getFirstname(),""+user.getPhone(),user.getAssociation()};
|
||||
transactionWrapper.sendTransaction("registerUser",userInfos);
|
||||
|
||||
//REGISTER IN REPLICA DB FOR BI
|
||||
Map<String,String> response = new HashMap<>();
|
||||
User dbUser = userDao.getUser(user.getEmail()); // check if user exist
|
||||
|
||||
if(dbUser != null){
|
||||
response.put("response","false");
|
||||
return response;
|
||||
}else {
|
||||
user.setPassword(hashPassword(user.getPassword()));
|
||||
long now = Instant.now().toEpochMilli();
|
||||
user.setCreation_date(now);
|
||||
user.setModification_date(now);
|
||||
user.setVerified(true);
|
||||
user.setApproved(false);
|
||||
|
||||
userDao.addUser(user);
|
||||
response.put("userHash",user.getUser_hash());
|
||||
response.put("response","true");
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
public User getUser(String email, String password) throws Exception{
|
||||
UserDao userDao = new UserDao();
|
||||
User user1 = userDao.getUser(email);
|
||||
if(user1 != null){
|
||||
String hash = user1.getPassword();
|
||||
if(BCrypt.checkpw(password, hash)){
|
||||
return user1;
|
||||
}else{
|
||||
return null;
|
||||
}
|
||||
}else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String,String> userLogger(User user) throws Exception {
|
||||
UserDao userDao = new UserDao();
|
||||
Map<String,String> response = new HashMap<>();
|
||||
|
||||
User userResponse = userDao.getUser(user.getEmail());
|
||||
if(userResponse != null){
|
||||
String hash = userResponse.getPassword();
|
||||
|
||||
if(!BCrypt.checkpw(user.getPassword(), hash)){
|
||||
response.put("response","Not Allowed");
|
||||
}else{
|
||||
response.put("response","Ok");
|
||||
response.put("userHash",userResponse.getUser_hash());
|
||||
}
|
||||
} else {
|
||||
response.put("response","Not Exist");
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
private String hashPassword(String plainTextPassword){
|
||||
return BCrypt.hashpw(plainTextPassword,BCrypt.gensalt());
|
||||
}
|
||||
|
||||
}
|
|
@ -13,10 +13,11 @@ import javax.validation.Valid;
|
|||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(value = "/api/rest/user")
|
||||
//@RestController
|
||||
//@RequestMapping(value = "/api/rest/user")
|
||||
public class DatabaseUserResource {
|
||||
|
||||
/*
|
||||
@RequestMapping(value = "/save", method = RequestMethod.POST,produces = "application/json")
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
public ResponseEntity<StringResponse> saveUser(@Valid @RequestBody User user){
|
||||
|
@ -41,7 +42,9 @@ public class DatabaseUserResource {
|
|||
return ResponseEntity.status(HttpStatus.CONFLICT).body(responseS);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
@RequestMapping(value = "/login", method = RequestMethod.POST,produces = "application/json")
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
public ResponseEntity<StringResponse> login(@Valid @RequestBody User user){
|
||||
|
@ -75,7 +78,9 @@ public class DatabaseUserResource {
|
|||
return ResponseEntity.status(HttpStatus.CONFLICT).body(responseS);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
@PostMapping(value = "/get", produces = "application/json")
|
||||
@ResponseBody
|
||||
public ResponseEntity<User> getUser(@RequestBody User user){
|
||||
|
@ -94,11 +99,14 @@ public class DatabaseUserResource {
|
|||
return new ResponseEntity(e.getMessage(), HttpStatus.CONFLICT);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
@PostMapping(value = "/update")
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
public ResponseEntity updateUser(@RequestBody User user){
|
||||
return new ResponseEntity(null, HttpStatus.OK);
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
|
|
10
src/main/java/restService/TransactionResource.java
Normal file
10
src/main/java/restService/TransactionResource.java
Normal file
|
@ -0,0 +1,10 @@
|
|||
package restService;
|
||||
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(value = "/api/rest/transaction")
|
||||
public class TransactionResource {
|
||||
|
||||
}
|
98
src/main/java/restService/UserResource.java
Normal file
98
src/main/java/restService/UserResource.java
Normal file
|
@ -0,0 +1,98 @@
|
|||
package restService;
|
||||
|
||||
import database.user.User;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import restImplementation.UserImplementation;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(value = "/api/rest/user")
|
||||
public class UserResource {
|
||||
|
||||
|
||||
@RequestMapping(value = "/register", method = RequestMethod.POST,produces = "application/json")
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
public ResponseEntity<StringResponse> registerUser(@Valid @RequestBody User user){
|
||||
try{
|
||||
UserImplementation userImplementation = new UserImplementation();
|
||||
Map<String,String> userHashResponse = userImplementation.registerUser(user);
|
||||
|
||||
if(Boolean.parseBoolean(userHashResponse.get("response"))){
|
||||
StringResponse responseS = new StringResponse("Ok",userHashResponse.get("userHash"));
|
||||
return ResponseEntity.status(HttpStatus.OK).body(responseS);
|
||||
}else {
|
||||
StringResponse responseS = new StringResponse("User already exist");
|
||||
return ResponseEntity.status(HttpStatus.FOUND).body(responseS);
|
||||
}
|
||||
}catch (Exception e){
|
||||
StringResponse responseS = new StringResponse(e.getMessage());
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseS);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@RequestMapping(value = "/login", method = RequestMethod.POST,produces = "application/json")
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
public ResponseEntity<StringResponse> login(@Valid @RequestBody User user){
|
||||
try{
|
||||
UserImplementation userImplementation = new UserImplementation();
|
||||
Map<String,String> response = userImplementation.userLogger(user);
|
||||
switch (response.get("response")){
|
||||
case "Not Exist" : {
|
||||
StringResponse responseS = new StringResponse("Not Found");
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(responseS);
|
||||
}
|
||||
case "Not Allowed" :{
|
||||
StringResponse responseS = new StringResponse("Forbidden");
|
||||
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(responseS);
|
||||
}
|
||||
case "" :{
|
||||
StringResponse responseS = new StringResponse("Error");
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseS);
|
||||
}
|
||||
case "Ok":{
|
||||
StringResponse responseS = new StringResponse("Ok",response.get("userHash"));
|
||||
return ResponseEntity.status(HttpStatus.OK).body(responseS);
|
||||
}
|
||||
default:{
|
||||
StringResponse responseS = new StringResponse("Error");
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseS);
|
||||
}
|
||||
}
|
||||
}catch (Exception e){
|
||||
StringResponse responseS = new StringResponse(e.getMessage());
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseS);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@PostMapping(value = "/get", produces = "application/json")
|
||||
@ResponseBody
|
||||
public ResponseEntity<User> getUser(@RequestBody User user){
|
||||
try{
|
||||
UserImplementation userImplementation = new UserImplementation();
|
||||
User response = userImplementation.getUser(user.getEmail(), user.getPassword());
|
||||
if(response != null){
|
||||
User userResponse = new User();
|
||||
userResponse.setUser_hash(response.getUser_hash());
|
||||
userResponse.setUserId(response.getUserId());
|
||||
return ResponseEntity.status(HttpStatus.OK).body(userResponse);
|
||||
}else{
|
||||
return new ResponseEntity("Error", HttpStatus.NOT_FOUND);
|
||||
}
|
||||
}catch (Exception e){
|
||||
return new ResponseEntity(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@PostMapping(value = "/update")
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
public ResponseEntity updateUser(@RequestBody User user){
|
||||
return new ResponseEntity(null, HttpStatus.OK);
|
||||
}
|
||||
}
|
9
src/main/java/restService/WalletResource.java
Normal file
9
src/main/java/restService/WalletResource.java
Normal file
|
@ -0,0 +1,9 @@
|
|||
package restService;
|
||||
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(value = "/api/rest/wallet")
|
||||
public class WalletResource {
|
||||
}
|
Loading…
Reference in a new issue