narsil/Tanshu.Accounts.PointOfSale/User Management/AssignRoleGroups.cs

120 lines
3.6 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Tanshu.Accounts.Helpers;
using System.Threading;
using Tanshu.Accounts.Repository;
using Tanshu.Accounts.Entities.Auth;
namespace Tanshu.Accounts.PointOfSale
{
public partial class AssignGroupRoles : Form
{
public AssignGroupRoles()
{
InitializeComponent();
}
private void AssignGroupRoles_Load(object sender, EventArgs e)
{
FillUsers();
}
private void FillUsers()
{
cmbGroup.DisplayMember = "Name";
cmbGroup.ValueMember = "GroupID";
cmbGroup.DataSource = MembershipBI.GetGroups();
}
private void cmbUsers_SelectedIndexChanged(object sender, EventArgs e)
{
GetLists();
}
private void GetLists()
{
IList<Role> roles;
IList<Role> roleGroups;
if (cmbGroup.SelectedValue == null)
{
roles = MembershipBI.GetRoles();
roleGroups = new List<Role>();
}
else
{
int groupID = (int)cmbGroup.SelectedValue;
roles = MembershipBI.GetRolesNotOfGroup(groupID);
roleGroups = MembershipBI.GetRolesOfGroup(groupID);
}
RefreshRoles(roles, roleGroups);
}
private void RefreshRoles(IList<Role> roles, IList<Role> roleGroups)
{
lstRoles.DisplayMember = "Name";
lstRoles.ValueMember = "RoleID";
lstRoles.DataSource = roles;
lstGroupRoles.DisplayMember = "Name";
lstGroupRoles.ValueMember = "RoleID";
lstGroupRoles.DataSource = roleGroups;
btnAddAll.Enabled = btnAddSelected.Enabled = roles.Count > 0;
btnRemoveAll.Enabled = btnRemoveSelected.Enabled = roleGroups.Count > 0;
}
private void btnAddSelected_Click(object sender, EventArgs e)
{
if (lstRoles.SelectedItem != null)
{
int groupID = (int)cmbGroup.SelectedValue;
int roleID = (int)lstRoles.SelectedValue;
MembershipBI.AddRoleToGroup(roleID, groupID);
GetLists();
}
}
private void btnRemoveSelected_Click(object sender, EventArgs e)
{
if (lstGroupRoles.SelectedItem != null)
{
int groupID = (int)cmbGroup.SelectedValue;
int roleID = (int)lstGroupRoles.SelectedValue;
MembershipBI.RemoveRoleFromGroup(roleID, groupID);
GetLists();
}
}
private void btnAddAll_Click(object sender, EventArgs e)
{
int groupID = (int)cmbGroup.SelectedValue;
int roleID;
for (int i = 0; i <= lstRoles.Items.Count - 1; i++)
{
roleID = ((Role)lstRoles.Items[i]).RoleID;
MembershipBI.AddRoleToGroup(roleID, groupID);
}
GetLists();
}
private void btnRemoveAll_Click(object sender, EventArgs e)
{
int groupID = (int)cmbGroup.SelectedValue;
int roleID;
for (int i = 0; i <= lstGroupRoles.Items.Count - 1; i++)
{
roleID = ((Role)lstGroupRoles.Items[i]).RoleID;
MembershipBI.RemoveRoleFromGroup(roleID, groupID);
}
GetLists();
}
}
}