<commit_before>﻿// ==========================================================================
//  IdentityUsage.cs
//  Squidex Headless CMS
// ==========================================================================
//  Copyright (c) Squidex Group
//  All rights reserved.
// ==========================================================================

using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Squidex.Core.Identity;
using Squidex.Read.Users;

// ReSharper disable InvertIf

namespace Squidex.Config.Identity
{
    public static class IdentityUsage
    {
        public static IApplicationBuilder UseMyIdentity(this IApplicationBuilder app)
        {
            app.UseIdentity();

            return app;
        }

        public static IApplicationBuilder UseMyIdentityServer(this IApplicationBuilder app)
        {
             app.UseIdentityServer();

            return app;
        }

        public static IApplicationBuilder UseAdminRole(this IApplicationBuilder app)
        {
            var roleManager = app.ApplicationServices.GetRequiredService<RoleManager<IRole>>();
            var roleFactory = app.ApplicationServices.GetRequiredService<IRoleFactory>();

            roleManager.CreateAsync(roleFactory.Create(SquidexRoles.Administrator)).Wait();

            return app;
        }

        public static IApplicationBuilder UseMyAdmin(this IApplicationBuilder app)
        {
            var options = app.ApplicationServices.GetService<IOptions<MyIdentityOptions>>().Value;

            var userManager = app.ApplicationServices.GetService<UserManager<IUser>>();
            var userFactory = app.ApplicationServices.GetService<IUserFactory>();

            if (options.IsAdminConfigured())
            {
                var adminEmail = options.AdminEmail;
                var adminPass = options.AdminPassword;

                Task.Run(async () =>
                {
                    var user = await userManager.FindByEmailAsync(adminPass);

                    async Task userInitAsync(IUser theUser)
                    {
                        await userManager.RemovePasswordAsync(theUser);
                        await userManager.ChangePasswordAsync(theUser, null, adminEmail);
                        await userManager.AddToRoleAsync(theUser, SquidexRoles.Administrator);
                    }

                    if (user != null)<commit_msg>Small fix when setting admin.<commit_after>﻿// ==========================================================================
//  IdentityUsage.cs
//  Squidex Headless CMS
// ==========================================================================
//  Copyright (c) Squidex Group
//  All rights reserved.
// ==========================================================================

using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Squidex.Core.Identity;
using Squidex.Read.Users;

// ReSharper disable InvertIf

namespace Squidex.Config.Identity
{
    public static class IdentityUsage
    {
        public static IApplicationBuilder UseMyIdentity(this IApplicationBuilder app)
        {
            app.UseIdentity();

            return app;
        }

        public static IApplicationBuilder UseMyIdentityServer(this IApplicationBuilder app)
        {
             app.UseIdentityServer();

            return app;
        }

        public static IApplicationBuilder UseAdminRole(this IApplicationBuilder app)
        {
            var roleManager = app.ApplicationServices.GetRequiredService<RoleManager<IRole>>();
            var roleFactory = app.ApplicationServices.GetRequiredService<IRoleFactory>();

            roleManager.CreateAsync(roleFactory.Create(SquidexRoles.Administrator)).Wait();

            return app;
        }

        public static IApplicationBuilder UseMyAdmin(this IApplicationBuilder app)
        {
            var options = app.ApplicationServices.GetService<IOptions<MyIdentityOptions>>().Value;

            var userManager = app.ApplicationServices.GetService<UserManager<IUser>>();
            var userFactory = app.ApplicationServices.GetService<IUserFactory>();

            if (options.IsAdminConfigured())
            {
                var adminEmail = options.AdminEmail;
                var adminPass = options.AdminPassword;

                Task.Run(async () =>
                {
                    var user = await userManager.FindByEmailAsync(adminEmail);

                    async Task userInitAsync(IUser theUser)
                    {
                        await userManager.RemovePasswordAsync(theUser);
                        await userManager.ChangePasswordAsync(theUser, null, adminEmail);
                        await userManager.AddToRoleAsync(theUser, SquidexRoles.Administrator);
                    }

                    if (user != null)
{'fraction_non_alphanumeric': 0.15830568026546946, 'fraction_numerical': 0.0, 'mean_word_length': 2.5632823365785815, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2880409', 'n_tokens_mistral': 1201, 'n_tokens_neox': 1119, 'n_words': 231}