secgateway/Product/user/user_manager/user.c

102 lines
2.3 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "../../common/common_user.h"
#include "array_index.h"
#include "user_group.h"
#include "user.h"
#define MULTI_MASK 0x0002
#define VALID_MASK 0x0001
#define MULTI_GET(element) ((element) >> 1)
#define MULTI_SET(element, value) (((element) & VALID_MASK) | (((value) << 1) & MULTI_MASK))
#define VALID_GET(element) ((element) & VALID_MASK)
#define VALID_SET(element, value) (((element) & MULTI_MASK) | ((value) & VALID_MASK))
extern ARRAY g_user_index_head;
extern USERGROUP g_group_table[GROUP_INDEX_MAX];
USERACCOUNT g_user_table[USER_INDEX_MAX];
/*初始化参数*/
int init_user()
{
/* 初始化用户的index */
int init_result = init_array(&g_user_index_head, USER_INDEX_MAX);
if (INIT_FAIL == init_result)
{
return INIT_FAIL;
}
memset(g_user_table, 0, sizeof(g_user_table));
return INIT_SUCCESS;
}
/*添加元素-新增用户*/
void usermanager_add_user(char* uname, char* gname, USERADD* uaddres)
{
if (NULL == uaddres)
{
return;
}
uaddres->userID = INVALID_INDEX;
/* 校验用户名长度 */
if (NULL == uname || (UNAMESIZE) < strlen(uname) || 0 >= strlen(uname))
{
uaddres->result = ADD_FAIL_NAMELEN;
return;
}
/* 校验用户名中不含特殊字符 */
if (SPECHAR(uname))
{
uaddres->result = ADD_FAIL_NAMESPE;
return;
}
/* 根据用户组名查询用户组ID */
if(NULL == gname)
{
uaddres->result = ADD_FAIL_NOGROUP;
return;
}
unsigned short GID_temp = get_groupid_by_name(gname);
if(INVALID_INDEX == GID_temp)
{
uaddres->result = ADD_FAIL_NOGROUP;
return;
}
/* 校验重名 */
for (int i = 0; i < USER_INDEX_MAX; i++)
{
if (0 == strcmp(uname, g_user_table[i].uname))
{
uaddres->result = ADD_FAIL_NAMEDUP;
return;
}
}
/* 生成用户ID判断用户是否满 */
unsigned short ID = alloc_index(&g_user_index_head);
if (INVALID_INDEX == ID)
{
uaddres->result = ADD_FAIL_USERFULL;
return;
}
/* 存内存 */
g_user_table[ID].ID = ID;
g_user_table[ID].GID = GID_temp;
strcpy(g_user_table[ID].uname, uname);
strcpy(g_user_table[ID].passwd, "123456");
/* 连接数据库存user表 */
/* INSERT INTO `user` SET id = , group_id = , user_name = "", password = "", multi_player = , valid_always = */
uaddres->result = ADD_SUCCESS;
uaddres->userID = ID;
return;
}