Beispiel #1
0
        public static async Task SeedAdmin(TmanagerServiceContext dbContext, UserManager <ApplicationUser> userManager)
        {
            var adminRoleString  = RoleValues.Admin.ToDescription();
            var adminClaimString = Claim.CreateAccount.ToDescription();

            foreach (var admin in DataValues.AdminData(dbContext))
            {
                if (await userManager.FindByNameAsync(admin.UserName) == null)
                {
                    await userManager.CreateAsync(admin, DataValues.default_admin_password);

                    var adminClaim = await userManager.GetClaimsAsync(admin);

                    if (!await userManager.IsInRoleAsync(admin, adminRoleString))
                    {
                        await userManager.AddToRoleAsync(admin, adminRoleString);
                    }

                    if (!adminClaim.Any(x => x.Type == adminClaimString))
                    {
                        await userManager.AddClaimAsync(admin, new System.Security.Claims.Claim(adminClaimString, true.ToString()));
                    }
                }
            }
        }
Beispiel #2
0
        public static async Task SeedSupervisor(TmanagerServiceContext dbContext, UserManager <ApplicationUser> userManager)
        {
            var supervisorRoleString  = RoleValues.Supervisor.ToDescription();
            var supervisorClaimString = Claim.InsertRequest.ToDescription();

            foreach (var user in await DataValues.SupervisorDataAsync(dbContext, userManager))
            {
                if (await userManager.FindByNameAsync(user.UserName) == null)
                {
                    await userManager.CreateAsync(user, DataValues.default_user_password);

                    var userClaim = await userManager.GetClaimsAsync(user);

                    if (!await userManager.IsInRoleAsync(user, supervisorRoleString))
                    {
                        await userManager.AddToRoleAsync(user, supervisorRoleString);
                    }

                    if (!userClaim.Any(x => x.Type == supervisorClaimString))
                    {
                        await userManager.AddClaimAsync(user, new System.Security.Claims.Claim(supervisorClaimString, true.ToString()));
                    }
                }
            }
        }
Beispiel #3
0
        public static async Task SeedAsync(TmanagerServiceContext dbContext,
                                           UserManager <ApplicationUser> userManager,
                                           RoleManager <IdentityRole> roleManager,
                                           ILoggerFactory loggerFactory, int?retry = 0)
        {
            int retryForAvailability = retry.Value;

            try
            {
                dbContext.Database.Migrate();
                await AddData.SeedRolesAndClaims(userManager, roleManager);

                //Seed Admin
                await AddData.SeedAdmin(dbContext, userManager);

                //Seed AreaWorking
                //if (!dbContext.AreaWorkings.Any())
                //{
                //    dbContext.AreaWorkings.AddRange(await DataValues.AreaWorkingDataAsync(dbContext, userManager));

                //    await dbContext.SaveChangesAsync();
                //}

                //Seed Admin
                if (!dbContext.Companys.Any())
                {
                    dbContext.Companys.AddRange(await DataValues.CompanyDataAsync(dbContext, userManager));

                    await dbContext.SaveChangesAsync();
                }

                await AddData.SeedSupervisor(dbContext, userManager);

                await AddData.SeedRepairPerson(dbContext, userManager);

                ////add listAreaWorking for Admin01
                //ApplicationUser admin = await userManager.FindByNameAsync(DataValues.adminName01);
                //List<string> listAreaWorking = (from arW in dbContext.AreaWorkings
                //                                where arW.AdminId == admin.Id
                //                                select arW.Id).ToList();
                //admin.ListAreaWorkingId = String_List.ToString(listAreaWorking);
                //await userManager.UpdateAsync(admin);

                //await dbContext.SaveChangesAsync();

                //Seed Request
                if (!dbContext.Requests.Any())
                {
                    dbContext.Requests.AddRange(await DataValues.RequestDataAsync(dbContext, userManager));

                    await dbContext.SaveChangesAsync();
                }
            }
            catch (Exception ex)
            {
                if (retryForAvailability < 10)
                {
                    retryForAvailability++;
                    var log = loggerFactory.CreateLogger <TmanagerServiceContext>();
                    log.LogError(ex.Message);
                    await SeedAsync(dbContext, userManager, roleManager, loggerFactory, retryForAvailability);
                }
            }
        }