Mod aaa-12 用户认证数据库获取配置信息,新增用户/用户组去除前后空格

RCA:
SOL:
修改人:zhouzian
检视人:zhouzian
This commit is contained in:
zhouzian 2019-09-09 20:13:18 +08:00
parent 341f77d29f
commit c37bc6e71d
8 changed files with 207 additions and 73 deletions

View File

@ -129,13 +129,13 @@ endif
usermanager:
ifeq ($(OPT), clean)
$(MLOG)make $(MAKE_FLAGS) -C Product/build -f user.usermanager-auth.Makefile cleanall MLOG=$(MLOG) MAKE_TARGET=usermanager-auth
$(MLOG)make $(MAKE_FLAGS) -C Product/build -f user.usermanager-test.Makefile cleanall MLOG=$(MLOG) MAKE_TARGET=usermanager-test
# $(MLOG)make $(MAKE_FLAGS) -C Product/build -f user.usermanager-test.Makefile cleanall MLOG=$(MLOG) MAKE_TARGET=usermanager-test
else ifeq ($(OPT), install)
$(MLOG)make $(MAKE_FLAGS) -C Product/build -f user.usermanager-auth.Makefile install DIR=$(DIR) MLOG=$(MLOG) MAKE_TARGET=usermanager-auth
$(MLOG)make $(MAKE_FLAGS) -C Product/build -f user.usermanager-test.Makefile install DIR=$(DIR) MLOG=$(MLOG) MAKE_TARGET=usermanager-test
# $(MLOG)make $(MAKE_FLAGS) -C Product/build -f user.usermanager-test.Makefile install DIR=$(DIR) MLOG=$(MLOG) MAKE_TARGET=usermanager-test
else
$(MLOG)make all $(MAKE_FLAGS) -C Product/build -f user.usermanager-auth.Makefile MLOG=$(MLOG) DISABLE_WARRING=$(DIS_BUILD_WARRING) MAKE_TARGET=usermanager-auth
$(MLOG)make all $(MAKE_FLAGS) -C Product/build -f user.usermanager-test.Makefile MLOG=$(MLOG) DISABLE_WARRING=$(DIS_BUILD_WARRING) MAKE_TARGET=usermanager-test
# $(MLOG)make all $(MAKE_FLAGS) -C Product/build -f user.usermanager-test.Makefile MLOG=$(MLOG) DISABLE_WARRING=$(DIS_BUILD_WARRING) MAKE_TARGET=usermanager-test
endif
configm:

View File

@ -170,6 +170,7 @@ ret_code userecover_config_get_all(uint source,
s2j_delete_struct_obj(temp_user);
UCHAR_FREE(user_db);
}
printf("success user_manager recovering ... \n");
return ret;
}

View File

@ -1,7 +1,8 @@
#ifndef COMMON_USER_H_
#define COMMON_USER_H_
#define SPECHAR(element) (strpbrk((element), "~!@#$%^&*()_+{}|:\"<>?\\,./;\'[]-=`")) //校验特殊字符
//#define SPECHAR(element) (strpbrk((element), "~!@#$%^&*()_+{}|:\"<>?\\,./;\'[]-=`")) //校验特殊字符
#define SPECHAR(element) (strpbrk((element), "@,;")) //校验特殊字符
#define GETID(ID, NAME1, NAME2) ((((ID) != 0) && (strcmp((NAME1), (NAME2)) == 0)) ? (ID) : 0) //根据name查询ID
#define CHECKOUTARG(element) ((NULL == (element) || "" == (element) || SPECHAR(element)) ? true : false) //校验参数
#define INVALID_INDEX (0)
@ -91,4 +92,18 @@ do{ \
} else {*id_int_out = 0;} \
}while(0)
/* 去除字符串前后空格 */
#define TRIM_NAME(in, out) \
do { \
char * c; \
int i; \
size_t nLen = strlen(in); \
i = 0; \
while(in[i] == ' ') {i++;} \
c = &in[i]; \
i = nLen - 1; \
while(in[i] == ' ') {i--;} \
memcpy(out, c, i + 1 - (c - in)); \
} while (0)
#endif

View File

@ -11,13 +11,11 @@
#include "database.h"
#include "cjson/cJSON.h"
#include "s2j/s2j.h"
//#include "sg/user/user_manager/user_auth.h"
extern ARRAY g_user_index_head;
extern USERGROUP g_group_table[GROUP_INDEX_MAX];
extern exce_mod_user g_user_modfunc_table[USER_ATTRIBUTE_NUM];
extern void * um_hdbc;
//extern USER_AUTH_LIST g_user_auth_ret_table[];
USERACCOUNT g_user_table[USER_INDEX_MAX];
/*初始化参数*/
@ -35,10 +33,11 @@ int init_user()
}
/*添加元素-新增用户*/
void usermanager_add_user(char* uname, char* gname, char* udesp, char* pwd, int multi, int valid, char* valid_begin_time, char* valid_end_time, USERADD* uaddres)
void usermanager_add_user(char* uname_in, char* gname, char* udesp, char* pwd, int multi, int valid, char* valid_begin_time, char* valid_end_time, USERADD* uaddres)
{
time_t begin_time;
time_t end_time;
char uname[UNAMESIZE] = {0};
if (NULL == uaddres)
{
@ -46,7 +45,7 @@ void usermanager_add_user(char* uname, char* gname, char* udesp, char* pwd, int
}
uaddres->userID = INVALID_INDEX;
if (NULL == uname || NULL == gname)
if (NULL == uname_in || NULL == gname)
{
uaddres->result = ADDGROUP_ERROR;
return;
@ -58,19 +57,22 @@ void usermanager_add_user(char* uname, char* gname, char* udesp, char* pwd, int
}
/* 校验用户名和描述长度 */
if (UNAMESIZE < strlen(uname) || 0 >= strlen(uname) || UDESIZE < strlen(udesp))
if (UNAMESIZE < strlen(uname_in) || 0 >= strlen(uname_in) || UDESIZE < strlen(udesp))
{
uaddres->result = ADDUSER_FAIL_LENGTH;
return;
}
/* 校验用户名中不含特殊字符 */
if (SPECHAR(uname))
if (SPECHAR(uname_in))
{
uaddres->result = ADDUSER_FAIL_NAMESPE;
return;
}
/* 去除用户名前后空格 */
TRIM_NAME(uname_in, uname);
if (NULL == pwd || 0 == strcmp("",pwd))
{
pwd = "A31as%4Gb";
@ -957,7 +959,7 @@ void um_getuser_db(char ** user_db)
UNIQUE KEY user_name (user_name) USING BTREE,\
KEY group_id (group_id) USING BTREE,\
CONSTRAINT user_account_ibfk_1 FOREIGN KEY (group_id) REFERENCES user_group (id) ON DELETE CASCADE ON UPDATE CASCADE\
) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8";
) ENGINE=InnoDB DEFAULT CHARSET=utf8";
ret_create = create_database_table(USER_MANAGER_DBID, um_hdbc, USER_TABLE, create_user);
if(DB_RET_OK != ret_create)
{

View File

@ -30,9 +30,11 @@ int init_group()
}
/* 新增用户组 */
int add_group(char* name, char* description)
int add_group(char* name_in, char* description)
{
if (NULL == name)
char name[GNAMESIZE] = {0};
if (NULL == name_in)
{
return ADDGROUP_ERROR;
}
@ -43,17 +45,20 @@ int add_group(char* name, char* description)
}
/* 校验用户组名和描述的长度 */
if (GNAMESIZE < strlen(name) || 0 >= strlen(name) || GDESIZE < strlen(description))
if (GNAMESIZE < strlen(name_in) || 0 >= strlen(name_in) || GDESIZE < strlen(description))
{
return ADDGROUP_FAIL_LENGTH;
}
/* 校验特殊字符 */
if (SPECHAR(name))
if (SPECHAR(name_in))
{
return ADDGROUP_FAIL_SPECHARS;
}
/* 去除用户组名前后空格 */
TRIM_NAME(name_in, name);
/* 连接数据库 */
re_connect();
if(NULL == um_hdbc)
@ -461,7 +466,7 @@ void um_getgroup_db(char ** group_db)
PRIMARY KEY (uuid),\
UNIQUE KEY id (id) USING BTREE,\
UNIQUE KEY gname (gname) USING BTREE\
) ENGINE=InnoDB AUTO_INCREMENT=1059 DEFAULT CHARSET=utf8";
) ENGINE=InnoDB DEFAULT CHARSET=utf8";
ret_create = create_database_table(USER_MANAGER_DBID, um_hdbc, USER_GROUP_TABLE, create_group);
if(DB_RET_OK != ret_create)
{

View File

@ -239,7 +239,7 @@ bool get_config_data(void * auth_hdbc, int * config_data)
return false;
}
char * select_sql = "SELECT locktime config_lock_time, failcount config_fail_num, timehorizon config_fail_time FROM `authparas` LIMIT 0,1";
char * select_sql = "SELECT locktime config_lock_time, failcount config_fail_num, timehorizon config_fail_time FROM authparas LIMIT 0,1";
char * ret_sql = select_datebase_by_number(20, auth_hdbc, "authparas", select_sql, 1, 0, &num, 0);
if(0 == num || NULL == ret_sql)
@ -311,7 +311,7 @@ bool get_user_from_database(char* username, void* hdbc, USERACCOUNT* user_info,
return false;
}
char * select_sql = "SELECT id, group_id, multi_player, valid_always, user_name,password, udescription,valid_begin_time,valid_end_time FROM `user_account`WHERE user_name = ?";
char * select_sql = "SELECT id, group_id, multi_player, valid_always, user_name,password, udescription,valid_begin_time,valid_end_time FROM user_account WHERE user_name = ?";
ret_sql = select_datebase_by_number(20, hdbc, "user_account", select_sql, 1, 0, num_sql, 1,
DB_DATA_STRING_TYPE, strlen(username)+1, username);
@ -455,6 +455,7 @@ void user_auth_login(char* username, char* password, USER_AUTH_RET *auth_result)
unsigned short user_id, group_id;
int init_queue_ret; //初始化循环列表的结果
int user_valid; //数据库中的数据
int user_multi;
int config_lock_time = 0; //锁定后的锁定时间,锁定后
int config_fail_num = 0; //规定时间内允许失败的次数,锁定次数,锁定前
int config_fail_time = 0; //规定时间,失败的时间范围,锁定前
@ -498,24 +499,6 @@ void user_auth_login(char* username, char* password, USER_AUTH_RET *auth_result)
/* 校验上述的三个参数都要大于0 */
//3、根据用户名查询用户信息-用户id和用户组id
// user_info = (USERACCOUNT*)malloc(sizeof(USERACCOUNT));
// memset(user_info, 0, sizeof(USERACCOUNT));
// if (NULL == user_info)
// {
// /* 记录日志 */
// //printf("user_auth()->user_auth->user_info:error. \n");
// auth_result->ret = AUTH_ERR;
// return;
// }
//读取json文件获取数据
// get_from_json(username, user_info);
// if (NULL == user_info)
// {
// auth_result->ret = AUTH_FAIL_PASSWD;
// xfree(user_info);
// return;
// }
/* 数据库查询 */
bool ret_getuser = get_user_from_database(username, auth_hdbc, &user_info, &sql_num);
if(!ret_getuser)
@ -598,7 +581,21 @@ void user_auth_login(char* username, char* password, USER_AUTH_RET *auth_result)
}
}
//7、判断在线用户是否到最大值
//7、判断是否允许多用户登陆
user_multi = AUTH_MULTI_GET(user_info.multi_valid);
if(1 == user_multi)
{
if(1 == AUTH_RECORD.online_num)
{
auth_result->ret = AUTH_FAIL_MULTI;
/* 认证失败处理 */
auth_fail_operate(user_id, login_time, config_fail_time);
return;
}
}
//8、判断在线用户是否到最大值(100)
if (AUTH_USER_INDEX_MAX - 2 <= AUTH_RECORD.online_num)
{
auth_result->ret = AUTH_FAIL_OVER;
@ -608,7 +605,7 @@ void user_auth_login(char* username, char* password, USER_AUTH_RET *auth_result)
return;
}
//8、匹配密码
//9、匹配密码
if (0 != strcmp(password, user_info.passwd))
{
auth_result->ret = AUTH_FAIL_PASSWD;
@ -618,7 +615,7 @@ void user_auth_login(char* username, char* password, USER_AUTH_RET *auth_result)
return;
}
//9、认证成功处理
//10、认证成功处理
AUTH_RECORD.group_id = group_id; //更新用户组id
empty_fail_time_queue(user_id);
AUTH_RECORD.online_num++;

View File

@ -4,6 +4,7 @@
#include <unistd.h>
#include <cjson/cJSON.h>
#include <string.h>
#include <ctype.h>
#include "common_user.h"
#include "array_index.h"
#include "user_group.h"
@ -70,22 +71,8 @@ int main6(void)
return 0;
}
void test()
int main5(void)
{
char *a = "和家亲";
char *b = "asd";
char *c = "123";
printf("%ld .\n",strlen(a));
printf("%ld .\n",strlen(b));
printf("%ld .\n",strlen(c));
return;
}
int main(void)
{
//test();
char * testrec;
int testnum;
userecover_config_get_all(CONFIG_FROM_RECOVER1,testrec, &testnum);
@ -96,6 +83,14 @@ int main(void)
ret_code ret_pro; //执行结果
int outlen = 1; //out数据长度
printf("\n************************************user uuid -> user 详情*******************************************\n");
testin = "{\"type\": 3,\"data\":{\"user_id\": 15}}";
ret_pro = user_config_get(1, testin, strlen(testin)+1, testout, &outlen);
printf("执行结果:%d, 返回数据:%s .\n", ret_pro, testout);
return 0;
printf("\n************************************修改 group 描述*******************************************\n");
testin = "{\"type\": 2,\"data\": {\"gid\": 1013,\"gdescription\": \"修改修改用户组的描述修改修改用户组的描述一二\"}}";
ret_pro = usergroup_config_proc(1, 1, testin, strlen(testin)+1, testout, &outlen);
@ -107,8 +102,6 @@ int main(void)
printf("执行结果:%d, 返回数据:%s .\n", ret_pro, testout);
return 0;
printf("************************************新增 group*******************************************\n");
testin = "{\"type\": 0,\"data\": {\"gname\": \"group011\",\"gdescription\": \"描述group011\"}}";
ret_pro = usergroup_config_proc(1, 1, testin, strlen(testin)+1, testout, &outlen);
@ -133,12 +126,44 @@ int main(void)
return 0;
}
int main4(void)
void trim(char *out, char *in)
{
char * c;
int i;
size_t nLen = strlen(in);
i = 0;
while(in[i] == ' ')
{
i++;
}
c = &in[i]; // 找到第一个非空字符指针
i = nLen - 1;
while(in[i] == ' ')
{
i--; // 找到最后一个非空字符
}
memcpy(out, c, i + 1 - (c - in));
return;
}
int main(void)
{
char *name1 = " xi阿斯蒂芬就aom ig ";
int len = strlen(name1);
char name2[100] = {0};
printf("name1=%s.\n",name1);
TRIM_NAME(name1, name2);
printf("name1=%s.\n",name2);
return 0;
USER_AUTH_RET user_ret;
user_auth_login("user04", "A31as%4G", &user_ret);
user_auth_login("user04", "A31as%4G", &user_ret);
user_auth_login("user04", "A31as%4Gb", &user_ret);
user_auth_login("user04", "A31as%4Gb", &user_ret);
user_auth_login("user04", "A31as%4G", &user_ret);
user_auth_login("user04", "A31as%4G", &user_ret);
user_auth_login("user04", "A31as%4G", &user_ret);

View File

@ -63,9 +63,9 @@ do { \
USER_AUTH_LIST g_user_auth_ret_table[AUTH_USER_INDEX_MAX] = { 0 };
/*
* config_lock_time -,()
* config_fail_num -
* config_fail_time -,()
* g_config_lock_time -,()
* g_config_fail_num -
* g_config_fail_time -,()
*/
static int g_config_lock_time, g_config_fail_num, g_config_fail_time;
@ -231,6 +231,77 @@ static void auth_fail_operate(unsigned short user_id, time_t login_time, int con
// user_info = NULL;
// }
bool get_config_data(void * auth_hdbc, int * config_data)
{
int num;
if(NULL == auth_hdbc || NULL == config_data)
{
return false;
}
char * select_sql = "SELECT locktime config_lock_time, failcount config_fail_num, timehorizon config_fail_time FROM authparas LIMIT 0,1";
char * ret_sql = select_datebase_by_number(20, auth_hdbc, "authparas", select_sql, 1, 0, &num, 0);
if(0 == num || NULL == ret_sql)
{
return false;
}
cJSON * root = cJSON_Parse(ret_sql);
if(!root)
{
return false;
}
cJSON * data = cJSON_GetObjectItem(root, "data");
if(!data)
{
cJSON_Delete(root);
return false;
}
int data_num = cJSON_GetArraySize(data);
if (1 != data_num)
{
cJSON_Delete(root);
return false;
}
cJSON * user_json = cJSON_GetArrayItem(data, 0);
if(!user_json)
{
cJSON_Delete(root);
return false;
}
/* 解析各个数据项 */
cJSON * config_lock_time = cJSON_GetObjectItem(user_json, "config_lock_time");
if(!config_lock_time)
{
cJSON_Delete(root);
return false;
}
config_data[0] = config_lock_time->valueint;
cJSON * config_fail_num = cJSON_GetObjectItem(user_json, "config_fail_num");
if(!config_fail_num)
{
cJSON_Delete(root);
return false;
}
config_data[1] = config_fail_num->valueint;
cJSON * config_fail_time = cJSON_GetObjectItem(user_json, "config_fail_time");
if(!config_fail_time)
{
cJSON_Delete(root);
return false;
}
config_data[2] = config_fail_time->valueint;
return true;
}
bool get_user_from_database(char* username, void* hdbc, USERACCOUNT* user_info, int* num_sql)
{
char * ret_sql = NULL;
@ -240,7 +311,7 @@ bool get_user_from_database(char* username, void* hdbc, USERACCOUNT* user_info,
return false;
}
char * select_sql = "SELECT id, group_id, multi_player, valid_always, user_name,password, udescription,valid_begin_time,valid_end_time FROM `user_account`WHERE user_name = ?";
char * select_sql = "SELECT id, group_id, multi_player, valid_always, user_name,password, udescription,valid_begin_time,valid_end_time FROM user_account WHERE user_name = ?";
ret_sql = select_datebase_by_number(20, hdbc, "user_account", select_sql, 1, 0, num_sql, 1,
DB_DATA_STRING_TYPE, strlen(username)+1, username);
@ -384,6 +455,7 @@ void user_auth_login(char* username, char* password, USER_AUTH_RET *auth_result)
unsigned short user_id, group_id;
int init_queue_ret; //初始化循环列表的结果
int user_valid; //数据库中的数据
int user_multi;
int config_lock_time = 0; //锁定后的锁定时间,锁定后
int config_fail_num = 0; //规定时间内允许失败的次数,锁定次数,锁定前
int config_fail_time = 0; //规定时间,失败的时间范围,锁定前
@ -412,15 +484,18 @@ void user_auth_login(char* username, char* password, USER_AUTH_RET *auth_result)
}
//2、数据库查询配置数据
/*if (false)
int config_data[3] = {0,0,0};
bool ret_getconfig = get_config_data(auth_hdbc, config_data);
if (!ret_getconfig)
{
auth_result->ret = AUTH_FAIL_LACKINFO;
return auth_result;
}*/
return;
}
config_lock_time = 2;
config_fail_num = 5;
config_fail_time = 40;
config_lock_time = config_data[0];
config_fail_num = config_data[1];
config_fail_time = config_data[2];
/* 校验上述的三个参数都要大于0 */
//3、根据用户名查询用户信息-用户id和用户组id
@ -507,7 +582,21 @@ void user_auth_login(char* username, char* password, USER_AUTH_RET *auth_result)
}
}
//7、判断在线用户是否到最大值
//7、判断是否允许多用户登陆
user_multi = AUTH_MULTI_GET(user_info.multi_valid);
if(1 == user_multi)
{
if(1 == AUTH_RECORD.online_num)
{
auth_result->ret = AUTH_FAIL_MULTI;
/* 认证失败处理 */
auth_fail_operate(user_id, login_time, config_fail_time);
return;
}
}
//8、判断在线用户是否到最大值(100)
if (AUTH_USER_INDEX_MAX - 2 <= AUTH_RECORD.online_num)
{
auth_result->ret = AUTH_FAIL_OVER;
@ -517,7 +606,7 @@ void user_auth_login(char* username, char* password, USER_AUTH_RET *auth_result)
return;
}
//8、匹配密码
//9、匹配密码
if (0 != strcmp(password, user_info.passwd))
{
auth_result->ret = AUTH_FAIL_PASSWD;
@ -527,7 +616,7 @@ void user_auth_login(char* username, char* password, USER_AUTH_RET *auth_result)
return;
}
//9、认证成功处理
//10、认证成功处理
AUTH_RECORD.group_id = group_id; //更新用户组id
empty_fail_time_queue(user_id);
AUTH_RECORD.online_num++;