﻿using System;
using Agiil.Domain.Auth;
using Agiil.Web.Models.Shared;
using CSF.ORM;

namespace Agiil.Web.Services.Auth
{
    public class LoginStateReader
    {
        readonly IIdentityReader userReader;
        readonly IEntityData data;

        public LoginStateModel GetLoginState()
        {
            var userInfo = userReader.GetCurrentUserInfo();

            return new LoginStateModel {
                UserInfo = userInfo,
                IsSiteAdmin = (userInfo != null)? data.Get(userInfo.Identity).SiteAdministrator : false,
            };
        }

        public LoginStateReader(IIdentityReader userReader, IEntityData data)
        {
            this.userReader = userReader ?? throw new ArgumentNullException(nameof(userReader));
            this.data = data ?? throw new ArgumentNullException(nameof(data));
        }
    }
}

﻿using System;
using Agiil.Domain.Auth;
using Agiil.Web.Models.Shared;
using CSF.ORM;

namespace Agiil.Web.Services.Auth
{
    public class LoginStateReader
    {
        readonly IIdentityReader userReader;
        readonly IEntityData data;

        public LoginStateModel GetLoginState()
        {
            var userInfo = userReader.GetCurrentUserInfo();
            var user = (userInfo?.Identity != null) ? data.Get(userInfo.Identity) : null;

            return new LoginStateModel {
                UserInfo = userInfo,
                IsSiteAdmin = user?.SiteAdministrator == true,
            };
        }

        public LoginStateReader(IIdentityReader userReader, IEntityData data)
        {
            this.userReader = userReader ?? throw new ArgumentNullException(nameof(userReader));
            this.data = data ?? throw new ArgumentNullException(nameof(data));
        }
    }
}

Fix crash error when login state is invalid
Fix crash error when login state is invalid

I think this was caused by having an existing browser cookie for the domain,
indicating that I am logged-in as a user which does not exist in the current
database.

This caused it to fail to retrieve a user from the DB and as a result crashed
because it had expected to find a user.
