secgateway/Product/user/user_manager/user.c

107 lines
2.6 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()
{
const int INIT_FAIL = 1;
const int INIT_SUCCESS = 0;
/* 初始化用户的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;
}
/*添加元素-新增用户*/
USERADD* add_user(char* uname, char* gname, USERADD* uaddres)
{
const int ADD_FAIL_NOGROUP = 1;
const int ADD_FAIL_NAMELEN = 2;
const int ADD_FAIL_NAMESPE = 3;
const int ADD_FAIL_NAMEDUP = 4;
const int ADD_FAIL_USERFULL = 5;
const int ADD_SUCCESS = 0;
const char DEFAULT_PWD[8] = "123456";
if (NULL == uaddres)
{
return NULL;
}
uaddres->userID = INVALID_INDEX;
/* 校验用户名长度 */
if (NULL == uname || (UNAMESIZE) < strlen(uname) || 0 >= strlen(uname))
{
uaddres->result = ADD_FAIL_NAMELEN;
return uaddres;
}
/* 校验用户名中不含特殊字符 */
if (SPECHAR(uname))
{
uaddres->result = ADD_FAIL_NAMESPE;
return uaddres;
}
/* 根据用户组名查询用户组ID */
unsigned short GID_temp = get_groupid_by_name(gname);
if (CHECKOUTARG(gname) || INVALID_INDEX == GID_temp)
{
uaddres->result = ADD_FAIL_NOGROUP;
return uaddres;
}
/* 校验重名 */
for (int i = 0; i < USER_INDEX_MAX; i++)
{
if (0 == strcmp(uname, g_user_table[i].uname))
{
uaddres->result = ADD_FAIL_NAMEDUP;
return uaddres;
}
}
/* 生成用户ID判断用户是否满 */
unsigned short ID = alloc_index(&g_user_index_head);
if (INVALID_INDEX == ID)
{
uaddres->result = ADD_FAIL_USERFULL;
return uaddres;
}
/* 存内存 */
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, DEFAULT_PWD);
/* 连接数据库存user表 */
/* INSERT INTO `user` SET id = , group_id = , user_name = "", password = "", multi_player = , valid_always = */
uaddres->result = ADD_SUCCESS;
uaddres->userID = ID;
return uaddres;
}