secgateway/Product/user/user_manager/user_group.c

174 lines
3.3 KiB
C
Raw Normal View History

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "array_index.h"
#include "user_group.h"
#include "../../common/common_user.h"
#define ADD_SUCCESS 0
#define ADD_FAIL_FULL 1
#define ADD_FAIL_LENGTH 2
#define ADD_FAIL_SPECHARS 3
#define ADD_FAIL_DUP 4
#define DEL_SUCCESS 0
#define DEL_FAIL_NOTEXIST 1
#define DEL_FAIL_STRTEGY 2
USERGROUP g_group_table[GROUP_INDEX_MAX];
/* 初始化参数 */
int InitUserGroup()
{
/* 初始化用户组的index */
int initResult = init_array(&g_group_index_head, GROUP_INDEX_MAX);
if(1 == initResult)
{
return 1;
}
memset(g_group_table, 0, sizeof(g_group_table));
return 0;
}
/* 添加元素 */
unsigned short AddGroup(USERGROUP* group, char* name, char* description)
{
/* 校验用户组名和描述的长度 */
if((GNAMESIZE-1) < strlen(name) || NULL == name || (GDESIZE - 1) < strlen(description))
{
return ADD_FAIL_LENGTH;
}
/* 校验特殊字符 */
if(SPECHAR(name))
{
return ADD_FAIL_SPECHARS;
}
/* 校验重名 */
for(int i = 0; i < GROUP_INDEX_MAX; i++)
{
if(strcmp(name, group[i].gname) == 0)
{
return ADD_FAIL_DUP;
}
}
/* 生成用户组ID */
unsigned short ID = alloc_index(&g_group_index_head);
if (0 == ID)
{
return ADD_FAIL_FULL;
}
group[ID].ID = ID;
strcpy(group[ID].gname, name);
strcpy(group[ID].gdescription, description);
return ADD_SUCCESS;
}
/* 查询用户组-按用户名 */
USERGROUP* findGroupByName(USERGROUP* group, char* gname, USERGROUP* groupout)
{
if (NULL == gname)
{
return NULL;
}
if ( 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))
{
groupout->ID = group[i].ID;
strcpy(groupout->gname, group[i].gname);
strcpy(groupout->gdescription, group[i].gdescription);
return groupout;
}
}
if( 0 == groupout->ID )
{
return NULL;
}
return groupout;
}
/* 获得用户组个数 */
unsigned short getGroupCount(USERGROUP* group)
{
int num = 0;
for (int i = 0; i < GROUP_INDEX_MAX; i++)
{
if(0 != group[i].ID)
{
num++;
}
}
return num;
}
/* 查询用户组列表 */
USERGROUP* showUserGroupList(USERGROUP* group, USERGROUP* grouplist)
{
int j = 0;
for(int i = 0; i < GROUP_INDEX_MAX; i++)
{
if(group[i].ID != 0)
{
grouplist[j].ID = group[i].ID;
strcpy(grouplist[j].gname, group[i].gname);
strcpy(grouplist[j].gdescription, group[i].gdescription);
j++;
}
}
return grouplist;
}
unsigned short getGroupIDByGName(USERGROUP* group, char* gname)
{
unsigned short GID_temp = 0;
for (int i = 0; i < GROUP_INDEX_MAX && GID_temp == 0; i++)
{
GID_temp = GETGROUPID(group[i].ID, gname, group[i].gname);
}
return GID_temp;
}
/*删除元素*/
unsigned short DelGroupByName(USERGROUP* group, char* gname)
{
if ( CHECKOUTARG(gname) )
{
return DEL_FAIL_NOTEXIST;
}
/* 判断被删除的用户组是否存在 */
unsigned short GID_temp = getGroupIDByGName(group, gname);
if ( 0 == GID_temp)
{
return DEL_FAIL_NOTEXIST;
}
/* 调用策略提供的接口,查询是否绑定策略*/
/* 根据用户组ID删除用户列表中对应的用户 */
/* 根据用户组ID删除用户组列表中对应的用户组 - 释放index、ID置0 */
free_index(&g_group_index_head, GID_temp);
group[GID_temp].ID = 0;
/* 强制用户下线 */
return DEL_SUCCESS;
}