2010-03-02 17:56:21 +00:00
|
|
|
|
using System;
|
|
|
|
|
using Tanshu.Accounts.Contracts;
|
|
|
|
|
|
|
|
|
|
namespace Tanshu.Accounts.BI
|
|
|
|
|
{
|
2013-11-28 10:39:33 +00:00
|
|
|
|
public class MembershipBI
|
2010-03-02 17:56:21 +00:00
|
|
|
|
{
|
|
|
|
|
public bool ValidateUser(string name, string password)
|
|
|
|
|
{
|
2013-11-16 06:01:58 +00:00
|
|
|
|
using (var connection = new SqlDAO.SqlConnectionDAO())
|
2010-03-02 17:56:21 +00:00
|
|
|
|
{
|
2013-11-16 06:01:58 +00:00
|
|
|
|
using (var dao = new SqlDAO.MembershipDAO(connection))
|
2010-03-02 17:56:21 +00:00
|
|
|
|
{
|
|
|
|
|
return dao.ValidateUser(name, password);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddUsersToRoles(string[] usernames, string[] roleNames)
|
|
|
|
|
{
|
2013-11-16 06:01:58 +00:00
|
|
|
|
|
|
|
|
|
using (var connection = new SqlDAO.SqlConnectionDAO())
|
2010-03-02 17:56:21 +00:00
|
|
|
|
{
|
2013-11-16 06:01:58 +00:00
|
|
|
|
using (var dao = new SqlDAO.MembershipDAO(connection))
|
2010-03-02 17:56:21 +00:00
|
|
|
|
{
|
|
|
|
|
dao.AddUsersToRoles(usernames, roleNames);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string[] GetAllRoles()
|
|
|
|
|
{
|
2013-11-16 06:01:58 +00:00
|
|
|
|
|
|
|
|
|
using (var connection = new SqlDAO.SqlConnectionDAO())
|
2010-03-02 17:56:21 +00:00
|
|
|
|
{
|
2013-11-16 06:01:58 +00:00
|
|
|
|
using (var dao = new SqlDAO.MembershipDAO(connection))
|
2010-03-02 17:56:21 +00:00
|
|
|
|
{
|
|
|
|
|
return dao.GetAllRoles();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string[] GetRolesForUser(string username)
|
|
|
|
|
{
|
2013-11-16 06:01:58 +00:00
|
|
|
|
|
|
|
|
|
using (var connection = new SqlDAO.SqlConnectionDAO())
|
2010-03-02 17:56:21 +00:00
|
|
|
|
{
|
2013-11-16 06:01:58 +00:00
|
|
|
|
using (var dao = new SqlDAO.MembershipDAO(connection))
|
2010-03-02 17:56:21 +00:00
|
|
|
|
{
|
|
|
|
|
return dao.GetRolesForUser(username);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsUserInRole(string username, string roleName)
|
|
|
|
|
{
|
2013-11-16 06:01:58 +00:00
|
|
|
|
|
|
|
|
|
using (var connection = new SqlDAO.SqlConnectionDAO())
|
2010-03-02 17:56:21 +00:00
|
|
|
|
{
|
2013-11-16 06:01:58 +00:00
|
|
|
|
using (var dao = new SqlDAO.MembershipDAO(connection))
|
2010-03-02 17:56:21 +00:00
|
|
|
|
{
|
|
|
|
|
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)
|
|
|
|
|
{
|
2013-11-16 06:01:58 +00:00
|
|
|
|
|
|
|
|
|
using (var connection = new SqlDAO.SqlConnectionDAO())
|
2010-03-02 17:56:21 +00:00
|
|
|
|
{
|
2013-11-16 06:01:58 +00:00
|
|
|
|
using (var dao = new SqlDAO.MembershipDAO(connection))
|
2010-03-02 17:56:21 +00:00
|
|
|
|
{
|
|
|
|
|
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))
|
|
|
|
|
{
|
2013-11-16 06:01:58 +00:00
|
|
|
|
|
|
|
|
|
using (var connection = new SqlDAO.SqlConnectionDAO())
|
2010-03-02 17:56:21 +00:00
|
|
|
|
{
|
2013-11-16 06:01:58 +00:00
|
|
|
|
using (var dao = new SqlDAO.MembershipDAO(connection))
|
2010-03-02 17:56:21 +00:00
|
|
|
|
{
|
|
|
|
|
dao.RemoveUsersFromRoles(usernames, roleNames);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public UserBO GetUserFromName(string name)
|
|
|
|
|
{
|
2013-11-16 06:01:58 +00:00
|
|
|
|
|
|
|
|
|
using (var connection = new SqlDAO.SqlConnectionDAO())
|
2010-03-02 17:56:21 +00:00
|
|
|
|
{
|
2013-11-16 06:01:58 +00:00
|
|
|
|
using (var dao = new SqlDAO.MembershipDAO(connection))
|
2010-03-02 17:56:21 +00:00
|
|
|
|
{
|
|
|
|
|
return dao.GetUserFromName(name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|