From 7fd9973319d85f03a2444fa9b4118349224f878e Mon Sep 17 00:00:00 2001 From: Lingxiao108105 <71265122+Lingxiao108105@users.noreply.github.com> Date: Tue, 19 Oct 2021 22:38:10 +1100 Subject: [PATCH] Add: check whether login from new place --- .../common/constants/EmailConstants.java | 2 + .../crm/crmserver/common/utils/IpUtil.java | 93 ++++++ .../crmserver/controller/UserController.java | 42 ++- .../tech/crm/crmserver/dao/IpAddress.java | 39 +++ .../crm/crmserver/mapper/IpAddressMapper.java | 18 ++ .../crmserver/service/IpAddressService.java | 39 +++ .../crm/crmserver/service/UserService.java | 4 +- .../service/impl/IpAddressServiceImpl.java | 102 +++++++ .../service/impl/UserServiceImpl.java | 4 +- src/main/resources/db/schema-test.sql | 284 +++++++++--------- 10 files changed, 483 insertions(+), 144 deletions(-) create mode 100644 src/main/java/tech/crm/crmserver/common/utils/IpUtil.java create mode 100644 src/main/java/tech/crm/crmserver/dao/IpAddress.java create mode 100644 src/main/java/tech/crm/crmserver/mapper/IpAddressMapper.java create mode 100644 src/main/java/tech/crm/crmserver/service/IpAddressService.java create mode 100644 src/main/java/tech/crm/crmserver/service/impl/IpAddressServiceImpl.java diff --git a/src/main/java/tech/crm/crmserver/common/constants/EmailConstants.java b/src/main/java/tech/crm/crmserver/common/constants/EmailConstants.java index 1515e8b..0b54d03 100644 --- a/src/main/java/tech/crm/crmserver/common/constants/EmailConstants.java +++ b/src/main/java/tech/crm/crmserver/common/constants/EmailConstants.java @@ -18,4 +18,6 @@ public class EmailConstants { public static final String ACTIVE_ACCOUNT_TITLE = "Active your account of ConnecTi"; + public static final String NEW_LOGIN_TITLE = "Safety Notice: Login from new place"; + } diff --git a/src/main/java/tech/crm/crmserver/common/utils/IpUtil.java b/src/main/java/tech/crm/crmserver/common/utils/IpUtil.java new file mode 100644 index 0000000..ed80f0e --- /dev/null +++ b/src/main/java/tech/crm/crmserver/common/utils/IpUtil.java @@ -0,0 +1,93 @@ +package tech.crm.crmserver.common.utils; + + +import javax.servlet.http.HttpServletRequest; +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.HashMap; +import java.util.Map; + +import org.springframework.http.*; +import org.springframework.util.MultiValueMap; +import org.springframework.web.client.RestTemplate; +import tech.crm.crmserver.dao.IpAddress; + +/** + * ip util + * + * @author Lingxiao Li + * @since 2021-10-19 + **/ +public class IpUtil { + + private final static String URL = "http://ip-api.com/json/"; + + /** + * get ip by httpServletRequest
+ * Keep a good mood + * from https://blog.csdn.net/qq_35387940/article/details/84391784 + * CC 4.0 BY-SA
+ * + * @author JCccc + * @since 2018-11-23 + **/ + public static String getIpAddr(HttpServletRequest request) { + String ipAddress = null; + try { + ipAddress = request.getHeader("x-forwarded-for"); + if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { + ipAddress = request.getHeader("Proxy-Client-IP"); + } + if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { + ipAddress = request.getHeader("WL-Proxy-Client-IP"); + } + if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { + ipAddress = request.getRemoteAddr(); + if (ipAddress.equals("127.0.0.1")) { + return null; + } + } + if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length() + // = 15 + if (ipAddress.indexOf(",") > 0) { + ipAddress = ipAddress.substring(0, ipAddress.indexOf(",")); + } + } + } catch (Exception e) { + ipAddress=""; + } + // ipAddress = this.getRequest().getRemoteAddr(); + + return ipAddress; + } + + + + + /** + * https://blog.csdn.net/zai_xia/article/details/80926157 + * @author Seven.wk + * @since 2018/07/04 + * @param ip ip address + */ + public static IpAddress sendPostRequest(String ip){ + + RestTemplate client = new RestTemplate(); + HttpHeaders headers = new HttpHeaders(); + HttpMethod method = HttpMethod.POST; + String url = URL + ip; + + headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); + + HttpEntity> requestEntity = new HttpEntity<>(null, headers); + + ResponseEntity response = client.exchange(URL, method, requestEntity, IpAddress.class); + if(response.getStatusCode() != HttpStatus.OK){ + return null; + } + + return response.getBody(); + } + + +} diff --git a/src/main/java/tech/crm/crmserver/controller/UserController.java b/src/main/java/tech/crm/crmserver/controller/UserController.java index 05b4339..40e2c02 100644 --- a/src/main/java/tech/crm/crmserver/controller/UserController.java +++ b/src/main/java/tech/crm/crmserver/controller/UserController.java @@ -9,12 +9,15 @@ import tech.crm.crmserver.common.constants.SecurityConstants; import tech.crm.crmserver.common.exception.BadPhotoException; import tech.crm.crmserver.common.response.ResponseResult; +import tech.crm.crmserver.common.utils.IpUtil; import tech.crm.crmserver.common.utils.NullAwareBeanUtilsBean; import tech.crm.crmserver.dao.User; import tech.crm.crmserver.dto.*; +import tech.crm.crmserver.service.IpAddressService; import tech.crm.crmserver.service.TokenKeyService; import tech.crm.crmserver.service.UserService; +import javax.servlet.http.HttpServletRequest; import java.io.IOException; @@ -36,6 +39,9 @@ public class UserController { @Autowired private UserService userService; + @Autowired + private IpAddressService ipAddressService; + /** * The login API * @@ -43,9 +49,16 @@ public class UserController { * @return return 200 when login successfully, return 400 and reason in the msg */ @PostMapping("/login") - public ResponseResult login(@Validated @RequestBody LoginRequest loginRequest){ + public ResponseResult login(@Validated @RequestBody LoginRequest loginRequest, + HttpServletRequest httpServletRequest){ //login and return the token - String token = userService.login(loginRequest); + User user = userService.verify(loginRequest); + userService.dealPendingUser(user); + //check ip + String ip = IpUtil.getIpAddr(httpServletRequest); + ipAddressService.checkIpAddress(user.getId(), ip); + //return the token + String token = tokenKeyService.createToken(user); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.set(SecurityConstants.TOKEN_HEADER, token); return ResponseResult.suc("successfully login!","",httpHeaders); @@ -70,10 +83,14 @@ public ResponseResult logout(@RequestHeader("Authorization") String toke * @return 200 when successfully register and set Authorization in response header */ @PostMapping - public ResponseResult register(@Validated @RequestBody UserRegisterDTO userRegisterDTO){ + public ResponseResult register(@Validated @RequestBody UserRegisterDTO userRegisterDTO, + HttpServletRequest httpServletRequest){ User user = userService.fromUserRegisterDTO(userRegisterDTO); //check whether there is same email already exist - userService.register(user); + User register = userService.register(user); + //save ip + String ip = IpUtil.getIpAddr(httpServletRequest); + ipAddressService.saveNewIp(register.getId(),ip); return ResponseResult.suc("successfully sign up!"); } @@ -105,8 +122,13 @@ public ResponseResult getUser() { * @return response with msg */ @PostMapping("/resetPassword") - public ResponseResult resetPassword(@Validated @RequestBody ResetPasswordDTO resetPasswordDTO){ - userService.resetPassword(resetPasswordDTO.getEmail()); + public ResponseResult resetPassword(@Validated @RequestBody ResetPasswordDTO resetPasswordDTO, + HttpServletRequest httpServletRequest){ + User user = userService.resetPassword(resetPasswordDTO.getEmail()); + //remove all ip and save new ip + String ip = IpUtil.getIpAddr(httpServletRequest); + ipAddressService.cleanIpByUserId(user.getId()); + ipAddressService.saveNewIp(user.getId(),ip); return ResponseResult.suc("Check your email for new password!"); } @@ -123,8 +145,12 @@ public ResponseResult updateUserDetail(@Validated @RequestBody UserUpdat } @PostMapping("/verify") - public ResponseResult verifyEmail(){ - userService.activePendingUser(userService.getId()); + public ResponseResult verifyEmail(HttpServletRequest httpServletRequest){ + Integer userId = userService.getId(); + userService.activePendingUser(userId); + //check ip + String ip = IpUtil.getIpAddr(httpServletRequest); + ipAddressService.checkIpAddress(userId, ip); return ResponseResult.suc("Successfully active user account"); } diff --git a/src/main/java/tech/crm/crmserver/dao/IpAddress.java b/src/main/java/tech/crm/crmserver/dao/IpAddress.java new file mode 100644 index 0000000..6f34072 --- /dev/null +++ b/src/main/java/tech/crm/crmserver/dao/IpAddress.java @@ -0,0 +1,39 @@ +package tech.crm.crmserver.dao; + +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; + +import java.io.Serializable; + +/** + *

+ * entity + *

+ * + * @author Lingxiao + * @since 2021-10-19 + */ +@Data +@AllArgsConstructor +@NoArgsConstructor +@EqualsAndHashCode(callSuper = false) +@TableName("ip_address") +public class IpAddress implements Serializable { + + private static final long serialVersionUID = 1L; + + private Integer userId; + + private String country; + + @TableField("region_name") + private String regionName; + + private String city; + + +} diff --git a/src/main/java/tech/crm/crmserver/mapper/IpAddressMapper.java b/src/main/java/tech/crm/crmserver/mapper/IpAddressMapper.java new file mode 100644 index 0000000..4811e09 --- /dev/null +++ b/src/main/java/tech/crm/crmserver/mapper/IpAddressMapper.java @@ -0,0 +1,18 @@ +package tech.crm.crmserver.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import org.springframework.stereotype.Repository; +import tech.crm.crmserver.dao.IpAddress; + +/** + *

+ * Mapper + *

+ * + * @author Lingxiao + * @since 2021-10-19 + */ +@Repository +public interface IpAddressMapper extends BaseMapper { + +} diff --git a/src/main/java/tech/crm/crmserver/service/IpAddressService.java b/src/main/java/tech/crm/crmserver/service/IpAddressService.java new file mode 100644 index 0000000..1b56516 --- /dev/null +++ b/src/main/java/tech/crm/crmserver/service/IpAddressService.java @@ -0,0 +1,39 @@ +package tech.crm.crmserver.service; + +import com.baomidou.mybatisplus.extension.service.IService; +import tech.crm.crmserver.dao.IpAddress; + +/** + *

+ * service + *

+ * + * @author Lingxiao + * @since 2021-10-19 + */ +public interface IpAddressService extends IService { + + /** + * check whether this ip is a new ip for the user
+ * if yes, send a email to user and return true + * if no, return false + * @param userId the id of user + * @param ip the ip + * @return whether is a new ip + */ + public boolean checkIpAddress(Integer userId, String ip); + + /** + * save a new address according to ip + * @param userId the id of user + * @param ip the ip + */ + public void saveNewIp(Integer userId, String ip); + + /** + * clean ip address by user id + * @param userId the id of user + */ + public void cleanIpByUserId(Integer userId); + +} diff --git a/src/main/java/tech/crm/crmserver/service/UserService.java b/src/main/java/tech/crm/crmserver/service/UserService.java index c9a7b65..07672db 100644 --- a/src/main/java/tech/crm/crmserver/service/UserService.java +++ b/src/main/java/tech/crm/crmserver/service/UserService.java @@ -94,8 +94,9 @@ public static Map searchKey(String searchKey){ * then send the password to the email
* and delete all the token of this user in database * @param email the email of user who needs reset password + * @return user who reset password */ - public void resetPassword(String email); + public User resetPassword(String email); /** * update the recent activity time of user @@ -144,4 +145,5 @@ public static Map searchKey(String searchKey){ * @param originalPhoto binary photo */ public void updatePhoto(Integer userId, MultipartFile originalPhoto) throws IOException; + } diff --git a/src/main/java/tech/crm/crmserver/service/impl/IpAddressServiceImpl.java b/src/main/java/tech/crm/crmserver/service/impl/IpAddressServiceImpl.java new file mode 100644 index 0000000..7be3c81 --- /dev/null +++ b/src/main/java/tech/crm/crmserver/service/impl/IpAddressServiceImpl.java @@ -0,0 +1,102 @@ +package tech.crm.crmserver.service.impl; + +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import tech.crm.crmserver.common.constants.EmailConstants; +import tech.crm.crmserver.common.utils.IpUtil; +import tech.crm.crmserver.dao.IpAddress; +import tech.crm.crmserver.dao.User; +import tech.crm.crmserver.mapper.IpAddressMapper; +import tech.crm.crmserver.service.IpAddressService; +import tech.crm.crmserver.service.MailService; +import tech.crm.crmserver.service.UserService; + +/** + *

+ * serviceImpl + *

+ * + * @author Lingxiao + * @since 2021-10-19 + */ +@Service +public class IpAddressServiceImpl extends ServiceImpl implements IpAddressService { + + @Autowired + private MailService mailService; + + @Autowired + private UserService userService; + + /** + * check whether this ip is a new ip for the user
+ * if yes, send a email to user and return true + * if no, return false + * + * @param userId the id of user + * @param ip the ip of user + * @return whether is a new ip + */ + @Override + public boolean checkIpAddress(Integer userId, String ip) { + if(ip == null){ + return false; + } + IpAddress ipAddress = IpUtil.sendPostRequest(ip); + // maybe we exceed the limit of 45 request / minutes + if(ipAddress == null){ + return false; + } + ipAddress.setUserId(userId); + + QueryWrapper wrapper = new QueryWrapper<>(); + wrapper.eq("user_id",userId).eq("country",ipAddress.getCountry()) + .eq("region_name",ipAddress.getRegionName()) + .eq("city",ipAddress.getCity()); + + IpAddress ipAddress1 = this.baseMapper.selectOne(wrapper); + if(ipAddress1 != null){ + return false; + } + else { + save(ipAddress); + User user = userService.getById(userId); + mailService.sendSimpleMail(user.getEmail(), EmailConstants.NEW_LOGIN_TITLE, + "New login to your ConnecTi account from " + ipAddress.getCity() + " " + ipAddress.getRegionName() + " " + ipAddress.getCountry()); + return true; + } + } + + /** + * save a new address according to ip + * + * @param userId the id of user + * @param ip the ip + */ + @Override + public void saveNewIp(Integer userId, String ip) { + if(ip == null){ + return; + } + IpAddress ipAddress = IpUtil.sendPostRequest(ip); + if(ipAddress == null){ + return; + } + ipAddress.setUserId(userId); + save(ipAddress); + } + + /** + * clean ip address by user id + * + * @param userId the id of user + */ + @Override + public void cleanIpByUserId(Integer userId) { + QueryWrapper wrapper = new QueryWrapper<>(); + wrapper.eq("user_id", userId); + this.baseMapper.delete(wrapper); + } +} diff --git a/src/main/java/tech/crm/crmserver/service/impl/UserServiceImpl.java b/src/main/java/tech/crm/crmserver/service/impl/UserServiceImpl.java index 66a769e..441b2da 100644 --- a/src/main/java/tech/crm/crmserver/service/impl/UserServiceImpl.java +++ b/src/main/java/tech/crm/crmserver/service/impl/UserServiceImpl.java @@ -196,9 +196,10 @@ public Integer getId(){ * and delete all the token of this user in database * * @param email the email of user who needs reset password + * @return user who reset password */ @Override - public void resetPassword(String email) { + public User resetPassword(String email) { UpdateWrapper wrapper = new UpdateWrapper<>(); wrapper.eq("email",email); User user = this.getOne(wrapper); @@ -217,6 +218,7 @@ public void resetPassword(String email) { update(wrapper); //send email mailService.sendSimpleMail(email, EmailConstants.EMAIL_RESET_PASSWORD_TITLE, rawPassword); + return user; } /** diff --git a/src/main/resources/db/schema-test.sql b/src/main/resources/db/schema-test.sql index 55071a0..60d22b7 100644 --- a/src/main/resources/db/schema-test.sql +++ b/src/main/resources/db/schema-test.sql @@ -21,19 +21,19 @@ USE `testdb` ; CREATE TABLE IF NOT EXISTS `testdb`.`user` ( `id` INT NOT NULL AUTO_INCREMENT, `email` VARCHAR(255) NOT NULL, - `password` VARCHAR(255) NOT NULL, - `first_name` VARCHAR(45) NOT NULL, - `middle_name` VARCHAR(45) NULL, - `last_name` VARCHAR(45) NOT NULL, - `phone` VARCHAR(45) NOT NULL, - `recent_activity` DATETIME NULL, - `website` VARCHAR(255) NULL, - `description` LONGTEXT NULL, - `status` ENUM('active', 'deleted', 'pending') NOT NULL DEFAULT 'active', - `photo` MEDIUMBLOB NULL, - PRIMARY KEY (`id`), - UNIQUE INDEX `name_UNIQUE` (`id` ASC) , - UNIQUE INDEX `email_UNIQUE` (`email` ASC) ) + `password` VARCHAR(255) NOT NULL, + `first_name` VARCHAR(45) NOT NULL, + `middle_name` VARCHAR(45) NULL, + `last_name` VARCHAR(45) NOT NULL, + `phone` VARCHAR(45) NOT NULL, + `recent_activity` DATETIME NULL, + `website` VARCHAR(255) NULL, + `description` LONGTEXT NULL, + `status` ENUM('active', 'deleted', 'pending') NOT NULL DEFAULT 'active', + `photo` MEDIUMBLOB NULL, + PRIMARY KEY (`id`), + UNIQUE INDEX `name_UNIQUE` (`id` ASC) , + UNIQUE INDEX `email_UNIQUE` (`email` ASC) ) ENGINE = InnoDB; @@ -43,17 +43,17 @@ CREATE TABLE IF NOT EXISTS `testdb`.`user` ( CREATE TABLE IF NOT EXISTS `testdb`.`organization` ( `id` INT NOT NULL AUTO_INCREMENT, `name` VARCHAR(255) NOT NULL, - `owner` INT NOT NULL, - `status` ENUM('active', 'deleted') NOT NULL DEFAULT 'active', - PRIMARY KEY (`id`), - UNIQUE INDEX `name_UNIQUE` (`name` ASC) , - UNIQUE INDEX `id_UNIQUE` (`id` ASC) , - INDEX `fk_orgnization_client_idx` (`owner` ASC) , - CONSTRAINT `fk_orgnization_client` - FOREIGN KEY (`owner`) - REFERENCES `testdb`.`user` (`id`) - ON DELETE NO ACTION - ON UPDATE NO ACTION) + `owner` INT NOT NULL, + `status` ENUM('active', 'deleted') NOT NULL DEFAULT 'active', + PRIMARY KEY (`id`), + UNIQUE INDEX `name_UNIQUE` (`name` ASC) , + UNIQUE INDEX `id_UNIQUE` (`id` ASC) , + INDEX `fk_orgnization_client_idx` (`owner` ASC) , + CONSTRAINT `fk_orgnization_client` + FOREIGN KEY (`owner`) + REFERENCES `testdb`.`user` (`id`) + ON DELETE NO ACTION + ON UPDATE NO ACTION) ENGINE = InnoDB; @@ -63,16 +63,16 @@ CREATE TABLE IF NOT EXISTS `testdb`.`organization` ( CREATE TABLE IF NOT EXISTS `testdb`.`department` ( `id` INT NOT NULL AUTO_INCREMENT, `name` VARCHAR(255) NOT NULL, - `organization_id` INT NOT NULL, - `status` ENUM('active', 'deleted') NOT NULL DEFAULT 'active', - PRIMARY KEY (`id`), - UNIQUE INDEX `id_UNIQUE` (`id` ASC) , - INDEX `fk_department_orgnization1_idx` (`organization_id` ASC) , - CONSTRAINT `fk_department_orgnization1` - FOREIGN KEY (`organization_id`) - REFERENCES `testdb`.`organization` (`id`) - ON DELETE NO ACTION - ON UPDATE NO ACTION) + `organization_id` INT NOT NULL, + `status` ENUM('active', 'deleted') NOT NULL DEFAULT 'active', + PRIMARY KEY (`id`), + UNIQUE INDEX `id_UNIQUE` (`id` ASC) , + INDEX `fk_department_orgnization1_idx` (`organization_id` ASC) , + CONSTRAINT `fk_department_orgnization1` + FOREIGN KEY (`organization_id`) + REFERENCES `testdb`.`organization` (`id`) + ON DELETE NO ACTION + ON UPDATE NO ACTION) ENGINE = InnoDB; @@ -83,26 +83,26 @@ CREATE TABLE IF NOT EXISTS `testdb`.`contact` ( `id` INT NOT NULL AUTO_INCREMENT, `department_id` INT NOT NULL, `email` VARCHAR(255) NOT NULL, - `first_name` VARCHAR(45) NOT NULL, - `middle_name` VARCHAR(45) NULL, - `last_name` VARCHAR(45) NOT NULL, - `phone` VARCHAR(45) NULL, - `description` LONGTEXT NULL, - `gender` ENUM('male', 'female', 'not specified') NULL, - `birthday` DATE NULL, - `address` VARCHAR(255) NULL, - `organization` VARCHAR(255) NULL, - `customer_type` ENUM('company', 'personal') NULL, - `status` ENUM('active', 'deleted') NOT NULL DEFAULT 'active', - `photo` MEDIUMBLOB NULL, - PRIMARY KEY (`id`), - UNIQUE INDEX `name_UNIQUE` (`id` ASC) , - INDEX `fk_customer_department1_idx` (`department_id` ASC) , - CONSTRAINT `fk_customer_department1` - FOREIGN KEY (`department_id`) - REFERENCES `testdb`.`department` (`id`) - ON DELETE NO ACTION - ON UPDATE NO ACTION) + `first_name` VARCHAR(45) NOT NULL, + `middle_name` VARCHAR(45) NULL, + `last_name` VARCHAR(45) NOT NULL, + `phone` VARCHAR(45) NULL, + `description` LONGTEXT NULL, + `gender` ENUM('male', 'female', 'not specified') NULL, + `birthday` DATE NULL, + `address` VARCHAR(255) NULL, + `organization` VARCHAR(255) NULL, + `customer_type` ENUM('company', 'personal') NULL, + `status` ENUM('active', 'deleted') NOT NULL DEFAULT 'active', + `photo` MEDIUMBLOB NULL, + PRIMARY KEY (`id`), + UNIQUE INDEX `name_UNIQUE` (`id` ASC) , + INDEX `fk_customer_department1_idx` (`department_id` ASC) , + CONSTRAINT `fk_customer_department1` + FOREIGN KEY (`department_id`) + REFERENCES `testdb`.`department` (`id`) + ON DELETE NO ACTION + ON UPDATE NO ACTION) ENGINE = InnoDB; @@ -115,15 +115,15 @@ CREATE TABLE IF NOT EXISTS `testdb`.`event` ( `start_time` DATETIME NOT NULL, `finish_time` DATETIME NOT NULL, `description` LONGTEXT NULL, - `status` ENUM('to do', 'in progress', 'done') NOT NULL DEFAULT 'to do', - PRIMARY KEY (`id`), - UNIQUE INDEX `id_UNIQUE` (`id` ASC) , - INDEX `fk_order_client1_idx` (`user_id` ASC) , - CONSTRAINT `fk_order_client10` - FOREIGN KEY (`user_id`) - REFERENCES `testdb`.`user` (`id`) - ON DELETE NO ACTION - ON UPDATE NO ACTION) + `status` ENUM('upcoming', 'in progress', 'done') NOT NULL DEFAULT 'upcoming', + PRIMARY KEY (`id`), + UNIQUE INDEX `id_UNIQUE` (`id` ASC) , + INDEX `fk_order_client1_idx` (`user_id` ASC) , + CONSTRAINT `fk_order_client10` + FOREIGN KEY (`user_id`) + REFERENCES `testdb`.`user` (`id`) + ON DELETE NO ACTION + ON UPDATE NO ACTION) ENGINE = InnoDB; @@ -135,19 +135,19 @@ CREATE TABLE IF NOT EXISTS `testdb`.`attend` ( `event_id` INT NOT NULL, `contact_id` INT NOT NULL, PRIMARY KEY (`id`), - UNIQUE INDEX `id_UNIQUE` (`id` ASC) , - INDEX `fk_attend_event1_idx` (`event_id` ASC) , - INDEX `fk_attend_customer1_idx` (`contact_id` ASC) , - CONSTRAINT `fk_attend_event1` - FOREIGN KEY (`event_id`) - REFERENCES `testdb`.`event` (`id`) - ON DELETE NO ACTION - ON UPDATE NO ACTION, - CONSTRAINT `fk_attend_customer1` - FOREIGN KEY (`contact_id`) - REFERENCES `testdb`.`contact` (`id`) - ON DELETE NO ACTION - ON UPDATE NO ACTION) + UNIQUE INDEX `id_UNIQUE` (`id` ASC) , + INDEX `fk_attend_event1_idx` (`event_id` ASC) , + INDEX `fk_attend_customer1_idx` (`contact_id` ASC) , + CONSTRAINT `fk_attend_event1` + FOREIGN KEY (`event_id`) + REFERENCES `testdb`.`event` (`id`) + ON DELETE NO ACTION + ON UPDATE NO ACTION, + CONSTRAINT `fk_attend_customer1` + FOREIGN KEY (`contact_id`) + REFERENCES `testdb`.`contact` (`id`) + ON DELETE NO ACTION + ON UPDATE NO ACTION) ENGINE = InnoDB; @@ -159,20 +159,20 @@ CREATE TABLE IF NOT EXISTS `testdb`.`belong_to` ( `user_id` INT NOT NULL, `organization_id` INT NOT NULL, `status` ENUM('active', 'deleted') NOT NULL DEFAULT 'active', - INDEX `fk_belong_to_client1_idx` (`user_id` ASC) , - INDEX `fk_belong_to_orgnization1_idx` (`organization_id` ASC) , - PRIMARY KEY (`id`), - UNIQUE INDEX `id_UNIQUE` (`id` ASC) , - CONSTRAINT `fk_belong_to_client1` - FOREIGN KEY (`user_id`) - REFERENCES `testdb`.`user` (`id`) - ON DELETE NO ACTION - ON UPDATE NO ACTION, - CONSTRAINT `fk_belong_to_orgnization1` - FOREIGN KEY (`organization_id`) - REFERENCES `testdb`.`organization` (`id`) - ON DELETE NO ACTION - ON UPDATE NO ACTION) + INDEX `fk_belong_to_client1_idx` (`user_id` ASC) , + INDEX `fk_belong_to_orgnization1_idx` (`organization_id` ASC) , + PRIMARY KEY (`id`), + UNIQUE INDEX `id_UNIQUE` (`id` ASC) , + CONSTRAINT `fk_belong_to_client1` + FOREIGN KEY (`user_id`) + REFERENCES `testdb`.`user` (`id`) + ON DELETE NO ACTION + ON UPDATE NO ACTION, + CONSTRAINT `fk_belong_to_orgnization1` + FOREIGN KEY (`organization_id`) + REFERENCES `testdb`.`organization` (`id`) + ON DELETE NO ACTION + ON UPDATE NO ACTION) ENGINE = InnoDB; @@ -185,20 +185,20 @@ CREATE TABLE IF NOT EXISTS `testdb`.`permission` ( `department_id` INT NOT NULL, `authority_level` INT NOT NULL DEFAULT 0, `status` ENUM('active', 'deleted', 'pending') NOT NULL DEFAULT 'active', - INDEX `fk_belong_to_client1_idx` (`user_id` ASC) , - PRIMARY KEY (`id`), - UNIQUE INDEX `id_UNIQUE` (`id` ASC) , - INDEX `fk_belong_to_copy1_department1_idx` (`department_id` ASC) , - CONSTRAINT `fk_belong_to_client10` - FOREIGN KEY (`user_id`) - REFERENCES `testdb`.`user` (`id`) - ON DELETE NO ACTION - ON UPDATE NO ACTION, - CONSTRAINT `fk_belong_to_copy1_department1` - FOREIGN KEY (`department_id`) - REFERENCES `testdb`.`department` (`id`) - ON DELETE NO ACTION - ON UPDATE NO ACTION) + INDEX `fk_belong_to_client1_idx` (`user_id` ASC) , + PRIMARY KEY (`id`), + UNIQUE INDEX `id_UNIQUE` (`id` ASC) , + INDEX `fk_belong_to_copy1_department1_idx` (`department_id` ASC) , + CONSTRAINT `fk_belong_to_client10` + FOREIGN KEY (`user_id`) + REFERENCES `testdb`.`user` (`id`) + ON DELETE NO ACTION + ON UPDATE NO ACTION, + CONSTRAINT `fk_belong_to_copy1_department1` + FOREIGN KEY (`department_id`) + REFERENCES `testdb`.`department` (`id`) + ON DELETE NO ACTION + ON UPDATE NO ACTION) ENGINE = InnoDB; @@ -211,14 +211,14 @@ CREATE TABLE IF NOT EXISTS `testdb`.`to_do_list` ( `date_time` DATETIME NOT NULL, `description` LONGTEXT NULL, `status` ENUM('to do', 'in progress', 'done') NOT NULL DEFAULT 'to do', - PRIMARY KEY (`id`), - UNIQUE INDEX `id_UNIQUE` (`id` ASC) , - INDEX `fk_order_client1_idx` (`user_id` ASC) , - CONSTRAINT `fk_order_client100` - FOREIGN KEY (`user_id`) - REFERENCES `testdb`.`user` (`id`) - ON DELETE NO ACTION - ON UPDATE NO ACTION) + PRIMARY KEY (`id`), + UNIQUE INDEX `id_UNIQUE` (`id` ASC) , + INDEX `fk_order_client1_idx` (`user_id` ASC) , + CONSTRAINT `fk_order_client100` + FOREIGN KEY (`user_id`) + REFERENCES `testdb`.`user` (`id`) + ON DELETE NO ACTION + ON UPDATE NO ACTION) ENGINE = InnoDB; @@ -230,18 +230,18 @@ CREATE TABLE IF NOT EXISTS `testdb`.`recent_contact` ( `user_id` INT NOT NULL, `last_contact` DATETIME NOT NULL, INDEX `fk_order_customer1_idx` (`contact_id` ASC) , - INDEX `fk_order_client1_idx` (`user_id` ASC) , - PRIMARY KEY (`contact_id`, `user_id`), - CONSTRAINT `fk_order_customer10` - FOREIGN KEY (`contact_id`) - REFERENCES `testdb`.`contact` (`id`) - ON DELETE NO ACTION - ON UPDATE NO ACTION, - CONSTRAINT `fk_order_client11` - FOREIGN KEY (`user_id`) - REFERENCES `testdb`.`user` (`id`) - ON DELETE NO ACTION - ON UPDATE NO ACTION) + INDEX `fk_order_client1_idx` (`user_id` ASC) , + PRIMARY KEY (`contact_id`, `user_id`), + CONSTRAINT `fk_order_customer10` + FOREIGN KEY (`contact_id`) + REFERENCES `testdb`.`contact` (`id`) + ON DELETE NO ACTION + ON UPDATE NO ACTION, + CONSTRAINT `fk_order_client11` + FOREIGN KEY (`user_id`) + REFERENCES `testdb`.`user` (`id`) + ON DELETE NO ACTION + ON UPDATE NO ACTION) ENGINE = InnoDB; @@ -252,18 +252,34 @@ CREATE TABLE IF NOT EXISTS `testdb`.`token_key` ( `id` INT NOT NULL AUTO_INCREMENT, `user_id` INT NOT NULL, `jwt_key` VARCHAR(255) NOT NULL, - `expired_time` DATETIME NOT NULL, - PRIMARY KEY (`id`), - UNIQUE INDEX `idtoken_UNIQUE` (`id` ASC) , - INDEX `fk_token_user1_idx` (`user_id` ASC) , - CONSTRAINT `fk_token_user1` - FOREIGN KEY (`user_id`) - REFERENCES `testdb`.`user` (`id`) - ON DELETE NO ACTION - ON UPDATE NO ACTION) + `expired_time` DATETIME NOT NULL, + PRIMARY KEY (`id`), + UNIQUE INDEX `idtoken_UNIQUE` (`id` ASC) , + INDEX `fk_token_user1_idx` (`user_id` ASC) , + CONSTRAINT `fk_token_user1` + FOREIGN KEY (`user_id`) + REFERENCES `testdb`.`user` (`id`) + ON DELETE NO ACTION + ON UPDATE NO ACTION) ENGINE = InnoDB; +-- ----------------------------------------------------- +-- Table `testdb`.`ip_address` +-- ----------------------------------------------------- +CREATE TABLE IF NOT EXISTS `testdb`.`ip_address` ( + `user_id` INT NULL, + `country` VARCHAR(255) NULL, + `region_name` VARCHAR(255) NULL, + `city` VARCHAR(255) NULL, + INDEX `fk_ip_address_user1_idx` (`user_id` ASC) , + CONSTRAINT `fk_ip_address_user1` + FOREIGN KEY (`user_id`) + REFERENCES `testdb`.`user` (`id`) + ON DELETE NO ACTION + ON UPDATE NO ACTION); + + SET SQL_MODE=@OLD_SQL_MODE; SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS; -SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS; \ No newline at end of file +SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;