How to manage group ?

Estimated reading: 4 minutes 424 views

The guide begins by detailing the necessary namespaces to be included in your code. It then explains how to define the Visual Guard runtime, which is a crucial step to perform any operation on VGGroup.

The guide provides detailed instructions on how to create, update, and delete VGGroups. It also explains how to create group profile attributes and how to get and set group profile values.

The guide further provides information on how to retrieve all groups from the VGRepository based on the rights of the user. It also explains how to find groups by various parameters such as group name pattern or profile attribute.


Add following namespaces in your code

using Novalys.VisualGuard.Security;
using Novalys.VisualGuard.Security.Common;
using Novalys.VisualGuard.Security.CommonProfileAttribute;
using Novalys.VisualGuard.Security.Database;
using Novalys.VisualGuard.Security.Membership;
using Novalys.VisualGuard.Security.UserProfile;
using System; 
using System.Collections.Generic;

Define VisualGuard runtime

VGSecurityRuntime runtime = VGSecurityManager.Runtime;

How to Create/Update/Delete VGGroup

//Create/Update/Delete Group 
//create group 
VGGroup groupHR = runtime.Groups.CreateGroup("HR Department", "This group represents a HR Division", null); 

//create group under specific group 
VGGroup groupHRBranchA = runtime.Groups.CreateGroup("HR Department- Branch A", groupHR); 
VGGroup groupHRBranchB = runtime.Groups.CreateGroup("HR Department- Branch B", groupHR); 
VGGroup groupHRBranchC = runtime.Groups.CreateGroup("HR Department- Branch C", groupHR); 

//update group 
groupHR.Description = "HR Division"; groupHR.Data1 = "Data1 - You can store any important information here"; 

groupHR.Data2 = "Data2 - You can store any important information here";
groupHR.Data3 = "Data3 - You can store any important information here"; 
groupHR.Name = "HR Dept"; runtime.Groups.UpdateGroup(groupHR); 

//delete group 
runtime.Groups.DeleteGroup(groupHRBranchC);

Create group profile attributes

//Create group profile attributes
var attrBranchCode = runtime.Groups.CreateProfileAttribute("BranchCode",VGCommonProfileDataType.String, "BranchCode", "This attribute represents a branch code");
var attrBranchActive = runtime.Groups.CreateProfileAttribute("IsBranchActive", VGCommonProfileDataType.Boolean, "IsActive", "This attribute represents if branch is active");


var profileAttribteDef = (VGProfileAttributeDefinitionDropDownList) runtime.Groups.GetProfileAttributeDefinition(VGCommonProfileDataType.DropDownList);
profileAttribteDef.Items = new string[] { "Zone 1", "Zone 2", "Zone 3" };
profileAttribteDef.DefaultValue = "Zone 1";

var attrBranchZone = runtime.Groups.CreateProfileAttribute("BranchZone",VGCommonProfileDataType.DropDownList, "BranchZone", "This attribute represents if branch is active", true, false, false, true, 1, "Basic Information", profileAttribteDef);

Create group profile attributes

//Create group profile attributes
var attrBranchCode = runtime.Groups.CreateProfileAttribute("BranchCode",VGCommonProfileDataType.String, "BranchCode", "This attribute represents a branch code");
var attrBranchActive = runtime.Groups.CreateProfileAttribute("IsBranchActive", VGCommonProfileDataType.Boolean, "IsActive", "This attribute represents if branch is active");


var profileAttribteDef = (VGProfileAttributeDefinitionDropDownList) runtime.Groups.GetProfileAttributeDefinition(VGCommonProfileDataType.DropDownList);
profileAttribteDef.Items = new string[] { "Zone 1", "Zone 2", "Zone 3" };
profileAttribteDef.DefaultValue = "Zone 1";

var attrBranchZone = runtime.Groups.CreateProfileAttribute("BranchZone",VGCommonProfileDataType.DropDownList, "BranchZone", "This attribute represents if branch is active", true, false, false, true, 1, "Basic Information", profileAttribteDef);

Get group profile values.

string branchCode = groupHRBranchA.GetValue<string>("BranchCode");
bool isBranchActive = groupHRBranchA.GetValue<bool>("IsBranchActive");
string branchZone = groupHRBranchA.GetValue<string>("BranchZone");

Save/Update group profile values

//Save/Update group profile values
groupHRBranchA.SetValue<string>(attrBranchCode.Id, "Code_442");
groupHRBranchA.SetValue<bool>(attrBranchActive.Id, true);
groupHRBranchA.SetValue<string>(attrBranchZone.Id, "Zone 2");

runtime.Groups.UpdateGroup(groupHRBranchA);

//Save/Update group profile values
groupHRBranchB.SetValue<string>(attrBranchCode.Id, "Code_443");
groupHRBranchB.SetValue<bool>(attrBranchActive.Id, false);
groupHRBranchB.SetValue<string>(attrBranchZone.Id, "Zone 1");

runtime.Groups.UpdateGroup(groupHRBranchB);


Get all groups from VGRepository depending on rights of the user

 //Get all groups
var allGroups = runtime.Groups.GetAllGroup();

//Get all descendant groups of particular group
var childGroups =runtime.Groups.GetAllDescendantGroupsFromGroup(groupHR);

//Get all parent groups of particular group
var parentGroups = runtime.Groups.GetAllParentGroups(groupHRBranchA)

Find groups by various parameters

//find groups by groupname pattern
var findGroups = runtime.Groups.FindGroupsByName("HR Department");
findGroups = runtime.Groups.FindGroupsByName("%Department%");

//find groups by profile attribute
findGroups =  runtime.Groups.FindGroupsByProfileAttributeValue(attrBranchCode.Id, "Code_442");

findGroups = runtime.Groups.FindGroupsByProfileAttributeValue(attrBranchActive.Id, false);
findGroups = runtime.Groups.FindGroupsByProfileAttributeValue(attrBranchActive.Id, true);

findGroups = runtime.Groups.FindGroupsByProfileAttributeValue(attrBranchZone.Id, "Zone 1");
findGroups = runtime.Groups.FindGroupsByProfileAttributeValue(attrBranchZone.Id, "Zone 2");

Assign/Remove user to/from group

var userJsmith = runtime.Membership.GetUser("jsmith");

//assign user to group
runtime.Groups.AddUserToGroup(userJsmith, groupHRBranchA);

//remove user from group
runtime.Groups.RemoveUserFromGroup(userJsmith, groupHRBranchA);