abpframework / abp

Open-source web application framework for ASP.NET Core! Offers an opinionated architecture to build enterprise software solutions with best practices on top of the .NET. Provides the fundamental infrastructure, cross-cutting-concern implementations, startup templates, application modules, UI themes, tooling and documentation.
https://abp.io
GNU Lesser General Public License v3.0
12.95k stars 3.45k forks source link

Nopcommerce ISetting #16793

Open vioKamran opened 1 year ago

vioKamran commented 1 year ago

Hi How to code write class like "Nopcommerce ISetting" automatic load value from database : For sample : đŸ‘‡

using Nop.Core.Configuration;

namespace Nop.Core.Domain.Customers
{

    public partial class CustomerSettings : ISettings
    {

        public bool UsernamesEnabled { get; set; }

        public bool CheckUsernameAvailabilityEnabled { get; set; }

        public bool AllowUsersToChangeUsernames { get; set; }

        public bool UsernameValidationEnabled { get; set; }
}

...........................................................................................

namespace Nop.Services.Authentication
{

    public partial class CookieAuthenticationService : IAuthenticationService
    {

        private readonly CustomerSettings _customerSettings;

        public CookieAuthenticationService(CustomerSettings customerSettings)
        {
            _customerSettings = customerSettings;
        }

        public virtual async Task<Customer> GetAuthenticatedCustomerAsync()
        {
            //whether there is a cached customer
            if (_cachedCustomer != null)
                return _cachedCustomer;

            //try to get authenticated user identity
            var authenticateResult = await _httpContextAccessor.HttpContext.AuthenticateAsync(NopAuthenticationDefaults.AuthenticationScheme);
            if (!authenticateResult.Succeeded)
                return null;

            Customer customer = null;
            if (_customerSettings.UsernamesEnabled)
            {
                //try to get customer by username
                var usernameClaim = authenticateResult.Principal.FindFirst(claim => claim.Type == ClaimTypes.Name
                    && claim.Issuer.Equals(NopAuthenticationDefaults.ClaimsIssuer, StringComparison.InvariantCultureIgnoreCase));
                if (usernameClaim != null)
                    customer = await _customerService.GetCustomerByUsernameAsync(usernameClaim.Value);
            }
            else
            {
                //try to get customer by email
                var emailClaim = authenticateResult.Principal.FindFirst(claim => claim.Type == ClaimTypes.Email
                    && claim.Issuer.Equals(NopAuthenticationDefaults.ClaimsIssuer, StringComparison.InvariantCultureIgnoreCase));
                if (emailClaim != null)
                    customer = await _customerService.GetCustomerByEmailAsync(emailClaim.Value);
            }

            //whether the found customer is available
            if (customer == null || !customer.Active || customer.RequireReLogin || customer.Deleted || !await _customerService.IsRegisteredAsync(customer))
                return null;

            static DateTime trimMilliseconds(DateTime dt) => new(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, 0, dt.Kind);

            //get the latest password
            var customerPassword = await _customerService.GetCurrentPasswordAsync(customer.Id);
            //required a customer to re-login after password changing
            if (trimMilliseconds(customerPassword.CreatedOnUtc).CompareTo(trimMilliseconds(authenticateResult.Properties.IssuedUtc?.DateTime ?? DateTime.UtcNow)) > 0)
                return null;

            //cache authenticated customer
            _cachedCustomer = customer;

            return _cachedCustomer;
        }

    }
}

Please update ABP ISettingManager automatic load class interface database.

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.