narsil/Tanshu.Accounts.Repository/UserBI.cs

96 lines
3.0 KiB
C#
Raw Normal View History

using System;
using System.Linq;
using System.Collections.Generic;
using NHibernate;
using Tanshu.Accounts.Entities.Auth;
namespace Tanshu.Accounts.Repository
{
public class UserBI : UnitOfWork<User>
{
public new IList<User> List()
{
return _session.QueryOver<User>()
.OrderBy(x => x.Name).Asc
.List<User>();
}
public IList<Group> GroupList()
{
return _session.QueryOver<Group>()
.OrderBy(x => x.Name).Asc
.List();
}
public IList<User> GetFilteredUsers(Dictionary<string, string> filter)
{
return _session.QueryOver<User>()
.WhereRestrictionOn(x => x.Name).IsLike(string.Format("%{0}%", filter["Name"]))
.List();
}
public void ChangePassword(User user, string password)
{
user.Password = Tanshu.Common.Md5.Hash(password, "v2");
_session.Update(user);
}
public User ValidateUser(string name, string password)
{
password = Tanshu.Common.Md5.Hash(password, "v2");
return Get(x => x.Name == name && x.Password == password);
}
public User MsrValidateUser(string msrString)
{
return Get(x => x.MsrString == msrString);
}
public IList<User> ListActive(DateTime startDate, DateTime finishDate)
{
const string query = @"
select distinct(u) from Voucher v
inner join v.User u
where v.Date >= :startDate and v.Date <= :finishDate
order by u.Name";
return _session.CreateQuery(query)
.SetParameter("startDate", startDate)
.SetParameter("finishDate", finishDate)
.List<User>();
}
public void Update(User user, IEnumerable<Group> selectedGroups)
{
var roles = new Dictionary<Group, bool>();
foreach (var item in GroupList())
{
roles.Add(item, selectedGroups.Any(x => x.GroupID == item.GroupID));
}
foreach (var item in roles)
{
var id = item.Key.GroupID;
var ug = user.UserGroups.SingleOrDefault(x => x.Group.GroupID == id);
if (item.Value && ug == null)
{
var rg = new UserGroup() { User = user, Group = item.Key };
_session.Save(rg);
}
else if (!item.Value && ug != null)
{
user.UserGroups.Remove(ug);
_session.Delete(ug);
}
}
}
public IList<Role> Roles(Guid userID)
{
const string query = @"
select distinct(r) from UserGroup ug
inner join ug.User u
inner join ug.Group g
inner join g.RoleGroups rg
inner join rg.Role r
where u.UserID = :userID
order by r.Name";
return _session.CreateQuery(query)
.SetParameter("userID", userID)
.List<Role>();
}
}
}