Webiant Logo Webiant Logo
  1. No results found.

    Try your search with a different keyword or use * as a wildcard.

ExternalAuthenticationModelFactory.cs

using Nop.Services.Authentication.External;
using Nop.Web.Areas.Admin.Infrastructure.Mapper.Extensions;
using Nop.Web.Areas.Admin.Models.ExternalAuthentication;
using Nop.Web.Framework.Models.Extensions;

namespace Nop.Web.Areas.Admin.Factories;

/// 
/// Represents the external authentication method model factory implementation
/// 
public partial class ExternalAuthenticationMethodModelFactory : IExternalAuthenticationMethodModelFactory
{
    #region Fields

    protected readonly IAuthenticationPluginManager _authenticationPluginManager;

    #endregion

    #region Ctor

    public ExternalAuthenticationMethodModelFactory(IAuthenticationPluginManager authenticationPluginManager)
    {
        _authenticationPluginManager = authenticationPluginManager;
    }

    #endregion

    #region Methods

    /// 
    /// Prepare external authentication method search model
    /// 
    /// External authentication method search model
    /// External authentication method search model
    public virtual ExternalAuthenticationMethodSearchModel PrepareExternalAuthenticationMethodSearchModel(
        ExternalAuthenticationMethodSearchModel searchModel)
    {
        ArgumentNullException.ThrowIfNull(searchModel);

        //prepare page parameters
        searchModel.SetGridPageSize();

        return searchModel;
    }

    /// 
    /// Prepare paged external authentication method list model
    /// 
    /// External authentication method search model
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the external authentication method list model
    /// 
    public virtual async Task PrepareExternalAuthenticationMethodListModelAsync(
        ExternalAuthenticationMethodSearchModel searchModel)
    {
        ArgumentNullException.ThrowIfNull(searchModel);

        //get external authentication methods
        var externalAuthenticationMethods = (await _authenticationPluginManager.LoadAllPluginsAsync()).ToPagedList(searchModel);

        //prepare grid model
        var model = new ExternalAuthenticationMethodListModel().PrepareToGrid(searchModel, externalAuthenticationMethods, () =>
        {
            //fill in model values from the entity
            return externalAuthenticationMethods.Select(method =>
            {
                //fill in model values from the entity
                var externalAuthenticationMethodModel = method.ToPluginModel();

                //fill in additional values (not existing in the entity)
                externalAuthenticationMethodModel.IsActive = _authenticationPluginManager.IsPluginActive(method);
                externalAuthenticationMethodModel.ConfigurationUrl = method.GetConfigurationPageUrl();

                return externalAuthenticationMethodModel;
            });
        });

        return model;
    }

    #endregion
}