Taylohtio/FBAUsersSearch/CustomFBADataSource/CodeFiles/CredentialsRepository.cs

84 lines
2.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Taloyhtio.CustomFBADataSource.CodeFiles
{
public class CredentialsRepository
{
private string connString;
private SqlConnection connection = null;
public CredentialsRepository(string connString)
{
this.connString = connString;
}
private SqlConnection Connection
{
get
{
if (this.connection == null && !string.IsNullOrEmpty(this.connString))
{
this.connection = new SqlConnection(this.connString);
}
if (this.connection != null && !this.connection.State.Equals(ConnectionState.Open))
{
this.connection.Open();
}
return this.connection;
}
}
public Guid GetUserId(string username)
{
Guid result = Guid.Empty;
string sql = "SELECT UserId FROM [aspnet_Users] WHERE LoweredUserName = @username";
using (var cmd = Connection.CreateCommand())
{
cmd.CommandText = sql;
cmd.Parameters.AddWithValue("@username", username.ToLowerInvariant());
using (var reader = cmd.ExecuteReader())
{
if (reader.Read())
{
result = Guid.Parse(reader["UserId"].ToString());
}
}
}
return result;
}
public List<CondoInfo> GetUserCondos(Guid userId)
{
var result = new List<CondoInfo>();
string sql = "SELECT c.PmcId, c.Url, c.Title, c.PmcRelativeUrl, uc.SiteId, uc.IsPMCLevelGroup FROM [mdb_RightsRedirect_aspnet_UserCondos] uc INNER JOIN [mdb_RightsRedirect_aspnet_Condos] c ON c.SiteId = uc.SiteId AND c.PmcId = uc.PmcId WHERE uc.UserId = @userId ORDER BY c.Url";
using (var cmd = Connection.CreateCommand())
{
cmd.CommandText = sql;
cmd.Parameters.AddWithValue("@userId", userId);
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
result.Add(new CondoInfo
{
Id = Guid.Parse(reader["SiteId"].ToString()),
PmcId = Guid.Parse(reader["PmcId"].ToString()),
Url = reader["Url"].ToString(),
Title = reader["Title"].ToString(),
PmcRelativeUrl = reader["PmcRelativeUrl"].ToString()
});
}
}
}
return result;
}
}
}