narsil/Tanshu.Accounts.Repository/BusinessLayer/MembershipBI.cs
unknown 59909a5ee7 Updated NH, Code Refactoring, made all DB transactions atomic.
Must use the Repositories with Using or else bad things will happen.
2011-06-30 01:57:07 +05:30

202 lines
7.5 KiB
C#

using System.Collections.Generic;
using Tanshu.Accounts.Entities.Auth;
using NHibernate;
namespace Tanshu.Accounts.Repository
{
public static class MembershipBI
{
//public string[] GetAllRoles()
//{
// using (var session = SessionManager.Session)
// {
// var roleList = session.CreateCriteria<Role>().List<Role>();
// string[] list = new string[roleList.Count];
// for (int i = 0; i < list.Length; i++)
// {
// list[i] = roleList[i].Name;
// }
// return list;
// }
//}
//public string[] GetRolesForUser(string username)
//{
// using (var session = SessionManager.Session)
// {
// var user = session.CreateCriteria<User>()
// .Add(Restrictions.Eq("Name", username))
// .UniqueResult<User>();
// List<string> roles = new List<string>();
// foreach (var group in user.Groups)
// {
// foreach (var item in group.RoleGroups)
// {
// roles.Add(item.Role.Name);
// }
// }
// return roles.ToArray();
// }
//}
//public bool IsUserInRole(string username, string roleName)
//{
// using (var session = SessionManager.Session)
// {
// var user = session.CreateCriteria<User>()
// .Add(Restrictions.Eq("Name", username))
// .UniqueResult<User>();
// return IsUserInRole(user.UserID, roleName);
// }
//}
public static bool IsUserInRole(int userID, string roleName)
{
string query = @"
SELECT COUNT(*) AS Role_Count FROM
Auth_UserGroups ug INNER JOIN Auth_RoleGroups rg ON ug.GroupID = rg.GroupID
INNER JOIN Auth_Roles r ON rg.RoleID = r.RoleID
WHERE ug.UserID = :UserID AND r.Name = :Role;";
using (var session = SessionManager.Session)
{
return session
.CreateSQLQuery(query)
.AddScalar("Role_Count", NHibernateUtil.Int32)
.SetInt32("UserID", userID)
.SetString("Role", roleName)
.UniqueResult<int>() > 0;
}
}
#region UserGroup
public static IList<Group> GetGroups()
{
using (var session = SessionManager.Session)
{
return session.CreateCriteria<Group>()
.List<Group>();
}
}
public static IList<Group> GetGroupsOfUser(int userID)
{
using (var session = SessionManager.Session)
{
string query = "select ug.Group from UserGroup ug where ug.User.UserID = :userID";
var list = session.CreateQuery(query)
.SetParameter("userID", userID)
.List<Group>();
foreach (var item in list)
NHibernateUtil.Initialize(item);
return list;
}
}
public static IList<Group> GetGroupsNotOfUser(int userID)
{
using (var session = SessionManager.Session)
{
string query = "select g from Group g where g not in (select ug.Group from UserGroup ug where ug.User.UserID = :userID)";
var list = session.CreateQuery(query)
.SetParameter("userID", userID)
.List<Group>();
foreach (var item in list)
NHibernateUtil.Initialize(item);
return list;
}
}
public static void AddUserToGroup(int userID, int groupID)
{
using (var session = SessionManager.Session)
{
var userGroup = session.CreateQuery("select count(*) from UserGroup ug where ug.User.UserID = :userID and ug.Group.GroupID = :groupID")
.SetParameter("userID", userID)
.SetParameter("groupID", groupID)
.UniqueResult<long>();
if (userGroup == 0)
{
var user = session.Get<User>(userID);
var group = session.Get<Group>(groupID);
session.Save(new UserGroup() { User = user, Group = group });
}
}
}
public static void RemoveUserFromGroup(int userID, int groupID)
{
using (var session = SessionManager.Session)
{
string query = "delete UserGroup ug where ug.User.UserID = :userID and ug.Group.GroupID = :groupID";
session.CreateQuery(query)
.SetParameter("userID", userID)
.SetParameter("groupID", groupID)
.ExecuteUpdate();
}
}
#endregion
#region RoleGroup
public static IList<Role> GetRoles()
{
using (var session = SessionManager.Session)
{
return session.CreateCriteria<Role>()
.List<Role>();
}
}
public static IList<Role> GetRolesOfGroup(int groupID)
{
using (var session = SessionManager.Session)
{
string query = "select rg.Role from RoleGroup rg where rg.Group.GroupID = :groupID";
var list = session.CreateQuery(query)
.SetParameter("groupID", groupID)
.List<Role>();
foreach (var item in list)
NHibernateUtil.Initialize(item);
return list;
}
}
public static IList<Role> GetRolesNotOfGroup(int groupID)
{
using (var session = SessionManager.Session)
{
string query = "select r from Role r where r not in (select rg.Role from RoleGroup rg where rg.Group.GroupID = :groupID)";
var list = session.CreateQuery(query)
.SetParameter("groupID", groupID)
.List<Role>();
foreach (var item in list)
NHibernateUtil.Initialize(item);
return list;
}
}
public static void AddRoleToGroup(int roleID, int groupID)
{
using (var session = SessionManager.Session)
{
var roleGroup = session.CreateQuery("select count(*) from RoleGroup rg where rg.Role.RoleID = :roleID and rg.Group.GroupID = :groupID")
.SetParameter("roleID", roleID)
.SetParameter("groupID", groupID)
.UniqueResult<long>();
if (roleGroup == 0)
{
var role = session.Get<Role>(roleID);
var group = session.Get<Group>(groupID);
session.Save(new RoleGroup() { Role = role, Group = group });
}
}
}
public static void RemoveRoleFromGroup(int roleID, int groupID)
{
using (var session = SessionManager.Session)
{
string query = "delete RoleGroup rg where rg.Role.RoleID = :roleID and rg.Group.GroupID = :groupID";
session.CreateQuery(query)
.SetParameter("roleID", roleID)
.SetParameter("groupID", groupID)
.ExecuteUpdate();
}
}
#endregion
}
}