narsil/Tanshu.Accounts.PointOfSale/User Management/AssignRoles.cs
2010-03-02 23:26:21 +05:30

121 lines
4.0 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.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)
{
string[] roles = new MembershipBI().GetAllRoles();
lstRoles.DataSource = roles;
// lstRoles.DataBind();
string[] userRoles = new string[0];
lstUserRoles.DataSource = userRoles;
// lstUserRoles.DataBind();
btnAddAll.Enabled = false;
btnAddSelected.Enabled = false;
btnRemoveAll.Enabled = false;
btnRemoveSelected.Enabled = false;
}
else
{
string[] roles = new MembershipBI().GetAllRoles();
string[] userRoles = new MembershipBI().GetRolesForUser(cmbUsers.Text.Trim());
roles = roles.Where(r => !userRoles.Contains(r)).ToArray();
lstRoles.DataSource = roles;
// lstRoles.DataBind();
lstUserRoles.DataSource = userRoles;
// lstUserRoles.DataBind();
btnAddAll.Enabled = true;
btnAddSelected.Enabled = true;
btnRemoveAll.Enabled = true;
btnRemoveSelected.Enabled = true;
}
}
private void btnAddSelected_Click(object sender, EventArgs e)
{
if (lstRoles.SelectedItem != null)
{
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)
{
string userName = Thread.CurrentPrincipal.Identity.Name;
AccountsPrincipal principal = AccountsPrincipal.CreateAccountsPrincipal(new MembershipBI().GetRolesForUser(userName), new MembershipBI().GetUserFromName(userName));
// bind the generic principal to the thread
Thread.CurrentPrincipal = principal;
}
}
}