narsil/Tanshu.Accounts.BI/MembershipBI.cs

112 lines
3.4 KiB
C#

using System;
using Tanshu.Accounts.Contracts;
namespace Tanshu.Accounts.BI
{
public class MembershipBI
{
public bool ValidateUser(string name, string password)
{
using (var connection = new SqlDAO.SqlConnectionDAO())
{
using (var dao = new SqlDAO.MembershipDAO(connection))
{
return dao.ValidateUser(name, password);
}
}
}
public void AddUsersToRoles(string[] usernames, string[] roleNames)
{
using (var connection = new SqlDAO.SqlConnectionDAO())
{
using (var dao = new SqlDAO.MembershipDAO(connection))
{
dao.AddUsersToRoles(usernames, roleNames);
}
}
}
public string[] GetAllRoles()
{
using (var connection = new SqlDAO.SqlConnectionDAO())
{
using (var dao = new SqlDAO.MembershipDAO(connection))
{
return dao.GetAllRoles();
}
}
}
public string[] GetRolesForUser(string username)
{
using (var connection = new SqlDAO.SqlConnectionDAO())
{
using (var dao = new SqlDAO.MembershipDAO(connection))
{
return dao.GetRolesForUser(username);
}
}
}
public bool IsUserInRole(string username, string roleName)
{
using (var connection = new SqlDAO.SqlConnectionDAO())
{
using (var dao = new SqlDAO.MembershipDAO(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)
{
using (var connection = new SqlDAO.SqlConnectionDAO())
{
using (var dao = new SqlDAO.MembershipDAO(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))
{
using (var connection = new SqlDAO.SqlConnectionDAO())
{
using (var dao = new SqlDAO.MembershipDAO(connection))
{
dao.RemoveUsersFromRoles(usernames, roleNames);
}
}
}
}
public UserBO GetUserFromName(string name)
{
using (var connection = new SqlDAO.SqlConnectionDAO())
{
using (var dao = new SqlDAO.MembershipDAO(connection))
{
return dao.GetUserFromName(name);
}
}
}
}
}