narsil/Tanshu.Accounts.BI/MembershipBI.cs

118 lines
4.0 KiB
C#
Raw Normal View History

2010-03-02 17:56:21 +00:00
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)
{
2011-12-05 09:23:02 +00:00
var factory = GetFactory.GetDAOFactory(Database.GetFactoryType);
using (var connection = factory.Connection)
2010-03-02 17:56:21 +00:00
{
using (IMembershipDAO dao = factory.GetMembershipDAO(connection))
{
return dao.ValidateUser(name, password);
}
}
}
public void AddUsersToRoles(string[] usernames, string[] roleNames)
{
2011-12-05 09:23:02 +00:00
var factory = GetFactory.GetDAOFactory(Database.GetFactoryType);
using (var connection = factory.Connection)
2010-03-02 17:56:21 +00:00
{
using (IMembershipDAO dao = factory.GetMembershipDAO(connection))
{
dao.AddUsersToRoles(usernames, roleNames);
}
}
}
public string[] GetAllRoles()
{
2011-12-05 09:23:02 +00:00
var factory = GetFactory.GetDAOFactory(Database.GetFactoryType);
using (var connection = factory.Connection)
2010-03-02 17:56:21 +00:00
{
2011-12-05 09:23:02 +00:00
using (var dao = factory.GetMembershipDAO(connection))
2010-03-02 17:56:21 +00:00
{
return dao.GetAllRoles();
}
}
}
public string[] GetRolesForUser(string username)
{
2011-12-05 09:23:02 +00:00
var factory = GetFactory.GetDAOFactory(Database.GetFactoryType);
using (var connection = factory.Connection)
2010-03-02 17:56:21 +00:00
{
using (IMembershipDAO dao = factory.GetMembershipDAO(connection))
{
return dao.GetRolesForUser(username);
}
}
}
public bool IsUserInRole(string username, string roleName)
{
2011-12-05 09:23:02 +00:00
var factory = GetFactory.GetDAOFactory(Database.GetFactoryType);
using (var connection = factory.Connection)
2010-03-02 17:56:21 +00:00
{
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)
{
2011-12-05 09:23:02 +00:00
var factory = GetFactory.GetDAOFactory(Database.GetFactoryType);
using (var connection = factory.Connection)
2010-03-02 17:56:21 +00:00
{
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))
{
2011-12-05 09:23:02 +00:00
var factory = GetFactory.GetDAOFactory(Database.GetFactoryType);
using (var connection = factory.Connection)
2010-03-02 17:56:21 +00:00
{
using (IMembershipDAO dao = factory.GetMembershipDAO(connection))
{
dao.RemoveUsersFromRoles(usernames, roleNames);
}
}
}
}
public UserBO GetUserFromName(string name)
{
2011-12-05 09:23:02 +00:00
var factory = GetFactory.GetDAOFactory(Database.GetFactoryType);
using (var connection = factory.Connection)
2010-03-02 17:56:21 +00:00
{
using (IMembershipDAO dao = factory.GetMembershipDAO(connection))
{
return dao.GetUserFromName(name);
}
}
}
}
}