narsil/Tanshu.Accounts.PointOfSale/User Management/AssignRoles.cs
2011-12-05 14:53:02 +05:30

107 lines
3.6 KiB
C#

using System;
using System.Linq;
using System.Windows.Forms;
using System.Threading;
using Tanshu.Accounts.BI;
namespace Tanshu.Accounts.PointOfSale
{
public partial class AssignRoles : Form
{
public AssignRoles()
{
InitializeComponent();
}
private void AssignRoles_Load(object sender, EventArgs e)
{
FillUsers();
}
private void FillUsers()
{
cmbUsers.DisplayMember = "Name";
cmbUsers.ValueMember = "UserID";
cmbUsers.DataSource = new UserBI().GetUsers();
}
private void cmbUsers_SelectedIndexChanged(object sender, EventArgs e)
{
RefreshRoles();
}
private void RefreshRoles()
{
if (cmbUsers.SelectedValue == null)
{
var roles = new MembershipBI().GetAllRoles();
lstRoles.DataSource = roles;
var userRoles = new string[0];
lstUserRoles.DataSource = userRoles;
btnAddAll.Enabled = false;
btnAddSelected.Enabled = false;
btnRemoveAll.Enabled = false;
btnRemoveSelected.Enabled = false;
}
else
{
var roles = new MembershipBI().GetAllRoles();
var userRoles = new MembershipBI().GetRolesForUser(cmbUsers.Text.Trim());
roles = roles.Where(r => !userRoles.Contains(r)).ToArray();
lstRoles.DataSource = roles;
lstUserRoles.DataSource = userRoles;
btnAddAll.Enabled = true;
btnAddSelected.Enabled = true;
btnRemoveAll.Enabled = true;
btnRemoveSelected.Enabled = true;
}
}
private void btnAddSelected_Click(object sender, EventArgs e)
{
if (lstRoles.SelectedItem == null)
return;
new MembershipBI().AddUsersToRoles(new string[] { cmbUsers.Text.Trim() }, new string[] { lstRoles.SelectedItem.ToString() });
RefreshRoles();
}
private void btnRemoveSelected_Click(object sender, EventArgs e)
{
if (lstUserRoles.SelectedItem != null)
{
new MembershipBI().RemoveUsersFromRoles(new string[] { cmbUsers.Text.Trim() }, new string[] { lstUserRoles.SelectedItem.ToString() });
RefreshRoles();
}
}
private void btnAddAll_Click(object sender, EventArgs e)
{
for (int i = 0; i <= lstRoles.Items.Count - 1; i++)
{
new MembershipBI().AddUsersToRoles(new string[] { cmbUsers.Text.Trim() }, new string[] { lstRoles.Items[i].ToString() });
}
RefreshRoles();
}
private void btnRemoveAll_Click(object sender, EventArgs e)
{
for (int i = 0; i <= lstUserRoles.Items.Count - 1; i++)
{
new MembershipBI().RemoveUsersFromRoles(new string[] { cmbUsers.Text.Trim() }, new string[] { lstUserRoles.Items[i].ToString() });
}
RefreshRoles();
}
private void AssignRoles_FormClosing(object sender, FormClosingEventArgs e)
{
var userName = Thread.CurrentPrincipal.Identity.Name;
var principal = AccountsPrincipal.CreateAccountsPrincipal(new MembershipBI().GetRolesForUser(userName), new MembershipBI().GetUserFromName(userName));
// bind the generic principal to the thread
Thread.CurrentPrincipal = principal;
}
}
}