Skip to content

Commit 28fdfe2

Browse files
author
heavyrian2012
committed
添加禁止用户注册的开关
1 parent f35cf44 commit 28fdfe2

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

config/application.properties

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ wfc.all_client_support_ssl=false
8080
## 用户设置密码时,不能设置为手机号码的后6位
8181
wfc.default_user_password=false
8282

83+
## 是否禁止用户注册。默认为false(允许注册)。
84+
## 当设置为true时,如果登录时用户不存在,不会自动创建用户,而是返回用户不存在的错误
85+
wfc.user_register_forbidden=true
86+
8387
## iOS系统使用share extension来处理分享,客户端无法调用SDK发送消息和文件,只能通过应用服务来进行。
8488
## 这里配置为了满足iOS设备在share extension中进行上传文件的需求。
8589
## 存储使用类型,0使用内置文件服务器(这里无法使用),1使用七牛云存储,2使用阿里云对象存储,3野火私有对象存储,

src/main/java/cn/wildfirechat/app/ServiceImpl.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ public class ServiceImpl implements Service {
119119
@Value("${ldap.search_base}")
120120
private String SEARCH_BASE;
121121

122+
123+
@Value("${wfc.user_register_forbidden:false}")
124+
private boolean userRegisterForbidden;
125+
122126
@Autowired
123127
private ShortUUIDGenerator userNameGenerator;
124128

@@ -274,6 +278,20 @@ public RestResult sendLoginCode(String mobile, String slideVerifyToken) {
274278
return RestResult.error(ERROR_USER_FORBIDDEN);
275279
}
276280

281+
// 如果禁止注册,检查用户是否存在
282+
if (userRegisterForbidden) {
283+
try {
284+
IMResult<InputOutputUserInfo> userResult = UserAdmin.getUserByMobile(mobile);
285+
if (userResult.getErrorCode() == ErrorCode.ERROR_CODE_NOT_EXIST) {
286+
LOG.info("User not exist and register is forbidden, cannot send login code");
287+
return RestResult.error(ERROR_NOT_EXIST);
288+
}
289+
} catch (Exception e) {
290+
LOG.error("Check user exist error", e);
291+
return RestResult.error(ERROR_SERVER_ERROR);
292+
}
293+
}
294+
277295
String code = Utils.getRandomCode(6);
278296
RestResult.RestCode restCode = authDataSource.insertRecord(mobile, code);
279297

@@ -378,6 +396,20 @@ public RestResult sendResetCode(String mobile, String slideVerifyToken) {
378396
return RestResult.error(ERROR_USER_FORBIDDEN);
379397
}
380398

399+
// 如果禁止注册,检查用户是否存在
400+
if (userRegisterForbidden) {
401+
try {
402+
IMResult<InputOutputUserInfo> userResult = UserAdmin.getUserByMobile(mobile);
403+
if (userResult.getErrorCode() == ErrorCode.ERROR_CODE_NOT_EXIST) {
404+
LOG.info("User not exist and register is forbidden, cannot send reset code");
405+
return RestResult.error(ERROR_NOT_EXIST);
406+
}
407+
} catch (Exception e) {
408+
LOG.error("Check user exist error", e);
409+
return RestResult.error(ERROR_SERVER_ERROR);
410+
}
411+
}
412+
381413
try {
382414
String code = Utils.getRandomCode(6);
383415
RestResult.RestCode restCode = smsService.sendCode(mobile, code);
@@ -718,6 +750,10 @@ private RestResult onLoginSuccess(HttpServletResponse httpResponse, String mobil
718750
InputOutputUserInfo user;
719751
boolean isNewUser = false;
720752
if (userResult.getErrorCode() == ErrorCode.ERROR_CODE_NOT_EXIST) {
753+
if (userRegisterForbidden) {
754+
LOG.info("User not exist and register is forbidden");
755+
return RestResult.error(RestResult.RestCode.ERROR_NOT_EXIST);
756+
}
721757
LOG.info("User not exist, try to create");
722758

723759
//获取用户名。如果用的是shortUUID生成器,是有极小概率会重复的,所以需要去检查是否已经存在相同的userName。

0 commit comments

Comments
 (0)