I need a bit of help on if I have coded this class correctly.
I want to handle the setting of client cookie/client store.
public class UserSession : IUserSession
{
private readonly IReadOnlySession _repo;
public UserSession(IReadOnlySession repo)
{
_repo = repo;
}
private string _loginId;
public string LoginID
{
get { return HttpContext.Current.User.Identity.Name; }
set { _loginId = value; }
}
private string _companyIdentifier;
public string CompanyIdentifier
{
get { return AuthenticateCookie.GetCompanyIdentifierFromTicket(LoginID); }
set { _companyIdentifier = value; }
}
/// <summary>
/// when the client store is set, this means the user has been changed
/// </summary>
/// <param name="loginId"></param>
/// <param name="identifier"></param>
public void SetClientStore(string loginId, string identifier)
{
MyProfile = null;
AuthenticateCookie.AddDetailsToCookie(loginId, identifier);
LoginID = loginId;
CompanyIdentifier = identifier;
}
public void Logout()
{
// we could throw an exception here?
MyProfile = null;
HttpContext.Current.User = null;
FormsAuthentication.SignOut();
//clear cookie values too?
}
private UserProfile _myprofile;
public UserProfile MyProfile
{
get { return SetupProfile(); }
private set { _myprofile = value; }
}
private UserProfile SetupProfile()
{
if (_myprofile == null)
{
if (string.IsNullOrEmpty(LoginID) || string.IsNullOrEmpty(CompanyIdentifier))
{
Logout();
return null;
}
//call repo to get object...
_myprofile = _repo.All<User>()
.Where(x => x.Login == LoginID)
.Join(_repo.All<Profile>().Where(x => x.IsActive),
x => x.UserID, y => y.UserID,
(x, y) =>
new
{
x.UserID, y.CompanyID, y.RoleID,
})
.Join(_repo.All<Company>()
.Where(x => x.IsActive && x.Identifier == CompanyIdentifier),
x => x.CompanyID, y => y.CompanyID,
(x, y) =>
new
{
x.UserID, y.CompanyID, x.RoleID, x.IsSurveyor,
CompanyFriendlyName = y.Name,
CompanyType = y.Type
})
.Join(_repo.All<Role>(), x => x.RoleID, y => y.RoleID,
(x, y) =>
new
{
x.UserID, x.CompanyID, y.RoleName, x.CompanyFriendlyName, x.CompanyType
})
.Join(_repo.All<Subscription>(),
x => x.CompanyID, y => y.CompanyID,
(x, y) =>
new UserProfile
{
UserID = x.UserID,
CompanyID = x.CompanyID,
Role = x.RoleName.Convert<RoleName>(),
CompanyFriendlyName = x.CompanyFriendlyName,
LoginID = LoginID,
Identifier = CompanyIdentifier,
PricePlan = y.Name.Convert<PricePlanType>(),
CompanyType = x.CompanyType.Convert<CompanyType>()
})
.SingleOrDefault();
if (_myprofile == null)
Logout();
}
return _myprofile;
}
}