You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
100 lines
3.9 KiB
100 lines
3.9 KiB
|
1 month ago
|
using System;
|
||
|
|
using System.Threading.Tasks;
|
||
|
|
|
||
|
|
namespace Housing.Services
|
||
|
|
{
|
||
|
|
public class LoginAccountRepository
|
||
|
|
{
|
||
|
|
private readonly DatabaseSettings _dbSettings;
|
||
|
|
private readonly LoginSettings _loginSettings;
|
||
|
|
|
||
|
|
public LoginAccountRepository(DatabaseSettings dbSettings, LoginSettings loginSettings)
|
||
|
|
{
|
||
|
|
_dbSettings = dbSettings;
|
||
|
|
_loginSettings = loginSettings;
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<bool> ExistsAsync(string operatorId, string password)
|
||
|
|
{
|
||
|
|
// 1. 데모 계정(test/test)인 경우 DB 조회 없이 즉시 로그인 통과
|
||
|
|
if (IsDefaultAccount(operatorId, password))
|
||
|
|
{
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 2. 오프라인 모드인 경우 기본 성공
|
||
|
|
if (_loginSettings.OfflinePreview)
|
||
|
|
{
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
// DB 커넥션 스트링이 비어있으면 실패 처리
|
||
|
|
if (string.IsNullOrWhiteSpace(_dbSettings.ConnectionString))
|
||
|
|
{
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
try
|
||
|
|
{
|
||
|
|
// 실제 DB에서 계정을 조회해보기 위한 시도
|
||
|
|
using (var conn = new System.Data.SqlClient.SqlConnection(_dbSettings.ConnectionString))
|
||
|
|
{
|
||
|
|
await conn.OpenAsync();
|
||
|
|
|
||
|
|
if (!string.IsNullOrWhiteSpace(_loginSettings.Procedure))
|
||
|
|
{
|
||
|
|
// 저장 프로시저 호출
|
||
|
|
using (var cmd = new System.Data.SqlClient.SqlCommand(_loginSettings.Procedure, conn))
|
||
|
|
{
|
||
|
|
cmd.CommandType = System.Data.CommandType.StoredProcedure;
|
||
|
|
cmd.Parameters.AddWithValue("@OperatorId", operatorId);
|
||
|
|
cmd.Parameters.AddWithValue("@Password", password);
|
||
|
|
cmd.Parameters.AddWithValue("@ProcessName", _loginSettings.ProcessName ?? string.Empty);
|
||
|
|
|
||
|
|
var result = await cmd.ExecuteScalarAsync();
|
||
|
|
if (result != null && result != DBNull.Value)
|
||
|
|
{
|
||
|
|
if (int.TryParse(result.ToString(), out var count))
|
||
|
|
{
|
||
|
|
return count > 0;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
// 일반 쿼리 수행
|
||
|
|
const string query = "SELECT COUNT(*) FROM [Users] WHERE [OperatorId] = @Id AND [Password] = @Pw";
|
||
|
|
using (var cmd = new System.Data.SqlClient.SqlCommand(query, conn))
|
||
|
|
{
|
||
|
|
cmd.Parameters.AddWithValue("@Id", operatorId);
|
||
|
|
cmd.Parameters.AddWithValue("@Pw", password);
|
||
|
|
|
||
|
|
var count = (int)await cmd.ExecuteScalarAsync();
|
||
|
|
return count > 0;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
System.Diagnostics.Debug.WriteLine($"[LoginAccountRepository ExistsAsync Error] {ex.Message}");
|
||
|
|
// DB 연결이나 쿼리 오류 발생 시 로그인 실패 처리
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private bool IsDefaultAccount(string operatorId, string password)
|
||
|
|
{
|
||
|
|
// 기본 개발/테스트용 데모 계정 처리 (test/test만 허용)
|
||
|
|
if (string.Equals(operatorId, "test", StringComparison.OrdinalIgnoreCase) &&
|
||
|
|
string.Equals(password, "test", StringComparison.OrdinalIgnoreCase))
|
||
|
|
{
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|