Skip to content

Instantly share code, notes, and snippets.

@dealproc
Created March 17, 2019 06:58
Show Gist options
  • Save dealproc/3ad96e34f94549e7d94530c932c4175c to your computer and use it in GitHub Desktop.
Save dealproc/3ad96e34f94549e7d94530c932c4175c to your computer and use it in GitHub Desktop.
Multi-Tenant Solutions - NancyContextAccountAccessor.cs
public class NancyContextAccountAccessor : IAccountAccessor {
static readonly ILog _log = LogProvider.For<NancyContextAccountAccessor>();
readonly IAccountRepository _accountRepository;
readonly NancyContext _nancyContext;
public NancyContextAccountAccessor(IAccountRepository accountRepository, NancyContext nancyContext) {
_accountRepository = accountRepository;
_nancyContext = nancyContext;
}
public Account GetCurrentAccount() {
var hostName = _nancyContext.Request.Url.HostName;
_log.DebugFormat("Hostname resolved from nancy context `{hostName}`", hostName);
var account = _accountRepository.GetFirst(x => x.Hostname.ToLower() == hostName);
if (account == null) {
_log.WarnFormat("Account was not found in the system for `{hostName}`", hostName);
throw new UnknownAccountException(3, string.Format("Hostname: {0} has not been registered.", hostName));
}
return account;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment