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

96 lines
2.7 KiB
C#
Raw Normal View History

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 AssignUserGroups : Form
{
public AssignUserGroups()
{
InitializeComponent();
}
private void AssignUserGroups_Load(object sender, EventArgs e)
{
FillUsers();
}
private void FillUsers()
{
cmbUser.DisplayMember = "Name";
cmbUser.ValueMember = "UserID";
using (var bi = new UserBI())
cmbUser.DataSource = bi.List();
}
private void cmbUsers_SelectedIndexChanged(object sender, EventArgs e)
{
if (cmbUser.SelectedValue == null)
GetGroups(null);
else
GetGroups((Guid)cmbUser.SelectedValue);
}
private void GetGroups(Guid? userID)
{
using (var bi = new UserBI())
{
var groups = bi.GroupList();
clbGroups.DataSource = groups;
for (int i = 0; i < clbGroups.Items.Count; i++)
{
clbGroups.SetItemChecked(i, false);
}
if (userID.HasValue)
{
var user = bi.Get(x => x.UserID == userID.Value);
for (int i = 0; i < clbGroups.Items.Count; i++)
{
var item = (Group)clbGroups.Items[i];
if (item.UserGroups.Any(x => x.User == user))
{
clbGroups.SetItemChecked(i, true);
}
}
}
}
}
private void btnOk_Click(object sender, EventArgs e)
{
if (cmbUser.SelectedValue != null)
Save((Guid)cmbUser.SelectedValue);
}
private void Save(Guid userID)
{
using (var bi = new UserBI())
{
var user = bi.Get(x => x.UserID == userID);
bi.Update(user, clbGroups.CheckedItems.OfType<Group>());
bi.SaveChanges();
}
MessageBox.Show("Update / Save Successful");
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void clbGroups_Format(object sender, ListControlConvertEventArgs e)
{
e.Value = ((Group)e.ListItem).Name;
}
}
}