Commit f7df64d9 authored by 0Tyler's avatar 0Tyler

PrivacyChoice api

parent 4806e8d7
This diff is collapsed.
package edu.prlab.tyler.iotgateway.cloud.config; package edu.prlab.tyler.iotgateway.cloud.config;
import edu.prlab.tyler.iotgateway.cloud.pojo.auth.SensitiveUser;
import edu.prlab.tyler.iotgateway.cloud.pojo.device.Device; import edu.prlab.tyler.iotgateway.cloud.pojo.device.Device;
import edu.prlab.tyler.iotgateway.cloud.pojo.device.Manufacturer; import edu.prlab.tyler.iotgateway.cloud.pojo.device.Manufacturer;
import edu.prlab.tyler.iotgateway.cloud.pojo.device.Model; import edu.prlab.tyler.iotgateway.cloud.pojo.device.Model;
...@@ -37,7 +36,7 @@ public class DefaultData implements ApplicationRunner { ...@@ -37,7 +36,7 @@ public class DefaultData implements ApplicationRunner {
@Override @Override
public void run(ApplicationArguments args) { public void run(ApplicationArguments args) {
// SensitiveUser user = SensitiveUser.builder() // User user = User.builder()
// .username("test") // .username("test")
// .password("test") // .password("test")
// .build(); // .build();
......
package edu.prlab.tyler.iotgateway.cloud.controllers; package edu.prlab.tyler.iotgateway.cloud.controllers;
import edu.prlab.tyler.iotgateway.cloud.pojo.auth.SensitiveUser;
import edu.prlab.tyler.iotgateway.cloud.pojo.device.Device; import edu.prlab.tyler.iotgateway.cloud.pojo.device.Device;
import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyPolicyReport; import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyPolicyReport;
import edu.prlab.tyler.iotgateway.cloud.services.DeviceService; import edu.prlab.tyler.iotgateway.cloud.services.DeviceService;
...@@ -45,7 +44,7 @@ public class DeviceController { ...@@ -45,7 +44,7 @@ public class DeviceController {
} }
// @GetMapping // @GetMapping
// public ResponseEntity<Iterable<Device>> readDevicesByUser(SensitiveUser user) { // public ResponseEntity<Iterable<Device>> readDevicesByUser(User user) {
// } // }
@PostMapping("/privacy") @PostMapping("/privacy")
......
package edu.prlab.tyler.iotgateway.cloud.controllers; package edu.prlab.tyler.iotgateway.cloud.controllers;
import edu.prlab.tyler.iotgateway.cloud.pojo.auth.SensitiveUser;
import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyChoice; import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyChoice;
import edu.prlab.tyler.iotgateway.cloud.services.PrivacyChoiceService; import edu.prlab.tyler.iotgateway.cloud.services.PrivacyChoiceService;
import edu.prlab.tyler.iotgateway.cloud.services.SensitiveUserService; import edu.prlab.tyler.iotgateway.cloud.services.SensitiveUserService;
......
...@@ -14,7 +14,7 @@ import javax.persistence.Id; ...@@ -14,7 +14,7 @@ import javax.persistence.Id;
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
@Entity @Entity
public class SensitiveUser { public class User {
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
......
package edu.prlab.tyler.iotgateway.cloud.pojo.privacy; package edu.prlab.tyler.iotgateway.cloud.pojo.privacy;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import edu.prlab.tyler.iotgateway.cloud.pojo.auth.SensitiveUser; import edu.prlab.tyler.iotgateway.cloud.pojo.auth.User;
import edu.prlab.tyler.iotgateway.cloud.pojo.device.Device; import edu.prlab.tyler.iotgateway.cloud.pojo.device.Device;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Builder; import lombok.Builder;
...@@ -25,7 +25,7 @@ public class PrivacyContent { ...@@ -25,7 +25,7 @@ public class PrivacyContent {
@OneToOne(cascade = CascadeType.ALL) @OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "user_id") @JoinColumn(name = "user_id")
private SensitiveUser user; private User user;
@OneToOne @OneToOne
@JoinColumn(name = "device_id") @JoinColumn(name = "device_id")
......
package edu.prlab.tyler.iotgateway.cloud.repositories; package edu.prlab.tyler.iotgateway.cloud.repositories;
import edu.prlab.tyler.iotgateway.cloud.pojo.auth.SensitiveUser; import edu.prlab.tyler.iotgateway.cloud.pojo.auth.User;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
@Repository @Repository
public interface SensitiveUserRepository extends JpaRepository<SensitiveUser, Long> { public interface SensitiveUserRepository extends JpaRepository<User, Long> {
} }
package edu.prlab.tyler.iotgateway.cloud.services; package edu.prlab.tyler.iotgateway.cloud.services;
import edu.prlab.tyler.iotgateway.cloud.pojo.auth.SensitiveUser; import edu.prlab.tyler.iotgateway.cloud.pojo.auth.User;
import edu.prlab.tyler.iotgateway.cloud.repositories.SensitiveUserRepository; import edu.prlab.tyler.iotgateway.cloud.repositories.SensitiveUserRepository;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -16,7 +16,7 @@ public class SensitiveUserService { ...@@ -16,7 +16,7 @@ public class SensitiveUserService {
this.repository = repository; this.repository = repository;
} }
public Optional<SensitiveUser> add(SensitiveUser pojo) { public Optional<User> add(User pojo) {
return Optional.of(repository.saveAndFlush(pojo)); return Optional.of(repository.saveAndFlush(pojo));
} }
} }
...@@ -3,7 +3,7 @@ package edu.prlab.tyler.iotgateway.cloud; ...@@ -3,7 +3,7 @@ package edu.prlab.tyler.iotgateway.cloud;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import edu.prlab.tyler.iotgateway.cloud.pojo.auth.SensitiveUser; import edu.prlab.tyler.iotgateway.cloud.pojo.auth.User;
import edu.prlab.tyler.iotgateway.cloud.pojo.device.Device; import edu.prlab.tyler.iotgateway.cloud.pojo.device.Device;
import edu.prlab.tyler.iotgateway.cloud.pojo.device.Icon; import edu.prlab.tyler.iotgateway.cloud.pojo.device.Icon;
import edu.prlab.tyler.iotgateway.cloud.pojo.device.Model; import edu.prlab.tyler.iotgateway.cloud.pojo.device.Model;
...@@ -178,7 +178,7 @@ public class CloudHttpApiTest { ...@@ -178,7 +178,7 @@ public class CloudHttpApiTest {
PrivacyChoice privacyChoice = PrivacyChoice.builder() PrivacyChoice privacyChoice = PrivacyChoice.builder()
.privacyContent(PrivacyContent.builder() .privacyContent(PrivacyContent.builder()
.user(SensitiveUser.builder() .user(User.builder()
.username("test") .username("test")
.password("test") .password("test")
.build()) .build())
......
package edu.prlab.tyler.iotgateway.gateway.config; package edu.prlab.tyler.iotgateway.gateway.config;
import edu.prlab.tyler.iotgateway.gateway.pojo.auth.User; import edu.prlab.tyler.iotgateway.gateway.pojo.auth.SensitiveUser;
import edu.prlab.tyler.iotgateway.gateway.services.UserService; import edu.prlab.tyler.iotgateway.gateway.services.UserService;
import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner; import org.springframework.boot.ApplicationRunner;
...@@ -16,7 +16,7 @@ public class DefaultData implements ApplicationRunner { ...@@ -16,7 +16,7 @@ public class DefaultData implements ApplicationRunner {
@Override @Override
public void run(ApplicationArguments args) throws Exception { public void run(ApplicationArguments args) throws Exception {
User defaultUser = userService.registerNewUser(User.builder().username("testUser0").password("testUser0").build()).get(); SensitiveUser defaultUser = userService.registerNewUser(SensitiveUser.builder().username("testUser0").password("testUser0").build()).get();
System.out.println(defaultUser.toString()); System.out.println(defaultUser.toString());
} }
} }
package edu.prlab.tyler.iotgateway.gateway.controllers; package edu.prlab.tyler.iotgateway.gateway.controllers;
import edu.prlab.tyler.iotgateway.gateway.pojo.auth.User; import edu.prlab.tyler.iotgateway.gateway.pojo.auth.SensitiveUser;
import edu.prlab.tyler.iotgateway.gateway.services.UserService; import edu.prlab.tyler.iotgateway.gateway.services.UserService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
...@@ -18,14 +18,14 @@ public class UserController { ...@@ -18,14 +18,14 @@ public class UserController {
} }
@PostMapping("/user") @PostMapping("/user")
public ResponseEntity<User> register(@RequestBody User user) { public ResponseEntity<SensitiveUser> register(@RequestBody SensitiveUser user) {
return userService.registerNewUser(user) return userService.registerNewUser(user)
.map(ResponseEntity::ok) .map(ResponseEntity::ok)
.orElseGet(() -> ResponseEntity.noContent().build()); .orElseGet(() -> ResponseEntity.noContent().build());
} }
@PostMapping("/login") @PostMapping("/login")
public ResponseEntity<User> login(@RequestBody User user) { public ResponseEntity<SensitiveUser> login(@RequestBody SensitiveUser user) {
return userService.login(user) return userService.login(user)
.map(ResponseEntity::ok) .map(ResponseEntity::ok)
.orElseGet(() -> ResponseEntity.noContent().build()); .orElseGet(() -> ResponseEntity.noContent().build());
......
...@@ -17,7 +17,7 @@ import javax.persistence.Id; ...@@ -17,7 +17,7 @@ import javax.persistence.Id;
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
@Entity @Entity
public class User { public class SensitiveUser {
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
@JsonIgnore @JsonIgnore
......
package edu.prlab.tyler.iotgateway.gateway.repositories; package edu.prlab.tyler.iotgateway.gateway.repositories;
import edu.prlab.tyler.iotgateway.gateway.pojo.auth.User; import edu.prlab.tyler.iotgateway.gateway.pojo.auth.SensitiveUser;
import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
@Repository @Repository
public interface UserRepository extends CrudRepository<User, Long> { public interface UserRepository extends CrudRepository<SensitiveUser, Long> {
User findUserByUsername(String username); SensitiveUser findUserByUsername(String username);
boolean existsByUsername(String username); boolean existsByUsername(String username);
} }
...@@ -59,15 +59,15 @@ public class PrivacyService { ...@@ -59,15 +59,15 @@ public class PrivacyService {
// } // }
public Iterable<PrivacyChoiceResponse> getPrivacyPolicyChoices() { public Iterable<PrivacyChoiceResponse> getPrivacyPolicyChoices() {
return () -> StreamSupport.stream(template.exchange(cloudAddress + "/choice", HttpMethod.GET, null, return () -> StreamSupport.stream(
new ParameterizedTypeReference<Iterable<PrivacyChoice>>() { template.exchange(cloudAddress + "/choice", HttpMethod.GET, null,
}).getBody().spliterator(), false) new ParameterizedTypeReference<Iterable<PrivacyChoice>>() { }).getBody().spliterator(),
false)
.map(choice -> privacyChoiceIndexRepository.findById(choice.getId()) .map(choice -> privacyChoiceIndexRepository.findById(choice.getId())
.map(choiceIndex -> PrivacyChoiceResponse.builder() .map(choiceIndex -> PrivacyChoiceResponse.builder()
.id(choiceIndex.getId()) .id(choiceIndex.getId())
.localDateTime(choiceIndex.getLocalDateTime()) .localDateTime(choiceIndex.getLocalDateTime())
.privacyContent(choice.getPrivacyContent()) .privacyContent(choice.getPrivacyContent())
.build()).get()) .build()).get()).iterator();
.iterator();
} }
} }
package edu.prlab.tyler.iotgateway.gateway.services; package edu.prlab.tyler.iotgateway.gateway.services;
import edu.prlab.tyler.iotgateway.gateway.pojo.auth.User; import edu.prlab.tyler.iotgateway.gateway.pojo.auth.SensitiveUser;
import edu.prlab.tyler.iotgateway.gateway.repositories.UserRepository; import edu.prlab.tyler.iotgateway.gateway.repositories.UserRepository;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -18,16 +18,16 @@ public class UserService { ...@@ -18,16 +18,16 @@ public class UserService {
//TODO //TODO
//需要增加加密機制 //需要增加加密機制
public Optional<User> registerNewUser(User user) { public Optional<SensitiveUser> registerNewUser(SensitiveUser user) {
return Optional.ofNullable(user) return Optional.ofNullable(user)
.map(User::getUsername) .map(SensitiveUser::getUsername)
.filter(username->!userRepository.existsByUsername(username)) .filter(username->!userRepository.existsByUsername(username))
.map(username -> userRepository.save(user)); .map(username -> userRepository.save(user));
} }
//TODO //TODO
//需要增加解密確認 //需要增加解密確認
public Optional<User> login(User user) { public Optional<SensitiveUser> login(SensitiveUser user) {
return Optional.ofNullable(user.getUsername()) return Optional.ofNullable(user.getUsername())
.map(username -> userRepository.findUserByUsername(username)) .map(username -> userRepository.findUserByUsername(username))
.filter(sensitiveUser->sensitiveUser.getPassword().equals(user.getPassword())); .filter(sensitiveUser->sensitiveUser.getPassword().equals(user.getPassword()));
......
...@@ -2,12 +2,12 @@ package edu.prlab.tyler.iotgateway.gateway; ...@@ -2,12 +2,12 @@ package edu.prlab.tyler.iotgateway.gateway;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import edu.prlab.tyler.iotgateway.cloud.pojo.auth.SensitiveUser; import edu.prlab.tyler.iotgateway.cloud.pojo.auth.User;
import edu.prlab.tyler.iotgateway.cloud.pojo.device.Device; import edu.prlab.tyler.iotgateway.cloud.pojo.device.Device;
import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyChoice; import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyChoice;
import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyContent; import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyContent;
import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyPolicyReport; import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyPolicyReport;
import edu.prlab.tyler.iotgateway.gateway.pojo.auth.User; import edu.prlab.tyler.iotgateway.gateway.pojo.auth.SensitiveUser;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
...@@ -51,7 +51,7 @@ public class GatewayHttpApiTest { ...@@ -51,7 +51,7 @@ public class GatewayHttpApiTest {
.post("/login") .post("/login")
.contentType(MediaType.APPLICATION_JSON_UTF8) .contentType(MediaType.APPLICATION_JSON_UTF8)
.accept(MediaType.APPLICATION_JSON_UTF8) .accept(MediaType.APPLICATION_JSON_UTF8)
.content(mapper.writeValueAsString(User.builder().username("testUser0").password("testUser0").build()))) .content(mapper.writeValueAsString(SensitiveUser.builder().username("testUser0").password("testUser0").build())))
.andDo(print()) .andDo(print())
.andExpect(status().isOk()) .andExpect(status().isOk())
.andReturn(); .andReturn();
...@@ -74,7 +74,7 @@ public class GatewayHttpApiTest { ...@@ -74,7 +74,7 @@ public class GatewayHttpApiTest {
.post("/device/" + "a1252c49-4188-4e6d-a32e-66604c664fb8") .post("/device/" + "a1252c49-4188-4e6d-a32e-66604c664fb8")
.contentType(MediaType.APPLICATION_JSON_UTF8) .contentType(MediaType.APPLICATION_JSON_UTF8)
.accept(MediaType.APPLICATION_JSON_UTF8) .accept(MediaType.APPLICATION_JSON_UTF8)
.content(mapper.writeValueAsString(User.builder().username("testUser0").password("testUser0").build()))) .content(mapper.writeValueAsString(SensitiveUser.builder().username("testUser0").password("testUser0").build())))
.andDo(print()) .andDo(print())
.andExpect(status().isOk()) .andExpect(status().isOk())
.andReturn(); .andReturn();
...@@ -95,7 +95,7 @@ public class GatewayHttpApiTest { ...@@ -95,7 +95,7 @@ public class GatewayHttpApiTest {
device = report.getDevice(); device = report.getDevice();
PrivacyChoice privacyChoice = PrivacyChoice.builder() PrivacyChoice privacyChoice = PrivacyChoice.builder()
.privacyContent(PrivacyContent.builder() .privacyContent(PrivacyContent.builder()
.user(SensitiveUser.builder() .user(User.builder()
.username("test") .username("test")
.password("test") .password("test")
.build()) .build())
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment