2011-01-30 07:14:05 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
//using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Data.SqlClient;
|
|
|
|
|
using Tanshu.Accounts.Contracts;
|
|
|
|
|
using Tanshu.Data.DAO;
|
|
|
|
|
using Tanshu.Accounts.SqlDAO;
|
|
|
|
|
using Tanshu.Accounts.Entities;
|
|
|
|
|
using Tanshu.Accounts.Entities.Auth;
|
|
|
|
|
using NHibernate.Criterion;
|
|
|
|
|
using NHibernate.Linq;
|
|
|
|
|
using NHibernate;
|
|
|
|
|
|
|
|
|
|
namespace Tanshu.Accounts.Repository
|
|
|
|
|
{
|
2011-02-09 12:03:22 +00:00
|
|
|
|
public static class MembershipBI
|
2011-01-30 07:14:05 +00:00
|
|
|
|
{
|
2011-02-09 12:03:22 +00:00
|
|
|
|
|
|
|
|
|
//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)
|
2011-01-30 07:14:05 +00:00
|
|
|
|
{
|
2011-02-09 12:03:22 +00:00
|
|
|
|
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;";
|
2011-01-30 07:14:05 +00:00
|
|
|
|
using (var session = SessionManager.Session)
|
|
|
|
|
{
|
2011-02-09 12:03:22 +00:00
|
|
|
|
return session
|
|
|
|
|
.CreateSQLQuery(query)
|
|
|
|
|
.AddScalar("Role_Count", NHibernateUtil.Int32)
|
|
|
|
|
.SetInt32("UserID", userID)
|
|
|
|
|
.SetString("Role", roleName)
|
|
|
|
|
.UniqueResult<int>() > 0;
|
2011-01-30 07:14:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2011-02-09 12:03:22 +00:00
|
|
|
|
#region UserGroup
|
|
|
|
|
public static IList<Group> GetGroups()
|
2011-01-30 07:14:05 +00:00
|
|
|
|
{
|
|
|
|
|
using (var session = SessionManager.Session)
|
|
|
|
|
{
|
2011-02-09 12:03:22 +00:00
|
|
|
|
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);
|
2011-01-30 07:14:05 +00:00
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-02-09 12:03:22 +00:00
|
|
|
|
public static IList<Group> GetGroupsNotOfUser(int userID)
|
2011-01-30 07:14:05 +00:00
|
|
|
|
{
|
|
|
|
|
using (var session = SessionManager.Session)
|
|
|
|
|
{
|
2011-02-09 12:03:22 +00:00
|
|
|
|
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)
|
2011-01-30 07:14:05 +00:00
|
|
|
|
{
|
2011-02-09 12:03:22 +00:00
|
|
|
|
var user = session.Get<User>(userID);
|
|
|
|
|
var group = session.Get<Group>(groupID);
|
|
|
|
|
session.Save(new UserGroup() { User = user, Group = group });
|
2011-01-30 07:14:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-02-09 12:03:22 +00:00
|
|
|
|
public static void RemoveUserFromGroup(int userID, int groupID)
|
2011-01-30 07:14:05 +00:00
|
|
|
|
{
|
|
|
|
|
using (var session = SessionManager.Session)
|
|
|
|
|
{
|
2011-02-09 12:03:22 +00:00
|
|
|
|
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();
|
2011-01-30 07:14:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2011-02-09 12:03:22 +00:00
|
|
|
|
#endregion
|
|
|
|
|
#region RoleGroup
|
|
|
|
|
public static IList<Role> GetRoles()
|
2011-01-30 07:14:05 +00:00
|
|
|
|
{
|
|
|
|
|
using (var session = SessionManager.Session)
|
|
|
|
|
{
|
2011-02-09 12:03:22 +00:00
|
|
|
|
return session.CreateCriteria<Role>()
|
|
|
|
|
.List<Role>();
|
2011-01-30 07:14:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2011-02-09 12:03:22 +00:00
|
|
|
|
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)
|
2011-01-30 07:14:05 +00:00
|
|
|
|
{
|
|
|
|
|
using (var session = SessionManager.Session)
|
|
|
|
|
{
|
2011-02-09 12:03:22 +00:00
|
|
|
|
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();
|
2011-01-30 07:14:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2011-02-09 12:03:22 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
2011-01-30 07:14:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|