| | |
| | | package com.vincent.rsf.server.system.controller; |
| | | |
| | | import com.vincent.rsf.common.enums.SystemModeType; |
| | | import com.vincent.rsf.common.utils.RedisSupport; |
| | | import com.vincent.rsf.common.utils.Utils; |
| | | import com.vincent.rsf.framework.common.BaseRes; |
| | | import com.vincent.rsf.framework.common.Cools; |
| | |
| | | import com.vincent.rsf.server.common.config.ConfigProperties; |
| | | import com.vincent.rsf.server.common.security.JwtSubject; |
| | | import com.vincent.rsf.server.common.service.EmailService; |
| | | import com.vincent.rsf.server.common.service.RedisService; |
| | | import com.vincent.rsf.server.common.utils.JwtUtil; |
| | | import com.vincent.rsf.server.system.controller.param.LoginParam; |
| | | import com.vincent.rsf.server.system.controller.param.RegisterParam; |
| | | import com.vincent.rsf.server.system.controller.param.TenantInitParam; |
| | | import com.vincent.rsf.server.system.controller.param.UpdatePasswordParam; |
| | | import com.vincent.rsf.server.system.controller.result.LoginResult; |
| | | import com.vincent.rsf.server.system.controller.result.MenuVo; |
| | |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.stream.Collectors; |
| | | |
| | |
| | | */ |
| | | @RestController |
| | | public class AuthController extends BaseController { |
| | | |
| | | private final RedisSupport redis = RedisSupport.defaultRedisSupport; |
| | | |
| | | @Resource |
| | | private ConfigProperties configProperties; |
| | |
| | | private TenantService tenantService; |
| | | @Autowired |
| | | private EmailService emailService; |
| | | @Autowired |
| | | private RedisService redisService; |
| | | |
| | | @PostMapping("/login") |
| | | public R login(@RequestBody LoginParam param, HttpServletRequest request) { |
| | |
| | | } |
| | | |
| | | @GetMapping("/email/code") |
| | | public R emailCode(@RequestParam("email") String email) { |
| | | public R emailCode(@RequestParam(value = "email", required = false) String email) { |
| | | if (Cools.isEmpty(email)) { |
| | | return R.parse(BaseRes.PARAM); |
| | | } |
| | | if (!emailService.isValid(email)) { |
| | | return R.error("Please enter a valid email address"); |
| | | } |
| | | if (null != userService.getByEmail(email, null)) { |
| | | return R.error("Email Already Exist"); |
| | | } |
| | | if (redis.getExpire(EmailType.REGISTER_VERIFY.toString(), email) > (configProperties.getCodeTime() - 60)) { |
| | | Long expire = redisService.getExpire(EmailType.REGISTER_VERIFY.toString(), email); |
| | | if (expire > (configProperties.getCodeTime() - 60)) { |
| | | return R.error("Please don't request code too frequently."); |
| | | } |
| | | String code = Utils.randomNumbers(configProperties.getCodeLength()); |
| | | if (emailService.sendEmail(email, EmailType.REGISTER_VERIFY, Cools.add("code", code))) { |
| | | redis.setValue(EmailType.REGISTER_VERIFY.toString(), email, code, configProperties.getCodeTime()); |
| | | redisService.setValue(EmailType.REGISTER_VERIFY.toString(), email, code, configProperties.getCodeTime()); |
| | | } |
| | | return R.ok(); |
| | | } |
| | |
| | | if (Cools.isEmpty(param.getUsername(), param.getPassword(), param.getEmail(), param.getCode())) { |
| | | return R.parse(BaseRes.PARAM); |
| | | } |
| | | String cacheCode = redis.getValue(EmailType.REGISTER_VERIFY.toString(), param.getEmail()); |
| | | // verify code |
| | | String cacheCode = redisService.getValue(EmailType.REGISTER_VERIFY.toString(), param.getEmail()); |
| | | if (Cools.isEmpty(cacheCode)) { |
| | | return R.error("The verification code has expired."); |
| | | } |
| | |
| | | return R.error("The verification code is incorrect."); |
| | | } |
| | | |
| | | |
| | | User user = new User(); |
| | | user.setUsername(param.getUsername()); |
| | | user.setNickname(param.getUsername()); |
| | | user.setPassword(userService.encodePassword(param.getPassword())); |
| | | user.setEmail(param.getEmail()); |
| | | user.setEmailVerified(1); |
| | | user.setStatus(StatusType.ENABLE.val); |
| | | user.setCreateTime(new Date()); |
| | | if (!userService.save(user)) { |
| | | throw new CoolException(""); |
| | | // register |
| | | TenantInitParam initParam = new TenantInitParam(); |
| | | initParam.setEmail(param.getEmail()); |
| | | initParam.setUsername(param.getUsername()); |
| | | initParam.setPassword(param.getPassword()); |
| | | initParam.setName(param.getUsername()); |
| | | initParam.setFlag(param.getUsername()); |
| | | if (!tenantService.initTenant(initParam)) { |
| | | throw new CoolException("Failed to register"); |
| | | } |
| | | |
| | | redisService.delete(EmailType.REGISTER_VERIFY.toString(), param.getEmail()); |
| | | return R.ok(); |
| | | } |
| | | |