narsil/Tanshu.Accounts.Repository/GroupBI.cs
2014-10-16 16:45:03 +05:30

48 lines
1.4 KiB
C#

using System;
using System.Linq;
using Tanshu.Accounts.Entities.Auth;
using System.Collections.Generic;
namespace Tanshu.Accounts.Repository
{
public class GroupBI : UnitOfWork<Group>
{
public new IList<Group> List()
{
return _session.QueryOver<Group>()
.OrderBy(x => x.Name).Asc
.List();
}
public IList<Role> RoleList()
{
return _session.QueryOver<Role>()
.OrderBy(x => x.Name).Asc
.List();
}
public void Update(Group group, IEnumerable<Role> selectedRoles)
{
var roles = new Dictionary<Role, bool>();
foreach (var item in RoleList())
{
roles.Add(item, selectedRoles.Any(x => x.RoleID == item.RoleID));
}
foreach (var item in roles)
{
var id = item.Key.RoleID;
var ug = group.RoleGroups.SingleOrDefault(x => x.Role.RoleID == id);
if (item.Value && ug == null)
{
var rg = new RoleGroup() { Group = group, Role = item.Key };
_session.Save(rg);
}
else if (!item.Value && ug != null)
{
group.RoleGroups.Remove(ug);
_session.Delete(ug);
}
}
}
}
}