diff --git a/Product/common/common_user.h b/Product/common/common_user.h index 67c3c26b1..448d22361 100644 --- a/Product/common/common_user.h +++ b/Product/common/common_user.h @@ -1,8 +1,9 @@ -#ifndef COMMOM_USER_H_ +#ifndef COMMON_USER_H_ #define COMMON_USER_H_ #define SPECHAR(element) (strpbrk((element), "~!@#$%^&*()_+{}|:\"<>?\\,./;\'[]-=`")) //校验特殊字符 #define GETGROUPID(ID, NAME1, NAME2) ((((ID) != 0) && (strcmp((NAME1), (NAME2)) == 0)) ? (ID) : 0) //根据用户名查询用户组ID -#define CHECKOUTARG(element) (((element) == NULL || (element) == "" || SPECHAR(element)) ? true : false) //校验参数 +#define CHECKOUTARG(element) ((NULL == (element) || "" == (element) || SPECHAR(element)) ? true : false) //校验参数 +#define INVALID_INDEX (0) #endif diff --git a/Product/user/user_manager/array_index.c b/Product/user/user_manager/array_index.c index da4103c6d..38ef64b02 100644 --- a/Product/user/user_manager/array_index.c +++ b/Product/user/user_manager/array_index.c @@ -1,7 +1,8 @@ #include #include #include -#include"array_index.h" +#include "array_index.h" +#include "../../common/common_user.h" ARRAY g_user_index_head = { 0 }; ARRAY g_group_index_head = { 0 }; @@ -18,7 +19,6 @@ ARRAY g_group_index_head = { 0 }; #define CUR_INDEX (head->cur) #define MAX_INDEX (head->size) -#define INVALID_INDEX (0) #define HI_ELEMENT_GET(element) ((element) >> INDEX_LEN) #define HI_ELEMENT_SET(element, value) (((element) & INDEX_LOW_MASK) | ((value) << INDEX_LEN)) #define LOW_ELEMENT_GET(element) ((element) & INDEX_LOW_MASK) diff --git a/Product/user/user_manager/array_index.h b/Product/user/user_manager/array_index.h index ea91daff8..e44363b70 100644 --- a/Product/user/user_manager/array_index.h +++ b/Product/user/user_manager/array_index.h @@ -23,7 +23,4 @@ unsigned short alloc_index_special(ARRAY* head, unsigned short index); /*指定释放*/ void free_index(ARRAY* head, unsigned short index); -ARRAY g_user_index_head; -ARRAY g_group_index_head; - #endif diff --git a/Product/user/user_manager/user_group.c b/Product/user/user_manager/user_group.c index 6937e36f9..1e485af02 100644 --- a/Product/user/user_manager/user_group.c +++ b/Product/user/user_manager/user_group.c @@ -5,6 +5,9 @@ #include "user_group.h" #include "../../common/common_user.h" +#define INIT_FAIL 1 +#define INIT_SUCCESS 0 + #define ADD_SUCCESS 0 #define ADD_FAIL_FULL 1 #define ADD_FAIL_LENGTH 2 @@ -15,7 +18,7 @@ #define DEL_FAIL_NOTEXIST 1 #define DEL_FAIL_STRTEGY 2 - +extern ARRAY g_group_index_head; USERGROUP g_group_table[GROUP_INDEX_MAX]; /* 初始化参数 */ @@ -23,19 +26,23 @@ int InitUserGroup() { /* 初始化用户组的index */ int initResult = init_array(&g_group_index_head, GROUP_INDEX_MAX); - if(1 == initResult) + if(INIT_FAIL == initResult) { - return 1; + return INIT_FAIL; } memset(g_group_table, 0, sizeof(g_group_table)); - return 0; + return INIT_SUCCESS; } /* 添加元素 */ -unsigned short AddGroup(USERGROUP* group, char* name, char* description) +unsigned short AddGroup(char* name, char* description) { /* 校验用户组名和描述的长度 */ - if((GNAMESIZE-1) < strlen(name) || NULL == name || (GDESIZE - 1) < strlen(description)) + if(NULL == description) + { + description = ""; + } + if(NULL ==name || GNAMESIZE < strlen(name) || GDESIZE < strlen(description)) { return ADD_FAIL_LENGTH; } @@ -49,7 +56,7 @@ unsigned short AddGroup(USERGROUP* group, char* name, char* description) /* 校验重名 */ for(int i = 0; i < GROUP_INDEX_MAX; i++) { - if(strcmp(name, group[i].gname) == 0) + if(strcmp(name, g_group_table[i].gname) == 0) { return ADD_FAIL_DUP; } @@ -57,93 +64,121 @@ unsigned short AddGroup(USERGROUP* group, char* name, char* description) /* 生成用户组ID */ unsigned short ID = alloc_index(&g_group_index_head); - if (0 == ID) + if (INVALID_INDEX == ID) { return ADD_FAIL_FULL; } - group[ID].ID = ID; - strcpy(group[ID].gname, name); - strcpy(group[ID].gdescription, description); + /* 添加数据到内存g_group_table */ + g_group_table[ID].ID = ID; + strcpy(g_group_table[ID].gname, name); + strcpy(g_group_table[ID].gdescription, description); + + /* 连接数据库,向user_group表中添加:用户组ID、用户组名和用户组描述 */ + /* INSERT INTO user_group SET id = "", gname = "", gdescription = "" */ return ADD_SUCCESS; } /* 查询用户组-按用户名 */ -USERGROUP* findGroupByName(USERGROUP* group, char* gname, USERGROUP* groupout) +USERGROUP* findGroupByName(char* gname, USERGROUP* groupout) { - if (NULL == gname) - { - return NULL; - } - if ( NULL == groupout) + if (NULL == gname || NULL == groupout) { return NULL; } + /* 内存中查询数据 */ groupout->ID = 0; for (int i = 0; i < GROUP_INDEX_MAX; i++) { - if ((strcmp(gname, group[i].gname) == 0) && (group[i].ID != 0)) + if ((strcmp(gname, g_group_table[i].gname) == 0) && (g_group_table[i].ID != 0)) { - groupout->ID = group[i].ID; - strcpy(groupout->gname, group[i].gname); - strcpy(groupout->gdescription, group[i].gdescription); + groupout->ID = g_group_table[i].ID; + strcpy(groupout->gname, g_group_table[i].gname); + strcpy(groupout->gdescription, g_group_table[i].gdescription); return groupout; } } - if( 0 == groupout->ID ) { return NULL; } + + /* 连接数据库,根据用户名查询表user_group */ + /* SELECT UG.id, UG.gname, UG.gdescription FROM user_group UG WHERE UG.gname = "" */ + return groupout; } /* 获得用户组个数 */ -unsigned short getGroupCount(USERGROUP* group) +unsigned short getGroupCount() { + /* 内存查询数据 */ int num = 0; for (int i = 0; i < GROUP_INDEX_MAX; i++) { - if(0 != group[i].ID) + if(0 != g_group_table[i].ID) { num++; } } + + /* 连接数据库,查询表user_group中用户组数量 */ + /* SELECT COUNT(1) FROM user_group; */ + return num; } /* 查询用户组列表 */ -USERGROUP* showUserGroupList(USERGROUP* group, USERGROUP* grouplist) +USERGROUP* showUserGroupList(USERGROUP* grouplist) { + if(NULL == grouplist) + { + return NULL; + } + + /* 内存查询数据 */ int j = 0; for(int i = 0; i < GROUP_INDEX_MAX; i++) { - if(group[i].ID != 0) + if(g_group_table[i].ID != 0) { - grouplist[j].ID = group[i].ID; - strcpy(grouplist[j].gname, group[i].gname); - strcpy(grouplist[j].gdescription, group[i].gdescription); + grouplist[j].ID = g_group_table[i].ID; + strcpy(grouplist[j].gname, g_group_table[i].gname); + strcpy(grouplist[j].gdescription, g_group_table[i].gdescription); j++; } } + + /* 连接数据库,查询表user_group列表 */ + /* SELECT UG.id, UG.gname, UG.gdescription FROM user_group UG */ + return grouplist; } -unsigned short getGroupIDByGName(USERGROUP* group, char* gname) +unsigned short getGroupIDByGName(char* gname) { + /* 内存查询数据 */ unsigned short GID_temp = 0; + if(NULL == gname) + { + return GID_temp; + } for (int i = 0; i < GROUP_INDEX_MAX && GID_temp == 0; i++) { - GID_temp = GETGROUPID(group[i].ID, gname, group[i].gname); + GID_temp = GETGROUPID(g_group_table[i].ID, gname, g_group_table[i].gname); } + + /* 连接数据库,根据用户组名查询user_grooup表中的id */ + /* SELECT UG.id FROM user_group UG WHERE UG.gname = "" */ + return GID_temp; } /*删除元素*/ -unsigned short DelGroupByName(USERGROUP* group, char* gname) +unsigned short DelGroupByName(char* gname) { if ( CHECKOUTARG(gname) ) { @@ -151,9 +186,9 @@ unsigned short DelGroupByName(USERGROUP* group, char* gname) } /* 判断被删除的用户组是否存在 */ - unsigned short GID_temp = getGroupIDByGName(group, gname); + unsigned short GID_temp = getGroupIDByGName(gname); - if ( 0 == GID_temp) + if (INVALID_INDEX == GID_temp) { return DEL_FAIL_NOTEXIST; } @@ -164,7 +199,9 @@ unsigned short DelGroupByName(USERGROUP* group, char* gname) /* 根据用户组ID删除用户组列表中对应的用户组 - 释放index、ID置0 */ free_index(&g_group_index_head, GID_temp); - group[GID_temp].ID = 0; + g_group_table[GID_temp].ID = 0; + + /* DELETE FROM user_group WHERE gname = "" */ /* 强制用户下线 */ diff --git a/Product/user/user_manager/user_group.h b/Product/user/user_manager/user_group.h index a074607ab..f91618719 100644 --- a/Product/user/user_manager/user_group.h +++ b/Product/user/user_manager/user_group.h @@ -6,33 +6,31 @@ typedef struct usergroup { - int ID; + unsigned short ID; char gname[GNAMESIZE]; char gdescription[GDESIZE]; }USERGROUP; -USERGROUP g_group_table[GROUP_INDEX_MAX]; - /* 初始化参数 */ int InitUserGroup(); /* 添加元素-新增用户组 */ -unsigned short AddGroup(USERGROUP* UG, char* UGNAME, char* UGDES); +unsigned short AddGroup(char* UGNAME, char* UGDES); /* 获得用户组个数 */ -unsigned short getGroupCount(USERGROUP* UG); +unsigned short getGroupCount(); /* 查询用户组列表 */ -USERGROUP* showUserGroupList(USERGROUP* UG, USERGROUP* UGLIST); +USERGROUP* showUserGroupList(USERGROUP* UGLIST); /* 根据用户组名查询用户组 */ -USERGROUP* findGroupByName(USERGROUP* UG, char* UGNAME, USERGROUP* UGRES); +USERGROUP* findGroupByName(char* UGNAME, USERGROUP* UGRES); /* 根据用户组名查询用户组ID */ -unsigned short getGroupIDByGName(USERGROUP* UG, char* UGNAME); +unsigned short getGroupIDByGName(char* UGNAME); /* 根据用户组名删除用户组 */ -unsigned short DelGroupByName(USERGROUP* UG, char* UGNAME); +unsigned short DelGroupByName(char* UGNAME); #endif