narsil/Tanshu.Accounts.BI/MembershipBI.cs
2010-03-02 23:26:21 +05:30

118 lines
4.2 KiB
C#

using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Tanshu.Accounts.Contracts;
using Tanshu.Accounts.DAOFactory;
using Tanshu.Data.DAO;
namespace Tanshu.Accounts.BI
{
public class MembershipBI : IMembershipBI
{
public bool ValidateUser(string name, string password)
{
GetFactory factory = GetFactory.GetDAOFactory(Database.GetFactoryType);
using (IConnectionDAO connection = factory.Connection)
{
using (IMembershipDAO dao = factory.GetMembershipDAO(connection))
{
return dao.ValidateUser(name, password);
}
}
}
public void AddUsersToRoles(string[] usernames, string[] roleNames)
{
GetFactory factory = GetFactory.GetDAOFactory(Database.GetFactoryType);
using (IConnectionDAO connection = factory.Connection)
{
using (IMembershipDAO dao = factory.GetMembershipDAO(connection))
{
dao.AddUsersToRoles(usernames, roleNames);
}
}
}
public string[] GetAllRoles()
{
GetFactory factory = GetFactory.GetDAOFactory(Database.GetFactoryType);
using (IConnectionDAO connection = factory.Connection)
{
using (IMembershipDAO dao = factory.GetMembershipDAO(connection))
{
return dao.GetAllRoles();
}
}
}
public string[] GetRolesForUser(string username)
{
GetFactory factory = GetFactory.GetDAOFactory(Database.GetFactoryType);
using (IConnectionDAO connection = factory.Connection)
{
using (IMembershipDAO dao = factory.GetMembershipDAO(connection))
{
return dao.GetRolesForUser(username);
}
}
}
public bool IsUserInRole(string username, string roleName)
{
GetFactory factory = GetFactory.GetDAOFactory(Database.GetFactoryType);
using (IConnectionDAO connection = factory.Connection)
{
using (IMembershipDAO dao = factory.GetMembershipDAO(connection))
{
if (!dao.RoleExists(roleName))
throw new Exception(string.Format("Role {0} does not exist in the database", roleName));
return dao.IsUserInRole(username, roleName);
}
}
}
public bool IsUserInRole(Guid userID, string roleName)
{
GetFactory factory = GetFactory.GetDAOFactory(Database.GetFactoryType);
using (IConnectionDAO connection = factory.Connection)
{
using (IMembershipDAO dao = factory.GetMembershipDAO(connection))
{
if (!dao.RoleExists(roleName))
throw new Exception(string.Format("Role {0} does not exist in the database", roleName));
return dao.IsUserInRole(userID, roleName);
}
}
}
public void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
{
if ((roleNames.Length > 0) || (usernames.Length > 0))
{
GetFactory factory = GetFactory.GetDAOFactory(Database.GetFactoryType);
using (IConnectionDAO connection = factory.Connection)
{
using (IMembershipDAO dao = factory.GetMembershipDAO(connection))
{
dao.RemoveUsersFromRoles(usernames, roleNames);
}
}
}
}
public UserBO GetUserFromName(string name)
{
GetFactory factory = GetFactory.GetDAOFactory(Database.GetFactoryType);
using (IConnectionDAO connection = factory.Connection)
{
using (IMembershipDAO dao = factory.GetMembershipDAO(connection))
{
return dao.GetUserFromName(name);
}
}
}
}
}