narsil/Tanshu.Accounts.Repository/BusinessLayer/UserBI.cs
2011-03-12 17:12:02 +05:30

145 lines
4.6 KiB
C#

using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using Tanshu.Accounts.Contracts;
using System.Data.SqlClient;
using Tanshu.Data.DAO;
using Tanshu.Accounts.Entities;
using Tanshu.Accounts.SqlDAO;
using NHibernate.Criterion;
using Tanshu.Accounts.Entities.Auth;
namespace Tanshu.Accounts.Repository
{
public static class UserBI
{
public static void Insert(User user)
{
using (var session = SessionManager.Session)
{
session.Save(user);
}
}
public static void Update(User user)
{
using (var session = SessionManager.Session)
{
using (var trans = session.BeginTransaction())
{
session.Update(user);
trans.Commit();
}
}
}
public static void Delete(int userID)
{
using (var session = SessionManager.Session)
{
session.Delete(new User() { UserID = userID });
}
}
public static User GetUser(int userID)
{
using (var session = SessionManager.Session)
{
return session.Get<User>(userID);
}
}
public static User GetUserFromName(string name)
{
using (var session = SessionManager.Session)
{
return (User)session.CreateCriteria<User>()
.Add(Restrictions.Eq("Name", name))
.UniqueResult();
}
}
public static IList<User> GetUsers()
{
using (var session = SessionManager.Session)
{
return session.CreateCriteria<User>()
.List<User>();
}
}
public static IList<User> GetFilteredUsers(Dictionary<string, string> filter)
{
using (var session = SessionManager.Session)
{
return session.CreateCriteria<User>()
.Add(Restrictions.Like("Name", string.Format("%{0}%", filter["Name"])))
.List<User>();
}
}
public static bool ChangePassword(User userData, string newPassword)
{
using (var session = SessionManager.Session)
{
using (var trans = session.BeginTransaction())
{
var dbUser = session.CreateCriteria<User>()
.Add(Restrictions.Eq("Name", userData.Name))
.Add(Restrictions.Eq("Password", userData.Password))
.UniqueResult<User>();
if (dbUser == null)
return false;
dbUser.Password = newPassword;
session.Update(dbUser);
trans.Commit();
}
return true;
}
}
public static bool Exists(string userName)
{
using (var session = SessionManager.Session)
{
var count = session.CreateCriteria<User>()
.Add(Restrictions.Eq("Name", userName))
.SetProjection(Projections.Count("UserID")).UniqueResult();
return (int)count > 0;
}
}
public static User ValidateUser(string name, string password)
{
using (var session = SessionManager.Session)
{
return session.CreateCriteria<User>()
.Add(Restrictions.Eq("Name", name))
.Add(Restrictions.Eq("Password", password))
.UniqueResult<User>();
}
}
public static User MsrValidateUser(string msrString)
{
using (var session = SessionManager.Session)
{
var user = session.CreateCriteria<User>()
.Add(Restrictions.Eq("MsrString", msrString))
.UniqueResult<User>();
return user;
}
}
public static IList<User> ListActive(DateTime startDate, DateTime finishDate)
{
using (var session = SessionManager.Session)
{
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>();
}
}
}
}