2011-01-30 07:14:05 +00:00
|
|
|
|
using System;
|
2014-10-16 11:11:55 +00:00
|
|
|
|
using System.Linq;
|
2011-01-30 07:14:05 +00:00
|
|
|
|
using System.Collections.Generic;
|
2011-06-23 12:47:48 +00:00
|
|
|
|
using NHibernate;
|
2011-01-30 07:14:05 +00:00
|
|
|
|
using Tanshu.Accounts.Entities.Auth;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace Tanshu.Accounts.Repository
|
|
|
|
|
{
|
2014-10-12 09:41:45 +00:00
|
|
|
|
public class UserBI : UnitOfWork<User>
|
2011-01-30 07:14:05 +00:00
|
|
|
|
{
|
2014-11-10 07:24:48 +00:00
|
|
|
|
public new IList<User> List()
|
|
|
|
|
{
|
|
|
|
|
return _session.QueryOver<User>()
|
|
|
|
|
.OrderBy(x => x.Name).Asc
|
|
|
|
|
.List<User>();
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-16 11:11:55 +00:00
|
|
|
|
public IList<Group> GroupList()
|
|
|
|
|
{
|
|
|
|
|
return _session.QueryOver<Group>()
|
|
|
|
|
.OrderBy(x => x.Name).Asc
|
|
|
|
|
.List();
|
|
|
|
|
}
|
2011-06-23 12:47:48 +00:00
|
|
|
|
public IList<User> GetFilteredUsers(Dictionary<string, string> filter)
|
2011-01-30 07:14:05 +00:00
|
|
|
|
{
|
2014-10-12 09:41:45 +00:00
|
|
|
|
return _session.QueryOver<User>()
|
2011-06-23 12:47:48 +00:00
|
|
|
|
.WhereRestrictionOn(x => x.Name).IsLike(string.Format("%{0}%", filter["Name"]))
|
|
|
|
|
.List();
|
2011-01-30 07:14:05 +00:00
|
|
|
|
}
|
2014-11-02 08:03:31 +00:00
|
|
|
|
public void ChangePassword(User user, string password)
|
2011-01-30 07:14:05 +00:00
|
|
|
|
{
|
2014-11-02 08:03:31 +00:00
|
|
|
|
user.Password = Tanshu.Common.Md5.Hash(password, "v2");
|
|
|
|
|
_session.Update(user);
|
2011-01-30 07:14:05 +00:00
|
|
|
|
}
|
2011-06-23 12:47:48 +00:00
|
|
|
|
public User ValidateUser(string name, string password)
|
2011-01-30 07:14:05 +00:00
|
|
|
|
{
|
2014-11-02 08:03:31 +00:00
|
|
|
|
password = Tanshu.Common.Md5.Hash(password, "v2");
|
2011-06-23 12:47:48 +00:00
|
|
|
|
return Get(x => x.Name == name && x.Password == password);
|
2011-01-30 07:14:05 +00:00
|
|
|
|
}
|
2011-06-23 12:47:48 +00:00
|
|
|
|
public User MsrValidateUser(string msrString)
|
2011-01-30 07:14:05 +00:00
|
|
|
|
{
|
2011-06-23 12:47:48 +00:00
|
|
|
|
return Get(x => x.MsrString == msrString);
|
2011-01-30 07:14:05 +00:00
|
|
|
|
}
|
2011-06-23 12:47:48 +00:00
|
|
|
|
public IList<User> ListActive(DateTime startDate, DateTime finishDate)
|
2011-01-30 07:14:05 +00:00
|
|
|
|
{
|
2011-06-29 20:27:07 +00:00
|
|
|
|
const string query = @"
|
2011-03-11 18:49:48 +00:00
|
|
|
|
select distinct(u) from Voucher v
|
|
|
|
|
inner join v.User u
|
|
|
|
|
where v.Date >= :startDate and v.Date <= :finishDate
|
|
|
|
|
order by u.Name";
|
2014-10-12 09:41:45 +00:00
|
|
|
|
return _session.CreateQuery(query)
|
2011-06-29 20:27:07 +00:00
|
|
|
|
.SetParameter("startDate", startDate)
|
|
|
|
|
.SetParameter("finishDate", finishDate)
|
|
|
|
|
.List<User>();
|
2011-03-11 18:49:48 +00:00
|
|
|
|
}
|
2014-10-16 11:11:55 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-11-06 10:39:11 +00:00
|
|
|
|
public IList<Role> Roles(Guid userID)
|
2014-10-16 11:11:55 +00:00
|
|
|
|
{
|
2014-11-06 10:39:11 +00:00
|
|
|
|
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>();
|
2014-10-16 11:11:55 +00:00
|
|
|
|
}
|
2011-06-29 20:27:07 +00:00
|
|
|
|
}
|
2011-01-30 07:14:05 +00:00
|
|
|
|
}
|